<script>
	import WechatAuth from '@/utils/wechat-auth.js'
	import { getUserInfo } from '@/http/api/profile.js'

	export default {
		onLaunch: function() {
			console.log('App Launch')
			this.initApp()
		},
		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		},
		methods: {
			// 应用初始化
			async initApp() {
				try {
					await this.checkLoginStatus()
				} catch (error) {
					// 静默处理初始化错误
				}
			},

			// 检查登录状态
			async checkLoginStatus() {
				const token = WechatAuth.getToken()

				if (!token) {
					return
				}

				try {
					// 验证token有效性
					const userInfo = await getUserInfo({
						custom: {
							loading: false,
							toast: false
						}
					})

					// 更新本地用户信息
					if (userInfo) {
						WechatAuth.saveLoginInfo(token, WechatAuth.getRefreshToken(), userInfo)
					}

				} catch (error) {
					// token无效，清除登录信息
					if (error.code === 401 || error.statusCode === 401) {
						WechatAuth.clearLoginInfo()
					}
				}
			}
		}
	}
</script>

<style lang="scss">
	/*每个页面公共css */
  @import "@/uni_modules/uview-next/index.scss";
</style>
