Newer
Older
smartwell_front / src / utils / sessionData.ts
liyaguang on 25 Dec 2 KB 需求与问题修改
// 避免页面请求数据时间太长,数据比较多的接口可以在系统挂载时候请求,别的页面使用时直接去缓存去取
// 因为sessionStorage  localStorage  存储大小问题 所以使用indexDB存储数据
// 设备列表
import { getDeviceListPage } from '@/api/home/device/device'
import { getWellList } from '@/api/home/well/well'
import { getStationList } from '@/api/home/station/station'
import { getDeptTreeList } from '@/api/system/dept'
import indexDB from '@/utils/indexDB'
export const indexDBHandler = (name: string, data: string) => {
  if (indexDB) {
    indexDB.getAll().then((allData) => {
      // 先判断indexDB中是否含有该数据
      if (allData.filter((item: any) => item.name === name).length) {
        indexDB.update({ id: allData.filter((item: any) => item.name === name)[0].id, name: name, data: data }).then(() => {
          console.log(`indexDB数据更新成功--${name}`)
        })
      }
      else {
        indexDB.add({ name: name, data: data }).then(() => {
          console.log(`indexDB数据添加成功--${name}`)
        })
      }
    })
  }
}
// 设备列表
export const fetchCacheDevice = () => {
  getDeviceListPage({ offset: 1, limit: 9999 }).then((res) => {
    const deviceList = res.data.rows.map((item: any) => ({
      name: item.typeName,
      id: item.id,
      value: item.devcode,
      productId: item.productId,
      position: item.position,
      tagNumber: item.tagNumber,
      deviceType: item.deviceType,
    })).filter((item: any) => item.productId && item.value)
    indexDBHandler('all-device-list', JSON.stringify(deviceList))
  })
}
// 闸井列表
export const fetchCacheWell = () => {
  getWellList({}).then(res => {
    indexDBHandler('all-well-list', JSON.stringify(res.data))
  })
}
// 场站列表
export const fetchCacheStation = () => {
  getStationList({}).then(res => {
    indexDBHandler('all-station-list', JSON.stringify(res.data))
  })
}
// 管线列表

// 组织机构
export const fetchCacheDept = () => {
  getDeptTreeList().then(res => {
    indexDBHandler('all-dept-list', JSON.stringify(res.data))
  })
}