import { onMounted } from "vue"; import { watch } from "vue"; import { onScopeDispose } from "vue"; import { ref } from "vue"; import { ApiError } from "~/lib/auth.js"; import { useApi } from "~/lib/auth.js"; import { t as useSiteConfig_t } from "~/lib/useSiteConfig.js"; var s = (e) => e === !0 || e === 1 || e === `1`; function c(e, t) { return s(e?.[`captcha_client_${t}`]) ? !0 : t === `login` && s(e?.captcha_client_login_error) && s(e?.captcha_client_login_error_3_times); } function l(e) { if (typeof e != `string` || e.length > 262144) throw Error(`验证码响应无效`); let t = new DOMParser().parseFromString(e, `text/html`), n = t.querySelector(`[data-captcha-provider]`); if (n) { let e = n.getAttribute(`data-captcha-id`) ?? ``; if ( n.getAttribute(`data-captcha-provider`) !== `geetest` || !/^[a-f0-9]{32}$/i.test(e) ) throw Error(`验证码服务尚未配置完成,请联系管理员`); return { provider: `geetest`, captchaId: e, }; } if ( e.includes(`/captcha/tp_captcha/index/refresh`) && t.querySelector(`#captcha-img`) ) return { provider: `image`, captchaId: ``, }; throw Error(`当前验证码插件暂不受支持,请联系管理员`); } function u(e) { if (!e || typeof e != `object`) return !1; let t = e; return [`lot_number`, `captcha_output`, `pass_token`, `gen_time`].every( (e) => typeof t[e] == `string` && t[e].trim().length > 0 && t[e].length <= 8192, ); } function d(d) { let f = useSiteConfig_t(), p = useApi(), m = ref(!1), h = ref(`image`), g = ref(``), _ = ref(``), v = ref(``), y = ref(``), b = ref(!1), x = ref(``), S = ref(!(!d || f.config.value)), C = !1, w = 0, T = null, E = !1; function D() { ((y.value = ``), (v.value = ``)); } async function O() { if (C || b.value) return; let e = ++w; ((b.value = !0), (x.value = ``), D(), (_.value = ``), (g.value = ``), (T = new AbortController())); let t = T, n = setTimeout(() => t.abort(), 1e4); try { let n = await p.get(`/captcha`, void 0, !1, { timeoutMs: 1e4, }); if (C || e !== w) return; let r = l(n.html); if ( ((h.value = r.provider), (g.value = r.captchaId), r.provider === `geetest`) ) return; let i = await fetch(`/captcha/tp_captcha/index/refresh`, { headers: { Accept: `application/json`, }, signal: t.signal, cache: `no-store`, }), a = await i.json(); if (C || e !== w) return; if ( !i.ok || typeof a?.base64 != `string` || !/^data:image\/(?:png|jpeg|gif|webp);base64,/.test(a.base64) || typeof a?.token != `string` || !a.token ) throw Error(`验证码加载失败`); ((_.value = a.base64), (v.value = a.token)); } catch (t) { if (C || e !== w) return; x.value = t instanceof Error && /验证码.*(?:配置|支持)/.test(t.message) ? t.message : `验证码加载失败,请点击重新加载`; } finally { (clearTimeout(n), !C && e === w && (b.value = !1)); } } function k(e) { (D(), !C && m.value && h.value === `geetest` && !b.value && u(e) && ((y.value = JSON.stringify({ lot_number: e.lot_number, captcha_output: e.captcha_output, pass_token: e.pass_token, gen_time: e.gen_time, })), (v.value = e.lot_number))); } function A() { return S.value ? `验证码设置正在加载,请稍候` : d && !f.config.value ? `验证码设置加载失败,请刷新页面重试` : m.value ? b.value ? `验证码正在加载,请稍候` : x.value ? x.value : h.value === `geetest` ? v.value && y.value ? `` : `请先完成人机验证` : v.value ? y.value.trim() ? `` : `请输入图形验证码` : `请先重新加载图形验证码` : ``; } async function j(e) { let t = e instanceof ApiError && s(e.data?.captcha); return (t && ((E = !0), (m.value = !0)), m.value && (await O()), t); } return ( d && (watch( () => c(f.config.value, d), (e) => { ((m.value = E || e), m.value ? O() : ((w += 1), T?.abort(), (b.value = !1), (x.value = ``), D())); }, { immediate: !0, }, ), onMounted(async () => { (await f.load(), !C && ((S.value = !1), f.config.value || (x.value = `验证码设置加载失败,请刷新页面重试`))); })), onScopeDispose(() => { ((C = !0), (w += 1), T?.abort(), D()); }), { needed: m, provider: h, captchaId: g, img: _, token: v, value: y, loading: b, error: x, configLoading: S, load: O, complete: k, invalidate: D, validationError: A, handleError: j, } ); } export { d as t };