Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
713b8ae31e | ||
|
|
e0754ad635 | ||
|
|
39626de073 |
+20
-13
@@ -77,15 +77,15 @@ 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: {
|
||||||
@@ -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}
|
||||||
}
|
}
|
||||||
@@ -170,10 +178,6 @@ export class SimApiCore {
|
|||||||
localStorage.removeItem(this.auth.token_name)
|
localStorage.removeItem(this.auth.token_name)
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
||||||
.toString(16)
|
.toString(16)
|
||||||
@@ -237,7 +241,8 @@ export class SimApiCore {
|
|||||||
uri: string,
|
uri: string,
|
||||||
params: any = {},
|
params: any = {},
|
||||||
endpointKey?: string,
|
endpointKey?: string,
|
||||||
extraHeaders?: Record<string, string>
|
extraHeaders?: Record<string, string>,
|
||||||
|
selfHandleError: boolean = false
|
||||||
): Promise<SimApiBaseResponse<T>> {
|
): Promise<SimApiBaseResponse<T>> {
|
||||||
const headers: Record<string, string> = {...extraHeaders, ...{}}
|
const headers: Record<string, string> = {...extraHeaders, ...{}}
|
||||||
const queryId = this.genS4()
|
const queryId = this.genS4()
|
||||||
@@ -271,10 +276,12 @@ export class SimApiCore {
|
|||||||
const processedData = this.api.responseCallback.success(respData) as SimApiBaseResponse<T>
|
const processedData = this.api.responseCallback.success(respData) as SimApiBaseResponse<T>
|
||||||
|
|
||||||
// 业务回调处理
|
// 业务回调处理
|
||||||
if (this.api.businessCallback.hasOwnProperty(processedData.code)) {
|
if (!selfHandleError) {
|
||||||
this.api.businessCallback[processedData.code](processedData)
|
if (this.api.businessCallback.hasOwnProperty(processedData.code)) {
|
||||||
} else if (this.api.businessCallback['common'] && processedData.code !== 200) {
|
this.api.businessCallback[processedData.code](processedData)
|
||||||
this.api.businessCallback['common'](processedData)
|
} else if (this.api.businessCallback['common'] && processedData.code !== 200) {
|
||||||
|
this.api.businessCallback['common'](processedData)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// code != 200 时抛出业务错误
|
// code != 200 时抛出业务错误
|
||||||
@@ -311,7 +318,7 @@ export class SimApiCore {
|
|||||||
|
|
||||||
async logout(url?: string | null): Promise<any> {
|
async logout(url?: string | null): Promise<any> {
|
||||||
if (url !== null) {
|
if (url !== null) {
|
||||||
return this.query(url ?? this.auth.logout_url).catch(() => true)
|
this.query(url ?? this.auth.logout_url).catch(() => true)
|
||||||
}
|
}
|
||||||
this.removeToken()
|
this.removeToken()
|
||||||
return true
|
return true
|
||||||
|
|||||||
+62
-68
@@ -1,91 +1,85 @@
|
|||||||
import { defineStore } from 'pinia'
|
import {defineStore} from 'pinia'
|
||||||
import { SimApiCore } from './simapi.core'
|
import {SimApiCore} from './simapi.core'
|
||||||
import type { SimApiBaseResponse, SimApiOptions, SimApiVersions } from './types'
|
import type {SimApiBaseResponse, SimApiOptions, SimApiVersions} from './types'
|
||||||
|
|
||||||
// ============ Pinia Store ============
|
// ============ Pinia Store ============
|
||||||
// 仅作为 core 的代理映射,不维护任何独立状态
|
// 仅作为 core 的代理映射,不维护任何独立状态
|
||||||
|
|
||||||
export const useSimApi = defineStore('simapi', {
|
export const useSimApi = defineStore('simapi', {
|
||||||
state: () => ({
|
state: () => ({
|
||||||
// 在 state 中实例化 core
|
// 在 state 中实例化 core
|
||||||
_core: new SimApiCore(),
|
_core: new SimApiCore(),
|
||||||
}),
|
}),
|
||||||
|
getters: {
|
||||||
getters: {
|
IsDebug: state => state._core.isDebug
|
||||||
// 直接映射 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()
|
|
||||||
},
|
},
|
||||||
|
actions: {
|
||||||
|
// 所有方法直接代理到 core
|
||||||
|
autoInit(): void {
|
||||||
|
this._core.autoInit()
|
||||||
|
},
|
||||||
|
|
||||||
configure(options: SimApiOptions): void {
|
configure(options: SimApiOptions): void {
|
||||||
this._core.configure(options)
|
this._core.configure(options)
|
||||||
},
|
},
|
||||||
|
|
||||||
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 {
|
||||||
this._core.setEndpoints(endpoints)
|
this._core.setEndpoints(endpoints)
|
||||||
},
|
},
|
||||||
|
|
||||||
setBusinessCallback(
|
setBusinessCallback(
|
||||||
code: number | string,
|
code: number | string,
|
||||||
callback: (data: SimApiBaseResponse) => void
|
callback: (data: SimApiBaseResponse) => void
|
||||||
): void {
|
): void {
|
||||||
this._core.setBusinessCallback(code, callback)
|
this._core.setBusinessCallback(code, callback)
|
||||||
},
|
},
|
||||||
|
|
||||||
getToken(): string {
|
getToken(): string {
|
||||||
return this._core.getToken()
|
return this._core.getToken()
|
||||||
},
|
},
|
||||||
|
|
||||||
setToken(token: string): void {
|
setToken(token: string): void {
|
||||||
this._core.setToken(token)
|
this._core.setToken(token)
|
||||||
},
|
},
|
||||||
|
|
||||||
removeToken(): void {
|
removeToken(): void {
|
||||||
this._core.removeToken()
|
this._core.removeToken()
|
||||||
},
|
},
|
||||||
|
|
||||||
async login(request: Record<string, any>): Promise<SimApiBaseResponse<string>> {
|
async login(request: Record<string, any>): Promise<SimApiBaseResponse<string>> {
|
||||||
return this._core.login(request)
|
return this._core.login(request)
|
||||||
},
|
},
|
||||||
|
|
||||||
async logout(url?: string | null): Promise<any> {
|
async logout(url?: string | null): Promise<any> {
|
||||||
return this._core.logout(url)
|
return this._core.logout(url)
|
||||||
},
|
},
|
||||||
|
|
||||||
async checkLogin(url?: string | null): Promise<void> {
|
async checkLogin(url?: string | null): Promise<void> {
|
||||||
return this._core.checkLogin(url)
|
return this._core.checkLogin(url)
|
||||||
},
|
},
|
||||||
|
|
||||||
async query<T = any>(
|
async query<T = any>(
|
||||||
uri: string,
|
uri: string,
|
||||||
params?: any,
|
params?: any,
|
||||||
endpointKey?: string,
|
endpointKey?: string,
|
||||||
extraHeaders?: Record<string, string>
|
extraHeaders?: Record<string, string>,
|
||||||
): Promise<SimApiBaseResponse<T>> {
|
selfHandleError: boolean = false
|
||||||
return this._core.query<T>(uri, params, endpointKey, extraHeaders)
|
): Promise<SimApiBaseResponse<T>> {
|
||||||
},
|
return this._core.query<T>(uri, params, endpointKey, extraHeaders, selfHandleError)
|
||||||
|
},
|
||||||
|
|
||||||
getEndpoint(name?: string): string {
|
getEndpoint(name?: string): string {
|
||||||
return this._core.getEndpoint(name)
|
return this._core.getEndpoint(name)
|
||||||
},
|
},
|
||||||
|
|
||||||
async getVersion(endpointName?: string): Promise<SimApiVersions> {
|
async getVersion(endpointName?: string): Promise<SimApiVersions> {
|
||||||
return this._core.getVersion(endpointName)
|
return this._core.getVersion(endpointName)
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user