Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1597c98f39 | ||
|
|
7dbb48ad90 | ||
|
|
88af4a2d96 | ||
|
|
9e44c6fcaf | ||
|
|
592607f1e2 | ||
|
|
7aee4b708d | ||
|
|
1af5900593 | ||
|
|
78ef6d9ca0 | ||
|
|
cee0bc4cfe | ||
|
|
9a8510d833 | ||
|
|
ee034a7252 | ||
|
|
65bb076384 |
+26
-31
@@ -80,10 +80,12 @@ export class SimApiCore {
|
|||||||
private debug: boolean = true
|
private debug: boolean = true
|
||||||
private auth: SimApiAuthConfig = {
|
private auth: SimApiAuthConfig = {
|
||||||
token_name: 'simapi-auth-token',
|
token_name: 'simapi-auth-token',
|
||||||
check_url: '/auth/check',
|
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 api: SimApiApiConfig = {
|
private api: SimApiApiConfig = {
|
||||||
endpoints: {default: ''},
|
endpoints: {default: ''},
|
||||||
@@ -98,7 +100,7 @@ export class SimApiCore {
|
|||||||
error: (_err: any) => {
|
error: (_err: any) => {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
timeout: 10000,
|
timeout: 10000
|
||||||
}
|
}
|
||||||
|
|
||||||
constructor(options?: SimApiOptions) {
|
constructor(options?: SimApiOptions) {
|
||||||
@@ -178,20 +180,31 @@ 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 {
|
||||||
|
if (this.auth.tokenStore === 'localstorage') {
|
||||||
|
localStorage.setItem(this.auth.token_name, token)
|
||||||
|
} else {
|
||||||
const name = this.auth.token_name
|
const name = this.auth.token_name
|
||||||
document.cookie = `${name}=${token}; path=/; secure; samesite=none`
|
document.cookie = `${name}=${token}; path=/; max-age=315360000; secure; samesite=none`
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
removeToken(): void {
|
removeToken(): void {
|
||||||
|
if (this.auth.tokenStore === 'localstorage') {
|
||||||
|
localStorage.removeItem(this.auth.token_name)
|
||||||
|
} else {
|
||||||
const name = this.auth.token_name
|
const name = this.auth.token_name
|
||||||
document.cookie = `${name}=; path=/; max-age=0; secure; samesite=none`
|
document.cookie = `${name}=; path=/; max-age=0; secure; samesite=none`
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
genS4(): string {
|
genS4(): string {
|
||||||
@@ -212,20 +225,8 @@ export class SimApiCore {
|
|||||||
console.log('[DEBUG]', ...args)
|
console.log('[DEBUG]', ...args)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
async getConfig(reload: boolean = false, endpointName: string = 'default'): Promise<Record<string, object>> {
|
||||||
* 获取版本信息
|
if (!this.webConfig.has(endpointName) || reload) {
|
||||||
*
|
|
||||||
* @param endpointName - 指定从哪个 endpoint 获取版本,默认使用 default endpoint
|
|
||||||
* @returns 版本信息对象
|
|
||||||
*
|
|
||||||
* @example
|
|
||||||
* // 从默认 endpoint 获取
|
|
||||||
* const versions = await api.getVersion()
|
|
||||||
*
|
|
||||||
* // 从指定 endpoint 获取
|
|
||||||
* const versions = await api.getVersion('backup')
|
|
||||||
*/
|
|
||||||
async getVersion(endpointName?: string): Promise<SimApiVersions> {
|
|
||||||
const versions: SimApiVersions = {
|
const versions: SimApiVersions = {
|
||||||
uiApp: typeof AppVersion === 'undefined' ? "0.0.0-develop" : AppVersion,
|
uiApp: typeof AppVersion === 'undefined' ? "0.0.0-develop" : AppVersion,
|
||||||
uiSimApi: typeof SimApiVersion === 'undefined' ? "0.0.0-develop" : SimApiVersion,
|
uiSimApi: typeof SimApiVersion === 'undefined' ? "0.0.0-develop" : SimApiVersion,
|
||||||
@@ -234,10 +235,9 @@ export class SimApiCore {
|
|||||||
apiAppFull: '0.0.0',
|
apiAppFull: '0.0.0',
|
||||||
apiSimApiFull: '0.0.0',
|
apiSimApiFull: '0.0.0',
|
||||||
};
|
};
|
||||||
try {
|
const resp = await this.query<any>('/config', {}, endpointName)
|
||||||
const resp = await this.query<any>('/versions', {}, endpointName)
|
|
||||||
if (resp?.data) {
|
if (resp?.data) {
|
||||||
const d = resp.data
|
const d = resp.data.Versions;
|
||||||
versions.apiApp = d.App?.split('+')[0] ?? '0.0.0';
|
versions.apiApp = d.App?.split('+')[0] ?? '0.0.0';
|
||||||
versions.apiSimApi = d.SimApi?.split('+')[0] ?? '0.0.0';
|
versions.apiSimApi = d.SimApi?.split('+')[0] ?? '0.0.0';
|
||||||
versions.apiAppFull = d.App ?? '0.0.0';
|
versions.apiAppFull = d.App ?? '0.0.0';
|
||||||
@@ -245,12 +245,12 @@ export class SimApiCore {
|
|||||||
if (this.debug) {
|
if (this.debug) {
|
||||||
console.log(`UI主应用版本: ${versions.uiApp}\nUISimApi版本: ${versions.uiSimApi}\nAPI主应用版本: ${versions.apiApp}\nAPISimApi版本: ${versions.apiSimApi}`)
|
console.log(`UI主应用版本: ${versions.uiApp}\nUISimApi版本: ${versions.uiSimApi}\nAPI主应用版本: ${versions.apiApp}\nAPISimApi版本: ${versions.apiSimApi}`)
|
||||||
}
|
}
|
||||||
return versions
|
const conf = JSON.parse(JSON.stringify(resp.data));
|
||||||
|
conf.Versions = versions;
|
||||||
|
this.webConfig.set(endpointName, conf);
|
||||||
}
|
}
|
||||||
} catch {
|
|
||||||
// 版本获取失败返回默认值
|
|
||||||
}
|
}
|
||||||
return versions;
|
return this.webConfig.get(endpointName)!;
|
||||||
}
|
}
|
||||||
|
|
||||||
async query<T = any>(
|
async query<T = any>(
|
||||||
@@ -340,12 +340,7 @@ export class SimApiCore {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
async checkLogin(url?: string | null): Promise<void> {
|
async checkLogin(url?: string | null): Promise<any> {
|
||||||
if (url !== null) {
|
return this.query(url ?? this.auth.check_url);
|
||||||
await this.query(url ?? this.auth.check_url).catch(() => {
|
|
||||||
})
|
|
||||||
} else if (this.getToken()) {
|
|
||||||
this.api.businessCallback[401]?.(null)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-3
@@ -15,7 +15,7 @@ export const useSimApi = defineStore('simapi', {
|
|||||||
},
|
},
|
||||||
actions: {
|
actions: {
|
||||||
// 所有方法直接代理到 core
|
// 所有方法直接代理到 core
|
||||||
async loadFromFile(file: string = 'config.json'): Promise<void> {
|
async loadFromFile(file: string = '/config.json'): Promise<void> {
|
||||||
return this._core.loadFromFile(file)
|
return this._core.loadFromFile(file)
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -76,8 +76,8 @@ export const useSimApi = defineStore('simapi', {
|
|||||||
return this._core.getEndpoint(name)
|
return this._core.getEndpoint(name)
|
||||||
},
|
},
|
||||||
|
|
||||||
async getVersion(endpointName?: string): Promise<SimApiVersions> {
|
async getConfig(reload = false, endpointName?: string): Promise<Record<string, object>> {
|
||||||
return this._core.getVersion(endpointName)
|
return this._core.getConfig(reload, endpointName)
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -22,6 +22,8 @@ export interface SimApiAuthConfig {
|
|||||||
logout_url: string
|
logout_url: string
|
||||||
/** 登录接口,默认 '/auth/login' */
|
/** 登录接口,默认 '/auth/login' */
|
||||||
login_url: string
|
login_url: string
|
||||||
|
/** Token 存储位置 */
|
||||||
|
tokenStore: string
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 业务错误码回调 */
|
/** 业务错误码回调 */
|
||||||
|
|||||||
Reference in New Issue
Block a user