Compare commits

...
1 Commits
Author SHA1 Message Date
xrain 1597c98f39 支持了localstorage存储token
Publish to npm / publish (push) Failing after 8s
2026-08-10 22:58:28 +08:00
2 changed files with 52 additions and 38 deletions
+17 -5
View File
@@ -83,6 +83,7 @@ export class SimApiCore {
check_url: '/user/info', check_url: '/user/info',
logout_url: '/auth/logout', logout_url: '/auth/logout',
login_url: '/auth/login', login_url: '/auth/login',
tokenStore: "localstorage"
} }
private webConfig: Map<string, Record<string, object>> = new Map(); private webConfig: Map<string, Record<string, object>> = new Map();
@@ -99,7 +100,7 @@ export class SimApiCore {
error: (_err: any) => { error: (_err: any) => {
}, },
}, },
timeout: 10000, timeout: 10000
} }
constructor(options?: SimApiOptions) { constructor(options?: SimApiOptions) {
@@ -179,19 +180,30 @@ export class SimApiCore {
} }
getToken(): string { getToken(): string {
if (this.auth.tokenStore === 'localstorage') {
return localStorage.getItem(this.auth.token_name) ?? ''
}
const name = this.auth.token_name const name = this.auth.token_name
const match = document.cookie.match(new RegExp(`(?:^|;)\\s?${name}=([^;]+)`)) const match = document.cookie.match(new RegExp(`(?:^|;)\\s?${name}=([^;]+)`))
return match ? match[1] : '' return match ? match[1] : ''
} }
setToken(token: string): void { setToken(token: string): void {
const name = this.auth.token_name if (this.auth.tokenStore === 'localstorage') {
document.cookie = `${name}=${token}; path=/; max-age=315360000; secure; samesite=none` 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 { removeToken(): void {
const name = this.auth.token_name if (this.auth.tokenStore === 'localstorage') {
document.cookie = `${name}=; path=/; max-age=0; secure; samesite=none` localStorage.removeItem(this.auth.token_name)
} else {
const name = this.auth.token_name
document.cookie = `${name}=; path=/; max-age=0; secure; samesite=none`
}
} }
+35 -33
View File
@@ -4,61 +4,63 @@
/** 版本信息 */ /** 版本信息 */
export interface SimApiVersions { export interface SimApiVersions {
uiApp: string uiApp: string
uiSimApi: string uiSimApi: string
apiApp: string apiApp: string
apiSimApi: string apiSimApi: string
apiAppFull: string apiAppFull: string
apiSimApiFull: string apiSimApiFull: string
} }
/** 认证配置 */ /** 认证配置 */
export interface SimApiAuthConfig { export interface SimApiAuthConfig {
/** localStorage key,默认 'simapi-auth-token' */ /** localStorage key,默认 'simapi-auth-token' */
token_name: string token_name: string
/** 检查登录接口,默认 '/auth/check' */ /** 检查登录接口,默认 '/auth/check' */
check_url: string check_url: string
/** 登出接口,默认 '/auth/logout' */ /** 登出接口,默认 '/auth/logout' */
logout_url: string logout_url: string
/** 登录接口,默认 '/auth/login' */ /** 登录接口,默认 '/auth/login' */
login_url: string login_url: string
/** Token 存储位置 */
tokenStore: string
} }
/** 业务错误码回调 */ /** 业务错误码回调 */
export interface SimApiBusinessCallback { export interface SimApiBusinessCallback {
[key: number | string]: (data: any) => void [key: number | string]: (data: any) => void
} }
/** 响应拦截回调 */ /** 响应拦截回调 */
export interface SimApiResponseCallback { export interface SimApiResponseCallback {
success: (response: any) => any success: (response: any) => any
error: (err: any) => void error: (err: any) => void
} }
/** API 配置 */ /** API 配置 */
export interface SimApiApiConfig { export interface SimApiApiConfig {
/** 多端点映射 */ /** 多端点映射 */
endpoints: { [name: string]: string } endpoints: { [name: string]: string }
/** 默认端点名称 */ /** 默认端点名称 */
defaultEndpoint: string defaultEndpoint: string
/** 业务错误码回调 */ /** 业务错误码回调 */
businessCallback: SimApiBusinessCallback businessCallback: SimApiBusinessCallback
/** 响应拦截器 */ /** 响应拦截器 */
responseCallback: SimApiResponseCallback responseCallback: SimApiResponseCallback
/** 请求超时时间(毫秒),默认 10000 */ /** 请求超时时间(毫秒),默认 10000 */
timeout?: number timeout?: number
} }
/** SimApi 完整配置 */ /** SimApi 完整配置 */
export interface SimApiOptions { export interface SimApiOptions {
debug?: boolean debug?: boolean
auth?: Partial<SimApiAuthConfig> auth?: Partial<SimApiAuthConfig>
api?: Partial<SimApiApiConfig> api?: Partial<SimApiApiConfig>
} }
/** SimApi 标准响应格式 */ /** SimApi 标准响应格式 */
export interface SimApiBaseResponse<T = any> { export interface SimApiBaseResponse<T = any> {
code: number code: number
message: string message: string
data?: T data?: T
} }