register.vue 5.6 KB
<template>
	<view class="login">
		<view class="icon">
			<image class="bgimg" src="../../static/ic_logo.png" mode=""></image>

			<view class="bar flexwrap">
				<view class="name">
					用户名:
				</view>
				<view class="lastname">
					<input v-model="username" maxlength="16" type="text" value="" placeholder="请输入用户名" />
				</view>
			</view>
			<view class="bar flexwrap">
				<view class="name">
					密码:
				</view>
				<view class="lastname">
					<input v-model="password" maxlength="16"  type="text" value="" placeholder="请输入密码" />
				</view>
			</view>
			<view class="bar flexwrap">
				<view class="name">
					手机号:
				</view>
				<view class="lastname">
					<input v-model="mobile" maxlength="11"  type="number" value="" placeholder="请输入手机号" />
				</view>
			</view>
			<view class="bar flexwrap">
				<view class="name">
					验证码:
				</view>
				<view class="lastname">
					<input v-model="code" maxlength="6"  type="text" value="" placeholder="请输入验证码" />
					<view class="code" @click="getCode">
						{{num==0?'获取验证码':num + '秒重新发送'}}
					</view>
				</view>
			</view>
			<view class="loginbtn" @click="toRegister">
				注册
			</view>
			<view class="passway" @click="goLogin">
				<text>已有账号,直接登录</text>
			</view>
			<view class="user">
				<image v-if="isSelect" @click="isSelect=!isSelect" src="/static/icon25.png" mode=""></image>
				<image v-else @click="isSelect=!isSelect" src="/static/icon24.png" mode=""></image>
				我已阅读并同意
				<text style="margin-left: 10rpx;" @click="toAgree(1)"> 用户协议、</text><text @click="toAgree(2)">隐私政策</text>
			</view>
		</view>
		
	</view>
</template>

<script>
	import {sendCode,register} from '@/api/login.js'
	import {toa} from '@/utils/toast.js'
	export default {
		data() {
			return {
				username:'',
				password:'',
				mobile:'',
				code:'',
				clearId:'',
				num:0,
				isSelect:false
			};
		},
		methods: {
			goLogin() {
				uni.navigateBack({})
				// uni.navigateTo({
				// 	url:'/pages/login/login'
				// })
			},
			toRegister(){
				if(!this.username) return toa.toast('请输入用户名')
				if(!this.password) return toa.toast('请输入密码')
				if(!this.mobile) return toa.toast('请输入手机号')
				if(!this.code) return toa.toast('请输入验证码')
				if(!this.isSelect) return toa.toast('请先同意用户协议和隐私政策')
				this.register()
			},
			async register(){
			  try {
			    const res = await register(this.username,this.password,this.mobile,this.code)
			    console.log('register', res)
				setTimeout(()=>{
					toa.success('注册成功')
				},200)
				uni.navigateBack({})
			    // 保存数据
			  } catch (err) {
			 	uni.showToast({ title:err,icon:'none' })
			    console.log('register', err)
			  }
			},
			getCode(){
				if(this.num>0) return
				if(!this.mobile) return toa.toast('请先输入手机号')
				if (!(/^1[34578]\d{9}$/.test(this.mobile))) return toa.toast('请输入正确手机号')
				this.sendCode()
			},
			async sendCode(){
			  try {
			    const res = await sendCode(this.mobile,'register')
				toa.success('发送成功')
				this.setInter()
			    console.log('sendCode', res)
			    // 保存数据
			  } catch (err) {
			 	uni.showToast({ title:err,icon:'none' })
			    console.log('sendCode', err)
			  }
			},
			setInter (){
			 	 this.num=60
				 this.clearId = setInterval(() => {
				         this.num --
				 }, 1000)
			},
			toAgree(type){
				uni.navigateTo({
					url:'/pages/login/agreement?type='+ type
				})
			}
		},
		onHide(){
			this.num = 0
			clearInterval(this.clearId)
		},
		watch: {
			num(newValue, oldValue) {
				if(newValue==0){
					this.num = 0
					clearInterval(this.clearId)
				}
			}
		},
	}
</script>

<style lang="scss">
	.login {
		padding: 0 48rpx;
		box-sizing: border-box;

		.icon {
			.bgimg {
				margin-top: 100rpx;
				margin-left: 50%;
				transform: translate(-50%);
				margin-bottom: 24rpx;
				width: 184rpx;
				height: 184rpx;
			}

			.bar {
				border-bottom: 1px solid #f0f0f0;
				padding: 40rpx 0;
				color: rgba(0, 0, 0, 1);
				font-size: 32rpx;
				font-weight: 500;
				font-family: "PingFang SC";

				.name {
					width: 180rpx;
				}

				.lastname {
					flex: 1;
					position: relative;

					input {
						// width: 260rpx;
						width: 100%;
					}

					.code {
						position: absolute;
						right: 0;
						top: 50%;
						transform: translate(-10%, -50%);
						color: rgba(0, 0, 0, 0.26);
						font-size: 32rpx;
						font-weight: 400;
						font-family: "PingFang SC";
					}
				}
			}

			.loginbtn {
				margin-top: 100rpx;
				height: 88rpx;
				border-radius: 28rpx;
				opacity: 1;
				color: rgba(0, 0, 0, 0.9);
				font-size: 32rpx;
				font-weight: 600;
				font-family: "PingFang SC";
				text-align: center;
				line-height: 88rpx;
				background: rgba(254, 208, 0, 1);
			}

			.passway {
				text-align: right;
				margin-top: 16rpx;

				text {
					color: rgba(254, 208, 0, 1);
					font-size: 26rpx;
					font-weight: 400;
					font-family: "PingFang SC";
				}
			}

			.user {
				margin-top: 200rpx;
				text-align: center;
				display: flex;
				align-items: center;
				justify-content: center;
				color: rgba(100, 101, 102, 1);
				font-size: 24rpx;
				font-weight: 400;
				font-family: "PingFang SC";

				image {
					margin-right: 22rpx;
					width: 36rpx;
					height: 36rpx;
				}

				text {
					margin-right: 24rpx;
					color: rgba(87, 107, 149, 1);
					font-size: 24rpx;
					font-weight: 400;
					font-family: "PingFang SC";
				}
			}
		}
	}
</style>