webapi.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import { defineStore } from 'pinia'
  2. import webapiImp from '@/utils/config/webapi'
  3. interface webapistate {
  4. _state: number
  5. /** 设置接口请求状态 */
  6. set(rs?: number): void
  7. /** 未请求 */
  8. unasked: boolean
  9. /** 请求中 */
  10. ing: boolean
  11. /** 请求成功 */
  12. success: boolean
  13. /** 首次请求成功 */
  14. firstSuccess
  15. /** 请求失败 */
  16. fail: boolean
  17. /** 请求结束 */
  18. finally: boolean
  19. /** 请求中|| 请求成功 */
  20. ios: boolean
  21. }
  22. type ISAPI<T> = {
  23. [P in keyof T]: {
  24. [C in keyof T[P]]: webapistate
  25. }
  26. }
  27. const webapi = {} as ISAPI<typeof webapiImp>
  28. for (const [gk, gv] of Object.entries(webapiImp)) {
  29. webapi[gk] = {}
  30. for (const [ik, iv] of Object.entries(gv)) {
  31. webapi[gk][ik] = {
  32. _state: undefined,
  33. firstSuccess: false,
  34. set(rs) {
  35. this._state = rs
  36. if (!this.firstSuccess && this.success) {
  37. this.firstSuccess = true
  38. }
  39. },
  40. get unasked() {
  41. return this._state === enums.ReqState.unasked
  42. },
  43. get ing() {
  44. return this._state === enums.ReqState.ing
  45. },
  46. get success() {
  47. return this._state === enums.ReqState.success
  48. },
  49. get fail() {
  50. return this._state === enums.ReqState.fail
  51. },
  52. get finally() {
  53. return this.success || this.fail
  54. },
  55. get ios() {
  56. return this.ing || this.success
  57. },
  58. } as webapistate
  59. }
  60. }
  61. // const getsun = () => {
  62. // return new Proxy(reactive({}), {
  63. // get(target, p) {
  64. // if (!target[p]) {
  65. // target[p] = reactive({
  66. // _state: undefined,
  67. // set(rs) {
  68. // this._state = rs
  69. // },
  70. // get ing() {
  71. // return this._state === enums.ReqState.ing
  72. // },
  73. // get success() {
  74. // return this._state === enums.ReqState.success
  75. // },
  76. // get fail() {
  77. // return this._state === enums.ReqState.fail
  78. // },
  79. // })
  80. // }
  81. // return target[p]
  82. // },
  83. // })
  84. // }
  85. // const webapiProxy = new Proxy<ISAPI<typeof webapiImp>>(webapi, {
  86. // get(target, p) {
  87. // if (!target[p]) {
  88. // target[p] = getsun()
  89. // }
  90. // return target[p]
  91. // },
  92. // })
  93. export default defineStore('webapi', () => {
  94. return reactive({
  95. ...webapi,
  96. })
  97. })