| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- 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<T> = {
- [P in keyof T]: {
- [C in keyof T[P]]: webapistate
- }
- }
- const webapi = {} as ISAPI<typeof webapiImp>
- 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<ISAPI<typeof webapiImp>>(webapi, {
- // get(target, p) {
- // if (!target[p]) {
- // target[p] = getsun()
- // }
- // return target[p]
- // },
- // })
- export default defineStore('webapi', () => {
- return reactive({
- ...webapi,
- })
- })
|