From b6bf036f8f9a996b247192dc9b4d9adeb4cdac14 Mon Sep 17 00:00:00 2001 From: liangzai <2440983361@qq.com> Date: Tue, 24 Feb 2026 18:42:03 +0800 Subject: [PATCH] f --- src/composables/useAliyunCaptcha.js | 47 ++++++++++++++++++++++++++--- 1 file changed, 42 insertions(+), 5 deletions(-) diff --git a/src/composables/useAliyunCaptcha.js b/src/composables/useAliyunCaptcha.js index 2717dcd..02c27e7 100644 --- a/src/composables/useAliyunCaptcha.js +++ b/src/composables/useAliyunCaptcha.js @@ -1,8 +1,11 @@ import { showToast } from "vant"; import useApiFetch from "@/composables/useApiFetch"; -// 阿里云验证码场景 ID,请在控制台获取后替换 +// 阿里云验证码场景 ID const ALIYUN_CAPTCHA_SCENE_ID = "wynt39to"; +// 是否启用加密模式(通过环境变量控制,非加密模式时前端不调用后端获取 EncryptedSceneId) +const ENABLE_ENCRYPTED = + import.meta.env.VITE_ALIYUN_CAPTCHA_ENCRYPTED === "false"; let captchaInitialised = false; /** 首次初始化后,SDK 会异步调用 getInstance,用此 Promise 在实例就绪后再 show */ @@ -21,22 +24,56 @@ async function ensureCaptchaInit() { captchaReadyResolve = resolve; }); - // 向后端获取加密后的 EncryptedSceneId + // 非加密模式:仅传 SceneId,不调用后端接口 + if (!ENABLE_ENCRYPTED) { + window.initAliyunCaptcha({ + SceneId: ALIYUN_CAPTCHA_SCENE_ID, + mode: "popup", + element: "#captcha-element", + getInstance(instance) { + window.captcha = instance; + if (typeof captchaReadyResolve === "function") { + captchaReadyResolve(); + captchaReadyResolve = null; + } + }, + captchaVerifyCallback(param) { + return typeof window.__captchaVerifyCallback === "function" + ? window.__captchaVerifyCallback(param) + : Promise.resolve({ + captchaResult: false, + bizResult: false, + }); + }, + onBizResultCallback(bizResult) { + if (typeof window.__onBizResultCallback === "function") { + window.__onBizResultCallback(bizResult); + } + window.__lastBizResponse = null; + window.__onCaptchaBizSuccess = null; + }, + slideStyle: { width: 360, height: 40 }, + language: "cn", + }); + return; + } + + // 加密模式:先从后端获取 EncryptedSceneId,再初始化 const { data, error } = await useApiFetch("/captcha/encryptedSceneId") .post() .json(); const resp = data?.value; - if (error?.value || !resp || !resp.data.encryptedSceneId) { + const encryptedSceneId = resp?.data?.encryptedSceneId; + if (error?.value || !encryptedSceneId) { showToast({ message: "获取验证码参数失败,请稍后重试" }); captchaInitialised = false; captchaReadyPromise = null; captchaReadyResolve = null; return; } - window.initAliyunCaptcha({ SceneId: ALIYUN_CAPTCHA_SCENE_ID, - EncryptedSceneId: resp.data.encryptedSceneId, + EncryptedSceneId: encryptedSceneId, mode: "popup", element: "#captcha-element", getInstance(instance) {