path.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import { ref } from 'vue'
  2. import { autoSetModeCallback, createTemploraryID } from './sys'
  3. import { getFuseModel, getFuseModelShowVariable } from './fuse-model'
  4. import {
  5. fetchPaths,
  6. postAddPath,
  7. postDeletePath,
  8. postUpdatePath,
  9. } from '@/api'
  10. import {
  11. deleteStoreItem,
  12. addStoreItem,
  13. updateStoreItem,
  14. fetchStoreItems,
  15. saveStoreItems,
  16. recoverStoreItems,
  17. asyncTimeout
  18. } from '@/utils'
  19. import type { Path } from '@/api'
  20. import { custom, params } from '@/env'
  21. import { Message } from 'bill/expose-common'
  22. import { useSelects } from '@/hook/ids'
  23. export type { Path } from '@/api'
  24. export type Paths = Path[]
  25. export const paths = ref<Paths>([])
  26. export const selectPaths = useSelects(paths, true)
  27. export const getPath = (id: Path['id']) => paths.value.find(path => path.id === id)
  28. export const getPathIsShow = (path: Path) => {
  29. if (!custom.showPaths && custom.showPath !== path.id) return false;
  30. return true;
  31. // const modelIds = path.points.map(item => item.modelId)
  32. // if (path.linePosition?.modelId) {
  33. // modelIds.push(path.linePosition.modelId)
  34. // }
  35. // return modelIds.every(modelId => {
  36. // if (modelId === '-100') return true;
  37. // const model = getFuseModel(modelId)
  38. // return model && getFuseModelShowVariable(model).value
  39. // })
  40. }
  41. export const createPath = (path: Partial<Path> = {}): Path => {
  42. return {
  43. id: createTemploraryID(),
  44. lineColor: '#ffffff',
  45. lineAltitudeAboveGround: 10,
  46. reverseDirection: false,
  47. lineWidth: 0.5,
  48. name: '',
  49. fontSize: 12,
  50. opacity: 1,
  51. showDirection: false,
  52. showName: true,
  53. visibilityRange: 30,
  54. globalVisibility: false,
  55. points: [],
  56. ...path
  57. }
  58. }
  59. let bcPaths: Paths = []
  60. export const getBackupPaths = () => {
  61. console.log('bcPaths', bcPaths)
  62. return bcPaths
  63. }
  64. export const backupPaths = () => {
  65. bcPaths = JSON.parse(JSON.stringify(paths.value))
  66. }
  67. export const initialPaths = fetchStoreItems(paths, async () => {
  68. const paths = await fetchPaths()
  69. return paths
  70. }, () => {
  71. backupPaths()
  72. selectPaths.all.value = true
  73. })
  74. export const recoverPaths = async () => {
  75. const backupItems = bcPaths;
  76. // paths.value.length = 0
  77. // await asyncTimeout(16)
  78. paths.value = backupItems.map((oldItem) => {
  79. const model = paths.value.find((item) => item.id === oldItem.id);
  80. return model ? Object.assign(model, oldItem) : oldItem;
  81. });
  82. }
  83. export const addPath = addStoreItem(paths, postAddPath)
  84. export const deletePath = deleteStoreItem(paths, path => postDeletePath(path.id))
  85. export const updatePath = updateStoreItem(paths, postUpdatePath)
  86. export const savePaths = saveStoreItems(
  87. paths,
  88. getBackupPaths,
  89. {
  90. add: addPath,
  91. delete: deletePath,
  92. update: updatePath
  93. }
  94. )
  95. export const autoSavePaths = autoSetModeCallback(paths, {
  96. backup: backupPaths,
  97. recovery: recoverPaths,
  98. save: async () => {
  99. if (!paths.value.every(record => record.name)) {
  100. Message.warning('路径名称不可为空')
  101. throw '路径名称不可为空'
  102. } else if (paths.value.some(path => path.points.length <= 1)) {
  103. Message.warning('路径点不可少于两个')
  104. throw '路径点不可少于两个'
  105. }
  106. await savePaths()
  107. }
  108. })