codeLogin.vue 3.6 KB
<template>
	<!-- 验证码登录 -->
	<view class="codeLogin">
		<!-- 验证 -->
		<view class="verification">
			<view class="codeSize">
				<p class="BigSize">手机号登录</p>
				<view class="firstLogin">
					已发送6位验证码至<span>{{$Route.query.phone}}</span>,请留意短信
				</view>
			</view>
			<u-message-input  class="codeInput"   :maxlength="6" @finish="finish" :breathe="true" :focus="true" mode="bottomLine">
			</u-message-input>
			<!-- 重新发送 -->
			<view class="wrap">
				<u-toast ref="uToast"></u-toast>
				<u-verification-code @end="end" :keep-running="true" :seconds="60" ref="uCode" @change="codeChange">
				</u-verification-code>
			</view>
			<view class="anew" @click="getCode" v-if="showTorF">
				{{tips}}
			</view>
			<view class="anew" @click="againCode" v-if="!showTorF">
				{{tips}}
			</view>
		</view>
		<!-- 提示框 -->
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	export default {
		data() {
			return {
				tips: '',
				showTorF:true
			};
		},
		onReady() {
			this.getCode()
		},
		methods: {
			// 自动登录
			async finish(code) {
				// uni.setStorageSync("token",1)
				// if(this.$Route.query.redirect){
				// 	this.$Router.push({
				// 		path:this.$Route.query.redirect
				// 	})
				// }else{
				// 	this.$Router.pushTab({
				// 		path: "/pages/index/index"
				// 	})
				// }
				
				try{
					await this.$store.dispatch("codeLogin",{
						phone:this.$Route.query.phone,
						code:code
					})
					this.$Router.pushTab({
						path: "/pages/index/index"
					})
					// if(this.$Route.query.redirect){
					// 	this.$Router.push({
					// 		path:this.$Route.query.redirect
					// 	})
					// }else{
					// 	this.$Router.pushTab({
					// 		path: "/pages/index/index"
					// 	})
					// }
				}catch(e){
					this.$refs.uToast.show({
						title: '验证码错误',
						type: 'error',
					})
				}
				
			},
			// 开始倒计时
			codeChange(text) {
				if(text!="获取验证码"){
					this.tips = text;
				}
			},
			getCode() {
				if (this.$refs.uCode.canGetCode) {
					// 模拟向后端请求验证码
					uni.showLoading({
						title: ''
					})
					setTimeout(() => {
						uni.hideLoading();
						// 这里此提示会被this.start()方法中的提示覆盖
						this.$u.toast('验证码已发送');
						// 通知验证码组件内部开始倒计时
						this.$refs.uCode.start();
					}, 1000);
				} else {
					this.$u.toast('不能重复获取');
				}
			},
			end(){
				this.showTorF=!this.showTorF
				console.log(1);
			},
			againCode(){
				this.showTorF=!this.showTorF
				this.getCode()
				this.$store.dispatch("gainCode",this.$Route.query.phone)
			}
		}
	}
</script>

<style lang="scss" scoped>
	// 验证码验证
	* {
		margin: 0;
		padding: 0;
	}

	.codeLogin {
		width: 750rpx;
		height: 100vh;
		display: flex;
		flex-direction: column;
		align-items: center;
		
		// 验证登录
		.verification {
			width: 680rpx;
			height: 700rpx;
			margin-top: 150rpx;
			display: flex;
			flex-direction: column;
			align-items: center;
			
			// 字体
			.codeSize {
				width: 600rpx;
				height: 160rpx;
				margin-bottom: 60rpx;

				.BigSize {
					font-size: 27px;
					font-weight: 550;
					margin-bottom: 15rpx;
				}

				.firstLogin {
					color: darkgray;
					font-size: 12px;
					margin-bottom: 50rpx;

					span {
						color: red;
					}
				}
			}

			.anew{
				color: darkgray;
				text-align: center;
				margin-top: 20rpx;
			}
			
		}
	}
</style>