wangrong
2026-08-30 761e5c5fa871959f60e15c971c8d83f2b53d0af5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { defineStore } from 'pinia'
import { store } from '../index'
 
interface RequestState {
  loadingMap: {
    [key: string]: number
  }
  cachedRequestList: []
}
 
export const useRequestStore = defineStore('request', {
  state: (): RequestState => {
    return {
      loadingMap: {},
      cachedRequestList: []
    }
  },
  getters: {
    getRequestList(): string {
      return this.cachedRequestList
    }
  },
  actions: {
    setLoadingMap(value: object) {
      this.loadingMap = value
    },
    resetLoadingMap() {
      for (const key in this.loadingMap) {
        this.loadingMap[key] = 0
      }
    },
    addLoading(key: string) {
      if (Object.prototype.hasOwnProperty.call(this.loadingMap, key)) {
        const map = this.loadingMap
        map[key] += 1
        this.loadingMap = map
      } else {
        const nMap = {}
        nMap[key] = 1
        this.loadingMap = nMap
      }
    },
    reduceLoading(key: string) {
      if (this.loadingMap) {
        const map = this.loadingMap
        map[key] -= 1
        this.loadingMap = map
      }
    },
    addCacheRequest(fun) {
      this.cachedRequestList.push(fun)
    },
    cleanCacheRequest() {
      this.cachedRequestList = []
    }
  }
})
 
export const useRequestStoreWithOut = () => {
  return useRequestStore(store)
}