AI-News/frontend/app/composables/useEmailCode.js
2025-12-04 10:04:21 +08:00

40 lines
929 B
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 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 }
}