zhengnaiwen_citu 1 年之前
父節點
當前提交
d2c69e09fb
共有 2 個文件被更改,包括 43 次插入1 次删除
  1. 0 1
      src/store/app.js
  2. 43 0
      src/store/dictionaries.js

+ 0 - 1
src/store/app.js

@@ -13,7 +13,6 @@ export const useAppStore = defineStore('app',
       title,
       setTitle
     }
-  
   },
   {
     persist: true, // ref() 持久化响应

+ 43 - 0
src/store/dictionaries.js

@@ -0,0 +1,43 @@
+/**
+ * 获取字典信息 配置api过期自动读取
+ */
+import { defineStore } from 'pinia'
+import { ref } from 'vue' 
+
+const DICT_CITY_API = {
+  DICT_CITY: Promise.resolve()
+}
+
+export const useDictionariesStore = defineStore('dictionaries', 
+  () => {
+
+    const DICT_CITY = ref({})
+
+    const setDict = (type, val, cacheTime) => {
+      [type].value = {
+        data: val,
+        expire: Date.now() + cacheTime
+      }
+    }
+
+    const getDict = (type) => {
+      return new Promise((resolve) => {
+        if ([type].value.expire && (Date.now() <= [type].value.expire)) {
+          return resolve([type].value.data)
+        }
+        DICT_CITY_API.DICT_CITY({ type }).then(({data}) => {
+          setDict(type, data, Date.now())
+          resolve(data)
+        })
+      })
+    }
+
+    return {
+      getDict,
+      setDict
+    }
+  },
+  {
+    persist: true, // ref() 持久化响应
+  }
+)