storage.ts 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import { getSessionStorage, fetchRouteParamsLocation, httpErrorHandle } from '@/utils'
  2. import { ResultEnum } from '@/enums/httpEnum'
  3. import { StorageEnum } from '@/enums/storageEnum'
  4. import { ChartEditStorage } from '@/store/modules/chartEditStore/chartEditStore.d'
  5. import { fetchProjectApi } from '@/api/path'
  6. export interface ChartEditStorageType extends ChartEditStorage {
  7. id: string
  8. }
  9. // 根据路由 id 获取存储数据的信息
  10. export const getSessionStorageInfo = async () => {
  11. const id = fetchRouteParamsLocation()
  12. const storageList: ChartEditStorageType[] = getSessionStorage(
  13. StorageEnum.GO_CHART_STORAGE_LIST
  14. )
  15. // 是否本地预览
  16. if (!storageList) {
  17. // 接口调用
  18. const res: any = await fetchProjectApi({ projectId: id })
  19. if (res.code === ResultEnum.SUCCESS) {
  20. const { content, state } = res.data
  21. if (state === -1) {
  22. // 跳转未发布页
  23. return { isRelease: false }
  24. }
  25. return { ...JSON.parse(content), id }
  26. } else {
  27. httpErrorHandle()
  28. }
  29. } else {
  30. // 本地读取
  31. for (let i = 0; i < storageList.length; i++) {
  32. if (id.toString() === storageList[i]['id']) {
  33. return storageList[i]
  34. }
  35. }
  36. }
  37. }