code_copy.vue 3.81 KB
<template>
	<view class="code-login">
		<view class="code-title" style="margin-bottom: 30px;">
			<view class="code-title-text" style="font-size: 23px;color: #202328">输入4位验证码</view>
			<view class="code-title-tips" style="display: flex; flex-direction: row; justify-content: space-between; font-size: 12px;font-weight: 400;color: rgba(109,119,143,0.82);">
				<view>验证码已发送</view>
				<view>
					<view v-if="waitTime">重新发送({{waitTime}}s)</view>
					<uv-button v-else type="primary" size="mini" text="重新发送" @click="handleRetry"></uv-button>
				</view>
			</view>
		</view>

		<view class="forget-form">
			<uv-form
				labelPosition="left"
				ref="formRef"
				labelWidth="0"
				:borderBottom="false"
			>
				<uv-form-item label="">
					<view class="code-list" style="display: flex;flex-direction: row;">
						<!-- num是从1开始 -->
						<uv-input
						    v-for="(num, index) in formNum"
							:key="num"
							ref="inputRef"
							:value="formModel[index]"
							type="number"
							maxlength="1"
							color="#202328"
							inputAlign="center"
							fontSize="22px"
							border="bottom"
							class="code-input"
							@input="(value) => handleInput(value, index)"
						></uv-input>
					</view>
				</uv-form-item>
			</uv-form>
		</view>
	</view>
</template>

<script setup>
	import {
		ref,
	} from 'vue';
	import {
		onLoad
	} from '@dcloudio/uni-app';
	import { doLoginCheckCodeApi } from '@/api';

	const formNum = ref(4)
	const formModel = ref(['', '', '', ''])
	const loginName = ref('')
	const contactWay = ref('')
	// const formModel = ref(['1', '1', '2', '3'])
	const inputRef = ref(null)

	onLoad((options) => {
		console.log('options', options)
		loginName.value = options.loginName || ''
		contactWay.value = options.contactWay || ''
	})

	const handleInput = (value, index) => {
		console.log('handleInput', value, index)
		if (parseInt(value) || parseInt(value) === 0) {
			formModel.value[index] = value
			// 跳到下一个input
			// TODO: 此功能,浏览器无法验证
			console.log('inputElList', inputRef)
			const targetIndex = index + 1
			if (targetIndex < formNum.value ) {
				const targetItem = inputRef.value[targetIndex]
				console.log('targetItem', targetItem)
				if (targetItem.value) {
					targetItem.value.$el.focus()
				}
			}

			// 判断是否已有4个有效值
			const isValid = formModel.value.every(item => {
				return (parseInt(item) || parseInt(item) === 0)
			})
			console.log('handleInput-isValid', isValid)
			if (isValid) {
				// TODO: 进行登录验证
				const params = {
					loginName: loginName.value,
					code: formModel.value.join(''),
				}
				doLoginCheckCodeApi(params).then((res) => {
					console.log('code', res)
					const checkCode = res.data || ''
					// 成功
					uni.navigateTo({
						url: `/subPackages/accountGroup/pages/login/forget/reset?loginName=${params.loginName}&code=${checkCode}`,
					})
				})
			}
		}
		console.log('handleInput-formModel', formModel.value)
	}

	// 发送验证码
	const waitTime = ref(60)
	// const waitTime = ref(10)
	const startTime = () => {
		const timer = setInterval(() => {
			if (waitTime.value) {
				waitTime.value -= 1
			} else {
				// 结束计时
				clearInterval(timer)
			}
		}, 1000)
	}
	const handleRetry = () => {
		const params = {
			loginName: loginName.value
		}
		if (contactWay.value.includes('@')) {
			// 邮箱
			params.email = contactWay.value
		} else {
			// 手机号
			params.phone = contactWay.value
		}
		doLoginGetCodeApi(params).then(() => {
			uni.showToast({icon: 'success', title: '发送成功'})
		}).catch(() => {
			uni.showToast({icon: 'error', title: '发送失败'})
		})
	}
	startTime()
</script>

<style scoped>
	.code-login {
		padding: 40px 25px 10px 25px;
	}
	.code-input {
		flex: 1;
		margin: 0px 10px;
		border-color: #6D778F !important
	}
</style>