<template>
	<view class="containar">
		<view class="avatarUrl" style="text-align: center">
			<!-- <image src="../../static/index/shouquan.png" style="width: 180rpx;height: 180rpx;" class="refreshIcon"></image> -->
			<view style="color: rgba(30, 125, 251, 1); font-weight: 500">新糖宝</view>
		</view>

		<view class="btn" style="margin-top: 60px; height: 600rpx">
			<button v-if="qiehuan" @click="denglu" style="background-color: rgba(30, 125, 251, 0.8); color: #fff; width: 650rpx">微信登录</button>
			<button v-else open-type="getPhoneNumber" @getphonenumber="getPhoneNumber" style="background-color: rgba(30, 125, 251, 0.8); width: 650rpx; color: #fff">
				微信登录
			</button>

			<checkbox-group @change="checkboxChange" style="margin-top: 30rpx; margin-left: 50rpx">
				<checkbox color="rgba(30, 125, 251, 1)" style="transform: scale(0.6)" value="agree" :checked="check" shape="circle" />
				<text style="color: rgba(166, 166, 166, 1); font-size: 26rpx" bindtap="toprotocol">我已阅读并同意</text>
				<text style="color: rgba(30, 125, 251, 1); font-size: 26rpx" @click="toptong">《用户协议》</text>
				、
				<text style="color: rgba(30, 125, 251, 1); font-size: 26rpx" @click="toptong">《隐私政策》</text>
			</checkbox-group>
		</view>
	</view>
</template>
<script>
export default {
	data() {
		return {
			access_token: '',
			code: '',
			nickName: '',
			encryptedData: '',
			iv: '',
			avatarUrl: '',
			agree: '', //默认勾选状态
			check: false,
			qiehuan: true
		}
	},
	onLoad() {
		// this.shouquan()
		this.access_token = wx.getStorageSync('access_token')

		uni.login({
			provider: 'weixin',
			success: (res) => {
				// 获取用户信息
				uni.getUserInfo({
					provider: 'weixin',
					success: function (res) {
						console.log('res', res)
					}
				})
				console.log('res-login', res)
				//请求登录接口
				if (res.errMsg == 'login:ok') {
				}
			}
		})
	},
	methods: {
		//勾选协议
		checkboxChange(e) {
			console.log(e.detail.value)
			this.agree = e.detail.value[0]
			this.qiehuan = this.agree == undefined || this.agree == ''
		},
		denglu() {
			wx.showToast({
				title: '请先勾选用户协议',
				icon: 'none',
				duration: 2000
			})
		},
		//用户协议跳转
		toptong() {
			uni.navigateTo({
				url: '../../packageThree/pages/yonghuxieyi/yonghuxieyi'
			})
		},
		getPhoneNumber(e) {
			if (this.qiehuan) {
				wx.showToast({
					title: '请先勾选用户协议',
					icon: 'none',
					duration: 2000
				})
			} else {
				const that = this
				console.log(e, '信息') //在这里的e里会有code等信息
				let numberCode = e.detail.code //取出code
				that.numberCode = numberCode
				console.log(numberCode)

				this.$axios
					.get('/mp/user/userAuthorizedLogin', {
						params: {
							code: wx.getStorageSync('code'),
							iv: wx.getStorageSync('iv'),
							encryptedData: wx.getStorageSync('encryptedData'),
							numberCode: that.numberCode
						},
						headers: {
							Authorization: 'Basic c2FiZXI6c2FiZXJfc2VjcmV0',
							'Blade-Auth': wx.getStorageSync('access_token'),
							'Tenant-Id': '000000'
						}
					})
					.then((response) => {
						let res = response
						console.log(res)
						if (res.success) {
							var userId = res.data.userId
							wx.setStorageSync('userId', userId)
							var huanzheNichen = res.data.name
							wx.setStorageSync('huanzheNichen', huanzheNichen)
							var createTime = res.data.createTime
							wx.setStorageSync('createTime', createTime)
							uni.switchTab({
								url: '/pages/index/index'
							})
						} else {
							console.log('患者端授权登录失败')
						}
					})
			}
		},
		//授权
		shouquan() {
			this.$axios
				.post(
					`/blade-auth/oauth/token?password=ceb8447cc4ab78d2ec34cd9f11e4bed2&tenantId=000000&username=test`,
					{},
					{
						headers: {
							Authorization: 'Basic c2FiZXI6c2FiZXJfc2VjcmV0'
						}
					}
				)
				.then((res) => {
					console.log(res)
					var access_token = res.access_token
					this.access_token = res.access_token
					// localStorage.setItem('access_token',access_token)网页
					wx.setStorageSync('access_token', access_token) //小程序
				})
		}
	}
}
</script>
<style lang="scss">
.containar {
	background: #fff;
	.avatarUrl {
		padding: 80rpx 0 40rpx;
		background: #fff;
		button {
			background: #fff;
			line-height: 80rpx;
			height: auto;
			width: auto;
			padding: 20rpx 30rpx;
			margin: 0;
			// display: flex;
			justify-content: center;
			align-items: center;
			.refreshIcon {
				// width: 160rpx;
				// height: 160rpx;
				border-radius: 50%;
			}
			.jt {
				width: 14rpx;
				height: 28rpx;
			}
		}
	}
	.nickname {
		background: #fff;
		padding: 20rpx 30rpx 80rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		.weui-input {
			padding-left: 60rpx;
		}
	}
	.btn {
		width: 100%;
		.btn-sub {
			width: 670rpx;
			margin: 80rpx auto 0;
			height: 90rpx;
			background: rgba(30, 125, 251, 1);
			border-radius: 45rpx;
			line-height: 90rpx;
			text-align: center;
			font-size: 36rpx;
			color: #fff;
		}
	}
}
</style>
