useFenceStore.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import {
  2. GetFenceParams,
  3. getFenceApi,
  4. saveFenceApi,
  5. SaveFenceParams,
  6. } from '@/api/camera/camera-preview';
  7. import { defineStore } from 'pinia';
  8. import { ref } from 'vue';
  9. import { ServerLines } from '../components/FenceEditor/constants';
  10. /** 当前电子围栏的store */
  11. export const useFenceStore = defineStore('electronicFencePolygonStore', () => {
  12. /** 后端返回的电子围栏点 */
  13. const serverFencePoints = ref<ServerLines>([]);
  14. /** 当前编辑的电子围栏的点 */
  15. const currentFencePoints = ref([]);
  16. const currentFenceId = ref<number>();
  17. /** 获取电子围栏 */
  18. const getFence = (param: GetFenceParams) => {
  19. // return new Promise((resolve) => {
  20. // setTimeout(() => {
  21. // const res = {
  22. // id: 2,
  23. // electronicFencePolygon:
  24. // Math.random() > 0.5
  25. // ? ``
  26. // : `[[[150.9196038878118,103.42855053676564],[350.91958691002293,54.42855423815479],[464.91957723268325,211.42854237860183],[222.9195977758078,225.42854132106206],[60.91961152781679,175.42854509798977]],[[325.9195890322465,259.42853875275125],[534.9195712904572,249.42853950813677],[624.9195636504521,362.42853097228016],[509.91957341268073,380.4285296125862],[309.91959039046964,345.4285322564356]]]`,
  27. // };
  28. // currentFenceId.value = res.id;
  29. // const points = res.electronicFencePolygon
  30. // ? (JSON.parse(res.electronicFencePolygon) as [])
  31. // : [];
  32. // currentFencePoints.value = points;
  33. // serverFencePoints.value = points;
  34. // resolve();
  35. // }, 200);
  36. // });
  37. return getFenceApi(param).then((res) => {
  38. currentFenceId.value = res.id;
  39. const points = res.electronicFencePolygon
  40. ? (JSON.parse(res.electronicFencePolygon) as [])
  41. : [];
  42. currentFencePoints.value = points;
  43. serverFencePoints.value = points;
  44. });
  45. };
  46. const saveFence = (param: SaveFenceParams) => {
  47. return saveFenceApi(param).then((res) => {
  48. console.log('save success', res);
  49. });
  50. };
  51. return { serverFencePoints, currentFencePoints, currentFenceId, getFence, saveFence };
  52. });
  53. export default useFenceStore;