useDictionaries.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import {
  2. getDictData,
  3. getIndustryListData,
  4. getIndustryTreeData,
  5. getAreaListData,
  6. getPositionTreeData,
  7. getAreaTreeData,
  8. getPositionData
  9. } from '@/api/common/index'
  10. // 定义对应的api
  11. // const DICT_CITY_API = {
  12. // menduner_exp_type: getDictData
  13. // }
  14. const setDict = (type, val, cacheTime = 7200) => {
  15. localStorage.setItem(type, JSON.stringify({
  16. data: val,
  17. expire: Date.now() + cacheTime * 1000
  18. }))
  19. }
  20. export const getDict = (type, params, apiType = 'dict') => {
  21. return new Promise((resolve) => {
  22. const item = localStorage.getItem(type)
  23. const catchData = item ? JSON.parse(item) : null
  24. if (catchData && catchData.expire && (Date.now() <= catchData.expire)) {
  25. return resolve({ data: catchData.data })
  26. }
  27. // 传参按照规范参数传
  28. const query = params ? params : { type }
  29. const apiFn = {
  30. dict: getDictData,
  31. positionTreeData: getPositionTreeData, // 职位tree
  32. areaTreeData: getAreaTreeData, // 区域tree
  33. industryTreeData: getIndustryTreeData, // 行业tree
  34. industryList: getIndustryListData,
  35. areaList: getAreaListData,
  36. positionData: getPositionData
  37. }
  38. apiFn[apiType](query).then(data => {
  39. setDict(type, data, Date.now())
  40. resolve({ data })
  41. })
  42. })
  43. }