<template>
	<view class="content">
		<view class="head">
			<view class="text">
				<view class="text1">
					<view style="color: #666666;">输入短信验证码</view>
					<view style="color: #303030;margin-top: 20rpx;">短信已发送至{{phone}}，请在下方输入框内输入4位数字验证码</view>
				</view>
			</view>
		</view>
		<view class="sction">
			<view class="mama">
				<view class="mamaz">
					<input type="text" v-model="msg" maxlength="4" placeholder="请输入验证码" placeholder-class="input-class" class="input" />
				</view>
				<view class="mamay">
					<view class="sada" v-if="sendAuthCode" style="font-size: 35rpx;" @click="getAuthCode">获取验证码</view>
					<text class="sada" v-if="!sendAuthCode">
						重新发送
						<text>({{ auth_time }})</text>
					</text>
				</view>
			</view>
			<view class="login-btn" :style="{ background: msg == '' ? '#F2F2F2' : '#2671E2' }" @click="denglu"><text
					class="">登录</text></view>
			<view class="code-login" @click="passwordlogin"><text>密码登录</text></view>
		</view>
	</view>
</template>

<script>
	var util = require("../../utils/util.js");
	var config = require("../../config");
	var app = getApp();
	var WXB_SESSION_LOGIN_DATA = 'weapp_session_login_data';
	export default {
		data() {
			return {
				sendAuthCode: true,
				auth_time: 0,
				msg: '',
				phonecet: '',
				role:'',
				phone:''
			};
		},
		onLoad: function(options) {
			var phonese = options.username;
			var phonexxz = phonese.substring(0, 3) + '****' + phonese.substring(7);
			this.phone = phonexxz,
			this.phonecet = options.username,
			this.role = options.role
			
			this.sendAuthCode = false;
			this.auth_time = 60;
			var auth_timetimer = setInterval(() => {
				this.auth_time--;
				if (this.auth_time <= 0) {
					this.sendAuthCode = true;
					clearInterval(auth_timetimer);
				}
			}, 1000);
		},
		methods: {
			//跳转密码登录页面
			passwordlogin() {
				uni.reLaunch({
					url: '/pages/login/index',
				})
			},
			//获取验证码
			getAuthCode() {
				uni.request({
					url: config.service.sendCode + "/" + this.phonecet,
					method: "GET",
					header: {
						'content-type': 'application/json',
						// 'Authorization': 'Bearer '+uni.getStorageSync('weapp_session_login_data').token
					},
					success: (data) => {
						console.log(data)
						// 成功地响应会话信息
						if (data.data.code == 0) {
							util.showSuccess('发送成功');
							    this.sendAuthCode = false;
							    this.auth_time = 60;
							    var auth_timetimer = setInterval(() => {
							    	this.auth_time--;
							    	if (this.auth_time <= 0) {
							    		this.sendAuthCode = true;
							    		clearInterval(auth_timetimer);
							    	}
							    }, 1000);
						} else {
							util.showNone(data.data.message);
							return false;
						}
					},
				})
			},
			//登录
			denglu() {
				var that = this;
				if (this.msg=='') {
					uni.showToast({
						title: '验证码不能为空',
						icon: 'none'
					});
					return
				}
				if (this.msg.length == 4) {
					// let porme = {
					// 	loginName: this.phonecet,
					// 	password: this.msg
					// }
					uni.request({
						url: config.service.plogin+'?mobile=SMS@'+this.phonecet+'&code='+this.msg+'&grant_type=mobile',
						method: "POST",
						header: {
							'Authorization': 'Basic cGlnOnBpZw=='
						},
						data: {
							mobile:'SMS@'+this.phonecet,
							code:this.msg,
							grant_type:'mobile'
						},
						success: (result) => {
						var data = result.data; //console.log("登陆信息", data);
							if (data) {
								var res = data;
								if (res.code!=1) {
									var data = {
										'token': res.access_token
									};
									uni.setStorageSync(WXB_SESSION_LOGIN_DATA, data); //写入缓存
									that.getMenu()
									that.getUser();
									util.showSuccess('登录成功');
								} else {
									util.showNone("账号名或密码错误，请重试");
									return false;
								} 
							} else {
								util.showNone(data.message);
								return false;
							}
						},
					})
				} else {
					uni.showToast({
						title: '验证码位数不正确',
						icon: 'none'
					});
				}
			},
			getMenu(){
				uni.request({
					url: config.service.getMenu,
					method: "POST",
					header: {
						'content-type': 'application/json',
						'Authorization': 'Bearer '+uni.getStorageSync('weapp_session_login_data').token
					},
					success: (data) => {
						let rescor=data.data;
						if (rescor.code == 0) {
							uni.setStorageSync("weapp_session_Menu_data", rescor.data)
						}else{
							uni.showToast({
							    title: rescor.msg,
							    duration: 2000,
								icon:"none"
							});
							return
						}
					}
				})
			},
		getUser(){
			util.getRequestPromise(config.service.getUser, {}, false, "GET").then(data => {
				if (data.user.total==0) {
					uni.showToast({
					    title: '暂无绑定楼盘',
					    duration: 2000,
						icon:"none"
					});
					return
				} else {
						let lopan = {
							id: data.houseList[0].id,
							name: data.houseList[0].propertyName
						}
						let users=data.user
						users.zkProperties=data.houseList
						uni.setStorageSync("fendianindex", 0); //写入缓存
						
						uni.setStorageSync("weapp_session_userInfo_data",users); //写入缓存
						
						uni.setStorageSync("buildingID", lopan); //楼盘id写入缓存
						uni.switchTab({
							url: '/pages/index/index'
						});
				}
			
			});
		},
		}
	};
</script>

<style lang="scss">
	.head {
		width: 750rpx;
		height:400rpx;
		position: relative;
		.text{
			width: 100%;
			position: absolute;
			top: 180rpx;
			left: 0rpx;
			.text1{
				width: 80%;
				margin: 0 auto;
			}
		}
	}
	.mama {
		width: 620rpx;
		display: flex;
		border-bottom: 1px solid #E1E1E1;
		margin: 60rpx auto;
	}

	.sadsadasdasdsadasd {
		font-size: 34rpx;
		font-family: PingFangSC-Medium, PingFang SC;
		font-weight: 500;
		color: #008EF2;
		width: 100%;
		text-align: center;
		padding-top: 340rpx;
	}

	.mamaz {
		width: 50%;
		height: 90rpx;
	}

	.mamay {
		width: 50%;
		height: 90rpx;
	}

	.input {
		width: 350rpx;
		color: #78DFB0;
		height: 100%;
		line-height: 90rpx;
		font-size: 17px;
		color: #171717;
	}

	.sada {
		width: 70%;
		line-height: 90rpx;
		color: #2B6EFF;
		text-align: right;
		font-size: 35rpx;
		margin-left: 100rpx;
		display: block;
	}
	.content {
		margin: 0;
		padding: 0;
		border-top: 1rpx solid #E0E0E0;
	}

	.login-text {
		font-size: 60rpx;
		font-family: PingFang SC;
		font-weight: 500;
		color: rgba(23, 23, 23, 1);
		letter-spacing: 8rpx;
		margin-left: 75rpx;
		font-weight: bold;
	}

	.login-input {
		border-bottom: 1px solid #e1e1e1;
		color: #c9cac9;
		margin: 98rpx 64rpx 200rpx 75rpx;
		font-size: 17px;

	}

	.login-btn {
		margin: 0 auto;
		margin-top: 240rpx;
		width: 630rpx;
		height: 86rpx;
		border-radius: 49rpx;
		font-size: 17px;
		font-weight: bold;
		border: none;
		display: flex;
		justify-content: center;
		align-items: center;
		color: #ffffff;
	}

	.code-login {
		display: flex;
		justify-content: center;
		margin-top: 40rpx;
		color: #D6D7D6;
		margin-bottom: 200rpx;
		font-size: 30rpx;
	}

	.log-box {
		display: flex;
		flex-direction: row;
		justify-content: center;
		align-items: center;
		font-size: 28rpx;
		color: #bfc0bf;
		margin-bottom: 65rpx;
	}

	.hengx {
		margin: 0 20rpx 0 20rpx;
		height: 2rpx;
		width: 232rpx;
		background: #e1e1e1;
	}

	.wechat {
		display: flex;
		justify-content: center;
		align-items: center;
		width: 93rpx;
		height: 93rpx;
		background: rgba(255, 255, 255, 1);
		box-shadow: 0px 10rpx 30rpx rgba(120, 223, 176, 0.22);
		border-radius: 50%;
		margin: 0 auto;
	}

	.btn-get {
		width: 250rpx;
		font-size: 17px;
		color: #c9cac9;
		position: relative;
		top: -290rpx;
		right: -450rpx;
	}

	.input-class {
		color: #D6D7D6;
		font-size: 17px;
		letter-spacing: 1rpx;
	}
</style>
