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(),
|
|
|
|
|
}),
|
2026-04-08 01:07:10 +08:00
|
|
|
|
2026-05-04 02:27:43 +08:00
|
|
|
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,
|
2026-04-08 01:07:10 +08:00
|
|
|
},
|
|
|
|
|
|
2026-05-04 02:27:43 +08:00
|
|
|
actions: {
|
|
|
|
|
// 所有方法直接代理到 core
|
|
|
|
|
autoInit(): void {
|
|
|
|
|
this._core.autoInit()
|
|
|
|
|
},
|
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.debug = debug
|
|
|
|
|
},
|
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)
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
async getVersion(endpointName?: string): Promise<SimApiVersions> {
|
|
|
|
|
return this._core.getVersion(endpointName)
|
|
|
|
|
},
|
2026-04-08 01:07:10 +08:00
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|