<template>
	<view>
		<image :src="src" class="img"></image>

		<view class="btn">
			<view style="width: 80%;">
				<u-button v-if="canIUseGetUserProfile" shape="circle"
					color="linear-gradient(to right, #F54319, #FF6D20)" @click="getUserProfile">选择餐车</u-button>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name: 'MyLogin',
		data() {
			return {
				src: '../../static/first.png',
				canIUseGetUserProfile: false,
			}
		},
		onLoad(e) {
			// 检查微信版本是否支持 getUserProfile
			if (wx.getUserProfile) {
				this.canIUseGetUserProfile = true;
			}
			// uni.setStorageSync('dinnerId', '1806258588536201217')
			// if(e.id){
			// 	uni.setStorageSync('dinnerId', e.id)
			// }
			
			let infoData = uni.getStorageSync('infoData');
			if(infoData){
				try{
					infoData = JSON.parse(infoData)
					// console.log('用户数据',infoData)
					if (infoData.expire_in && Date.now() / 1000 - infoData.time > infoData.expire_in) {
					  uni.removeStorageSync('infoData')
					  return null
					}
					
					if(infoData && infoData.access_token){
						uni.switchTab({
							url:'/pages/home/home'
						})
					}
				}catch(e){}
			}
		},
		methods: {
			getUserProfile() {
				wx.showModal({
					title: '温馨提示',
					content: '亲，授权微信登录后才能正常使用小程序功能',
				})
				wx.getUserProfile({
					desc: '获取用户信息',
					success: (res) => {
						console.log('res', res);
						// 调用微信登录接口
						wx.login({
							success: async (loginRes) => {
								console.log('数据：', loginRes)
								// 发送code到开发者服务器换取用户信息
								const result = await this.$request('post', '/system/appUser/miniLogin?tenantId=000000&code='+loginRes.code)
									console.log('登录成功：', result);
								if (result && result.data !== false) {
									// 保存token
									uni.setStorageSync("token", result.data.access_token);
									uni.setStorageSync("openId", result.data.open_id);
									//uni.setStorageSync("tenantId", result.data.tenantId);
									uni.setStorageSync("appUserId", result.data.app_user_id);
									
									let d = result.data;
									d.time = Date.now() / 1000;
									uni.setStorageSync('infoData', JSON.stringify(d))
									
									// 跳转到下一个页面
									// uni.switchTab({
									// 	url:'/pages/home/home'
									// })
									uni.navigateTo({
										url: `/pages/diningList/diningList`
									});
								} else {
									// 处理错误情况
								}
								// 更新用户信息
								const id = uni.getStorageSync('appUserId');
								// console.log('id', id)
								uni.setStorageSync("userName", res.userInfo.nickName);
								const userInfo = {
									"userId": id,
									"nickName": res.userInfo.nickName,
									"profilePhoto": res.userInfo.avatarUrl
								}
								console.log('userInfo', userInfo)
								this.$request('post', '/system/user/editSysUser',
									userInfo, true)
							},
							fail: (error) => {
								console.error('登录失败：', error);
							}
						})
					},
					fail: (error) => {
						console.error('获取用户信息失败：', error);
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.img {
		height: 375px;
		width: 100%;
	}

	.btn {
		display: flex;
		justify-content: center;
		align-items: center;
		margin-top: 20px;
	}
</style>