AI-News/frontend/app/composables/useEmailCode.js

40 lines
929 B
JavaScript
Raw Permalink Normal View History

2025-12-04 10:04:21 +08:00
// 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 }
}