useDictionaries.js 969 B

1234567891011121314151617181920212223242526272829303132
  1. import { getDictData, getIndustryListData } from '@/api/common/index'
  2. // 定义对应的api
  3. // const DICT_CITY_API = {
  4. // menduner_exp_type: getDictData
  5. // }
  6. const setDict = (type, val, cacheTime = 7200) => {
  7. localStorage.setItem(type, JSON.stringify({
  8. data: val,
  9. expire: Date.now() + cacheTime * 1000
  10. }))
  11. }
  12. export const getDict = (type, params, apiType = 'dict') => {
  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({ data: catchData.data })
  18. }
  19. // 传参按照规范参数传
  20. const query = params ? params : { type }
  21. const apiFn = {
  22. dict: getDictData,
  23. industryList: getIndustryListData
  24. }
  25. apiFn[apiType](query).then(data => {
  26. setDict(type, data, Date.now())
  27. resolve({ data })
  28. })
  29. })
  30. }