Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e0754ad635 |
+8
-5
@@ -237,7 +237,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()
|
||||
@@ -271,10 +272,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 时抛出业务错误
|
||||
|
||||
+68
-67
@@ -1,91 +1,92 @@
|
||||
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(),
|
||||
}),
|
||||
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()
|
||||
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,
|
||||
},
|
||||
|
||||
configure(options: SimApiOptions): void {
|
||||
this._core.configure(options)
|
||||
},
|
||||
actions: {
|
||||
// 所有方法直接代理到 core
|
||||
autoInit(): void {
|
||||
this._core.autoInit()
|
||||
},
|
||||
|
||||
setDebug(debug: boolean): void {
|
||||
this._core.debug = debug
|
||||
},
|
||||
configure(options: SimApiOptions): void {
|
||||
this._core.configure(options)
|
||||
},
|
||||
|
||||
setEndpoints(endpoints: { [name: string]: string }): void {
|
||||
this._core.setEndpoints(endpoints)
|
||||
},
|
||||
setDebug(debug: boolean): void {
|
||||
this._core.debug = debug
|
||||
},
|
||||
|
||||
setBusinessCallback(
|
||||
code: number | string,
|
||||
callback: (data: SimApiBaseResponse) => void
|
||||
): void {
|
||||
this._core.setBusinessCallback(code, callback)
|
||||
},
|
||||
setEndpoints(endpoints: { [name: string]: string }): void {
|
||||
this._core.setEndpoints(endpoints)
|
||||
},
|
||||
|
||||
getToken(): string {
|
||||
return this._core.getToken()
|
||||
},
|
||||
setBusinessCallback(
|
||||
code: number | string,
|
||||
callback: (data: SimApiBaseResponse) => void
|
||||
): void {
|
||||
this._core.setBusinessCallback(code, callback)
|
||||
},
|
||||
|
||||
setToken(token: string): void {
|
||||
this._core.setToken(token)
|
||||
},
|
||||
getToken(): string {
|
||||
return this._core.getToken()
|
||||
},
|
||||
|
||||
removeToken(): void {
|
||||
this._core.removeToken()
|
||||
},
|
||||
setToken(token: string): void {
|
||||
this._core.setToken(token)
|
||||
},
|
||||
|
||||
async login(request: Record<string, any>): Promise<SimApiBaseResponse<string>> {
|
||||
return this._core.login(request)
|
||||
},
|
||||
removeToken(): void {
|
||||
this._core.removeToken()
|
||||
},
|
||||
|
||||
async logout(url?: string | null): Promise<any> {
|
||||
return this._core.logout(url)
|
||||
},
|
||||
async login(request: Record<string, any>): Promise<SimApiBaseResponse<string>> {
|
||||
return this._core.login(request)
|
||||
},
|
||||
|
||||
async checkLogin(url?: string | null): Promise<void> {
|
||||
return this._core.checkLogin(url)
|
||||
},
|
||||
async logout(url?: string | null): Promise<any> {
|
||||
return this._core.logout(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 checkLogin(url?: string | null): Promise<void> {
|
||||
return this._core.checkLogin(url)
|
||||
},
|
||||
|
||||
getEndpoint(name?: string): string {
|
||||
return this._core.getEndpoint(name)
|
||||
},
|
||||
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)
|
||||
},
|
||||
|
||||
async getVersion(endpointName?: string): Promise<SimApiVersions> {
|
||||
return this._core.getVersion(endpointName)
|
||||
getEndpoint(name?: string): string {
|
||||
return this._core.getEndpoint(name)
|
||||
},
|
||||
|
||||
async getVersion(endpointName?: string): Promise<SimApiVersions> {
|
||||
return this._core.getVersion(endpointName)
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user