<template>
	<view>
		<view class="logo">
			<view class="img">
				<image mode="widthFix"
					src="data:image/png;base64, ">
				</image>
			</view>
		</view>
		<view class="form re">
			<view class="username">
				<view class="get-code" style="color: #676767;" @click.stop="getCode()">{{getCodeText}}</view>
				<input placeholder="请输入手机号" v-model="phoneNumber" placeholder-style="color: #676767;" />
			</view>
			<view class="code">
				<input placeholder="请输入验证码" v-model="code" placeholder-style="color: #676767;" />
			</view>
			<!-- 	<view class="password">
				<input placeholder="请输入密码" v-model="passwd" password=true placeholder-style="color: rgba(255,255,255,0.8);"/>
			</view> -->
			<view class="btn mart50" v-if="Typeid == 1" @tap="doReg">立即注册</view>
			<view class="btn mart50" v-else @tap="doReg">绑定手机号</view>
			<!-- 	<view class="res">
				<view @tap="toLogin">已有账号立即登录</view>
			</view> -->

			<!-- <image v-if="Typeid == 1" @click="openWx" :src="imagewxUrl+imgadres" class="wximg"></image> -->
		</view>

	</view>
</template>

<script>
	import md5 from "@/common/SDK/md5.min.js";
	import {
		sendSmsCodeByHw,
		bindUserPhone,
		H5login
	} from "../../Utils/Api.js";
	let app = getApp();
	export default {
		data() {
			return {
				phoneNumber: "",
				imagewxUrl: app.globalData.imageWxImg,
				imgadres: 'wx.png',
				code: '',
				passwd: "",
				getCodeText: '获取验证码',
				getCodeBtnColor: "#ffffff",
				getCodeisWaiting: false,
				Typeid: ''
			}
		},
		onLoad(options) {
			this.Typeid = options.id
		},
		methods: {
			Timer() {},
			getCode() {
				uni.hideKeyboard()
				if (this.getCodeisWaiting) {
					return;
				}
				if (!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(this.phoneNumber))) {
					uni.showToast({
						title: '请填写正确手机号码',
						icon: "none"
					});
					return false;
				}
				uni.showLoading({
					title: '发送中...'
				})
				let params = {
					phone: this.phoneNumber,
					HWMSG:3
				}
				sendSmsCodeByHw(params).then(res => {
					if (res.return_code == '000000') {
						this.getCodeisWaiting = true;
						this.getCodeBtnColor = "rgba(255,255,255,0.5)"
						uni.showToast({
							title: '验证码已发送',
							icon: "none"
						});
						this.setTimer();
					} else {
						uni.hideLoading()
					}
				})
			},
			setTimer() {
				let holdTime = 60;
				this.getCodeText = "重新获取(60)"
				this.Timer = setInterval(() => {
					if (holdTime <= 0) {
						this.getCodeisWaiting = false;
						this.getCodeBtnColor = "#ffffff";
						this.getCodeText = "获取验证码"
						clearInterval(this.Timer);
						return;
					}
					this.getCodeText = "重新获取(" + holdTime + ")"
					holdTime--;

				}, 1000)
			},
			doReg() {
				uni.hideKeyboard()
				//模板示例部分验证规则
				if (!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(this.phoneNumber))) {
					uni.showToast({
						title: '请填写正确手机号码',
						icon: "none"
					});
					return false;
				}
				if (!this.code) {
					uni.showToast({
						title: '请填写验证码',
						icon: "none"
					});
					return false;
				}
				uni.showLoading({
					title: '提交中...'
				})
				let params = {
					phone: this.phoneNumber,
					code: this.code
				}
				if (this.Typeid == 1) {
					H5login(params).then(res => {
						uni.hideLoading()
						if (res.return_code == '000000') {
							app.globalData.userInfo = res.return_data
								.object
								.highUser;
							app.globalData.token = res.return_data.uniqueCode;
							uni.setStorage({
								key: "user",
								data: res.return_data
									.object
									.highUser
							})
							uni.setStorage({
								key: "token",
								data: res.return_data.uniqueCode
							})
							uni.reLaunch({
								url: '../unionPay/unionPay-entrance/unionPay-entrance'
							})
						} else {
							uni.showToast({
								title: res.return_msg,
								icon: "none"
							});
						}
					})
				} else {
					bindUserPhone(params).then(res => {
						uni.hideLoading()
						if (res.return_code == '000000') {
							app.globalData.userInfo = res.return_data
								.object
								.highUser;
							app.globalData.token = res.return_data.uniqueCode;
							uni.setStorage({
								key: "user",
								data: res.return_data
									.object
									.highUser
							})
							uni.setStorage({
								key: "token",
								data: res.return_data.uniqueCode
							})
							uni.navigateBack({})
						} else {
							uni.showToast({
								title: res.return_msg,
								icon: "none"
							});
						}
					})
				}

			},
			//微信内核浏览器
			isWeiXinLogin() {
				var ua = navigator.userAgent.toLowerCase();
				if (ua.search(/MicroMessenger/i) > -1) {
					return true;
				} else {
					return false;
				}
			},
			//微信登录
			openWx() {
				location.href =
					'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxa075e8509802f826&redirect_uri=https%3A%2F%2Fhsgcs.dctpay.com%2FH5%2Findex.html%23%2F&response_type=code&scope=snsapi_userinfo&state=2#wechat_redirect';
			},
			toLogin() {
				uni.hideKeyboard()
				uni.redirectTo({
					url: 'login'
				});
				uni.navigateBack();
			}
		}
	}
</script>

<style lang="scss">
	@import "../../static/css/login.scss";

	.wximg {
		width: 50px;
		height: 50px;
		margin-left: 40%;
		margin-top: 90px;
		margin-bottom: 40px;
	}
</style>
