code.vue 3.22 KB
<template>
	<view class="wrap">
		<view class="key-input">
			<view class="title">输入验证码</view>
			<view class="tips">验证码已发送至 +{{ startIphoneNumber }}</view>
			<u-message-input :value="value" :focus="true" @change="change" @finish="finish" mode="bottomLine"
				:maxlength="maxlength"></u-message-input>
			<text v-if="verifyFlag" class="error">验证码错误,请重新输入</text>
			<view class="captcha">
				<text :class="{ noCaptcha: show }" @tap="noCaptcha">收不到验证码点这里</text>
				<text :class="{ regain: !show }">{{ second }}秒后重新获取验证码</text>
			</view>
		</view>
	</view>
</template>

<script setup>
import { sendCode, userLogin } from "@/apis/user.js";
import { useMainStore } from '@/stores/index.js';
import { onLoad } from "@dcloudio/uni-app";
import { getCurrentInstance, ref } from "vue";
const { proxy } = getCurrentInstance();
const store = useMainStore();
const verifyFlag = ref(false)
const iphoneNumber = ref("")
const startIphoneNumber = ref("")
const maxlength = ref(4)
const value = ref("")
const second = ref(10);
const show = ref(false)

onLoad((options) => {
	iphoneNumber.value = options.iphoneNumber;
	startIphoneNumber.value = handleIphoneNumber("" + options.iphoneNumber);
	getCaptcha(iphoneNumber.value)
})
const handleIphoneNumber = (tel) => {
	return tel.replace(tel.substring(3, 7), "****");
}

const getCaptcha = (iphoneNumber) => {
	value.value = ''
	second.value = 10
	show.value = false;
	sendCode(iphoneNumber).then(res => {
		proxy.$u.toast(res.data.data)
	})
	let interval = setInterval(() => {
		second.value--;
		if (second.value <= 0) {
			show.value = true
			clearInterval(interval);
		}
	}, 1000);

}
// 收不到验证码选择时的选择
const noCaptcha = () => {
	uni.showActionSheet({
		itemList: ['重新获取验证码'],
		success: function (res) {
			getCaptcha(iphoneNumber.value)
		},
		fail: function (res) {

		}
	});
}

// 校验验证码
const checkVerifyNum = (code) => {
	userLogin({ loginType: 0, tel: iphoneNumber.value, code: code }).then(res => {
		// 登录成功
		if (res.data.success) {
			verifyFlag.value = false;
			store.tempToken = res.data.data.token
			
			store.userName = res.data.data.name;
			uni.navigateTo({
				url: `/pages/wode-info/wode-info-choose/index?ruleVos= ${encodeURIComponent(JSON.stringify(res.data.data.ruleVos))}&userName=${encodeURIComponent(res.data.data.name)}`
			});
		} else {
			verifyFlag.value = true;
		}
	})
}

// change事件侦听
const change = (value) => {
	// console.log("value:", value);
}

// 输入完验证码最后一位执行
const finish = (value) => {
	if (value.length == 4) {
		checkVerifyNum(value);
	}
}
</script>

<style lang="scss" scoped>
.wrap {
	padding: 80rpx;
}

.box {
	margin: 30rpx 0;
	font-size: 30rpx;
	color: 555;
}

.key-input {
	padding: 30rpx 0;

	-webkit-user-select: text !important;

	text {
		display: none;
	}

	.error {
		display: block;
		color: red;
		font-size: 30rpx;
		margin: 20rpx 0;
	}
}

.title {
	font-size: 50rpx;
	color: #333;
}

.key-input .tips {
	font-size: 30rpx;
	color: #333;
	margin-top: 20rpx;
	margin-bottom: 60rpx;
}

.captcha {
	color: $u-warning;
	font-size: 30rpx;
	margin-top: 40rpx;

	.noCaptcha {
		display: block;
	}

	.regain {
		display: block;
	}
}
</style>