<template>
	<view class="authGuide_container container">
		<view class="cont_top">
			<view class="top_info">
				<image src="@/static/img/logo.png" class="logo"></image>
				<text class="title">查研观向</text>
				<text class="info_detail">弘则弥道（上海）投资咨询有限公司</text>
			</view>
		</view>
		<view class="cont_bottom">
			<text class="auth_tips">该程序将获取以下授权：</text>
			<text class="info_detail">·获得您的公开信息（昵称、头像等）</text>
		</view>
		<!-- 授权按钮 -->
		<view class="btn_box">
			<button @click="cancelBind" class='infobutton infobutton2'>拒绝</button>
			<button open-type="getUserInfo" value="用户授权" @getuserinfo="getUserInfo" class='infobutton'>允许</button> 
		</view>
	</view>
</template>

<script>
	import { User } from '@/config/api.js'
	let app = getApp();
	export default {
		data() {
			return {
				
			};
		},
		onLoad() {
			/* 校验session */
			uni.checkSession({
				success: res => {
					if(res.errMsg != 'checkSession:ok') {
						uni.login({
						   success: result=> {
								User.wechatLog({
									Code:result.code
								}).then(res => {
									let token = res.Data.Authorization;
									this.$db.set('access_token',token);
								})
						   }
						});
					}
				},
				fail: (err) => {
					uni.login({
					   success: result=> {
							User.wechatLog({
								Code:result.code
							}).then(res => {
								let token = res.Data.Authorization;
								this.$db.set('access_token',token);
							})
					  }
					});
				}
			})
		},
		methods: {
			/* 拒绝返回上一页面 */
			cancelBind() {
				uni.navigateBack({
					delta: 1
				})
			},
			/* 传输unid 加密数据 */
			sendData(data) {
				User.sendInfo({
					EncryptedData: data.encryptedData,
					Iv: data.iv,
					RawData: data.rawData,
					Signature: data.signature
				}).then(res => {
					if(res.Ret === 200) {
						//console.log(app.globalData)
						/* 判断是否绑定过 未绑定去绑定 绑定过返回上一页面 */
						app.globalData.isBind && uni.redirectTo({
							url:'/pages/login/login'
						})
						!app.globalData.isBind && uni.navigateBack({
							delta:1
						})
					}
				})
			},
			//微信授权登录
			getUserInfo(e){
				console.log(e)
				if(e.detail.userInfo) {
					let userInfo = e.detail.userInfo;
					this.sendData(e.detail);
				}else {
					/* 用户点击了拒绝 */
					this.cancelBind();
				}
				// let that = this;
				// var p = this.getSetting();
				// p.then(function(isAuth) {
				// 	console.log('是否已经授权', isAuth);
				// 	if (isAuth) {
				// 		console.log('用户信息', e);
				// 		let userInfo = e.detail.userInfo;
				// 		that.sendData(e.detail);
				// 	} else {
				// 		/* 用户点击了拒绝 */
				// 		that.cancelBind();
				// 	}
				// });
			},
			//获取用户的当前设置
			getSetting() {
				return new Promise(function(resolve, reject) {
					uni.getSetting({
						success: function(res) {
							if (res.authSetting['scope.userInfo']) {
								resolve(true);
							} else {
								resolve(false);
							}
						},
					})
				}).catch((e) => {
					// console.log(e)
				});;
			},
			
		}
	}
</script>

<style lang="scss">
.authGuide_container {
	.cont_top {
	   padding: 119rpx 0 100rpx;
		font-size: 28rpx;
	   text-align: center;
		.top_info{
			margin: 0 auto;
		}
		.logo {
			width: 120rpx;
			height: 120rpx;
			margin: 0 auto;
		}
		.title {
			margin: 30rpx 0;
			font-size: 39rpx;
			color: #000;
		}
		.info_detail {
			color: #B2B2B2;
		}
	}
	.cont_bottom {
	   padding-left: 34rpx;
		.auth_tips {
			color: #333;
			margin-bottom: 30rpx;
		}
		.info_detail {
			color: #B2B2B2;
		}
	}
	.btn_box {
	   display: flex;
	   margin-top: 80rpx;
		justify-content: center;
		align-items: center;
		.infobutton {
			width: 216rpx;
			height: 72rpx;
			text-align: center;
			line-height: 72rpx;
			color: #fff;
			font-size: 34rpx;
			background: #07C160;
			border-radius: 4rpx;
			display: block;
			margin: 0;
			border:none;
		}
		.infobutton2 {
			background: rgba(0, 0, 0, 0.05);
			margin-right: 28rpx;
			color: #07C160;
		}
	}

}
</style>
