<template>
	<view>
		<view>
			<view>
				<view>
					<view class='header'>
						<image src='../../static/new_images/wx_login.png'></image>
					</view>
					<view class='content'>
						<view>申请获取以下权限</view>
						<text>获得你的公开信息(手机号等)</text>
					</view>
					<button class='bottom' type='primary' lang="zh_CN" open-type="getPhoneNumber"
						@getphonenumber="decryptPhoneNumber">
						一键授权登录
					</button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				code: '',
				listdata: {},
				aesIv: '',
				encryptedData: '',
				// 上级绑定下级用户
				form: {
					CommissionerUserId: 0,
					ManagerUserId: 0,
				},
				// 类型
				// Type:0,
				model:{
					username:'',
					picture:''
				}
			}
		},

		// 打开页面的时候
		onLoad(data) {
			this.getCode()
			console.log('wwwww',data)
			console.log('bbbbbb',JSON.parse(decodeURIComponent(data.forms)))
				let dataform=JSON.parse(decodeURIComponent(data.forms))
				this.form.ManagerUserId=dataform.ManagerUserId
				// this.Type=dataform.Type
			console.log('登录传参',this.form)
			// console.log('登录Type',this.Type)
			// console.log('直接跳转login携带分享者id', decodeURIComponent(data.scene))
			// this.form.ManagerUserId =parseInt(decodeURIComponent(data.scene).split(',')[0])
			// this.form.Type =decodeURIComponent(data.scene).split(',')[1]
			// console.log('类型',this.form.Type)
			// console.log('登录二维码数据复制后', this.form.ManagerUserId)
		},
		methods: {
			//1，code
			getCode() {
				//获取code
				let that = this
				uni.login({
					success(res) {
						that.code = res.code
					}
				})
			},
			IsType(){
					//By:wangming
					//这里是绑定分享者
					this.API.BindUserInfo(this.form).then(res=>{
						console.log('绑定后的数据专员', res)
						if (res.data.code == 200) {
							uni.showToast({
								title: '绑定成功',
								icon: 'success'
							})
							let storagedata = uni.getStorageSync('userinfo')
							storagedata.data = res.data.data
							uni.setStorageSync('userinfo', storagedata)
						} else {
							uni.showToast({
								title: res.data.message,
								icon: 'none'
							})
						}
						setTimeout(() => {
							uni.switchTab({
								url: '../index/index'
							})
						}, 2000)
					})
				
				
				
			},
			
			decryptPhoneNumber(res) {
				// this.GetUserInfo()
				let that = this
				let data111 = {
					code: this.code,
					aesIv: res.detail.iv,
					encryptedData: res.detail.encryptedData,
				}
				//登录
				that.API.WeCharLogin(data111).then(res1 => {
					console.log('返回数据', res1)
					console.log('登录后的信息', res1.data.data)
					if (res1.data.code == 200) {
						uni.setStorage({
							key: 'userinfo',
							data: res1.data.data
						})
						uni.setStorage({
							key: 'token',
							data: res1.data.data.token
						})
						uni.showToast({
							title: '登录成功',
							icon: 'success'
						})
						console.log('manager',this.form.ManagerUserId)
						if (this.form.ManagerUserId == 0 || this.form.ManagerUserId == undefined || this.form.ManagerUserId == 'undefined' || isNaN(this.form.ManagerUserId)) {
							// 不分享二维码跳转的页面
							setTimeout(() => {
								uni.switchTab({
									url: '../index/index'
								})
							}, 1000)
						} else {
							this.form.CommissionerUserId = parseInt(res1.data.data.data.id)
							this.IsType()
						}
					} else {
						uni.showToast({
							title: '登录错误',
							icon: 'none'
						})
					}
					console.log(res1)
				})
			},
			// ------------------------------------
			oneClickLogin() {
				let _this = this;
			}

		}
	}
</script>

<style>
	.header {
		margin: 90rpx 0 90rpx 50rpx;
		border-bottom: 1px solid #ccc;
		text-align: center;
		width: 650rpx;
		height: 300rpx;
		line-height: 450rpx;
	}

	.header image {
		width: 200rpx;
		height: 200rpx;
	}

	.content {
		margin-left: 50rpx;
		margin-bottom: 90rpx;
	}

	.content text {
		display: block;
		color: #9d9d9d;
		margin-top: 40rpx;
	}

	.bottom {
		border-radius: 80rpx;
		margin: 70rpx 50rpx;
		font-size: 35rpx;
	}
</style>
