import { defineStore } from 'pinia' import webapiImp from '@/utils/config/webapi' interface webapistate { _state: number /** 设置接口请求状态 */ set(rs?: number): void /** 未请求 */ unasked: boolean /** 请求中 */ ing: boolean /** 请求成功 */ success: boolean /** 首次请求成功 */ firstSuccess /** 请求失败 */ fail: boolean /** 请求结束 */ finally: boolean /** 请求中|| 请求成功 */ ios: boolean } type ISAPI = { [P in keyof T]: { [C in keyof T[P]]: webapistate } } const webapi = {} as ISAPI for (const [gk, gv] of Object.entries(webapiImp)) { webapi[gk] = {} for (const [ik, iv] of Object.entries(gv)) { webapi[gk][ik] = { _state: undefined, firstSuccess: false, set(rs) { this._state = rs if (!this.firstSuccess && this.success) { this.firstSuccess = true } }, get unasked() { return this._state === enums.ReqState.unasked }, get ing() { return this._state === enums.ReqState.ing }, get success() { return this._state === enums.ReqState.success }, get fail() { return this._state === enums.ReqState.fail }, get finally() { return this.success || this.fail }, get ios() { return this.ing || this.success }, } as webapistate } } // const getsun = () => { // return new Proxy(reactive({}), { // get(target, p) { // if (!target[p]) { // target[p] = reactive({ // _state: undefined, // set(rs) { // this._state = rs // }, // get ing() { // return this._state === enums.ReqState.ing // }, // get success() { // return this._state === enums.ReqState.success // }, // get fail() { // return this._state === enums.ReqState.fail // }, // }) // } // return target[p] // }, // }) // } // const webapiProxy = new Proxy>(webapi, { // get(target, p) { // if (!target[p]) { // target[p] = getsun() // } // return target[p] // }, // }) export default defineStore('webapi', () => { return reactive({ ...webapi, }) })