useDictionaries.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. const setDict = (type, val) => {
  14. if (type === 'areaTreeData') {
  15. const obj = val.data.find(e => e.name === '中国')
  16. val.data = obj?.children ? obj.children.map(e =>{
  17. // 市辖区直接显示区
  18. const municipality = e.children && e.children.length && e.children[0].name === '市辖区'
  19. if (municipality && e.children[0].children?.length) e.children = e.children[0].children
  20. return e
  21. }) : []
  22. }
  23. // 一小时过期
  24. const currentTime = new Date()
  25. currentTime.setTime(currentTime.getTime() + 3600 * 1000)
  26. uni.setStorageSync(type, JSON.stringify({
  27. data: val,
  28. // expire: Date.now() + cacheTime * 1000
  29. expire: currentTime.getTime()
  30. }))
  31. }
  32. export const getDict = (type, params, apiType = 'dict') => {
  33. if (!type) {
  34. return []
  35. }
  36. return new Promise((resolve) => {
  37. const item = uni.getStorageSync(type)
  38. const catchData = item ? JSON.parse(item) : null
  39. if (catchData && catchData.expire && (Date.now() <= catchData.expire) && catchData.data && catchData.data?.data?.length) {
  40. return resolve({ data: catchData.data })
  41. }
  42. // 传参按照规范参数传
  43. const query = params ? params : { type }
  44. const apiFn = {
  45. dict: getDictData,
  46. positionTreeData: getPositionTreeData, // 职位tree
  47. areaTreeData: getAreaTreeData, // 区域tree
  48. industryTreeData: getIndustryTreeData, // 行业tree
  49. industryList: getIndustryListData,
  50. skillList: getSkillList,
  51. areaList: getAreaListData,
  52. areaMap: getAreaMapData,
  53. positionData: getPositionData
  54. }
  55. apiFn[apiType](query).then(data => {
  56. // setDict(type, data, Date.now())
  57. setDict(type, data)
  58. resolve({ data })
  59. })
  60. })
  61. }