diff --git a/src/simapi.core.ts b/src/simapi.core.ts index 5941226..59e7a6c 100644 --- a/src/simapi.core.ts +++ b/src/simapi.core.ts @@ -237,7 +237,8 @@ export class SimApiCore { uri: string, params: any = {}, endpointKey?: string, - extraHeaders?: Record + extraHeaders?: Record, + selfHandleError: boolean = false ): Promise> { const headers: Record = {...extraHeaders, ...{}} const queryId = this.genS4() @@ -271,10 +272,12 @@ export class SimApiCore { const processedData = this.api.responseCallback.success(respData) as SimApiBaseResponse // 业务回调处理 - 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 时抛出业务错误 diff --git a/src/simapi.pinia.ts b/src/simapi.pinia.ts index 11db0b9..b4f7fb9 100644 --- a/src/simapi.pinia.ts +++ b/src/simapi.pinia.ts @@ -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): Promise> { - return this._core.login(request) - }, + removeToken(): void { + this._core.removeToken() + }, - async logout(url?: string | null): Promise { - return this._core.logout(url) - }, + async login(request: Record): Promise> { + return this._core.login(request) + }, - async checkLogin(url?: string | null): Promise { - return this._core.checkLogin(url) - }, + async logout(url?: string | null): Promise { + return this._core.logout(url) + }, - async query( - uri: string, - params?: any, - endpointKey?: string, - extraHeaders?: Record - ): Promise> { - return this._core.query(uri, params, endpointKey, extraHeaders) - }, + async checkLogin(url?: string | null): Promise { + return this._core.checkLogin(url) + }, - getEndpoint(name?: string): string { - return this._core.getEndpoint(name) - }, + async query( + uri: string, + params?: any, + endpointKey?: string, + extraHeaders?: Record, + selfHandleError: boolean = false + ): Promise> { + return this._core.query(uri, params, endpointKey, extraHeaders, selfHandleError) + }, - async getVersion(endpointName?: string): Promise { - return this._core.getVersion(endpointName) + getEndpoint(name?: string): string { + return this._core.getEndpoint(name) + }, + + async getVersion(endpointName?: string): Promise { + return this._core.getVersion(endpointName) + }, }, - }, })