<template>
	<view class="u-page content">
		<u-icon class="back-btn" name="arrow-left" color="black" size="20" @click="navBack"></u-icon>
		<view class="wrapper">
			<h2 class="Semibold">手机号注册</h2>
			<u--form labelPosition="left" :model="registerData" ref="registerForm" labelWidth="80">
				<u-form-item label="电话" prop="phone" borderBottom>
					<u--input v-model="registerData.phone" border="none" placeholder="请输入手机号码"></u--input>
				</u-form-item>

				<u-form-item label="验证码" prop="verifyCode" labelWidth="80" borderBottom>
					<u--input v-model="registerData.verifyCode" border="none" placeholder="请填写验证码"></u--input>
					<u-button slot="right" @tap="getCode" :text="tips" size="mini" :disabled="disabled1"
						class="code-style">
					</u-button>
				</u-form-item>
				<u-form-item label="密码" prop="password" borderBottom>
					<u--input v-model="registerData.password" border="none" placeholder="请输入密码，6-16位字符"></u--input>
				</u-form-item>

				<u-code ref="uCode" @change="codeChange" seconds="20" @start="disabled1 = true"
					@end="disabled1 = false">
				</u-code>
			</u--form>
			<u-button class="code-style register" @click='register'>注册</u-button>
			<view class="flex service align-center">
				<u--image @click='consent'
					:src="consentStatus==true?'../../static/images/mine/duihao@2x.png':'../../static/images/mine/wxz.png'"
					width="12px" height="12px"></u--image>
				我已阅读并同意
				<navigator
					url="/pages/sale/webview?can_share=false&url=https://liangxin.zthymaoyi.com/userAgreement.html"
					class="path" hover-class="navigator-hover">《服务协议》</navigator>和<navigator
					url="/pages/sale/webview?can_share=false&url=https://liangxin.zthymaoyi.com/screctAgreement.html"
					class="path" hover-class="navigator-hover">《隐私政策》</navigator>
			</view>
		</view>

	</view>
</template>

<script>
	var _this
	import helper from '@/common/helper.js';
	export default {
		data() {
			return {
				disabled1: false,
				tips: '',
				consentStatus: false,
				codestatus: false,
				registerData: {
					phone: '',
					password: '',
					verifyCode: '',
				},
				rules: {
					phone: [{
							required: true,
							message: '请输入手机号',
							trigger: ['change', 'blur'],
						},
						{
							// 自定义验证函数，见上说明
							validator: (rule, value, callback) => {
								// 上面有说，返回true表示校验通过，返回false表示不通过
								// uni.$u.test.mobile()就是返回true或者false的
								return uni.$u.test.mobile(value);
							},
							message: '手机号码不正确',
							// 触发器可以同时用blur和change
							trigger: ['blur'],
						}
					],
					verifyCode: {
						type: 'string',
						required: true,
						len: 6,
						message: '请填写6位验证码',
						trigger: ['blur'],
					},
					password: {
						type: 'string',
						required: true,
						min: 6,
						max: 16,
						message: '长度在6-16位字符之间',
						trigger: ['blur'],
					}
				}
			}
		},
		onReady() {
			// 如果需要兼容微信小程序，并且校验规则中含有方法等，只能通过setRules方法设置规则
			this.$refs.registerForm.setRules(this.rules)
		},
		onLoad() {
			_this = this
		},
		methods: {
			navBack() {
				uni.navigateBack();
			},
			codeChange(text) {
				this.tips = text;
			},
			// 获取验证码
			getCode() {
				if (/^0?1[3|4|5|6|7|8][0-9]\d{8}$/.test(this.registerData.phone)) {
					if (this.$refs.uCode.canGetCode) {

						// 模拟向后端请求验证码
						uni.showLoading({
							title: '正在获取验证码'
						})
						_this.$request.baseRequest('get', '/commonUser/sendVerifyCode', {
								phone: this.registerData.phone
							}).then(res => {
								uni.hideLoading();
								// 这里此提示会被this.start()方法中的提示覆盖
								uni.$u.toast('验证码已发送');
								// 通知验证码组件内部开始倒计时
								this.$refs.uCode.start();

							})
							.catch(res => {
								uni.$u.toast(res.message);
							});

						// setTimeout(() => {

						// }, 2000);
					} else {
						uni.$u.toast('倒计时结束后再发送');
					}
				} else {
					uni.$u.toast('请输入正确手机号');
				}

			},
			consent() {
				this.consentStatus = !this.consentStatus
				uni.setStorage({
					key: 'policyStorageKey',
					data: this.consentStatus
				});
			},
			// 随机数
			randomAccess(min, max) {
				return Math.floor(Math.random() * (min - max) + max)
			},
			getRandomName(NameLength) {
				let name = ""
				for (let i = 0; i < NameLength; i++) {
					let unicodeNum = ""
					unicodeNum = this.randomAccess(0x4e00, 0x9fa5).toString(16)
					name += this.decodeUnicode(unicodeNum)
				}
				return name
			},
			// 解码
			decodeUnicode(str) {
				//Unicode显示方式是\u4e00
				str = "\\u" + str
				str = str.replace(/\\/g, "%");
				//转换中文
				str = unescape(str);
				//将其他受影响的转换回原来
				str = str.replace(/%/g, "\\");
				return str;
			},
			//注册
			register() {
				if (!this.consentStatus) {
					uni.showToast({
						title: '请勾选协议',
						icon: 'none',
						duration: 2000
					})
					return
				}
				this.$refs.registerForm.validate().then(res => {
					_this.registerData.userName = _this.getRandomName(3)
					uni.$u.toast('校验通过')
					_this.isLoading = true
					_this.$request.baseRequest('post', '/commonUser/register', _this.registerData).then(res => {
							if (res.data.code == '11002') {
								uni.$u.toast('验证码错误或过期，请重新获取')

								return
							} else if (res.data.code == '11004') {
								uni.$u.toast('该手机号已注册，可直接登录')

								return
							}
							uni.showLoading({
								title: '登录中',
								mask: true
							})
							_this.$request.baseRequest('get', '/commonUser/login', {
									phone: _this.registerData.phone,
									password: _this.registerData.password,
									cid: uni.getStorageSync("clientId")
								}).then(res => {
									uni.hideLoading()
									uni.setStorageSync('userInfo', res.data)
									_this.$store.commit('login', res.data)
									uni.switchTab({
										url: '/pages/goodSource/index'
									});

								})
								.catch(res => {
									uni.$u.toast(res.message)
								});



						})
						.catch(res => {
							uni.$u.toast(res.message)
						});
				}).catch(errors => {
					uni.$u.toast('校验失败')
				})
			},
		},
	}
</script>

<style lang="scss">
	.content {
		background: url(../../static/images/mine/bg@2x.png);
		background-size: 100% 100%;
		height: 100vh;
		padding-top: 85px;
		position: relative;
	}

	.back-btn {
		position: absolute;
		left: 40upx;
		z-index: 9999;
		padding-top: var(--status-bar-height);
		top: 40upx;
		font-size: 40upx;
		color: red;
	}

	.wrapper {
		position: relative;
		z-index: 90;
		padding-bottom: 40upx;
		height: 100%;
		padding: 0 30px;
	}

	.code-style {
		background: #2772FB;
		color: white;
	}

	.register {
		margin-top: 50rpx;
	}

	.service {
		font-size: 24rpx;
		margin: 20rpx;
		justify-content: center;

		/deep/.u-image {
			margin: 0 20rpx;
		}
	}

	.path {
		color: #2772FB;
	}

	.Semibold {
		margin-bottom: 50rpx;
	}
</style>
