import {
  getDictData,
  getIndustryListData,
  getIndustryTreeData,
  getAreaListData,
  getPositionTreeData,
  getAreaTreeData
} from '@/api/common/index'
// 定义对应的api
// const DICT_CITY_API = {
//   menduner_exp_type: getDictData
// }

const setDict = (type, val, cacheTime = 7200) => {
  localStorage.setItem(type, JSON.stringify({
    data: val,
    expire: Date.now() + cacheTime * 1000
  }))
}

export const getDict = (type, params, apiType = 'dict') => {
    return new Promise((resolve) => {
      const item = localStorage.getItem(type)
      const catchData = item ? JSON.parse(item) : null
      if (catchData && catchData.expire && (Date.now() <= catchData.expire)) {
        return resolve({ data: catchData.data })
      }
      // 传参按照规范参数传
      const query = params ? params : { type }
      const apiFn = {
        dict: getDictData,
        positionTreeData: getPositionTreeData, // 职位tree
        areaTreeData: getAreaTreeData, // 区域tree
        industryTreeData: getIndustryTreeData, // 行业tree
        industryList: getIndustryListData,
        areaList: getAreaListData
      }
      apiFn[apiType](query).then(data => {
        setDict(type, data, Date.now())
        resolve({ data })
      })
    })
}