zhengnaiwen_citu 1 年之前
父节点
当前提交
29a33ff087
共有 2 个文件被更改,包括 33 次插入13 次删除
  1. 0 13
      src/hooks/web/useCache.js
  2. 33 0
      src/hooks/web/useDictionaries.js

+ 0 - 13
src/hooks/web/useCache.js

@@ -1,15 +1,2 @@
-/**
- * 配置浏览器本地存储的方式,可直接存储对象数组。
- */
 
-// import WebStorageCache from 'web-storage-cache'
 
-// export const useCache = (type = 'localStorage') => {
-//   const wsCache = new WebStorageCache({
-//     storage: type
-//   })
-
-//   return {
-//     wsCache
-//   }
-// }

+ 33 - 0
src/hooks/web/useDictionaries.js

@@ -0,0 +1,33 @@
+
+
+// 定义对应的api
+const DICT_CITY_API = {
+  DICT_CITY: Promise.resolve()
+}
+
+const setDict = (type, val, cacheTime) => {
+  localStorage.setItem(type, JSON.stringify({
+    data: val,
+    expire: Date.now() + cacheTime
+  }))
+}
+
+export const getDict = (type) => {
+    return new Promise((resolve) => {
+      const catchData = localStorage.getItem(type)
+      if (!catchData) {
+        return
+      }
+      if (catchData.expire && (Date.now() <= catchData.expire)) {
+        return resolve(catchData.data)
+      }
+      // 传参按照规范参数传
+      const query = {
+        type
+      }
+      DICT_CITY_API[type](query).then(({data}) => {
+        setDict(type, data, Date.now())
+        resolve(data)
+      })
+    })
+}