<template>
	<view class="container">
		<view class="authModal">
			<view class='header'>
				<image src='https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1582442269885&di=244f11fe921a75d4be2b7f41bded8a48&imgtype=0&src=http%3A%2F%2Fimg.qqzhi.com%2Fuploads%2F2018-12-06%2F062323200.jpg'></image>
			</view>
			<view class='content'>
				<view>申请获取以下权限</view>
				<text>获得你的公开信息(昵称，头像、地区等)</text>
			</view>

	<!-- 		<button class='bottom' type='primary' open-type="getUserInfo" withCredentials="true" lang="zh_CN" @getuserinfo="wxLogin">
				授权登录
			</button> -->
		</view>
	</view>
</template>

	
<script>
	import {showCustomToast} from '../../common/util.js'
	import {login} from '../../api/login.js'
	export default {
		onLoad() {
			/**
			 * 
			 * {"errMsg":"login:ok","code":"0f1q6Tll2gxgEe4Apiml2fxzdl4q6Tlh"}
			 * {
					"encryptedData": "z47dFJgWWcFlRiN38qzwykrxraYUj/caxt8nNJb/YonFtuGtMHCirL5G/GKWs/T7UDfmREUgyJ3z9nBiuCDE9XIbDslKTO3hpdM0Lw98gVZKOMtSjbmtAYTLbkUdDn4J6RdPYLIdCF8pUaqSZxkD+41tXW1JXKYrEuLWAZ5fmxn4rV0vCsnS+ciRdUWToq8s/KkMNHb0/1nlIyho5dz5GO1ac+BNvfk0YPrgkQdvMSCzB5NAQxUfFQERyErMW9dfQeQLXpelu0F00q778V1xAgmFWFh9IFIwVUyzSsCsMQHZpDPZT8nM3l/kNPRpK50L2msSxJ9H7lp2M8oUcQDPRttPakj1TyYb3iGDs6dx4lMBA38qbw5IYvWmrXw+A7avz/o+0u0RRrUR99u+ert6V1cg8ubDbMJGDqzAPlsTt6OdzIsGNZfddVZ/HR/9vJQc5OCgnMFRVDU9S+OTZbFLHQ==", 
					"iv": "kdDnOvCrhjUS5J1vA5fK9A==", 
					"signature": "7b9c21a62909b06fc1ba8291c808b4ec9fddde6d", 
					"userInfo": {
						"nickName": "微信用户", 
						"gender": 0, 
						"language": "", 
						"city": "", 
						"province": "", 
						"country": "", 
						"avatarUrl": "https://thirdwx.qlogo.cn/mmopen/vi_32/POgEwh4mIHO4nibH0KlMECNjjGxQUq24ZEaGT4poC6icRiccVGKSyXwibcPq4BWmiaIGuG1icwxaQX6grC9VemZoJ8rg/132"
					}, 
					"rawData": "{\"nickName\":\"微信用户\",\"gender\":0,\"language\":\"\",\"city\":\"\",\"province\":\"\",\"country\":\"\",\"avatarUrl\":\"https://thirdwx.qlogo.cn/mmopen/vi_32/POgEwh4mIHO4nibH0KlMECNjjGxQUq24ZEaGT4poC6icRiccVGKSyXwibcPq4BWmiaIGuG1icwxaQX6grC9VemZoJ8rg/132\"}", 
					"errMsg": "getUserInfo:ok"
				}
			 */
			const token = uni.getStorageSync('accessToken')
			if(!token){
				this.wxLogin()
			}
			

		},
		data() {
			return {
				
			}
		},
		methods: {
			async wxLogin(){
				  const loginRes = await this.getWxCode()
				  if(!loginRes.code){
					  return
				  }
				  const userInfo = await this.getUserInfo()
				  if(!userInfo.iv){
						return
				  }
				  await this.getToken(loginRes.code, userInfo)
				  
			},
			getWxCode(){
				return new Promise((resolve, reject) => {
				      uni.login({
				        provider: 'weixin',
				        success: (loginRes) => {
				          console.log('loginRes: ' + JSON.stringify(loginRes));
				          resolve(loginRes);
				        },
				        fail: (err) => reject(err) // 添加失败处理
				      });
				    });
			},
			getUserInfo(){
				return new Promise((resolve, reject) => {
				  uni.getUserInfo({
					provider: 'weixin',
					success: (infoRes) => {
					  console.log('infoRes：' + JSON.stringify(infoRes));
					  resolve(infoRes);
					},
					fail: (err) => reject(err) // 添加失败处理
				  });
				});
			},
			async getToken(code, userInfo){
				let res = await login({
					data: {
						 "code": code,
						  "encryptedIv": userInfo.iv,
						  "encryptedData": userInfo.encryptedData
					},
				})
				console.log('res', res)
				if(res){
					uni.setStorageSync('accessToken', res.token)
					console.log('获取token', res.token)
				}
				
				// let [error, res] = await uni.request({
				// 	url: `http://192.168.2.14:8080/weixin/wxLogin`,
				// 	data: {
				// 		 "code": code,
				// 		  "encryptedIv": userInfo.iv,
				// 		  "encryptedData": userInfo.encryptedData
				// 	},
				// 	method: 'POST',
				// 	header: {
				// 		// 'accessToken': uni.getStorageSync('accessToken'),
				// 	}
				// });
				
				// if (error) {
				// 	showCustomToast({
				// 		title: '网络错误'
				// 	})
				// 	return
				// }
				// if(res.data.code === 200){
				// 	uni.setStorageSync('accessToken', res.data.token)
				// }
			}
			
		}
	}
</script>

<style>

</style>
