Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fa4032f239 | ||
|
|
713b8ae31e |
+20
-11
@@ -77,19 +77,19 @@ async function fetchPost<T = any>(
|
|||||||
// ── SimApiCore ────────────────────────────────────────
|
// ── SimApiCore ────────────────────────────────────────
|
||||||
|
|
||||||
export class SimApiCore {
|
export class SimApiCore {
|
||||||
debug: boolean = true
|
private debug: boolean = true
|
||||||
auth: SimApiAuthConfig = {
|
private auth: SimApiAuthConfig = {
|
||||||
token_name: 'simapi-auth-token',
|
token_name: 'simapi-auth-token',
|
||||||
check_url: '/auth/check',
|
check_url: '/auth/check',
|
||||||
logout_url: '/auth/logout',
|
logout_url: '/auth/logout',
|
||||||
login_url: '/auth/login',
|
login_url: '/auth/login',
|
||||||
}
|
}
|
||||||
|
|
||||||
api: SimApiApiConfig = {
|
private api: SimApiApiConfig = {
|
||||||
endpoints: {default: ''},
|
endpoints: {default: ''},
|
||||||
defaultEndpoint: 'default',
|
defaultEndpoint: 'default',
|
||||||
businessCallback: {
|
businessCallback: {
|
||||||
401: () => localStorage.removeItem(this.auth.token_name),
|
401: () => this.removeToken(),
|
||||||
common: () => {
|
common: () => {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -146,6 +146,14 @@ export class SimApiCore {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get isDebug() {
|
||||||
|
return this.debug
|
||||||
|
}
|
||||||
|
|
||||||
|
setDebug(debug: boolean) {
|
||||||
|
this.debug = debug;
|
||||||
|
}
|
||||||
|
|
||||||
setEndpoints(endpoints: { [name: string]: string }): void {
|
setEndpoints(endpoints: { [name: string]: string }): void {
|
||||||
this.api.endpoints = {...this.api.endpoints, ...endpoints}
|
this.api.endpoints = {...this.api.endpoints, ...endpoints}
|
||||||
}
|
}
|
||||||
@@ -159,20 +167,21 @@ export class SimApiCore {
|
|||||||
}
|
}
|
||||||
|
|
||||||
getToken(): string {
|
getToken(): string {
|
||||||
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 {
|
setToken(token: string): void {
|
||||||
localStorage.setItem(this.auth.token_name, token)
|
const name = this.auth.token_name
|
||||||
|
document.cookie = `${name}=${token}; path=/; secure; samesite=none`
|
||||||
}
|
}
|
||||||
|
|
||||||
removeToken(): void {
|
removeToken(): void {
|
||||||
localStorage.removeItem(this.auth.token_name)
|
const name = this.auth.token_name
|
||||||
|
document.cookie = `${name}=; path=/; max-age=0; secure; samesite=none`
|
||||||
}
|
}
|
||||||
|
|
||||||
get isLoggedIn(): boolean {
|
|
||||||
return !!localStorage.getItem(this.auth.token_name)
|
|
||||||
}
|
|
||||||
|
|
||||||
genS4(): string {
|
genS4(): string {
|
||||||
return (((1 + Math.random()) * 0x10000 * Date.parse(new Date().toString())) | 0)
|
return (((1 + Math.random()) * 0x10000 * Date.parse(new Date().toString())) | 0)
|
||||||
@@ -254,7 +263,7 @@ export class SimApiCore {
|
|||||||
|
|
||||||
if (this.debug) {
|
if (this.debug) {
|
||||||
headers['Query-Id'] = queryId
|
headers['Query-Id'] = queryId
|
||||||
console.log('[REQUEST*]', queryId, '->', uri, 'AUTH:', localStorage.getItem(this.auth.token_name))
|
console.log('[REQUEST*]', queryId, '->', uri, 'AUTH:', this.getToken())
|
||||||
}
|
}
|
||||||
|
|
||||||
const url = this.getEndpoint(endpointKey) + uri
|
const url = this.getEndpoint(endpointKey) + uri
|
||||||
|
|||||||
+2
-9
@@ -10,16 +10,9 @@ export const useSimApi = defineStore('simapi', {
|
|||||||
// 在 state 中实例化 core
|
// 在 state 中实例化 core
|
||||||
_core: new SimApiCore(),
|
_core: new SimApiCore(),
|
||||||
}),
|
}),
|
||||||
|
|
||||||
getters: {
|
getters: {
|
||||||
// 直接映射 core 的属性和方法
|
IsDebug: state => state._core.isDebug
|
||||||
debug: (state) => state._core.debug,
|
|
||||||
token: (state) => state._core.getToken(),
|
|
||||||
isLoggedIn: (state) => state._core.isLoggedIn,
|
|
||||||
api: (state) => state._core.api,
|
|
||||||
auth: (state) => state._core.auth,
|
|
||||||
},
|
},
|
||||||
|
|
||||||
actions: {
|
actions: {
|
||||||
// 所有方法直接代理到 core
|
// 所有方法直接代理到 core
|
||||||
autoInit(): void {
|
autoInit(): void {
|
||||||
@@ -31,7 +24,7 @@ export const useSimApi = defineStore('simapi', {
|
|||||||
},
|
},
|
||||||
|
|
||||||
setDebug(debug: boolean): void {
|
setDebug(debug: boolean): void {
|
||||||
this._core.debug = debug
|
this._core.setDebug(debug);
|
||||||
},
|
},
|
||||||
|
|
||||||
setEndpoints(endpoints: { [name: string]: string }): void {
|
setEndpoints(endpoints: { [name: string]: string }): void {
|
||||||
|
|||||||
Reference in New Issue
Block a user