<!-- <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 {
				infoData: {},
				src: '../../static/first.png',
				userInfo: {},
				hasUserInfo: false,
				canIUseGetUserProfile: wx.getUserProfile ? true : false,
			}
		},
		methods: {
			getUserProfile() {
				wx.login({
					success: async (e) => {
						console.log('数据：', e)
						try {
							const result = await this.$request('post', '/auth/miniLogin', {
								tenantId: '000000',
								clientId: '8871d05eacc4406083d3bb0a085b6999',
								code: e.code
							})
							console.log('登录成功：', result);
							if (result && result.result !== false) {
								var token = result.data.accessToken;
								var haveOpenid = result.data.openId;
								var tenantId = result.data.tenantId
								uni.setStorageSync("token", token);
								wx.getUserProfile({
									desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
									success: (res) => {
										this.setData({
											userInfo: res.userInfo,
											hasUserInfo: true
										})
									}
								})
								console.log('userInfo', this.userInfo)
								uni.navigateTo({
									url: `/pages/diningList/diningList`
								});
							} else {
								// 处理错误情况
							}
						} catch (error) {
							// 处理请求失败的情况
							console.error('登录失败：', error);
						}
					}
				})

				// let that = this;
				// wx.login({
				// 	success: function(e) {
				// 		console.log('数据：', e)

				// 		uni.request({
				// 			url: 'http://36.137.224.81:8030/auth/miniLogin?tenantId=000000&code=' + e
				// 				.code,
				// 			header: {
				// 				"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8", //
				// 				// "Content-Type": "application/json;charset=UTF-8", //
				// 			},
				// 			method: 'post',
				// 			success: res => {
				// 				console.log('登录成功：', res)
				// 				uni.hideLoading()
				// 				var token = res.data.data.accessToken;
				// 				console.log('token：', token)
				// 				uni.setStorageSync("token", token);
				// 				uni.navigateTo({
				// 					url: `/pages/classify/classify`
				// 				})
				// 				// if (res.data.code == 200) {
				// 				// 	that.infoData = res.data.data
				// 				// }
				// 			},
				// 			fail: err => {
				// 				uni.hideLoading()
				// 			}
				// 		})
				// 	}
				// })

			}
		}
	}
</script>

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

	.btn {
		display: flex;
		justify-content: center;
		align-items: center;
		margin-top: 20px;
	}
</style> -->
<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() {
			// 检查微信版本是否支持 getUserProfile
			if (wx.getUserProfile) {
				this.canIUseGetUserProfile = true;
			}
		},
		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', '/auth/miniLogin', {
									tenantId: '000000',
									// clientId: '8871d05eacc4406083d3bb0a085b6999',
									code: loginRes.code
								})
								console.log('登录成功：', result);
								if (result && result.data !== false) {
									// 保存token
									uni.setStorageSync("token", result.data.accessToken);
									uni.setStorageSync("openId", result.data.openId);
									uni.setStorageSync("tenantId", result.data.tenantId);
									uni.setStorageSync("appUserId", result.data.appUserId);
									// 跳转到下一个页面
									uni.navigateTo({
										url: `/pages/diningList/diningList`
									});
								} else {
									// 处理错误情况
								}
								// 更新用户信息
								const id = uni.getStorageSync('appUserId');
								console.log('id', id)

								const userInfo = {
									"id": id,
									"nickName": res.userInfo.nickName,
									"profilePhoto": res.userInfo.avatarUrl,
								}
								console.log('userInfo', userInfo)
								this.$request('post', '/system/appUser/editAppUser',
									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>