feat: add query by login web wechat

This commit is contained in:
2025-03-27 10:58:59 +08:00
parent e0bf83f062
commit 05f0373834

View File

@@ -86,6 +86,32 @@ export class QueryLogin {
}
return res;
}
async loginByWechat(data: { code: string }) {
const res = await this.post<QueryLoginResult>({ path: 'wx', key: 'open-login', code: data.code });
if (res.code === 200) {
const { accessToken, refreshToken } = res?.data || {};
this.storage.setItem('token', accessToken || '');
await this.beforeSetLoginUser({ accessToken, refreshToken });
}
return res;
}
/**
* 检测微信登录登陆成功后调用onSuccess否则调用onError
* @param param0
*/
async checkWechat({ onSuccess, onError }: { onSuccess?: (res: QueryLoginResult) => void; onError?: (res: any) => void }) {
const url = new URL(window.location.href);
const code = url.searchParams.get('code');
const state = url.searchParams.get('state');
if (code && state) {
const res = await this.loginByWechat({ code });
if (res.code === 200) {
onSuccess?.(res.data);
} else {
onError?.(res);
}
}
}
/**
* 登陆成功,需要获取用户信息进行缓存
* @param param0
@@ -177,7 +203,7 @@ export class QueryLogin {
/**
* 一个简单的401处理 如果401则刷新token, 如果refreshToken不存在则返回401
* refetch 是否重新请求, 会有bug无限循环按需要使用
* TODO:
* TODO:
* @param response
* @param ctx
* @param opts