import { onScopeDispose } from "vue"; import { ref } from "vue"; import { useApi } from "~/lib/auth.js"; import { t as interval_t } from "~/lib/interval.js"; function i(i) { let a = useApi(), o = ref(!1), s = ref(0), c = null, l = !1; function u() { c &&= (clearInterval(c), null); } function d() { l || (u(), (s.value = i.seconds ?? 60), (c = interval_t(() => { (--s.value, s.value <= 0 && u()); }, 1e3))); } onScopeDispose(() => { ((l = !0), u()); }); async function f(e, t, n) { if (l || o.value || s.value > 0) return; if (((i.error.value = ``), !t)) { i.error.value = n; return; } let r = i.captcha.validationError(); if (r) { i.error.value = r; return; } o.value = !0; try { let n = { action: i.action, captcha: i.captcha.value.value.trim(), token: i.captcha.token.value, }; if (e === `email`) await a.post(`/email/code`, { ...n, email: t, }); else { let e = String( typeof i.phoneCode == `function` ? i.phoneCode() : (i.phoneCode ?? 86), ).trim(); if (!/^[1-9]\d{0,3}$/.test(e)) throw Error(`请输入有效的国际电话区号`); await a.post(`/phone/code`, { ...n, phone_code: Number(e), phone: t, }); } (d(), !l && i.captcha.needed.value && (i.captcha.provider.value === `geetest` ? i.captcha.invalidate() : i.captcha.load())); } catch (e) { if (l) return; ((i.error.value = e instanceof Error ? e.message : `验证码发送失败`), await i.captcha.handleError(e)); } finally { o.value = !1; } } return { sending: o, countdown: s, send: f, start: d, stop: u, }; } export { i as t };