Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fa4032f239 | ||
|
|
713b8ae31e | ||
|
|
e0754ad635 |
+28
-16
@@ -77,19 +77,19 @@ async function fetchPost<T = any>(
|
||||
// ── SimApiCore ────────────────────────────────────────
|
||||
|
||||
export class SimApiCore {
|
||||
debug: boolean = true
|
||||
auth: SimApiAuthConfig = {
|
||||
private debug: boolean = true
|
||||
private auth: SimApiAuthConfig = {
|
||||
token_name: 'simapi-auth-token',
|
||||
check_url: '/auth/check',
|
||||
logout_url: '/auth/logout',
|
||||
login_url: '/auth/login',
|
||||
}
|
||||
|
||||
api: SimApiApiConfig = {
|
||||
private api: SimApiApiConfig = {
|
||||
endpoints: {default: ''},
|
||||
defaultEndpoint: 'default',
|
||||
businessCallback: {
|
||||
401: () => localStorage.removeItem(this.auth.token_name),
|
||||
401: () => this.removeToken(),
|
||||
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 {
|
||||
this.api.endpoints = {...this.api.endpoints, ...endpoints}
|
||||
}
|
||||
@@ -159,20 +167,21 @@ export class SimApiCore {
|
||||
}
|
||||
|
||||
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 {
|
||||
localStorage.setItem(this.auth.token_name, token)
|
||||
const name = this.auth.token_name
|
||||
document.cookie = `${name}=${token}; path=/; secure; samesite=none`
|
||||
}
|
||||
|
||||
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 {
|
||||
return (((1 + Math.random()) * 0x10000 * Date.parse(new Date().toString())) | 0)
|
||||
@@ -237,7 +246,8 @@ export class SimApiCore {
|
||||
uri: string,
|
||||
params: any = {},
|
||||
endpointKey?: string,
|
||||
extraHeaders?: Record<string, string>
|
||||
extraHeaders?: Record<string, string>,
|
||||
selfHandleError: boolean = false
|
||||
): Promise<SimApiBaseResponse<T>> {
|
||||
const headers: Record<string, string> = {...extraHeaders, ...{}}
|
||||
const queryId = this.genS4()
|
||||
@@ -253,7 +263,7 @@ export class SimApiCore {
|
||||
|
||||
if (this.debug) {
|
||||
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
|
||||
@@ -271,10 +281,12 @@ export class SimApiCore {
|
||||
const processedData = this.api.responseCallback.success(respData) as SimApiBaseResponse<T>
|
||||
|
||||
// 业务回调处理
|
||||
if (this.api.businessCallback.hasOwnProperty(processedData.code)) {
|
||||
this.api.businessCallback[processedData.code](processedData)
|
||||
} else if (this.api.businessCallback['common'] && processedData.code !== 200) {
|
||||
this.api.businessCallback['common'](processedData)
|
||||
if (!selfHandleError) {
|
||||
if (this.api.businessCallback.hasOwnProperty(processedData.code)) {
|
||||
this.api.businessCallback[processedData.code](processedData)
|
||||
} else if (this.api.businessCallback['common'] && processedData.code !== 200) {
|
||||
this.api.businessCallback['common'](processedData)
|
||||
}
|
||||
}
|
||||
|
||||
// code != 200 时抛出业务错误
|
||||
|
||||
+62
-68
@@ -1,91 +1,85 @@
|
||||
import { defineStore } from 'pinia'
|
||||
import { SimApiCore } from './simapi.core'
|
||||
import type { SimApiBaseResponse, SimApiOptions, SimApiVersions } from './types'
|
||||
import {defineStore} from 'pinia'
|
||||
import {SimApiCore} from './simapi.core'
|
||||
import type {SimApiBaseResponse, SimApiOptions, SimApiVersions} from './types'
|
||||
|
||||
// ============ Pinia Store ============
|
||||
// 仅作为 core 的代理映射,不维护任何独立状态
|
||||
|
||||
export const useSimApi = defineStore('simapi', {
|
||||
state: () => ({
|
||||
// 在 state 中实例化 core
|
||||
_core: new SimApiCore(),
|
||||
}),
|
||||
|
||||
getters: {
|
||||
// 直接映射 core 的属性和方法
|
||||
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: {
|
||||
// 所有方法直接代理到 core
|
||||
autoInit(): void {
|
||||
this._core.autoInit()
|
||||
state: () => ({
|
||||
// 在 state 中实例化 core
|
||||
_core: new SimApiCore(),
|
||||
}),
|
||||
getters: {
|
||||
IsDebug: state => state._core.isDebug
|
||||
},
|
||||
actions: {
|
||||
// 所有方法直接代理到 core
|
||||
autoInit(): void {
|
||||
this._core.autoInit()
|
||||
},
|
||||
|
||||
configure(options: SimApiOptions): void {
|
||||
this._core.configure(options)
|
||||
},
|
||||
configure(options: SimApiOptions): void {
|
||||
this._core.configure(options)
|
||||
},
|
||||
|
||||
setDebug(debug: boolean): void {
|
||||
this._core.debug = debug
|
||||
},
|
||||
setDebug(debug: boolean): void {
|
||||
this._core.setDebug(debug);
|
||||
},
|
||||
|
||||
setEndpoints(endpoints: { [name: string]: string }): void {
|
||||
this._core.setEndpoints(endpoints)
|
||||
},
|
||||
setEndpoints(endpoints: { [name: string]: string }): void {
|
||||
this._core.setEndpoints(endpoints)
|
||||
},
|
||||
|
||||
setBusinessCallback(
|
||||
code: number | string,
|
||||
callback: (data: SimApiBaseResponse) => void
|
||||
): void {
|
||||
this._core.setBusinessCallback(code, callback)
|
||||
},
|
||||
setBusinessCallback(
|
||||
code: number | string,
|
||||
callback: (data: SimApiBaseResponse) => void
|
||||
): void {
|
||||
this._core.setBusinessCallback(code, callback)
|
||||
},
|
||||
|
||||
getToken(): string {
|
||||
return this._core.getToken()
|
||||
},
|
||||
getToken(): string {
|
||||
return this._core.getToken()
|
||||
},
|
||||
|
||||
setToken(token: string): void {
|
||||
this._core.setToken(token)
|
||||
},
|
||||
setToken(token: string): void {
|
||||
this._core.setToken(token)
|
||||
},
|
||||
|
||||
removeToken(): void {
|
||||
this._core.removeToken()
|
||||
},
|
||||
removeToken(): void {
|
||||
this._core.removeToken()
|
||||
},
|
||||
|
||||
async login(request: Record<string, any>): Promise<SimApiBaseResponse<string>> {
|
||||
return this._core.login(request)
|
||||
},
|
||||
async login(request: Record<string, any>): Promise<SimApiBaseResponse<string>> {
|
||||
return this._core.login(request)
|
||||
},
|
||||
|
||||
async logout(url?: string | null): Promise<any> {
|
||||
return this._core.logout(url)
|
||||
},
|
||||
async logout(url?: string | null): Promise<any> {
|
||||
return this._core.logout(url)
|
||||
},
|
||||
|
||||
async checkLogin(url?: string | null): Promise<void> {
|
||||
return this._core.checkLogin(url)
|
||||
},
|
||||
async checkLogin(url?: string | null): Promise<void> {
|
||||
return this._core.checkLogin(url)
|
||||
},
|
||||
|
||||
async query<T = any>(
|
||||
uri: string,
|
||||
params?: any,
|
||||
endpointKey?: string,
|
||||
extraHeaders?: Record<string, string>
|
||||
): Promise<SimApiBaseResponse<T>> {
|
||||
return this._core.query<T>(uri, params, endpointKey, extraHeaders)
|
||||
},
|
||||
async query<T = any>(
|
||||
uri: string,
|
||||
params?: any,
|
||||
endpointKey?: string,
|
||||
extraHeaders?: Record<string, string>,
|
||||
selfHandleError: boolean = false
|
||||
): Promise<SimApiBaseResponse<T>> {
|
||||
return this._core.query<T>(uri, params, endpointKey, extraHeaders, selfHandleError)
|
||||
},
|
||||
|
||||
getEndpoint(name?: string): string {
|
||||
return this._core.getEndpoint(name)
|
||||
},
|
||||
getEndpoint(name?: string): string {
|
||||
return this._core.getEndpoint(name)
|
||||
},
|
||||
|
||||
async getVersion(endpointName?: string): Promise<SimApiVersions> {
|
||||
return this._core.getVersion(endpointName)
|
||||
async getVersion(endpointName?: string): Promise<SimApiVersions> {
|
||||
return this._core.getVersion(endpointName)
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user