From 1597c98f39bed6fead6360f4e4919a6c50eca152 Mon Sep 17 00:00:00 2001 From: xRain Date: Mon, 10 Aug 2026 22:58:28 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E4=BA=86localstorage?= =?UTF-8?q?=E5=AD=98=E5=82=A8token?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/simapi.core.ts | 22 +++++++++++---- src/types.ts | 68 ++++++++++++++++++++++++---------------------- 2 files changed, 52 insertions(+), 38 deletions(-) diff --git a/src/simapi.core.ts b/src/simapi.core.ts index 55055c0..9630e8e 100644 --- a/src/simapi.core.ts +++ b/src/simapi.core.ts @@ -83,6 +83,7 @@ export class SimApiCore { check_url: '/user/info', logout_url: '/auth/logout', login_url: '/auth/login', + tokenStore: "localstorage" } private webConfig: Map> = new Map(); @@ -99,7 +100,7 @@ export class SimApiCore { error: (_err: any) => { }, }, - timeout: 10000, + timeout: 10000 } constructor(options?: SimApiOptions) { @@ -179,19 +180,30 @@ export class SimApiCore { } getToken(): string { + if (this.auth.tokenStore === 'localstorage') { + return localStorage.getItem(this.auth.token_name) ?? '' + } const name = this.auth.token_name const match = document.cookie.match(new RegExp(`(?:^|;)\\s?${name}=([^;]+)`)) return match ? match[1] : '' } setToken(token: string): void { - const name = this.auth.token_name - document.cookie = `${name}=${token}; path=/; max-age=315360000; secure; samesite=none` + if (this.auth.tokenStore === 'localstorage') { + localStorage.setItem(this.auth.token_name, token) + } else { + const name = this.auth.token_name + document.cookie = `${name}=${token}; path=/; max-age=315360000; secure; samesite=none` + } } removeToken(): void { - const name = this.auth.token_name - document.cookie = `${name}=; path=/; max-age=0; secure; samesite=none` + if (this.auth.tokenStore === 'localstorage') { + localStorage.removeItem(this.auth.token_name) + } else { + const name = this.auth.token_name + document.cookie = `${name}=; path=/; max-age=0; secure; samesite=none` + } } diff --git a/src/types.ts b/src/types.ts index cdacc72..408c0e8 100644 --- a/src/types.ts +++ b/src/types.ts @@ -4,61 +4,63 @@ /** 版本信息 */ export interface SimApiVersions { - uiApp: string - uiSimApi: string - apiApp: string - apiSimApi: string - apiAppFull: string - apiSimApiFull: string + uiApp: string + uiSimApi: string + apiApp: string + apiSimApi: string + apiAppFull: string + apiSimApiFull: string } /** 认证配置 */ export interface SimApiAuthConfig { - /** localStorage key,默认 'simapi-auth-token' */ - token_name: string - /** 检查登录接口,默认 '/auth/check' */ - check_url: string - /** 登出接口,默认 '/auth/logout' */ - logout_url: string - /** 登录接口,默认 '/auth/login' */ - login_url: string + /** localStorage key,默认 'simapi-auth-token' */ + token_name: string + /** 检查登录接口,默认 '/auth/check' */ + check_url: string + /** 登出接口,默认 '/auth/logout' */ + logout_url: string + /** 登录接口,默认 '/auth/login' */ + login_url: string + /** Token 存储位置 */ + tokenStore: string } /** 业务错误码回调 */ export interface SimApiBusinessCallback { - [key: number | string]: (data: any) => void + [key: number | string]: (data: any) => void } /** 响应拦截回调 */ export interface SimApiResponseCallback { - success: (response: any) => any - error: (err: any) => void + success: (response: any) => any + error: (err: any) => void } /** API 配置 */ export interface SimApiApiConfig { - /** 多端点映射 */ - endpoints: { [name: string]: string } - /** 默认端点名称 */ - defaultEndpoint: string - /** 业务错误码回调 */ - businessCallback: SimApiBusinessCallback - /** 响应拦截器 */ - responseCallback: SimApiResponseCallback - /** 请求超时时间(毫秒),默认 10000 */ - timeout?: number + /** 多端点映射 */ + endpoints: { [name: string]: string } + /** 默认端点名称 */ + defaultEndpoint: string + /** 业务错误码回调 */ + businessCallback: SimApiBusinessCallback + /** 响应拦截器 */ + responseCallback: SimApiResponseCallback + /** 请求超时时间(毫秒),默认 10000 */ + timeout?: number } /** SimApi 完整配置 */ export interface SimApiOptions { - debug?: boolean - auth?: Partial - api?: Partial + debug?: boolean + auth?: Partial + api?: Partial } /** SimApi 标准响应格式 */ export interface SimApiBaseResponse { - code: number - message: string - data?: T + code: number + message: string + data?: T }