useFenceStore.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import { GetFenceParams, getFenceApi, UpdateBatchCameraFenceParam } from '@/api/camera/camera-preview';
  2. import { defineStore } from 'pinia';
  3. import { computed, ref } from 'vue';
  4. import { ServerLine, ServerLineInfos } from '../components/FenceEditor/constants';
  5. import safeParse from '@/utils/safeParse';
  6. import { ElMessage, ElMessageBox } from 'element-plus';
  7. import { updateBatchCameraFenceApi } from '@/api/camera/camera-preview';
  8. /** 当前电子围栏的store */
  9. export const useFenceStore = defineStore('fencePolygonStore', () => {
  10. /** 初始的电子围栏。用于取消时恢复数据 */
  11. const initialFence = ref<ServerLineInfos>([]);
  12. /** 当前相机-预置位-算法对应的所有的电子围栏 */
  13. const allFences = ref<ServerLineInfos>(initialFence.value);
  14. /** 当前正在操作的电子围栏id */
  15. const currentFenceId = ref<number | null>(null);
  16. /** 电子围栏的分组id */
  17. const currentFenceGroupId = ref<number | null>(null);
  18. const loading = ref(false);
  19. // 是否展示电子围栏的工具栏
  20. const showFenceTool = ref(false);
  21. /** 电子围栏参数是否修改过 */
  22. const isChanged = computed(() => JSON.stringify(initialFence.value) !== JSON.stringify(allFences.value));
  23. /** 获取电子围栏 */
  24. const getFence = (param: GetFenceParams) => {
  25. loading.value = true;
  26. if (!param.algoId || !param.cameraId || !param.presetToken) return;
  27. return getFenceApi(param)
  28. .then((res) => {
  29. currentFenceGroupId.value = res?.id;
  30. const fence = res?.electronicFencePolygon
  31. ? (JSON.parse(res?.electronicFencePolygon) as [])
  32. : ([] as { polygon: string }[]);
  33. // const pointsJSON = points.poly
  34. const newFence = fence.map((x) => {
  35. return { ...x, polygon: typeof x.polygon === 'string' ? (safeParse(x.polygon) as ServerLine) : x.polygon };
  36. }) as unknown as ServerLineInfos;
  37. allFences.value = newFence;
  38. initialFence.value = newFence;
  39. })
  40. .catch(() => {
  41. currentFenceId.value = null;
  42. allFences.value = [];
  43. })
  44. .finally(() => {
  45. loading.value = false;
  46. currentFenceId.value = null;
  47. });
  48. };
  49. const clear = () => {
  50. allFences.value = [];
  51. initialFence.value = [];
  52. currentFenceId.value = null;
  53. currentFenceGroupId.value = null;
  54. showFenceTool.value = false;
  55. };
  56. /** 重置到修改前的状态 */
  57. const reset = () => {
  58. allFences.value = initialFence.value;
  59. };
  60. /** 确认是否要离开电子围栏 */
  61. const confirmExitFence = (): Promise<unknown> => {
  62. if (isChanged.value) {
  63. return ElMessageBox.confirm('电子围栏设置未保存,离开后无法保存设置内容。', '', {
  64. confirmButtonText: '确定',
  65. cancelButtonText: '取消',
  66. type: 'warning',
  67. dangerouslyUseHTMLString: true,
  68. }).then(() => {
  69. return Promise.resolve();
  70. });
  71. } else {
  72. return Promise.resolve();
  73. }
  74. };
  75. /** 本地电子围栏数据保存到后端 */
  76. const updateBatchCameraFence = (data: UpdateBatchCameraFenceParam) => {
  77. updateBatchCameraFenceApi(data).then(() => {
  78. // 保存成功后初始值要和当前值保持一致,避免触发confirmExitFence逻辑
  79. initialFence.value = allFences.value;
  80. ElMessage.success('保存成功');
  81. });
  82. };
  83. return {
  84. allFences,
  85. isChanged,
  86. currentFenceId,
  87. currentFenceGroupId,
  88. initialFence,
  89. clear,
  90. showFenceTool,
  91. getFence,
  92. reset,
  93. confirmExitFence,
  94. updateBatchCameraFence,
  95. };
  96. });
  97. export default useFenceStore;