data.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import { computed, ref, watch } from "vue";
  2. import { Mode } from "@/views/graphic/menus";
  3. import { accidentPhotos, types, AccidentPhoto } from "@/store/accidentPhotos";
  4. import { roadPhotos, RoadPhoto } from "@/store/roadPhotos";
  5. import { router } from "@/router";
  6. import { getId } from "@/utils";
  7. import { photos } from "@/store/photos";
  8. import { trackMode } from "@/views/scene/trackMeasureWidth";
  9. export const useData = () => {
  10. const data = ref<AccidentPhoto | RoadPhoto>(null);
  11. watch(
  12. () => ({
  13. params: router.currentRoute.value.params,
  14. accidentPhotos: accidentPhotos.value,
  15. roadPhotos: roadPhotos.value,
  16. }),
  17. ({ params }) => {
  18. if (trackMode.value) {
  19. return;
  20. }
  21. if (!params.action) {
  22. data.value = null;
  23. } else if (params.action === "add") {
  24. const photo = photos.value.find((data) => data.id === params.id);
  25. if (!photo) {
  26. return;
  27. }
  28. data.value = {
  29. data: null,
  30. title: "",
  31. type: types[2],
  32. sceneData: {
  33. meterPerPixel: photo.meterPerPixel,
  34. measures: photo.measures,
  35. basePoints: photo.basePoints,
  36. baseLines: photo.baseLines,
  37. fixGraphs: photo.fixGraphs,
  38. fixPoints: photo.fixPoints,
  39. },
  40. photoUrl: photo.url,
  41. id: getId(),
  42. url: null,
  43. time: new Date().getTime(),
  44. };
  45. } else {
  46. const mode = Number(params.mode) as Mode;
  47. if (mode === Mode.Photo) {
  48. return (data.value = accidentPhotos.value.find(
  49. (data) => data.id === params.id
  50. ));
  51. } else {
  52. return (data.value = roadPhotos.value.find(
  53. (data) => data.id === params.id
  54. ));
  55. }
  56. }
  57. },
  58. { immediate: true }
  59. );
  60. return data;
  61. };