This commit is contained in:
Mrx
2026-02-27 10:29:40 +08:00
parent 2a7bde50db
commit b73de98a71
7 changed files with 225 additions and 77 deletions

View File

@@ -83,6 +83,12 @@
delete window.wx; delete window.wx;
</script> </script>
<!-- 阿里云滑块验证码 -->
<script>
window.AliyunCaptchaConfig = { region: "cn", prefix: "12zxnj" };
</script>
<script type="text/javascript" src="https://o.alicdn.com/captcha-frontend/aliyunCaptcha/AliyunCaptcha.js"></script>
<!-- 预加载关键资源 --> <!-- 预加载关键资源 -->
<link rel="preconnect" href="https://www.xingfucha.cn"> <link rel="preconnect" href="https://www.xingfucha.cn">
<link rel="preconnect" href="https://res.wx.qq.com"> <link rel="preconnect" href="https://res.wx.qq.com">
@@ -181,6 +187,7 @@
<div class="loading-text">加载中</div> <div class="loading-text">加载中</div>
</div> </div>
<div id="app"></div> <div id="app"></div>
<div id="captcha-element"></div>
<script type="module" src="/src/main.js"></script> <script type="module" src="/src/main.js"></script>
</body> </body>

View File

@@ -117,6 +117,7 @@ declare global {
const useActiveElement: typeof import('@vueuse/core')['useActiveElement'] const useActiveElement: typeof import('@vueuse/core')['useActiveElement']
const useAgent: typeof import('./composables/useAgent.js')['useAgent'] const useAgent: typeof import('./composables/useAgent.js')['useAgent']
const useAgentStore: typeof import('./stores/agentStore.js')['useAgentStore'] const useAgentStore: typeof import('./stores/agentStore.js')['useAgentStore']
const useAliyunCaptcha: typeof import('./composables/useAliyunCaptcha.js')['default']
const useAnimate: typeof import('@vueuse/core')['useAnimate'] const useAnimate: typeof import('@vueuse/core')['useAnimate']
const useApiFetch: typeof import('./composables/useApiFetch.js')['default'] const useApiFetch: typeof import('./composables/useApiFetch.js')['default']
const useArrayDifference: typeof import('@vueuse/core')['useArrayDifference'] const useArrayDifference: typeof import('@vueuse/core')['useArrayDifference']

View File

@@ -121,8 +121,11 @@
const router = useRouter(); const router = useRouter();
const show = defineModel("show"); const show = defineModel("show");
import { useCascaderAreaData } from "@vant/area-data"; import { useCascaderAreaData } from "@vant/area-data";
import { useAliyunCaptcha } from "@/composables/useAliyunCaptcha";
import { showToast } from "vant"; // 引入 showToast 方法 import { showToast } from "vant"; // 引入 showToast 方法
const emit = defineEmits(); // 确保 emit 可以正确使用 const emit = defineEmits(); // 确保 emit 可以正确使用
const { runWithCaptcha } = useAliyunCaptcha();
const props = defineProps({ const props = defineProps({
ancestor: { ancestor: {
type: String, type: String,
@@ -169,22 +172,20 @@ const getSmsCode = async () => {
return; return;
} }
loadingSms.value = true; runWithCaptcha(
(captchaVerifyParam) =>
const { data, error } = await useApiFetch("auth/sendSms") useApiFetch("auth/sendSms")
.post({ mobile: form.value.mobile, actionType: "agentApply" }) .post({ mobile: form.value.mobile, actionType: "agentApply", captchaVerifyParam })
.json(); .json(),
(res) => {
loadingSms.value = false; if (res.code === 200) {
showToast({ message: "获取成功" });
if (data.value && !error.value) { startCountdown();
if (data.value.code === 200) { } else {
showToast({ message: "获取成功" }); showToast(res.msg || "获取失败,请重试");
startCountdown(); // 启动倒计时 }
} else {
showToast(data.value.msg);
} }
} );
}; };
let timer = null; let timer = null;

View File

@@ -282,8 +282,11 @@ import { useRoute, useRouter } from "vue-router";
import { useUserStore } from "@/stores/userStore"; import { useUserStore } from "@/stores/userStore";
import { useDialogStore } from "@/stores/dialogStore"; import { useDialogStore } from "@/stores/dialogStore";
import { useEnv } from "@/composables/useEnv"; import { useEnv } from "@/composables/useEnv";
import { useAliyunCaptcha } from "@/composables/useAliyunCaptcha";
import { showConfirmDialog } from "vant"; import { showConfirmDialog } from "vant";
const { runWithCaptcha } = useAliyunCaptcha();
import Payment from "@/components/Payment.vue"; import Payment from "@/components/Payment.vue";
import BindPhoneDialog from "@/components/BindPhoneDialog.vue"; import BindPhoneDialog from "@/components/BindPhoneDialog.vue";
import LoginDialog from "@/components/LoginDialog.vue"; import LoginDialog from "@/components/LoginDialog.vue";
@@ -631,22 +634,26 @@ async function sendVerificationCode() {
return; return;
} }
const { data, error } = await useApiFetch("/auth/sendSms") runWithCaptcha(
.post({ mobile: formData.mobile, actionType: "query" }) (captchaVerifyParam) =>
.json(); useApiFetch("/auth/sendSms")
.post({ mobile: formData.mobile, actionType: "query", captchaVerifyParam })
if (!error.value && data.value.code === 200) { .json(),
showToast({ message: "验证码发送成功", type: "success" }); (res) => {
startCountdown(); if (res.code === 200) {
nextTick(() => { showToast({ message: "验证码发送成功", type: "success" });
const verificationCodeInput = document.getElementById('verificationCode'); startCountdown();
if (verificationCodeInput) { nextTick(() => {
verificationCodeInput.focus(); const verificationCodeInput = document.getElementById('verificationCode');
if (verificationCodeInput) {
verificationCodeInput.focus();
}
});
} else {
showToast({ message: res.msg || "验证码发送失败,请重试" });
} }
}); }
} else { );
showToast({ message: "验证码发送失败,请重试" });
}
} }
let timer = null; let timer = null;

View File

@@ -0,0 +1,155 @@
import { showToast, showLoadingToast, closeToast } from "vant";
import useApiFetch from "@/composables/useApiFetch";
const ALIYUN_CAPTCHA_SCENE_ID = "wynt39to";
const ENABLE_ENCRYPTED = false;
let captchaInitialised = false;
let captchaReadyPromise = null;
let captchaReadyResolve = null;
async function ensureCaptchaInit() {
if (captchaInitialised || typeof window === "undefined") return;
if (typeof window.initAliyunCaptcha !== "function") return;
captchaInitialised = true;
window.captcha = null;
window.__lastBizResponse = null;
window.__onCaptchaBizSuccess = null;
captchaReadyPromise = new Promise((resolve) => {
captchaReadyResolve = resolve;
});
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;
}
const { data, error } = await useApiFetch("/captcha/encryptedSceneId")
.post()
.json();
const resp = data?.value;
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: encryptedSceneId,
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",
});
}
export function useAliyunCaptcha() {
async function runWithCaptcha(bizVerify, onSuccess) {
if (typeof window === "undefined") {
showToast({ message: "验证码仅支持浏览器环境" });
return;
}
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();
if (captchaReadyPromise) {
await captchaReadyPromise;
captchaReadyPromise = null;
}
if (!window.captcha) {
showToast({ message: "验证码未加载,请刷新页面重试" });
return;
}
window.__onCaptchaBizSuccess = onSuccess;
window.captcha.show();
} finally {
closeToast();
}
}
return { runWithCaptcha };
}
export default useAliyunCaptcha;

View File

@@ -1,7 +1,9 @@
<script setup> <script setup>
import { ref, computed, onUnmounted, nextTick } from 'vue' import { ref, computed, onUnmounted, nextTick } from 'vue'
import { showToast } from 'vant' import { showToast } from 'vant'
import ClickCaptcha from '@/components/ClickCaptcha.vue' import { useAliyunCaptcha } from '@/composables/useAliyunCaptcha'
const { runWithCaptcha } = useAliyunCaptcha()
const router = useRouter() const router = useRouter()
const phoneNumber = ref('') const phoneNumber = ref('')
@@ -13,15 +15,6 @@ const isCountingDown = ref(false)
const countdown = ref(60) const countdown = ref(60)
let timer = null let timer = null
// 验证组件状态
const showCaptcha = ref(false)
const captchaVerified = ref(false)
// 聚焦状态变量
const phoneFocused = ref(false)
const codeFocused = ref(false)
const passwordFocused = ref(false)
const isPhoneNumberValid = computed(() => { const isPhoneNumberValid = computed(() => {
return /^1[3-9]\d{9}$/.test(phoneNumber.value) return /^1[3-9]\d{9}$/.test(phoneNumber.value)
}) })
@@ -41,25 +34,27 @@ async function sendVerificationCode() {
showToast({ message: "请输入有效的手机号" }); showToast({ message: "请输入有效的手机号" });
return return
} }
const { data, error } = await useApiFetch('auth/sendSms')
.post({ mobile: phoneNumber.value, actionType: 'login' })
.json()
if (data.value && !error.value) { runWithCaptcha(
if (data.value.code === 200) { (captchaVerifyParam) =>
showToast({ message: "获取成功" }); useApiFetch('auth/sendSms')
startCountdown() .post({ mobile: phoneNumber.value, actionType: 'login', captchaVerifyParam })
// 聚焦到验证码输入框 .json(),
nextTick(() => { (res) => {
const verificationCodeInput = document.getElementById('verificationCode'); if (res.code === 200) {
if (verificationCodeInput) { showToast({ message: "获取成功" });
verificationCodeInput.focus(); startCountdown()
} nextTick(() => {
}); const verificationCodeInput = document.getElementById('verificationCode');
} else { if (verificationCodeInput) {
showToast(data.value.msg) verificationCodeInput.focus();
}
});
} else {
showToast(res.msg)
}
} }
} )
} }
function startCountdown() { function startCountdown() {
@@ -95,24 +90,9 @@ async function handleLogin() {
showToast({ message: "请先同意用户协议" }); showToast({ message: "请先同意用户协议" });
return return
} }
// 显示验证组件
showCaptcha.value = true
}
// 验证成功回调
function handleCaptchaSuccess() {
captchaVerified.value = true
showCaptcha.value = false
// 执行实际的登录逻辑
performLogin() performLogin()
} }
// 验证关闭回调
function handleCaptchaClose() {
showCaptcha.value = false
}
// 执行实际的登录逻辑
async function performLogin() { async function performLogin() {
const { data, error } = await useApiFetch('/user/mobileCodeLogin') const { data, error } = await useApiFetch('/user/mobileCodeLogin')
.post({ mobile: phoneNumber.value, code: verificationCode.value }) .post({ mobile: phoneNumber.value, code: verificationCode.value })
@@ -200,9 +180,6 @@ const onClickLeft = () => {
</div> </div>
</div> </div>
<!-- 点击验证组件 -->
<ClickCaptcha :visible="showCaptcha" @success="handleCaptchaSuccess" @close="handleCaptchaClose" />
</div> </div>
</template> </template>

View File

@@ -15,8 +15,8 @@ export default defineConfig({
strictPort: true, // 如果端口被占用则抛出错误而不是使用下一个可用端口 strictPort: true, // 如果端口被占用则抛出错误而不是使用下一个可用端口
proxy: { proxy: {
"/api/v1": { "/api/v1": {
// target: "http://127.0.0.1:8888", // 本地接口地址 target: "http://127.0.0.1:8888", // 本地接口地址
target: "https://www.xingfucha.cn", // 本地接口地址 // target: "https://www.xingfucha.cn", // 本地接口地址
changeOrigin: true, changeOrigin: true,
}, },
}, },