This commit is contained in:
Mrx
2026-02-28 11:21:11 +08:00
parent 2fad9bd3dd
commit ca575800ba

View File

@@ -80,54 +80,38 @@ onBeforeMount(async () => {
await store.fetchAgentStatus(); await store.fetchAgentStatus();
} }
}); });
import { applyForAgent, registerByInviteCode } from "@/api/agent"; import useApiFetch from '@/composables/useApiFetch'
const submitApplication = async (formData) => { const submitApplication = async (formData) => {
const { region, mobile, wechat_id, code, referrer } = formData; const { region, mobile, wechat_id, code, referrer } = formData;
// 根据是否已登录选择不同的API // 根据是否已登录选择不同的API
const isLoggedIn = !!localStorage.getItem("token"); const isLoggedIn = !!localStorage.getItem("token");
const apiCall = isLoggedIn ? applyForAgent : registerByInviteCode; const apiUrl = isLoggedIn ? 'agent/apply' : 'agent/register/invite';
// 使用滑块验证码保护申请接口 // 使用滑块验证码保护申请接口
runWithCaptcha( runWithCaptcha(
(captchaVerifyParam) => { (captchaVerifyParam) =>
let postData = { useApiFetch(apiUrl)
region, .post({ region, mobile, wechat_id, code, referrer, captchaVerifyParam })
mobile, .json(),
wechat_id, (res) => {
code, if (res.code === 200) {
referrer,
captchaVerifyParam,
};
return apiCall(postData);
},
async ({ data, error }) => {
if (data.value && !error.value) {
if (data.value.code === 200) {
showApplyPopup.value = false; showApplyPopup.value = false;
showToast({ message: "注册成功,您已成为代理!" }); showToast({ message: "注册成功,您已成为代理!" });
// 更新token和状态 // 更新token和状态
if (data.value.data.accessToken) { if (res.data.accessToken) {
localStorage.setItem("token", data.value.data.accessToken); localStorage.setItem("token", res.data.accessToken);
localStorage.setItem( localStorage.setItem("refreshAfter", res.data.refreshAfter);
"refreshAfter", localStorage.setItem("accessExpire", res.data.accessExpire);
data.value.data.refreshAfter
);
localStorage.setItem(
"accessExpire",
data.value.data.accessExpire
);
// 重新获取代理状态 // 重新获取代理状态
await store.fetchAgentStatus(); store.fetchAgentStatus();
await userStore.fetchUserInfo(); userStore.fetchUserInfo();
// 跳转到代理主页 // 跳转到代理主页
router.replace("/agent"); router.replace("/agent");
} }
} else { } else {
console.log("申请失败", data.value); showToast({ message: res.msg || "申请失败" });
showToast({ message: data.value.msg || "申请失败" });
}
} }
} }
); );