Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1597c98f39 | ||
|
|
7dbb48ad90 | ||
|
|
88af4a2d96 | ||
|
|
9e44c6fcaf | ||
|
|
592607f1e2 |
+23
-11
@@ -83,8 +83,9 @@ export class SimApiCore {
|
||||
check_url: '/user/info',
|
||||
logout_url: '/auth/logout',
|
||||
login_url: '/auth/login',
|
||||
tokenStore: "localstorage"
|
||||
}
|
||||
webConfig: Map<string, Map<string, object>> = new Map();
|
||||
private webConfig: Map<string, Record<string, object>> = new Map();
|
||||
|
||||
private api: SimApiApiConfig = {
|
||||
endpoints: {default: ''},
|
||||
@@ -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`
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -213,7 +225,7 @@ export class SimApiCore {
|
||||
console.log('[DEBUG]', ...args)
|
||||
}
|
||||
|
||||
async getConfig(reload: boolean = false, endpointName: string = 'default'): Promise<Map<string, object>> {
|
||||
async getConfig(reload: boolean = false, endpointName: string = 'default'): Promise<Record<string, object>> {
|
||||
if (!this.webConfig.has(endpointName) || reload) {
|
||||
const versions: SimApiVersions = {
|
||||
uiApp: typeof AppVersion === 'undefined' ? "0.0.0-develop" : AppVersion,
|
||||
@@ -225,7 +237,7 @@ export class SimApiCore {
|
||||
};
|
||||
const resp = await this.query<any>('/config', {}, endpointName)
|
||||
if (resp?.data) {
|
||||
const d = resp.data.versions;
|
||||
const d = resp.data.Versions;
|
||||
versions.apiApp = d.App?.split('+')[0] ?? '0.0.0';
|
||||
versions.apiSimApi = d.SimApi?.split('+')[0] ?? '0.0.0';
|
||||
versions.apiAppFull = d.App ?? '0.0.0';
|
||||
@@ -233,9 +245,9 @@ export class SimApiCore {
|
||||
if (this.debug) {
|
||||
console.log(`UI主应用版本: ${versions.uiApp}\nUISimApi版本: ${versions.uiSimApi}\nAPI主应用版本: ${versions.apiApp}\nAPISimApi版本: ${versions.apiSimApi}`)
|
||||
}
|
||||
resp.data.versions = versions;
|
||||
this.webConfig.set(endpointName, resp.data);
|
||||
|
||||
const conf = JSON.parse(JSON.stringify(resp.data));
|
||||
conf.Versions = versions;
|
||||
this.webConfig.set(endpointName, conf);
|
||||
}
|
||||
}
|
||||
return this.webConfig.get(endpointName)!;
|
||||
|
||||
+1
-2
@@ -11,7 +11,6 @@ export const useSimApi = defineStore('simapi', {
|
||||
_core: new SimApiCore(),
|
||||
}),
|
||||
getters: {
|
||||
webConfig: state => state._core.webConfig,
|
||||
IsDebug: state => state._core.isDebug
|
||||
},
|
||||
actions: {
|
||||
@@ -77,7 +76,7 @@ export const useSimApi = defineStore('simapi', {
|
||||
return this._core.getEndpoint(name)
|
||||
},
|
||||
|
||||
async getConfig(reload = false, endpointName?: string): Promise<Map<string, object>> {
|
||||
async getConfig(reload = false, endpointName?: string): Promise<Record<string, object>> {
|
||||
return this._core.getConfig(reload, endpointName)
|
||||
},
|
||||
},
|
||||
|
||||
+35
-33
@@ -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<SimApiAuthConfig>
|
||||
api?: Partial<SimApiApiConfig>
|
||||
debug?: boolean
|
||||
auth?: Partial<SimApiAuthConfig>
|
||||
api?: Partial<SimApiApiConfig>
|
||||
}
|
||||
|
||||
/** SimApi 标准响应格式 */
|
||||
export interface SimApiBaseResponse<T = any> {
|
||||
code: number
|
||||
message: string
|
||||
data?: T
|
||||
code: number
|
||||
message: string
|
||||
data?: T
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user