use-mini-map.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import { computed, ref, toRefs } from 'vue';
  2. import useSceneInfos from '@/hooks/useSceneInfos';
  3. import { CameraItem, getWorkshopMiniMapLayoutApi } from '@/api/scene/scene';
  4. import { defineStore } from 'pinia';
  5. import safeParse from '@/utils/safeParse';
  6. type ShopMapCamera = CameraItem & {
  7. /** 相机是否已经设置 0-false 1-true */
  8. isSet: number;
  9. /** 工位名称 */
  10. workSpaceName: string;
  11. };
  12. export const useMiniMap = defineStore('mini-map', () => {
  13. const sceneInfos = useSceneInfos();
  14. const { scenesTree, workSpaces } = toRefs(sceneInfos);
  15. const { getScenesTree, getCameraList } = sceneInfos;
  16. const selectedShopId = ref<number>();
  17. const selectedShop = computed(() =>
  18. workSpaces.value.find((space) => space.id === selectedShopId.value),
  19. );
  20. const bgImgUrl = ref('');
  21. const shopCameraList = ref<ShopMapCamera[]>([]);
  22. const getShowCameras = () => {
  23. shopCameraList.value = [];
  24. getCameraList(selectedShopId.value!).then((res) => {
  25. res.forEach((space) => {
  26. if (space.cameraList && space.cameraList.length > 0) {
  27. space.cameraList.forEach((camera) => {
  28. shopCameraList.value.push({ ...camera, isSet: 0, workSpaceName: space.name });
  29. });
  30. }
  31. });
  32. });
  33. };
  34. const getMapLayout = () => {
  35. return getWorkshopMiniMapLayoutApi(selectedShopId.value + '').then((res) => {
  36. const layoutJSON = res?.layout ? safeParse(res.layout) : null;
  37. return layoutJSON;
  38. });
  39. };
  40. return {
  41. selectedShop,
  42. bgImgUrl,
  43. scenesTree,
  44. shopCameraList,
  45. getScenesTree,
  46. getShowCameras,
  47. getMapLayout,
  48. selectedShopId,
  49. };
  50. });
  51. export default useMiniMap;