|
@@ -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() 持久化响应
|
|
|
+ }
|
|
|
+)
|