Files
simapi-vue/src/simapi.pinia.ts
T

84 lines
2.4 KiB
TypeScript
Raw Normal View History

2026-05-04 02:27:43 +08:00
import {defineStore} from 'pinia'
import {SimApiCore} from './simapi.core'
import type {SimApiBaseResponse, SimApiOptions, SimApiVersions} from './types'
2026-04-08 01:07:10 +08:00
// ============ Pinia Store ============
// 仅作为 core 的代理映射,不维护任何独立状态
export const useSimApi = defineStore('simapi', {
2026-05-04 02:27:43 +08:00
state: () => ({
// 在 state 中实例化 core
_core: new SimApiCore(),
}),
getters: {
IsDebug: state => state._core.isDebug
2026-04-08 01:07:10 +08:00
},
2026-05-04 02:27:43 +08:00
actions: {
// 所有方法直接代理到 core
2026-08-10 20:37:23 +08:00
async loadFromFile(file: string = '/config.json'): Promise<void> {
2026-05-04 19:56:48 +08:00
return this._core.loadFromFile(file)
2026-05-04 02:27:43 +08:00
},
2026-04-08 01:07:10 +08:00
2026-05-04 02:27:43 +08:00
configure(options: SimApiOptions): void {
this._core.configure(options)
},
2026-04-08 01:07:10 +08:00
2026-05-04 02:27:43 +08:00
setDebug(debug: boolean): void {
this._core.setDebug(debug);
2026-05-04 02:27:43 +08:00
},
2026-04-08 01:07:10 +08:00
2026-05-04 02:27:43 +08:00
setEndpoints(endpoints: { [name: string]: string }): void {
this._core.setEndpoints(endpoints)
},
2026-04-08 01:07:10 +08:00
2026-05-04 02:27:43 +08:00
setBusinessCallback(
code: number | string,
callback: (data: SimApiBaseResponse) => void
): void {
this._core.setBusinessCallback(code, callback)
},
2026-04-08 01:07:10 +08:00
2026-05-04 02:27:43 +08:00
getToken(): string {
return this._core.getToken()
},
2026-04-08 01:07:10 +08:00
2026-05-04 02:27:43 +08:00
setToken(token: string): void {
this._core.setToken(token)
},
2026-04-08 01:07:10 +08:00
2026-05-04 02:27:43 +08:00
removeToken(): void {
this._core.removeToken()
},
2026-04-08 01:07:10 +08:00
2026-05-04 02:27:43 +08:00
async login(request: Record<string, any>): Promise<SimApiBaseResponse<string>> {
return this._core.login(request)
},
2026-04-08 01:07:10 +08:00
2026-05-04 02:27:43 +08:00
async logout(url?: string | null): Promise<any> {
return this._core.logout(url)
},
2026-04-08 01:07:10 +08:00
2026-05-04 02:27:43 +08:00
async checkLogin(url?: string | null): Promise<void> {
return this._core.checkLogin(url)
},
2026-04-08 01:07:10 +08:00
2026-05-04 02:27:43 +08:00
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)
},
2026-04-08 01:07:10 +08:00
2026-05-04 02:27:43 +08:00
getEndpoint(name?: string): string {
return this._core.getEndpoint(name)
},
2026-08-10 20:37:23 +08:00
async getConfig(reload = false, endpointName?: string): Promise<Map<string, object>> {
return this._core.getConfig(reload, endpointName)
2026-05-04 02:27:43 +08:00
},
2026-04-08 01:07:10 +08:00
},
})