update query login

This commit is contained in:
2025-03-21 20:40:43 +08:00
parent ca269e5ae2
commit b2ad3da8d8
8 changed files with 75 additions and 52 deletions

View File

@@ -5,7 +5,8 @@
"main": "dist/query-login.js",
"types": "dist/query-login.d.ts",
"scripts": {
"build": "tsup"
"build": "tsup",
"watch": "tsup --watch"
},
"keywords": [],
"author": "abearxiong <xiongxiao@xiongxiao.me>",

View File

@@ -103,19 +103,6 @@ export class QueryLogin {
},
);
}
/**
* 检查401错误并刷新token, 如果refreshToken存在则刷新token, 否则返回401
* @param res
* @returns
*/
async check401ToRefreshToken(res: Result) {
const refreshToken = await this.cache.getRefreshToken();
if (refreshToken) {
const res = await this.queryRefreshToken(refreshToken);
return res;
}
return res;
}
/**
* 检查401错误并刷新token, 如果refreshToken存在则刷新token, 否则返回401
* @param response
@@ -153,6 +140,7 @@ export class QueryLogin {
return res;
}
}
return response as any;
}
/**
* 获取用户信息
@@ -180,8 +168,12 @@ export class QueryLogin {
},
);
}
async postSwitchUser(username: string) {
/**
* 请求更新,切换用户, 使用switchUser
* @param username
* @returns
*/
private async postSwitchUser(username: string) {
return this.post({ key: 'switchCheck', data: { username } });
}
/**
@@ -194,7 +186,7 @@ export class QueryLogin {
const user = localUserList.find((userItem) => userItem.user.username === username);
if (user) {
this.storage.setItem('token', user.accessToken || '');
await this.cache.setLoginUser(user);
await this.beforeSetLoginUser({ accessToken: user.accessToken, refreshToken: user.refreshToken });
return {
code: 200,
data: {
@@ -208,8 +200,9 @@ export class QueryLogin {
const res = await this.postSwitchUser(username);
if (res.code === 200) {
this.cache.setLoginUser(res.data);
await this.beforeSetLoginUser({ accessToken: res.data.accessToken, refreshToken: res.data.refreshToken });
const { accessToken, refreshToken } = res?.data || {};
this.storage.setItem('token', accessToken || '');
await this.beforeSetLoginUser({ accessToken, refreshToken });
}
return res;
}