70 lines
1.6 KiB
TypeScript
70 lines
1.6 KiB
TypeScript
import { readError } from '@/shared/utils/error'
|
|
import { onUnmounted, ref } from 'vue'
|
|
import { showToast } from 'vant'
|
|
import { sendSmsCode } from '@/features/auth/api/auth'
|
|
|
|
export function useSmsCountdown() {
|
|
const countDown = ref(0)
|
|
const sending = ref(false)
|
|
let timer: ReturnType<typeof setInterval> | null = null
|
|
|
|
function startCountDown() {
|
|
countDown.value = 60
|
|
timer = setInterval(() => {
|
|
countDown.value--
|
|
if (countDown.value <= 0) {
|
|
clearInterval(timer!)
|
|
timer = null
|
|
}
|
|
}, 1000)
|
|
}
|
|
|
|
onUnmounted(() => {
|
|
if (timer) {
|
|
clearInterval(timer)
|
|
timer = null
|
|
}
|
|
})
|
|
|
|
async function handleSendCode(
|
|
phone: string,
|
|
captcha?: { captchaId: string; captchaCode: string }
|
|
) {
|
|
if (!phone.trim()) {
|
|
showToast({ message: '请输入手机号', icon: 'warning-o' })
|
|
return false
|
|
}
|
|
if (!captcha?.captchaId || !captcha.captchaCode.trim()) {
|
|
showToast({ message: '请输入图形验证码', icon: 'warning-o' })
|
|
return false
|
|
}
|
|
|
|
sending.value = true
|
|
try {
|
|
await sendSmsCode(phone, captcha.captchaId, captcha.captchaCode)
|
|
showToast({
|
|
message: '验证码已发送,请注意查收',
|
|
icon: 'passed',
|
|
})
|
|
startCountDown()
|
|
return true
|
|
} catch (error) {
|
|
showToast({
|
|
message: readError(error, '验证码发送失败,请稍后重试'),
|
|
icon: 'cross',
|
|
})
|
|
return false
|
|
} finally {
|
|
sending.value = false
|
|
}
|
|
}
|
|
|
|
|
|
return {
|
|
countDown,
|
|
sending,
|
|
handleSendCode,
|
|
readError,
|
|
}
|
|
}
|