storage.ts 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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(StorageEnum.GO_CHART_STORAGE_LIST)
  13. // 是否本地预览
  14. if (!storageList || storageList.findIndex(e => e.id === id.toString()) === -1) {
  15. // 接口调用
  16. const res = await fetchProjectApi({ projectId: id })
  17. if (res && res.code === ResultEnum.SUCCESS) {
  18. const { content, state } = res.data
  19. if (state === -1) {
  20. // 跳转未发布页
  21. return { isRelease: false }
  22. }
  23. return { ...JSON.parse(content), id }
  24. } else {
  25. httpErrorHandle()
  26. }
  27. } else {
  28. // 本地读取
  29. for (let i = 0; i < storageList.length; i++) {
  30. if (id.toString() === storageList[i]['id']) {
  31. return storageList[i]
  32. }
  33. }
  34. }
  35. }