Compare commits

..
2 Commits
Author SHA1 Message Date
xrain 713b8ae31e 取消了一些乱七八糟不同步的方法
Publish to npm / publish (push) Failing after 10s
2026-05-04 03:32:05 +08:00
xrain e0754ad635 query 增加了自处理异常
Publish to npm / publish (push) Failing after 8s
2026-05-04 02:27:43 +08:00
2 changed files with 81 additions and 80 deletions
+15 -8
View File
@@ -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,11 +276,13 @@ export class SimApiCore {
const processedData = this.api.responseCallback.success(respData) as SimApiBaseResponse<T> const processedData = this.api.responseCallback.success(respData) as SimApiBaseResponse<T>
// 业务回调处理 // 业务回调处理
if (!selfHandleError) {
if (this.api.businessCallback.hasOwnProperty(processedData.code)) { if (this.api.businessCallback.hasOwnProperty(processedData.code)) {
this.api.businessCallback[processedData.code](processedData) this.api.businessCallback[processedData.code](processedData)
} else if (this.api.businessCallback['common'] && processedData.code !== 200) { } else if (this.api.businessCallback['common'] && processedData.code !== 200) {
this.api.businessCallback['common'](processedData) this.api.businessCallback['common'](processedData)
} }
}
// code != 200 时抛出业务错误 // code != 200 时抛出业务错误
if (processedData.code !== 200) { if (processedData.code !== 200) {
+8 -14
View File
@@ -1,6 +1,6 @@
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 的代理映射,不维护任何独立状态
@@ -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 {
@@ -73,9 +66,10 @@ export const useSimApi = defineStore('simapi', {
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>> {
return this._core.query<T>(uri, params, endpointKey, extraHeaders) return this._core.query<T>(uri, params, endpointKey, extraHeaders, selfHandleError)
}, },
getEndpoint(name?: string): string { getEndpoint(name?: string): string {