diff --git a/package.json b/package.json index 28967be..5301496 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@kevisual/api", - "version": "0.0.59", + "version": "0.0.60", "description": "", "main": "mod.ts", "scripts": { diff --git a/query/query-login/login-cache.ts b/query/query-login/login-cache.ts index d8e6930..3501858 100644 --- a/query/query-login/login-cache.ts +++ b/query/query-login/login-cache.ts @@ -90,6 +90,15 @@ export type LoginCacheStoreOpts = { name: string; cache: T; }; +const defaultCacheData: CacheLogin = { + loginUsers: [], + user: undefined, + id: undefined, + accessToken: undefined, + refreshToken: undefined, + accessTokenExpiresIn: undefined, + createdAt: undefined, +} export class LoginCacheStore implements CacheStore { cache: T; name: string; @@ -100,12 +109,16 @@ export class LoginCacheStore implements CacheStore { } // @ts-ignore this.cache = opts.cache; - this.cacheData = { - loginUsers: [], - user: undefined, - id: undefined, - accessToken: undefined, - refreshToken: undefined, + this.cacheData = { ...defaultCacheData }; + this.name = opts.name; + } + /** + * 设置缓存 + * @param key + * @param value + * @returns + accessTokenExpiresIn: undefined, + createdAt: undefined, }; this.name = opts.name; } @@ -125,13 +138,7 @@ export class LoginCacheStore implements CacheStore { */ async delValue() { await this.cache.del(); - this.cacheData = { - loginUsers: [], - user: undefined, - id: undefined, - accessToken: undefined, - refreshToken: undefined, - }; + this.cacheData = { ...defaultCacheData }; } getValue(): Promise { return this.cache.get(this.name); @@ -140,15 +147,7 @@ export class LoginCacheStore implements CacheStore { * 初始化,设置默认值 */ async init() { - const defaultData: CacheLogin = { - loginUsers: [], - user: undefined, - id: undefined, - accessToken: undefined, - refreshToken: undefined, - accessTokenExpiresIn: undefined, - createdAt: undefined, - }; + const defaultData: CacheLogin = { ...this.cacheData }; if (this.cache.init) { try { const cacheData = await this.cache.init(); @@ -165,18 +164,18 @@ export class LoginCacheStore implements CacheStore { * 设置当前用户 * @param user */ - async setLoginUser(user: CacheLoginUser) { - const has = this.cacheData.loginUsers.find((u) => u.id === user.id); + async setLoginUser(loginUser: CacheLoginUser) { + const has = this.cacheData.loginUsers.find((u) => u.id === loginUser.id); if (has) { - this.cacheData.loginUsers = this.cacheData?.loginUsers?.filter((u) => u?.id && u.id !== user.id); + this.cacheData.loginUsers = this.cacheData?.loginUsers?.filter((u) => u?.id && u.id !== loginUser.id); } - this.cacheData.loginUsers.push(user); - this.cacheData.user = user.user; - this.cacheData.id = user.id; - this.cacheData.accessToken = user.accessToken; - this.cacheData.refreshToken = user.refreshToken; - this.cacheData.accessTokenExpiresIn = user.accessTokenExpiresIn; - this.cacheData.createdAt = user.createdAt; + this.cacheData.loginUsers.push(loginUser); + this.cacheData.user = loginUser.user; + this.cacheData.id = loginUser.id; + this.cacheData.accessToken = loginUser.accessToken; + this.cacheData.refreshToken = loginUser.refreshToken; + this.cacheData.accessTokenExpiresIn = loginUser.accessTokenExpiresIn; + this.cacheData.createdAt = loginUser.createdAt; await this.setValue(this.cacheData); } @@ -214,22 +213,22 @@ export class LoginCacheStore implements CacheStore { if (has) { this.cacheData.loginUsers = this.cacheData?.loginUsers?.filter((u) => u?.id && u.id !== user.id); } - this.cacheData.user = undefined; - this.cacheData.id = undefined; - this.cacheData.accessToken = undefined; - this.cacheData.refreshToken = undefined; - this.cacheData.accessTokenExpiresIn = undefined; - this.cacheData.createdAt = undefined; + const hasOther = this.cacheData.loginUsers.length > 0; + const current = this.cacheData.loginUsers[this.cacheData.loginUsers.length - 1]; + if (hasOther && current) { + this.cacheData.user = current.user; + this.cacheData.id = current.id; + this.cacheData.accessToken = current.accessToken; + this.cacheData.refreshToken = current.refreshToken; + this.cacheData.accessTokenExpiresIn = current.accessTokenExpiresIn; + this.cacheData.createdAt = current.createdAt; + } else { + this.cacheData = { ...defaultCacheData }; + } await this.setValue(this.cacheData); } async clearAll() { - this.cacheData.loginUsers = []; - this.cacheData.user = undefined; - this.cacheData.id = undefined; - this.cacheData.accessToken = undefined; - this.cacheData.refreshToken = undefined; - this.cacheData.accessTokenExpiresIn = undefined; - this.cacheData.createdAt = undefined; + this.cacheData = { ...defaultCacheData }; await this.setValue(this.cacheData); } } diff --git a/query/query-login/query-login.ts b/query/query-login/query-login.ts index b5f973a..6933601 100644 --- a/query/query-login/query-login.ts +++ b/query/query-login/query-login.ts @@ -360,6 +360,7 @@ export class QueryLogin extends BaseQuery { } const isExpired = await this.cacheStore.getIsExpired(); if (isExpired) { + console.log('token过期,正在刷新token', this.cacheStore.cacheData); const res = await this.refreshLoginUser() if (res.code === 200) { // 刷新成功,返回新的token diff --git a/query/query-login/test/expire.ts b/query/query-login/test/expire.ts new file mode 100644 index 0000000..65a9da2 --- /dev/null +++ b/query/query-login/test/expire.ts @@ -0,0 +1,10 @@ +const cacheData = { + accessTokenExpiresIn: 604800, + createdAt: 1771926793545 +}; + +const expiresIn = cacheData.createdAt + cacheData.accessTokenExpiresIn * 1000; +console.log('expiresIn', expiresIn); +const now = Date.now(); +console.log('now', now); +console.log('isExpired', now >= expiresIn); \ No newline at end of file