40 lines
929 B
JavaScript
40 lines
929 B
JavaScript
// app/composables/useEmailCode.js
|
||
import { useApi } from './useApi'
|
||
|
||
export function useEmailCode() {
|
||
const api = useApi()
|
||
const sending = useState('emailCode:sending', () => false)
|
||
const countdown = useState('emailCode:countdown', () => 0)
|
||
|
||
let timer = null
|
||
|
||
async function sendCode(email, scene = 'register') {
|
||
if (sending.value || countdown.value > 0) return
|
||
sending.value = true
|
||
try {
|
||
// 统一新路由:POST /auth/email-code
|
||
await api.post('/auth/email-code', { email, scene })
|
||
start(60)
|
||
} finally {
|
||
sending.value = false
|
||
}
|
||
}
|
||
|
||
function start(sec = 60) {
|
||
clear()
|
||
countdown.value = sec
|
||
timer = setInterval(() => {
|
||
countdown.value -= 1
|
||
if (countdown.value <= 0) clear()
|
||
}, 1000)
|
||
}
|
||
|
||
function clear() {
|
||
if (timer) clearInterval(timer)
|
||
timer = null
|
||
countdown.value = 0
|
||
}
|
||
|
||
return { sending, countdown, sendCode, clear }
|
||
}
|