diff --git a/src/composables/useAliyunCaptcha.js b/src/composables/useAliyunCaptcha.js index 02c27e7..e50b624 100644 --- a/src/composables/useAliyunCaptcha.js +++ b/src/composables/useAliyunCaptcha.js @@ -1,4 +1,4 @@ -import { showToast } from "vant"; +import { showToast, showLoadingToast, closeToast } from "vant"; import useApiFetch from "@/composables/useApiFetch"; // 阿里云验证码场景 ID @@ -117,42 +117,53 @@ export function useAliyunCaptcha() { return; } - window.__captchaVerifyCallback = async (captchaVerifyParam) => { - window.__lastBizResponse = null; - const { data, error } = await bizVerify(captchaVerifyParam); - const result = data?.value ?? data; - if (error?.value || !result) { - return { captchaResult: false, bizResult: false }; + const loading = showLoadingToast({ + message: "安全验证加载中...", + forbidClick: true, + duration: 0, + loadingType: "spinner", + }); + + try { + window.__captchaVerifyCallback = async (captchaVerifyParam) => { + window.__lastBizResponse = null; + const { data, error } = await bizVerify(captchaVerifyParam); + const result = data?.value ?? data; + if (error?.value || !result) { + return { captchaResult: false, bizResult: false }; + } + window.__lastBizResponse = result; + const captchaOk = result.captchaVerifyResult !== false; + const bizOk = result.code === 200; + return { captchaResult: captchaOk, bizResult: bizOk }; + }; + + window.__onBizResultCallback = (bizResult) => { + if ( + bizResult === true && + window.__lastBizResponse && + typeof window.__onCaptchaBizSuccess === "function" + ) { + window.__onCaptchaBizSuccess(window.__lastBizResponse); + } + }; + + await ensureCaptchaInit(); + + // 首次初始化时 SDK 会异步调用 getInstance,需等待实例就绪后再 show + if (captchaReadyPromise) { + await captchaReadyPromise; + captchaReadyPromise = null; } - window.__lastBizResponse = result; - const captchaOk = result.captchaVerifyResult !== false; - const bizOk = result.code === 200; - return { captchaResult: captchaOk, bizResult: bizOk }; - }; - - window.__onBizResultCallback = (bizResult) => { - if ( - bizResult === true && - window.__lastBizResponse && - typeof window.__onCaptchaBizSuccess === "function" - ) { - window.__onCaptchaBizSuccess(window.__lastBizResponse); + if (!window.captcha) { + showToast({ message: "验证码未加载,请刷新页面重试" }); + return; } - }; - - await ensureCaptchaInit(); - - // 首次初始化时 SDK 会异步调用 getInstance,需等待实例就绪后再 show - if (captchaReadyPromise) { - await captchaReadyPromise; - captchaReadyPromise = null; + window.__onCaptchaBizSuccess = onSuccess; + window.captcha.show(); + } finally { + closeToast(); } - if (!window.captcha) { - showToast({ message: "验证码未加载,请刷新页面重试" }); - return; - } - window.__onCaptchaBizSuccess = onSuccess; - window.captcha.show(); } return { runWithCaptcha };