useDictionaries.js 792 B

12345678910111213141516171819202122232425262728293031
  1. import { getDictData } from '@/api/common/index'
  2. // 定义对应的api
  3. const DICT_CITY_API = {
  4. menduner_exp_type: getDictData
  5. }
  6. const setDict = (type, val, cacheTime) => {
  7. localStorage.setItem(type, JSON.stringify({
  8. data: val,
  9. expire: Date.now() + cacheTime
  10. }))
  11. }
  12. export const getDict = (type) => {
  13. return new Promise((resolve) => {
  14. const item = localStorage.getItem(type)
  15. const catchData = item ? JSON.parse(item) : null
  16. if (catchData && catchData.expire && (Date.now() <= catchData.expire)) {
  17. return resolve(catchData.data)
  18. }
  19. // 传参按照规范参数传
  20. const query = {
  21. type
  22. }
  23. DICT_CITY_API[type](query).then(data => {
  24. setDict(type, data, Date.now())
  25. resolve({ data })
  26. })
  27. })
  28. }