<template>
	<view class="disjcac fc">
		<image src="/static/icon/icon-login.png" style="width: 220rpx;height: 220rpx;margin: 320rpx 0 70rpx 0;" mode=""></image>
		<view class="fon36 bold">洛河民意通</view>
		<view class="disjcac enter-btn posir">
			授权登录
			<button :disabled="ifFlag" @click="bindGetUserInfo" class="posia syxzo">立即授权</button>
		</view>
		<view class="fon30" style="color: #484848;">如您点击授权,您将同意授权《免责声明》</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userInfo:'',
				ifFlag:false
			}
		},
		methods: {
			//调起登录授权
			bindGetUserInfo() {
				if(!this.ifFlag){
					this.$toolAll.tools.showToast('正在调起授权','none',6000);
					this.ifFlag = true;
					wx.getSetting({
						success: (result)=> {
							uni.hideToast();
					        // 判断是否授权
					        if (result.authSetting['scope.userInfo']) {
								// 已授权
								this.getUserInfoEv();
							} else {
								// 未授权
								this.getUserInfoEv();
							}
					    }
					})
				}
			},
			// 获取用户信息事件
			getUserInfoEv(){
				// #ifdef MP-WEIXIN
				// 微信新版获取用户信息方式(也可用于授权手机号)
				wx.getUserProfile({
					lang:'zh_CN',
					desc:'获取亲的昵称、头像及性别',
					success: (resEnd)=> {
						this.userInfo = resEnd.userInfo;
						console.log(resEnd,7777);
						//用户已经授权过,添加用户信息
						uni.login({
							provider: 'weixin',
							success: (rescode)=> {
								uni.reLaunch({
									url:'/pagesA/passageway/passageway'
								})
								// this.updateUserInfo(rescode.code);
							},
						});
					},
					complete: () => {
						this.ifFlag = false;
					}
				});
				// #endif
			},
			//调用登录接口
			updateUserInfo(code) {
				var params = {
					code:code,
					nickname: this.userInfo.nickName,//用户昵称
					headimgurl: this.userInfo.avatarUrl,//用户头像
					country: this.userInfo.country,//用户所在国家
					province: this.userInfo.province,//用户所在省份
					city: this.userInfo.city,//用户所在城市
					gender: this.userInfo.gender,//用户性别
					language:this.userInfo.language,//语言
					is_active:1
				}
				this.$requst.post('user/login',params).then(res => {
					if(res.data.token!=''){
						if(uni.getStorageSync('urlparams')) {
							uni.reLaunch({ // 重新进入当前页面
								url:uni.getStorageSync('urlparams')
							})
						} else {
							uni.reLaunch({
								url:'/pagesA/passageway/passageway'
							})
						}
					}
				},error => {})
			}
		}
	}
</script>

<style>

</style>