<script>
	import USER from '@/api/user.js'
	export default {
		onLaunch: function() {
			this.handleWxLogin()
		},
		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		},
		methods: {
			async handleWxLogin() {
				// #ifdef MP-WEIXIN
				try {
					// 1. 获取code
					const loginRes = await new Promise((resolve, reject) => {
						uni.login({
							success:async({code}) =>{
								// 2、登录获取用户信息
								const {data} = USER.fetchLogin({code})
								uni.setStorageSync('userInfo', data);
								resolve(data)
							},
							fail: reject
						});
					});
				} catch (err) {
					console.error('登录失败:', err);
					reject(err)
				}
				// #endif
				
				// #ifdef H5
				const {data} = await USER.userInfo()
				uni.setStorageSync('userInfo', data);
				// #endif
			}
		}
	}
</script>

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