feat: 更新版本至 0.0.56,增强登录缓存功能,添加 token 有效性检查

This commit is contained in:
2026-02-21 02:58:23 +08:00
parent 15fd2d3dc8
commit c97e246ba7
3 changed files with 38 additions and 7 deletions

View File

@@ -1,6 +1,6 @@
{
"name": "@kevisual/api",
"version": "0.0.55",
"version": "0.0.56",
"description": "",
"main": "mod.ts",
"scripts": {

View File

@@ -82,7 +82,8 @@ export type CacheStore<T extends Cache = Cache> = {
getValue(): Promise<CacheLogin>;
setValue(value: CacheLogin): Promise<CacheLogin>;
delValue(): Promise<void>;
init(): Promise<any>;
init(): Promise<CacheLogin>;
getIsExpired(): Promise<boolean>;
};
export type LoginCacheStoreOpts<T extends Cache = Cache> = {
@@ -124,6 +125,13 @@ export class LoginCacheStore<T extends Cache = Cache> implements CacheStore<T> {
*/
async delValue() {
await this.cache.del();
this.cacheData = {
loginUsers: [],
user: undefined,
id: undefined,
accessToken: undefined,
refreshToken: undefined,
};
}
getValue(): Promise<CacheLogin> {
return this.cache.get(this.name);
@@ -132,12 +140,14 @@ export class LoginCacheStore<T extends Cache = Cache> implements CacheStore<T> {
* 初始化,设置默认值
*/
async init() {
const defaultData = {
const defaultData: CacheLogin = {
loginUsers: [],
user: null,
id: null,
accessToken: null,
refreshToken: null,
user: undefined,
id: undefined,
accessToken: undefined,
refreshToken: undefined,
accessTokenExpiresIn: undefined,
createdAt: undefined,
};
if (this.cache.init) {
try {
@@ -149,6 +159,7 @@ export class LoginCacheStore<T extends Cache = Cache> implements CacheStore<T> {
} else {
this.cacheData = (await this.getValue()) || defaultData;
}
return this.cacheData;
}
/**
* 设置当前用户
@@ -184,6 +195,18 @@ export class LoginCacheStore<T extends Cache = Cache> implements CacheStore<T> {
const cacheData = this.cacheData;
return Promise.resolve(cacheData.accessToken || '');
}
getIsExpired(): Promise<boolean> {
const cacheData = this.cacheData;
if (!cacheData.accessToken) {
return Promise.resolve(true);
}
if (!cacheData.createdAt || !cacheData.accessTokenExpiresIn) {
return Promise.resolve(false);
}
const now = Date.now();
const expiresIn = cacheData.createdAt + cacheData.accessTokenExpiresIn * 1000;
return Promise.resolve(now >= expiresIn);
}
async clearCurrentUser() {
const user = await this.getCurrentUser();
@@ -195,6 +218,8 @@ export class LoginCacheStore<T extends Cache = Cache> implements CacheStore<T> {
this.cacheData.id = undefined;
this.cacheData.accessToken = undefined;
this.cacheData.refreshToken = undefined;
this.cacheData.accessTokenExpiresIn = undefined;
this.cacheData.createdAt = undefined;
await this.setValue(this.cacheData);
}
async clearAll() {
@@ -203,6 +228,8 @@ export class LoginCacheStore<T extends Cache = Cache> implements CacheStore<T> {
this.cacheData.id = undefined;
this.cacheData.accessToken = undefined;
this.cacheData.refreshToken = undefined;
this.cacheData.accessTokenExpiresIn = undefined;
this.cacheData.createdAt = undefined;
await this.setValue(this.cacheData);
}
}

View File

@@ -305,6 +305,9 @@ export class QueryLogin<T extends Cache = Cache> extends BaseQuery {
const token = this.storage.getItem('token');
return !!token;
}
async checkTokenValid() {
return this.cacheStore.getIsExpired();
}
/**
* 检查本地用户列表
* @returns
@@ -313,6 +316,7 @@ export class QueryLogin<T extends Cache = Cache> extends BaseQuery {
const token = this.storage.getItem('token');
return token || '';
}
async beforeRequest(opts: any = {}) {
const token = this.storage.getItem('token');
if (token) {