use-mini-map.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import { computed, ref, toRefs } from 'vue';
  2. import useSceneInfos from '@/hooks/useSceneInfos';
  3. import { getWorkshopMiniMapLayoutApi } from '@/api/scene/scene';
  4. import { CameraItem } from '@/types/scene/type.ts';
  5. import { defineStore } from 'pinia';
  6. import safeParse from '@/utils/safeParse';
  7. import { ElMessage } from 'element-plus';
  8. type ShopMapCamera = CameraItem & {
  9. /** 相机是否已经设置 0-false 1-true */
  10. isSet: number;
  11. /** 工位名称 */
  12. workSpaceName: string;
  13. integrationState?: number;
  14. };
  15. export const useMiniMap = defineStore('mini-map', () => {
  16. const sceneInfos = useSceneInfos();
  17. const { scenesTree, flattenedWorkshops } = toRefs(sceneInfos);
  18. const { getScenesTree, getCameraList } = sceneInfos;
  19. const selectedShopCode = ref<string>();
  20. const selectedShopDetail = computed(() =>
  21. flattenedWorkshops.value.find((space) => space.code === selectedShopCode.value),
  22. );
  23. const getShopDetailByCode = (code: string) => {
  24. return flattenedWorkshops.value.find((space) => space.code === code);
  25. };
  26. const bgImgUrl = ref('');
  27. const shopCameraList = ref<ShopMapCamera[]>([]);
  28. const getShowCameras = (code: string) => {
  29. shopCameraList.value = [];
  30. const id = getShopDetailByCode(code)?.id;
  31. if (!id) {
  32. ElMessage.error('摄像头code未找到对应信息');
  33. return Promise.resolve();
  34. }
  35. return getCameraList(id).then((res) => {
  36. res.forEach((space) => {
  37. if (space.cameraList && space.cameraList.length > 0) {
  38. space.cameraList.forEach((camera) => {
  39. shopCameraList.value.push({ ...camera, isSet: 0, workSpaceName: space.name });
  40. });
  41. }
  42. });
  43. });
  44. };
  45. const getMapLayout = (code: string) => {
  46. const shopId = getShopDetailByCode(code)?.id || '';
  47. if (!shopId) {
  48. ElMessage.error('摄像头code未找到对应信息');
  49. return Promise.reject();
  50. }
  51. return getWorkshopMiniMapLayoutApi(String(shopId)).then((res) => {
  52. const layoutJSON = res?.layout ? safeParse(res.layout) : null;
  53. return layoutJSON;
  54. });
  55. };
  56. return {
  57. selectedShopDetail,
  58. bgImgUrl,
  59. scenesTree,
  60. shopCameraList,
  61. getScenesTree,
  62. getShowCameras,
  63. getMapLayout,
  64. selectedShopCode,
  65. };
  66. });
  67. export default useMiniMap;