fix: harden wechat mp oauth

This commit is contained in:
empty
2026-02-04 01:29:05 +08:00
parent 99fe68e851
commit c2731ce1dc
7 changed files with 105 additions and 6 deletions

View File

@@ -24,7 +24,7 @@ function isWechatBrowser(): boolean {
/**
* 用code完成登录
*/
async function loginWithCode(code: string) {
async function loginWithCode(code: string, state?: string) {
isProcessing.value = true;
showLoadingToast({ message: '登录中...', forbidClick: true });
@@ -32,7 +32,7 @@ async function loginWithCode(code: string) {
const response = await fetch(`${apiUrl}/api/mp/login`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ code }),
body: JSON.stringify({ code, state }),
});
const result = await response.json();
@@ -92,11 +92,12 @@ async function redirectToWechatAuth() {
async function handleWechatAuth() {
// 检查URL中是否有code参数授权回调
const code = route.query.code as string;
const state = route.query.state as string;
if (code) {
// 有code用code完成登录
console.log('[HomeView] Got code from callback, logging in...');
await loginWithCode(code);
await loginWithCode(code, state);
} else {
// 无code跳转到授权页面
console.log('[HomeView] No code, redirecting to auth...');