<script>
	import api from './utils/api'
	export default {
		onLaunch: function() {
			console.log('App Launch')
			// 执行微信静默登录
			this.wechatSilentLogin()
		},
		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		},
		methods: {
			// 微信静默登录
			wechatSilentLogin() {
				// 检查是否在微信小程序环境中
				if (uni.getSystemInfoSync().platform === 'devtools' || uni.getSystemInfoSync().platform === 'mp-weixin') {
					// 调用微信登录API获取code
					uni.login({
						provider: 'weixin',
						success: (loginRes) => {
							if (loginRes.code) {
								// 将code发送到后端进行登录
								api.auth.wechatLogin(loginRes.code)
									.then(res => {
										if (res.code === 200) {
											// 登录成功，存储token
											uni.setStorageSync('token', res.data.token)
											uni.setStorageSync('userInfo', res.data.userInfo)
											console.log('微信静默登录成功')
										} else {
											console.log('微信静默登录失败:', res.message)
										}
									})
									.catch(err => {
										console.log('微信登录API调用失败:', err)
									})
							} else {
								console.log('获取微信登录code失败:', loginRes.errMsg)
							}
						},
						fail: (err) => {
							console.log('微信登录失败:', err)
						}
					})
				}
			}
		}
	}
</script>

<style>
	/* 每个页面公共css */
	page {
		background-color: #F3F4F6;
		font-family: 'Inter', 'Noto Sans SC', sans-serif;
		--brand-color: #064e3b;
		--brand-light: #10B981;
	}
	
	/* 通用工具类 */
	.bg-white { background-color: #ffffff; }
	.rounded-2xl { border-radius: 32rpx; }
	.shadow-sm { box-shadow: 0 2rpx 6rpx rgba(0,0,0,0.05); }
	.flex-row { display: flex; flex-direction: row; }
	.flex-col { display: flex; flex-direction: column; }
	.items-center { align-items: center; }
	.justify-between { justify-content: space-between; }
</style>