useDictionaries.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. import { getSecondNodes } from '@/utils/dealData'
  13. // const setDict = (type, val, cacheTime = 7200) => {
  14. const setDict = (type, val) => {
  15. if (!val) {
  16. return
  17. }
  18. if (type === 'areaTreeData') {
  19. const obj = val.data.find(e => e.name === '中国')
  20. val.data = obj?.children ? obj.children.map(e =>{
  21. // 市辖区直接显示区
  22. const municipality = e.children && e.children.length && e.children[0].name === '市辖区'
  23. if (municipality && e.children[0].children?.length) e.children = e.children[0].children
  24. return e
  25. }) : []
  26. }
  27. if (type === 'areaTreeDataExtend') { // 前排加上不限
  28. const obj = val.data.find(e => e.name === '中国')
  29. if (obj?.children?.length) {
  30. // const province =
  31. obj.children.forEach(e=> {
  32. // 市辖区直接显示区
  33. const municipality = e.children && e.children.length && e.children[0].name === '市辖区'
  34. if (municipality && e.children[0].children?.length) e.children = e.children[0].children
  35. // 不限
  36. extendFun(e)
  37. })
  38. val.data = obj.children
  39. } else val.data = []
  40. function extendFun (e) {
  41. const idType = Number.isInteger(e.id) ? 'int' : 'str'
  42. if(e.children?.length) {
  43. e.children.unshift({ name: '不限', id: e.id+'unlimited', idType })
  44. e.children.forEach(i => extendFun(i))
  45. }
  46. }
  47. }
  48. // 一小时过期
  49. const currentTime = new Date()
  50. currentTime.setTime(currentTime.getTime() + 3600 * 1000)
  51. uni.setStorageSync(type, JSON.stringify({
  52. data: val,
  53. // expire: Date.now() + cacheTime * 1000
  54. expire: currentTime.getTime()
  55. }))
  56. }
  57. export const getDict = (type, params, apiType = 'dict') => {
  58. if (!type) {
  59. return []
  60. }
  61. return new Promise((resolve) => {
  62. const item = uni.getStorageSync(type)
  63. const catchData = item ? JSON.parse(item) : null
  64. if (catchData && catchData.expire && (Date.now() <= catchData.expire) && catchData.data && catchData.data?.data?.length) {
  65. return resolve({ data: catchData.data })
  66. }
  67. // 传参按照规范参数传
  68. const query = params ? params : { type }
  69. const apiFn = {
  70. dict: getDictData,
  71. positionTreeData: getPositionTreeData, // 职位tree
  72. areaTreeData: getAreaTreeData, // 区域tree
  73. areaTreeDataExtend: getAreaTreeData, // 区域tree(二级以后含不限)
  74. industryTreeData: getIndustryTreeData, // 行业tree
  75. industryList: getIndustryListData,
  76. skillList: getSkillList,
  77. areaList: getAreaListData,
  78. areaMap: getAreaMapData,
  79. positionSecondData: getPositionTreeData,
  80. positionData: getPositionData
  81. }
  82. apiFn[apiType](query).then(data => {
  83. // setDict(type, data, Date.now())
  84. if (type === 'positionSecondData') {
  85. data.data = getSecondNodes(data.data)
  86. }
  87. setDict(type, data)
  88. resolve({ data })
  89. })
  90. })
  91. }