useDictionaries.js 1.3 KB

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