dictionaryCache.ts 892 B

123456789101112131415161718192021222324252627282930313233
  1. import { defineStore } from "pinia"
  2. import { store } from "../index"
  3. import { getDictionary } from "@/api/user.api"
  4. import { httpAjax } from "@/plugin/httpAjax"
  5. const useStore = defineStore("dictionaryCache", {
  6. state: (): { cache: Map<string, any[]> } => {
  7. return {
  8. //字典缓存区
  9. cache: new Map()
  10. }
  11. },
  12. actions: {
  13. async getDictionary(key: string) {
  14. //有缓存取缓存 没缓存ajax 取值
  15. const cache = this.cache
  16. if (cache.has(key)) {
  17. return cache.get(key)
  18. } else {
  19. const dictionaryRes = await httpAjax(getDictionary, key)
  20. if (dictionaryRes.code === 200) {
  21. this.cache.set(key, dictionaryRes.data)
  22. return dictionaryRes.data
  23. }
  24. return []
  25. }
  26. }
  27. }
  28. })
  29. export default () => {
  30. return useStore(store)
  31. }