<template>
	<view class="normal-login-container">
		<homebar titlenav="登录"></homebar>
<!-- 		<view class="">
			<view class="login-form-content">
				<view class="input-item flex align-center">
					<view class="iconfont icon-user icon"></view>
					<input v-model="loginForm.username" class="input" type="text" placeholder="请输入账号" maxlength="30" />
				</view>
				<view class="input-item flex align-center">
					<view class="iconfont icon-password icon"></view>
					<input v-model="loginForm.password" type="password" class="input" placeholder="请输入密码"
						maxlength="20" />
				</view>
				<view class="input-item flex align-center" style="width: 60%;margin: 0px;" v-if="captchaEnabled">
					<view class="iconfont icon-code icon"></view>
					<input v-model="loginForm.code" type="number" class="input" placeholder="请输入验证码" maxlength="4" />
					<view class="login-code">
						<image :src="codeUrl" @click="getCode" class="login-code-img"></image>
					</view>
				</view>
				<view class="action-btn">
					<button @click="handleLogin" class="login-btn cu-btn block bg-blue lg round">登录</button>
				</view>
			</view>
		</view> -->
	<!-- 	<view v-else style="display: flex;justify-content: center;align-items: center;flex-direction:column;">
			<view class="h200px">
				<view class="logo-content align-center justify-center flex">
					<view style="display: flex;justify-content: center;align-items: center;flex-direction:column;">
						<image style="width: 120rpx;height: 120rpx;" :src="globalConfig.appInfo.logo" mode="widthFix">
						</image>
						<text style="font-size: 18px;margin-top: 20px;">陕西福彩</text>
					</view>
				</view>
			</view>
			<view class="w300px">
				<button :open-type="this.xy.length>0?'getPhoneNumber':''" @getphonenumber="getPhoneNumber" @click="alck()" style="background-color: #F76957;"
			</view>
			<view style="margin-top: 10rpx;">
				<u-checkbox-group v-model="xy" shape='circle' placement="row">
					<u-checkbox activeColor="red" name='zun' labelSize='12'>
					</u-checkbox>
					<view class="" style="display: flex; font-size: 25rpx; line-height: 18px;">
						我已阅读并同意<u--text type="primary" size='12' text="《隐私协议》" @click="toageree('/pages_loader/pages/agreement/agreement')"></u--text>
						与<u--text type="primary" size='12' text="《服务协议》" @click="toageree('/pages_loader/pages/agreement/service')"></u--text>
					</view>
				</u-checkbox-group>
			</view>
		</view> -->
		<view style="display: flex;justify-content: center;align-items: center;flex-direction:column;margin-top: 500rpx;">
			<!-- <view class="w300px">
				<button :open-type="this.xy.length>0?'getPhoneNumber':''" @getphonenumber="getPhoneNumber" @click="alck()" style="background-color: #F76957;"/>
			</view> -->
			<view class="w300px">
				<button open-type="getPhoneNumber"  @getphonenumber="getPhoneNumber"  style="background-color: seagreen;color: #ffffff;">一键登录</button>
			</view>
			<view style="margin-top: 10rpx;">
				<u-checkbox-group v-model="xy" shape='circle' placement="row">
					<u-checkbox activeColor="red" name='zun' labelSize='12'>
					</u-checkbox>
					<view class="" style="display: flex; font-size: 25rpx; line-height: 18px;">
						我已阅读并同意<u--text type="primary" size='12' text="《隐私协议》"></u--text>
						与<u--text type="primary" size='12' text="《服务协议》"></u--text>
					</view>
				</u-checkbox-group>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getCodeImg,
	} from '@/api/login'
	import {
		getOpenId
	} from '@/utils/auth'
	export default {
		data() {
			return {
				xy: [],
				codeUrl: "",
				captchaEnabled: true,
				// 用户注册开关
				register: false,
				showanniu: false,
				globalConfig: getApp().globalData.config,
				loginForm: {
					username: "admin",
					password: "admin123",
					code: "",
					uuid: ''
				},
				userInfo: {},
			}
		},
		onLoad() {
			// this.getCode()
		},
		onShow() {
			this.getuserinfoxxV1();
		},
		methods: {
			// 查看协议
			toageree(url){
				uni.navigateTo({
					url:url
				})
			},
			alck(){
				if (this.xy.length == 0) {
					uni.$u.toast('请勾选协议!')
					return;
				}else{
					uni.reLaunch({
						url:'/pages/centerlogin'
					})
				}
			},
			//绑定手机号
			getPhoneNumber(e) {
				let that = this
				 console.log(e,'xxadadadada')
				if (e.detail.errMsg == "getPhoneNumber:fail user deny") { //用户决绝授权         
					//拒绝授权后弹出一些提示  
					this.$modal.msgError("请允许获取本机手机号一键登录");
				} else {
					let data = {
						encryptedData: e.detail.encryptedData,
						iv: e.detail.iv,
						openId: getOpenId()
					}
					that.$store.dispatch('getWxUserPhone', data).then((res) => {
						let data1 = {
							nickName: '微信用户',
							avatar: undefined,
							phone: res.msg,
							openId: getOpenId()
						}
						that.$store.dispatch('phoneLogin', data1).then((res1) => {
							that.loginSuccess()
						}).catch((err) => {
							//console.log(err, 'err');
						})
					}).catch((err) => {
						//console.log(err, 'err');
					})
				}
			},
			// 用户注册
			handleUserRegister() {
				this.$tab.redirectTo(`/pages/register`)
			},
			// 隐私协议
			handlePrivacy() {
				let site = this.globalConfig.appInfo.agreements[0]
				this.$tab.navigateTo(`/pages/common/webview/index?title=${site.title}&url=${site.url}`)
			},
			// getuserinfoxx() {
			// 	let that = this
			// 	uni.showLoading({
			// 		title: "正在加载"
			// 	})
			// 	wx.getUserProfile({
			// 		desc: '用于完善用户资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
			// 		success: (res) => {
			// 			console.log(res, '获取的用户资料');
			// 			uni.login({
			// 				"provider": "weixin",
			// 				"onlyAuthorize": true, // 微信登录仅请求授权认证
			// 				success: function(event) {
			// 					console.log(event, '获取的code');
			// 					let data = {
			// 						appId: 'wx3f2d8a2a2afdcc37',
			// 						code: event.code,
			// 						nickName: res.userInfo.nickName,
			// 						avatar: res.userInfo.avatarUrl
			// 					}
			// 					that.$store.dispatch('wxLogin', data).then((res) => {
			// 						//console.log('111', res);
			// 						that.$modal.closeLoading()
			// 						that.loginSuccess()
			// 					}).catch((err) => {
			// 						//console.log(err, 'err');
			// 					})
			// 					wxLogin().then(ress => {
			// 						if (res.code == 200) {
			// 							//console.log(ress, '登录成功');
			// 						}
			// 					})
			// 				},
			// 				fail: function(err) {
			// 					// 登录授权失败  
			// 					// err.code是错误码
			// 					//console.log(err, '.......');
			// 				}
			// 			})
			// 			//console.log(res, '获取的用户信息');
			// 		}
			// 	})
			// },
			getuserinfoxxV1() {
				let that = this
				uni.login({
					"provider": "weixin",
					"onlyAuthorize": true, // 微信登录仅请求授权认证
					success: function(event) {
						// console.log(event, '获取的code');
						let data = {
							appId: 'wxbde64edd3cb15f72',
							code: event.code,
						}
						that.$store.dispatch('getOpenIdByCode', data).then((res) => {
							
						}).catch((err) => {
							//console.log(err, 'err');
						})
					},
					fail: function(err) {}
				})
			},
			// 用户协议
			handleUserAgrement() {
				let site = this.globalConfig.appInfo.agreements[1]
				this.$tab.navigateTo(`/pages/common/webview/index?title=${site.title}&url=${site.url}`)
			},
			// 获取图形验证码
			getCode() {
				// //console.log(1111, '获取的验证码');
				getCodeImg().then(res => {
					//console.log(res, '获取的验证码');
					this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled
					if (this.captchaEnabled) {
						this.codeUrl = 'data:image/gif;base64,' + res.img
						this.loginForm.uuid = res.uuid
					}
				})
			},
			// 登录方法
			handleLogin() {
				if (this.loginForm.username === "") {
					this.$modal.msgError("请输入您的账号")
				} else if (this.loginForm.password === "") {
					this.$modal.msgError("请输入您的密码")
				} else {
					this.$modal.loading("登录中，请耐心等待...")
					this.pwdLogin()
				}
				// else if (this.loginForm.code === "" && this.captchaEnabled) {
				// 				this.$modal.msgError("请输入验证码")
				// 			}
			},
			// 密码登录
			pwdLogin() {
				// this.loginForm.password = rsa.getRsaCode(this.loginForm.password);
				this.$store.dispatch('Login', this.loginForm).then((res) => {
					this.loginSuccess()
					// uni.switchTab({
					// 	url: '/pages/index'
					// });
				}).catch((err) => {
					alert(err.msg)
					if (this.captchaEnabled) {
						this.$modal.msgError("哇大王的")
						this.getCode()
					}
				})
			},
			// 登录成功后，处理函数
			loginSuccess(result) {
				// 设置用户信息
				//console.log('aaaaaa');
				this.$store.dispatch('GetInfo').then(res => {
					this.$tab.reLaunch('/pages/centerlogin')
				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #ffffff;
	}

	.normal-login-container {
		width: 100%;

		.logo-content {
			width: 100%;
			font-size: 21px;
			text-align: center;
			padding-top: 15%;

			image {
				border-radius: 4px;
			}

			.title {
				margin-left: 10px;
			}
		}

		.h200px {
			display: flex;
			align-items: center;
			justify-content: center;
			height: 370px;
		}

		.w300px {
			width: 500rpx;
		}

		.login-form-content {
			text-align: center;
			margin: 20px auto;
			margin-top: 15%;
			width: 80%;

			.input-item {
				margin: 20px auto;
				background-color: #f5f6f7;
				height: 45px;
				border-radius: 20px;

				.icon {
					font-size: 38rpx;
					margin-left: 10px;
					color: #999;
				}

				.input {
					width: 100%;
					font-size: 14px;
					line-height: 20px;
					text-align: left;
					padding-left: 15px;
				}

			}

			.login-btn {
				margin-top: 40px;
				height: 45px;
				background-color: blue;
			}

			.reg {
				margin-top: 15px;
			}

			.xieyi {
				color: #333;
				margin-top: 20px;
			}

			.login-code {
				height: 38px;
				float: right;

				.login-code-img {
					height: 38px;
					position: absolute;
					margin-left: 10px;
					width: 200rpx;
				}
			}
		}
	}
</style>