index.vue 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273
  1. <template>
  2. <a-spin :spinning="spinning">
  3. <a-alert
  4. v-if="areaAvailable"
  5. message="检测区域范围未配置或数值较小,请在设备配置调整参数!"
  6. banner
  7. />
  8. <furnitureCard
  9. v-if="isEditDraggable"
  10. v-model:is-edit="isEditDraggable"
  11. :style="{ marginTop: '30px' }"
  12. @add="addHnadler"
  13. ></furnitureCard>
  14. <div class="viewer">
  15. <div class="viewer-header">
  16. <div>
  17. <div class="viewer-header-title">家具配置</div>
  18. <div class="viewer-header-subtitle">
  19. <span>检测范围 {{ areaWidth }} x {{ areaHeight }} cm {{ props.ranges }}</span>
  20. </div>
  21. </div>
  22. <div class="viewer-header-extra">
  23. <a-space>
  24. <span v-if="props.online === 0" style="color: red">⚠️设备离线,不允许编辑保存</span>
  25. <a-switch
  26. :checked="isEditDraggable"
  27. checked-children="启用"
  28. un-checked-children="禁用"
  29. :disabled="props.online === 0"
  30. @change="isEditDraggable = !isEditDraggable"
  31. />
  32. <a-button
  33. type="primary"
  34. size="small"
  35. :disabled="!isEditDraggable"
  36. @click="saveAllConfig"
  37. >保存配置</a-button
  38. >
  39. </a-space>
  40. </div>
  41. </div>
  42. <div v-if="!areaAvailable" class="viewer-content">
  43. <div
  44. ref="contentEl"
  45. class="mapBox"
  46. :style="{
  47. width: `${areaWidth}px`,
  48. height: `${areaHeight}px`,
  49. cursor: !isEditDraggable ? 'no-drop' : 'default',
  50. }"
  51. @mousedown="handleMouseDownMapCanvas"
  52. >
  53. <furniture-icon
  54. v-for="(item, index) in mapCanvasList"
  55. :key="index"
  56. :icon="item.type"
  57. :width="item.width"
  58. :height="item.height"
  59. :style="{
  60. left: `${item.left}px`,
  61. top: `${item.top}px`,
  62. position: 'absolute',
  63. transform: `rotate(${item.rotate}deg)`,
  64. cursor: item.type === 'radar' ? 'default' : isEditDraggable ? 'move' : 'default',
  65. pointerEvents: item.type === 'radar' ? 'none' : isEditDraggable ? 'auto' : 'none',
  66. border: `${item.isActice && item.type !== 'radar' ? '2px solid yellow' : 'none'}`,
  67. }"
  68. :class="{ 'dragging-item': item.isDragging }"
  69. tabindex="0"
  70. :draggable="item.type === 'radar' ? false : isEditDraggable"
  71. @dragstart="onDragstartListItem($event, item)"
  72. @dragend="onDragendEndListItem($event, item)"
  73. @click="onClickMapItem($event, item)"
  74. />
  75. </div>
  76. <div v-if="clickedDragItem" class="mapConfig">
  77. <div class="mapConfig-header">家具属性</div>
  78. <div class="mapConfig-item">
  79. <div class="mapConfig-item-label">家具名称:</div>
  80. <div class="mapConfig-item-content">
  81. <a-input
  82. v-if="clickedDragItem"
  83. v-model:value.trim="clickedDragItem.name"
  84. size="small"
  85. style="width: 128px"
  86. />
  87. </div>
  88. </div>
  89. <div class="mapConfig-item">
  90. <div class="mapConfig-item-label">家具大小:</div>
  91. <div class="mapConfig-item-content">
  92. <a-space v-if="clickedDragItem">
  93. <a-input-number
  94. v-model:value.trim="clickedDragItem.width"
  95. size="small"
  96. style="width: 60px"
  97. />
  98. <a-input-number
  99. v-model:value.trim="clickedDragItem.height"
  100. size="small"
  101. style="width: 60px"
  102. />
  103. </a-space>
  104. </div>
  105. </div>
  106. <div class="mapConfig-item">
  107. <div class="mapConfig-item-label">家具旋转:</div>
  108. <div class="mapConfig-item-content">
  109. <a-space>
  110. <UndoOutlined
  111. :rotate="-270"
  112. @click="rotateFurnitureIcon(1, clickedDragItem?.nanoid || '')"
  113. />
  114. <RedoOutlined
  115. :rotate="-90"
  116. @click="rotateFurnitureIcon(2, clickedDragItem?.nanoid || '')"
  117. />
  118. </a-space>
  119. </div>
  120. </div>
  121. <div class="mapConfig-item">
  122. <div class="mapConfig-item-label">微调位置:</div>
  123. <div class="mapConfig-item-content">
  124. <a-space>
  125. <ArrowLeftOutlined
  126. @click="positonFurnitureIcon('left', clickedDragItem?.nanoid || '', distance)"
  127. />
  128. <ArrowUpOutlined
  129. @click="positonFurnitureIcon('up', clickedDragItem?.nanoid || '', distance)"
  130. />
  131. <ArrowDownOutlined
  132. @click="positonFurnitureIcon('down', clickedDragItem?.nanoid || '', distance)"
  133. />
  134. <ArrowRightOutlined
  135. @click="positonFurnitureIcon('right', clickedDragItem?.nanoid || '', distance)"
  136. />
  137. <a-input-number v-model:value.trim="distance" size="small" style="width: 60px" />
  138. </a-space>
  139. </div>
  140. </div>
  141. <div class="mapConfig-item">
  142. <div class="mapConfig-item-label">删除家具:</div>
  143. <div class="mapConfig-item-content">
  144. <a-popconfirm
  145. v-if="clickedDragItem.type === 'bed'"
  146. placement="bottom"
  147. @confirm="deleteFurnitureBed(clickedDragItem?.nanoid || '')"
  148. >
  149. <template #icon><question-circle-outlined style="color: red" /></template>
  150. <template #title>
  151. <div>删除 “床”也会删除子区域,</div>
  152. <div>是否继续删除?</div>
  153. </template>
  154. <DeleteOutlined />
  155. </a-popconfirm>
  156. <DeleteOutlined v-else @click="deleteFurnitureIcon(clickedDragItem?.nanoid || '')" />
  157. </div>
  158. </div>
  159. </div>
  160. </div>
  161. </div>
  162. <div class="viewer">
  163. <div class="viewer-header">
  164. <div>
  165. <div class="viewer-header-title">屏蔽子区域配置</div>
  166. <div class="viewer-header-subtitle"
  167. >检测范围 {{ areaWidth }} x {{ areaHeight }} cm {{ props.ranges }}</div
  168. >
  169. </div>
  170. <div class="viewer-header-extra">
  171. <a-space>
  172. <a-button size="small" :disabled="!isEditDraggable" @click="createNewBlock">{{
  173. isCreating ? '创建中...' : '新建区域'
  174. }}</a-button>
  175. </a-space>
  176. </div>
  177. </div>
  178. <div v-if="!areaAvailable" class="viewer-content">
  179. <div
  180. class="mapBox blockArea"
  181. :style="{
  182. width: `${areaWidth}px`,
  183. height: `${areaHeight}px`,
  184. cursor: !isEditDraggable ? 'no-drop' : isCreating ? 'crosshair' : 'default',
  185. }"
  186. @mousedown="handleMouseDown"
  187. >
  188. <furniture-icon
  189. v-for="(item, index) in furnitureItems"
  190. :key="index"
  191. :icon="item.type"
  192. :width="item.width"
  193. :height="item.length"
  194. :style="{
  195. left: `${item.left}px`,
  196. top: `${item.top}px`,
  197. position: 'absolute',
  198. transform: `rotate(${item.rotate}deg)`,
  199. cursor: 'default',
  200. }"
  201. :draggable="false"
  202. />
  203. <!-- 绘制临时选区 -->
  204. <div
  205. v-if="currentBlock"
  206. class="temp-block"
  207. :style="{
  208. left: `${Math.min(currentBlock.startX, currentBlock.currentX)}px`,
  209. top: `${Math.min(currentBlock.startY, currentBlock.currentY)}px`,
  210. width: `${Math.abs(currentBlock.currentX - currentBlock.startX)}px`,
  211. height: `${Math.abs(currentBlock.currentY - currentBlock.startY)}px`,
  212. }"
  213. ></div>
  214. <!-- 已创建区块 -->
  215. <div
  216. v-for="(block, blockIndex) in blocks"
  217. :key="block.id"
  218. class="block-item"
  219. :style="{
  220. left: `${block.x}px`,
  221. top: `${block.y}px`,
  222. width: `${block.width}px`,
  223. height: `${block.height}px`,
  224. border: `2px solid ${block?.isBed ? '#1abc1a' : block.isActice ? 'yellow' : '#1890ff'}`,
  225. position: 'absolute',
  226. cursor: !isEditDraggable ? 'no-drop' : 'move',
  227. backgroundColor: block.isBed ? 'rgba(26, 188, 26, 0.1)' : 'rgba(24, 144, 255, 0.1)',
  228. }"
  229. @mousedown="startDrag(block, $event)"
  230. @click="selectBlock(block)"
  231. >
  232. <div
  233. class="resize-handle"
  234. :style="{
  235. backgroundColor: block.isBed ? '#1abc1a' : '#1890ff',
  236. }"
  237. @mousedown.stop="startResize(block, $event)"
  238. >
  239. {{ blockIndex + 1 }}
  240. </div>
  241. </div>
  242. </div>
  243. <div v-if="selectedBlock" class="mapConfig">
  244. <div class="mapConfig-header">子区域属性</div>
  245. <div class="mapConfig-item">
  246. <div class="mapConfig-item-label">X范围:</div>
  247. <div class="mapConfig-item-content">
  248. <a-space>
  249. <a-input
  250. v-model:value.trim="selectedBlock.startXx"
  251. :style="{ width: '50px' }"
  252. size="small"
  253. @pressEnter="blockInputPressEnter($event, selectedBlock, 'startXx')"
  254. @blur="blockInputBlur($event, selectedBlock, 'startXx')"
  255. />
  256. <a-input
  257. v-model:value.trim="selectedBlock.stopXx"
  258. :style="{ width: '50px' }"
  259. size="small"
  260. @pressEnter="blockInputPressEnter($event, selectedBlock, 'stopXx')"
  261. @blur="blockInputBlur($event, selectedBlock, 'stopXx')"
  262. />
  263. </a-space>
  264. </div>
  265. </div>
  266. <div class="mapConfig-item">
  267. <div class="mapConfig-item-label">Y范围:</div>
  268. <div class="mapConfig-item-content">
  269. <a-space>
  270. <a-input
  271. v-model:value.trim="selectedBlock.startYy"
  272. :style="{ width: '50px' }"
  273. size="small"
  274. @pressEnter="blockInputPressEnter($event, selectedBlock, 'startYy')"
  275. @blur="blockInputBlur($event, selectedBlock, 'startYy')"
  276. />
  277. <a-input
  278. v-model:value.trim="selectedBlock.stopYy"
  279. :style="{ width: '50px' }"
  280. size="small"
  281. @pressEnter="blockInputPressEnter($event, selectedBlock, 'stopYy')"
  282. @blur="blockInputBlur($event, selectedBlock, 'stopYy')"
  283. />
  284. </a-space>
  285. </div>
  286. </div>
  287. <div class="mapConfig-item">
  288. <div class="mapConfig-item-label">Z范围:</div>
  289. <div class="mapConfig-item-content">
  290. <a-space>
  291. <a-input
  292. v-model:value.trim="selectedBlock.startZz"
  293. :style="{ width: '50px' }"
  294. size="small"
  295. />
  296. <a-input
  297. v-model:value.trim="selectedBlock.stopZz"
  298. :style="{ width: '50px' }"
  299. size="small"
  300. />
  301. </a-space>
  302. </div>
  303. </div>
  304. <div class="mapConfig-item">
  305. <div class="mapConfig-item-label">区域跟踪:</div>
  306. <div class="mapConfig-item-content">
  307. <a-switch v-model:checked="selectedBlock.isTracking" size="small" />
  308. </div>
  309. </div>
  310. <div class="mapConfig-item">
  311. <div class="mapConfig-item-label">区域跌倒:</div>
  312. <div class="mapConfig-item-content">
  313. <a-switch v-model:checked="selectedBlock.isFalling" size="small" />
  314. </div>
  315. </div>
  316. <div v-if="selectedBlock.isBed" class="mapConfig-item">
  317. <div class="mapConfig-item-label">呼吸检测:</div>
  318. <div class="mapConfig-item-content"> 默认开启 </div>
  319. </div>
  320. <div class="mapConfig-item">
  321. <div class="mapConfig-item-label">删除区域:</div>
  322. <div class="mapConfig-item-content">
  323. <DeleteOutlined @click="deleteBlockArea(selectedBlock.id || '')" />
  324. </div>
  325. </div>
  326. </div>
  327. </div>
  328. </div>
  329. </a-spin>
  330. </template>
  331. <script setup lang="ts">
  332. import { ref, computed } from 'vue'
  333. import * as roomApi from '@/api/room'
  334. import type { FurnitureType, Furniture } from '@/api/room/types'
  335. import { message } from 'ant-design-vue'
  336. import { nanoid } from 'nanoid'
  337. import { useDropZone } from '@vueuse/core'
  338. import { furnitureIconNameMap, furnitureIconSizeMap } from '@/const/furniture'
  339. import type { FurnitureIconType } from '@/types/furniture'
  340. import furnitureCard from '../furnitureCard/index.vue'
  341. import {
  342. RedoOutlined,
  343. UndoOutlined,
  344. ArrowUpOutlined,
  345. ArrowDownOutlined,
  346. ArrowLeftOutlined,
  347. ArrowRightOutlined,
  348. DeleteOutlined,
  349. QuestionCircleOutlined,
  350. } from '@ant-design/icons-vue'
  351. import { getOriginPosition } from '@/utils'
  352. defineOptions({
  353. name: 'deviceAreaConfig',
  354. })
  355. type Props = {
  356. devId: string // 设备id,用于获取房间布局
  357. // x,y的范围,用于初始化画布的大小
  358. length: number
  359. width: number
  360. ranges: number[] // 区域范围
  361. online?: SwitchType // 设备在线状态,用于判断是否可以保存配置
  362. }
  363. const emit = defineEmits<{
  364. (e: 'success', value: void): void
  365. }>()
  366. const props = withDefaults(defineProps<Props>(), {
  367. devId: '',
  368. length: 0, // 区域宽度
  369. width: 0, // 区域高度
  370. ranges: () => [], // 区域范围
  371. online: 0,
  372. })
  373. const deviceRoomId = ref('')
  374. const furnitureItems = ref<Furniture[]>([])
  375. // 检测区域宽度 length
  376. const areaWidth = computed(() => {
  377. return Math.abs(props.length)
  378. })
  379. // 检测区域高度 width
  380. const areaHeight = computed(() => {
  381. return Math.abs(props.width)
  382. })
  383. // 检测区域是否可用,小于50cm的区域,不可用
  384. const areaAvailable = computed(() => {
  385. return areaWidth.value < 50 || areaHeight.value < 50
  386. })
  387. const spinning = ref(false)
  388. // 获取房间布局
  389. const fetchRoomLayout = async () => {
  390. console.log('fetchRoomLayout', props, props.devId)
  391. if (!props.devId) {
  392. message.error('设备ID不能为空')
  393. return
  394. }
  395. try {
  396. spinning.value = true
  397. const res = await roomApi.queryRoomInfo({
  398. devId: props.devId,
  399. })
  400. console.log('✅获取到房间布局信息', res)
  401. if (!res) {
  402. spinning.value = false
  403. return
  404. }
  405. const { furnitures, roomId, subRegions } = res.data
  406. deviceRoomId.value = roomId || ''
  407. if (furnitures) {
  408. furnitureItems.value = furnitures!.map((item) => {
  409. // 将接口的家具,添加在家具画布上
  410. mapCanvasList.value.push({
  411. name: item.name,
  412. type: item.type,
  413. width: item.width,
  414. height: item.length,
  415. top: item.top,
  416. left: item.left,
  417. x: item.x,
  418. y: item.y,
  419. rotate: item.rotate,
  420. nanoid: nanoid(),
  421. })
  422. // 将接口的家具,添加在屏蔽子区域画布上
  423. return {
  424. ...item,
  425. width: item.width || 45,
  426. length: item.length || 45,
  427. top: item.top || 0,
  428. left: item.left || 0,
  429. rotate: item.rotate || 0,
  430. x: item.x || 0,
  431. y: item.y || 0,
  432. }
  433. })
  434. }
  435. if (subRegions) {
  436. // 将接口的子区域,添加在子区域画布上
  437. subRegions.forEach((item, index) => {
  438. blocks.value.push({
  439. // 本地需要使用的数据
  440. id: nanoid(),
  441. x: item.startXx + originX,
  442. y: originY - item.startYy,
  443. ox: item.startXx + originX - originX,
  444. oy: originY - item.startYy - originY,
  445. width: Math.abs(item.stopXx - item.startXx),
  446. height: Math.abs(item.stopYy - item.startYy),
  447. isDragging: false,
  448. isResizing: false,
  449. isActice: false,
  450. isTracking: Boolean(item.trackPresence),
  451. isFalling: Boolean(item.excludeFalling),
  452. isBed: index === 0 && mapCanvasList.value.some((item) => item.type === 'bed'),
  453. // 来自接口回显的数据
  454. startXx: item.startXx,
  455. stopXx: item.stopXx,
  456. startYy: item.startYy,
  457. stopYy: item.stopYy,
  458. startZz: item.startZz,
  459. stopZz: item.stopZz,
  460. isLowSnr: item.isLowSnr,
  461. isDoor: item.isDoor,
  462. presenceEnterDuration: item.presenceEnterDuration,
  463. presenceExitDuration: item.presenceExitDuration,
  464. trackPresence: item.trackPresence,
  465. excludeFalling: item.excludeFalling,
  466. })
  467. })
  468. console.log('🚀', blocks.value)
  469. }
  470. spinning.value = false
  471. } catch (error) {
  472. console.error('❌获取房间布局信息失败', error)
  473. spinning.value = false
  474. }
  475. }
  476. fetchRoomLayout().finally(() => {
  477. // 获取房间信息后,初始化雷达图标
  478. initRadarIcon()
  479. })
  480. interface CanvaseItem {
  481. name: string // 名称
  482. type: string // 类型,图标icon
  483. width: number // 家具宽度
  484. height: number // 家具长度
  485. top: number // 距离检测范围上方位置 cm
  486. left: number // 距离检测范围左边位置 cm
  487. rotate: number // 旋转角度: 0°,90°,180°,270°
  488. x?: number // 距离雷达的X距离
  489. y?: number // 距离雷达的Y距离
  490. nanoid?: string // 本地使用
  491. isActice?: boolean // 是否选中 本地使用
  492. isDragging?: boolean // 是否拖拽 本地使用
  493. }
  494. // 画布上的家具列表
  495. const mapCanvasList = ref<CanvaseItem[]>([])
  496. const isEditDraggable = ref(false)
  497. // 家具列表添加
  498. const addHnadler = (icon: FurnitureIconType) => {
  499. console.log('addHnadler', icon)
  500. // 检查画布上是否已经添加过了 icon 为 bed 的家具
  501. if (icon === 'bed') {
  502. const isExist = mapCanvasList.value.some((item) => item.type === icon)
  503. if (isExist) {
  504. message.error('床已经添加过了,不可重复添加')
  505. return
  506. }
  507. }
  508. // 家具原始宽高
  509. const originWidth = furnitureIconSizeMap[icon].width || 30
  510. const originHeight = furnitureIconSizeMap[icon].height || 30
  511. mapCanvasList.value.push({
  512. name: furnitureIconNameMap[icon],
  513. type: icon,
  514. width: originWidth,
  515. height: originHeight,
  516. top: 0,
  517. left: 0,
  518. rotate: 0,
  519. x: originOffsetX,
  520. y: originOffsetY,
  521. nanoid: nanoid(),
  522. isActice: false,
  523. })
  524. message.success('已添加家具')
  525. if (icon === 'bed') {
  526. // 同步添加一个子区域
  527. blocks.value.unshift({
  528. // 本地用
  529. id: nanoid(),
  530. x: 20,
  531. y: 15,
  532. ox: -150,
  533. oy: 180,
  534. width: originWidth,
  535. height: originHeight,
  536. isDragging: false,
  537. isResizing: false,
  538. isActice: false,
  539. isTracking: false,
  540. isFalling: false,
  541. isBed: true,
  542. // 接口用
  543. startXx: -150,
  544. stopXx: -100,
  545. startYy: 180,
  546. stopYy: 120,
  547. startZz: 0,
  548. stopZz: 0,
  549. isLowSnr: 1,
  550. isDoor: 0,
  551. presenceEnterDuration: 3,
  552. presenceExitDuration: 3,
  553. trackPresence: 0,
  554. excludeFalling: 0,
  555. })
  556. console.log('blocks', blocks.value)
  557. message.success('已添加子区域')
  558. }
  559. }
  560. const contentEl = ref<HTMLElement>()
  561. const currentDragItem = ref<CanvaseItem | null>(null)
  562. // 内容区域放置处理
  563. useDropZone(contentEl, {
  564. onDrop(files: File[] | null, event: DragEvent) {
  565. if (contentEl.value && currentDragItem.value) {
  566. const rect = contentEl.value.getBoundingClientRect()
  567. // 计算基于画布容器的精确坐标
  568. const x = event.clientX - rect.left
  569. const y = event.clientY - rect.top
  570. // 中心点对齐计算
  571. currentDragItem.value.top = y - currentDragItem.value.height / 2
  572. currentDragItem.value.left = x - currentDragItem.value.width / 2
  573. // 添加边界检查
  574. // const maxX = contentEl.value.offsetWidth - currentDragItem.value.width
  575. // const maxY = contentEl.value.offsetHeight - currentDragItem.value.height
  576. // currentDragItem.value.left = Math.max(0, Math.min(x - currentDragItem.value.width / 2, maxX))
  577. // currentDragItem.value.top = Math.max(0, Math.min(y - currentDragItem.value.height / 2, maxY))
  578. }
  579. },
  580. })
  581. // 家具列表元素开始拖拽
  582. const onDragstartListItem = (event: DragEvent, item: CanvaseItem) => {
  583. console.log('🔥onDragstartListItem', event, item)
  584. currentDragItem.value = item
  585. item.isDragging = true
  586. // 创建拖拽镜像
  587. const target = event.currentTarget as HTMLElement
  588. const clone = target.cloneNode(true) as HTMLElement
  589. const rect = target.getBoundingClientRect()
  590. // 获取实际渲染样式
  591. const computedStyle = window.getComputedStyle(target)
  592. // 复制所有关键样式
  593. clone.style.cssText = `
  594. position: fixed;
  595. left: -9999px;
  596. opacity: 0.5;
  597. pointer-events: none;
  598. width: ${rect.width}px;
  599. height: ${rect.height}px;
  600. transform: ${computedStyle.transform || `rotate(${item.rotate}deg)`};
  601. transform-origin: ${computedStyle.transformOrigin || 'center center'};
  602. border: ${computedStyle.border};
  603. box-shadow: ${computedStyle.boxShadow};
  604. background: ${computedStyle.background};
  605. `
  606. console.log('克隆元素尺寸:', clone.style.cssText)
  607. document.body.appendChild(clone)
  608. // 计算中心点偏移量
  609. const offsetX = rect.width / 2 // 水平中心偏移
  610. const offsetY = rect.height / 2 // 垂直中心偏移
  611. // 设置拖拽镜像和偏移量
  612. event.dataTransfer?.setDragImage(clone, offsetX, offsetY)
  613. // 隐藏原元素
  614. target.style.opacity = '0'
  615. }
  616. // 家具列表元素结束拖拽
  617. const onDragendEndListItem = (event: DragEvent, item: CanvaseItem) => {
  618. item.isDragging = false
  619. if (currentDragItem.value) {
  620. currentDragItem.value.x = originOffsetX
  621. currentDragItem.value.y = originOffsetY
  622. currentDragItem.value.isDragging = false
  623. }
  624. requestAnimationFrame(() => {
  625. item.isActice = true
  626. clickedDragItem.value = item
  627. })
  628. console.log('🔥onDragendEndListItem', event, item, {
  629. x: currentDragItem.value?.x,
  630. y: currentDragItem.value?.y,
  631. })
  632. if (event.currentTarget) {
  633. ;(event.currentTarget as HTMLElement).style.opacity = '1'
  634. }
  635. }
  636. const clickedDragItem = ref<CanvaseItem | null>()
  637. const onClickMapItem = (event: MouseEvent, item: CanvaseItem) => {
  638. if (!isEditDraggable.value || item.type === 'radar' || item.isDragging) return
  639. console.log('onClickMapItem', event, item)
  640. item.isActice = true
  641. clickedDragItem.value = item
  642. }
  643. const handleMouseDownMapCanvas = () => {
  644. mapCanvasList.value.forEach((item) => {
  645. item.isActice = false
  646. })
  647. clickedDragItem.value = null
  648. }
  649. // 家具旋转
  650. const rotateFurnitureIcon = (type: number, nanoid: string) => {
  651. console.log('rotateFurnitureIcon', type, nanoid, mapCanvasList.value)
  652. const rotateMap = [0, 90, 180, 270]
  653. if (nanoid) {
  654. mapCanvasList.value.forEach((item) => {
  655. if (item.nanoid === nanoid) {
  656. // 获取当前角度在rotateMap中的索引
  657. const currentIndex = rotateMap.indexOf(item.rotate)
  658. if (type === 1) {
  659. // 逆时针(索引递减)
  660. const newIndex = (currentIndex - 1 + rotateMap.length) % rotateMap.length
  661. item.rotate = rotateMap[newIndex]
  662. } else if (type === 2) {
  663. // 顺时针(索引递增)
  664. const newIndex = (currentIndex + 1) % rotateMap.length
  665. item.rotate = rotateMap[newIndex]
  666. }
  667. }
  668. })
  669. }
  670. }
  671. // 微调距离
  672. const distance = ref(5)
  673. // 家具位置微调
  674. const positonFurnitureIcon = (type: string, nanoid: string, distance: number) => {
  675. console.log('positonFurnitureIcon', type, nanoid, mapCanvasList.value)
  676. if (nanoid) {
  677. mapCanvasList.value.forEach((item) => {
  678. if (item.nanoid === nanoid) {
  679. if (type === 'up') {
  680. item.top -= distance
  681. }
  682. if (type === 'down') {
  683. item.top += distance
  684. }
  685. if (type === 'left') {
  686. item.left -= distance
  687. }
  688. if (type === 'right') {
  689. item.left += distance
  690. }
  691. }
  692. })
  693. }
  694. }
  695. // 删除家具
  696. const deleteFurnitureIcon = (nanoid: string) => {
  697. console.log('deleteFurnitureIcon', clickedDragItem.value)
  698. if (nanoid) {
  699. mapCanvasList.value = mapCanvasList.value.filter((item) => item.nanoid !== nanoid)
  700. clickedDragItem.value = null
  701. }
  702. }
  703. // 删除家具床
  704. const deleteFurnitureBed = (nanoid: string) => {
  705. console.log('deleteFurnitureBed', nanoid)
  706. // 先从家具画布移除床
  707. deleteFurnitureIcon(nanoid)
  708. // 再从子区域画布删除对应的子区域
  709. blocks.value.shift()
  710. }
  711. // 新增区块类型
  712. interface BlockItem {
  713. // 本地用
  714. id: string // 唯一标识
  715. x: number // 区块基于父元素的X偏移量,区块的左上角x坐标
  716. y: number // 区块基于父元素的Y偏移量,区块的左上角y坐标
  717. ox: number // 区块基于原点的X偏移量,区块的左上角x坐标
  718. oy: number // 区块基于原点的Y偏移量,区块的左上角y坐标
  719. width: number // 区块宽度
  720. height: number // 区块高度
  721. isDragging: boolean // 是否正在拖动
  722. isResizing: boolean // 是否正在调整大小
  723. isActice: boolean // 是否选中
  724. isTracking: boolean // 是否开启区域跟踪 0-否,1-是 对应 trackPresence 字段
  725. isFalling: boolean // 是否屏蔽区域跌倒检测 0-否,1-是 对应 excludeFalling 字段
  726. isBed?: boolean // 是否是床 本地判断使用
  727. // 接口用
  728. startXx: number // 屏蔽子区域X开始
  729. stopXx: number // 屏蔽子区域X结束
  730. startYy: number // 屏蔽子区域Y开始
  731. stopYy: number // 屏蔽子区域Y结束
  732. startZz: number // 屏蔽子区域Z开始
  733. stopZz: number // 屏蔽子区域Z结束
  734. isLowSnr: number // 是否为床 0-不是,1-是
  735. isDoor: number // 是否是门 0-否,1-是 默认0
  736. presenceEnterDuration: number // 人员进入时间 默认3
  737. presenceExitDuration: number // 人员离开时间 默认3
  738. trackPresence: number // 是否开启区域跟踪存在 0-否,1-是
  739. excludeFalling: number // 是否屏蔽区域跌倒检测 0-否,1-是
  740. }
  741. const blocks = ref<BlockItem[]>([])
  742. const isCreating = ref(false)
  743. const currentBlock = ref<{
  744. startX: number
  745. startY: number
  746. currentX: number
  747. currentY: number
  748. } | null>(null)
  749. const selectedBlock = ref<BlockItem | null>(null)
  750. // 新建区块处理
  751. const createNewBlock = () => {
  752. if (blocks.value && blocks.value.length > 5) {
  753. message.warn('最多只能创建6个区块')
  754. return
  755. }
  756. isCreating.value = true
  757. }
  758. // 获取容器边界
  759. const getContainerRect = () => {
  760. const container = document.querySelector('.blockArea') as HTMLElement
  761. return container?.getBoundingClientRect() || { left: 0, top: 0 }
  762. }
  763. // 鼠标事件处理
  764. const handleMouseDown = (e: MouseEvent) => {
  765. if (!isEditDraggable.value) return
  766. console.log('handleMouseDown', e)
  767. blocks.value.forEach((item) => {
  768. item.isActice = false
  769. })
  770. selectedBlock.value = null
  771. if (!isCreating.value) return
  772. const rect = getContainerRect()
  773. const startX = e.clientX - rect.left
  774. const startY = e.clientY - rect.top
  775. currentBlock.value = {
  776. startX,
  777. startY,
  778. currentX: startX,
  779. currentY: startY,
  780. }
  781. document.addEventListener('mousemove', handleMouseMove)
  782. document.addEventListener('mouseup', handleMouseUp)
  783. }
  784. // 鼠标移动处理
  785. const handleMouseMove = (e: MouseEvent) => {
  786. if (!currentBlock.value) return
  787. const rect = getContainerRect()
  788. currentBlock.value.currentX = Math.max(0, Math.min(e.clientX - rect.left, rect.width))
  789. currentBlock.value.currentY = Math.max(0, Math.min(e.clientY - rect.top, rect.height))
  790. }
  791. // 鼠标释放处理
  792. const handleMouseUp = () => {
  793. if (!currentBlock.value) return
  794. const { startX, startY, currentX, currentY } = currentBlock.value
  795. const width = Math.abs(currentX - startX)
  796. const height = Math.abs(currentY - startY)
  797. if (width > 10 && height > 10) {
  798. blocks.value.push({
  799. // 本地用
  800. id: nanoid(),
  801. x: Math.round(Math.min(startX, currentX)),
  802. y: Math.round(Math.min(startY, currentY)),
  803. ox: Math.round(Math.min(startX, currentX)) - originX,
  804. oy: Math.round(Math.min(startY, currentY)) - originY,
  805. width,
  806. height,
  807. isDragging: false,
  808. isResizing: false,
  809. isActice: false,
  810. isTracking: false,
  811. isFalling: false,
  812. // 接口用
  813. startXx: Math.round(Math.min(startX, currentX)) - originX,
  814. stopXx: Math.round(Math.min(startX, currentX)) - originX + width,
  815. startYy: Math.round(Math.min(startY, currentY)) - originY,
  816. stopYy: Math.round(Math.min(startY, currentY)) - originY + height,
  817. startZz: 0,
  818. stopZz: 0,
  819. isLowSnr: 0,
  820. isDoor: 0,
  821. presenceEnterDuration: 3,
  822. presenceExitDuration: 3,
  823. trackPresence: 0,
  824. excludeFalling: 0,
  825. })
  826. }
  827. currentBlock.value = null
  828. isCreating.value = false
  829. document.removeEventListener('mousemove', handleMouseMove)
  830. document.removeEventListener('mouseup', handleMouseUp)
  831. }
  832. // 区块拖动
  833. const startDrag = (block: BlockItem, e: MouseEvent) => {
  834. if (!isEditDraggable.value) return
  835. console.log('startDrag', block)
  836. e.stopPropagation()
  837. block.isDragging = true
  838. block.isActice = true
  839. const container = document.querySelector('.blockArea') as HTMLElement
  840. const rect = container.getBoundingClientRect()
  841. const offsetX = e.clientX - rect.left - block.x
  842. const offsetY = e.clientY - rect.top - block.y
  843. const moveHandler = (e: MouseEvent) => {
  844. const newX = e.clientX - rect.left - offsetX
  845. const newY = e.clientY - rect.top - offsetY
  846. const containerWidth = container.offsetWidth
  847. const containerHeight = container.offsetHeight
  848. block.x = Math.max(0, Math.min(newX, containerWidth - block.width))
  849. block.y = Math.max(0, Math.min(newY, containerHeight - block.height))
  850. block.ox = block.x - originX
  851. block.oy = originY - block.y
  852. block.startXx = block.ox
  853. block.stopXx = block.ox + block.width
  854. block.startYy = block.oy
  855. block.stopYy = block.oy - block.height
  856. }
  857. const upHandler = () => {
  858. block.isDragging = false
  859. block.isActice = false
  860. document.removeEventListener('mousemove', moveHandler)
  861. document.removeEventListener('mouseup', upHandler)
  862. }
  863. document.addEventListener('mousemove', moveHandler)
  864. document.addEventListener('mouseup', upHandler)
  865. }
  866. const selectBlock = (block: BlockItem) => {
  867. if (!isEditDraggable.value) return
  868. console.log('selectBlock', block)
  869. selectedBlock.value = block
  870. blocks.value.forEach((item) => {
  871. item.isActice = item === block
  872. })
  873. }
  874. // 保存子区域配置
  875. // const saveBlockConfig = () => {
  876. // const blockData = blocks.value.map((item) => {
  877. // return {
  878. // startXx: item.startXx,
  879. // stopXx: item.stopXx,
  880. // startYy: item.startYy,
  881. // stopYy: item.stopYy,
  882. // startZz: Number(item.startZz) || 0,
  883. // stopZz: Number(item.stopZz) || 0,
  884. // isLowSnr: item.isLowSnr,
  885. // isDoor: item.isDoor,
  886. // presenceEnterDuration: item.presenceEnterDuration,
  887. // presenceExitDuration: item.presenceExitDuration,
  888. // trackPresence: Number(item.isTracking),
  889. // excludeFalling: Number(item.isFalling),
  890. // }
  891. // })
  892. // console.log('当前所有区块配置:', blockData)
  893. // try {
  894. // const res = roomApi.saveRoomInfo({
  895. // roomId: deviceRoomId.value,
  896. // devId: props.devId,
  897. // subRegions: blockData,
  898. // })
  899. // console.log('saveBlockConfig 保存成功', res)
  900. // message.success('保存成功')
  901. // emit('success')
  902. // } catch (error) {
  903. // console.error('saveBlockConfig 保存失败', error)
  904. // }
  905. // }
  906. const { originX, originY, originOffsetX, originOffsetY, radarX, radarY } = getOriginPosition(
  907. props.ranges,
  908. [currentDragItem.value?.left as number, currentDragItem.value?.top as number]
  909. )
  910. // 初始化添加雷达图标
  911. const initRadarIcon = () => {
  912. console.log('initRadarIcon', mapCanvasList.value, furnitureItems.value)
  913. // 在家具地图添加雷达图标
  914. mapCanvasList.value.push({
  915. name: '雷达',
  916. type: 'radar',
  917. width: furnitureIconSizeMap['radar'].width,
  918. height: furnitureIconSizeMap['radar'].height,
  919. top: radarY,
  920. left: radarX,
  921. x: originOffsetX,
  922. y: originOffsetY,
  923. rotate: 0,
  924. nanoid: nanoid(),
  925. })
  926. // 在屏蔽子区域添加雷达图标
  927. furnitureItems.value.push({
  928. name: '雷达',
  929. type: 'radar',
  930. width: furnitureIconSizeMap['radar'].width,
  931. length: furnitureIconSizeMap['radar'].height,
  932. top: radarY,
  933. left: radarX,
  934. x: originOffsetX,
  935. y: originOffsetY,
  936. rotate: 0,
  937. })
  938. }
  939. // 保存家具配置
  940. // const saveFurnitureMapConfig = () => {
  941. // console.log('saveFurnitureMapConfig', mapCanvasList.value)
  942. // try {
  943. // const res = roomApi.saveRoomInfo({
  944. // roomId: deviceRoomId.value,
  945. // devId: props.devId,
  946. // furnitures: mapCanvasList.value
  947. // .filter((item) => item.type !== 'radar')
  948. // .map((item) => {
  949. // return {
  950. // name: item.name,
  951. // type: item.type as FurnitureType,
  952. // width: item.width,
  953. // length: item.height,
  954. // top: item.top,
  955. // left: item.left,
  956. // rotate: item.rotate as 0 | 90 | 180 | 270,
  957. // x: item?.x || 0,
  958. // y: item?.y || 0,
  959. // }
  960. // }),
  961. // })
  962. // console.log('保存家具配置 成功', res)
  963. // message.success('保存成功')
  964. // emit('success')
  965. // } catch (error) {
  966. // console.error('保存家具配置 失败', error)
  967. // }
  968. // }
  969. // 保存所有配置
  970. const saveAllConfig = () => {
  971. console.log('保存所有配置')
  972. const blockData = blocks.value.map((item) => {
  973. return {
  974. startXx: item.startXx,
  975. stopXx: item.stopXx,
  976. startYy: item.startYy,
  977. stopYy: item.stopYy,
  978. startZz: Number(item.startZz) || 0,
  979. stopZz: Number(item.stopZz) || 0,
  980. isLowSnr: item.isLowSnr,
  981. isDoor: item.isDoor,
  982. presenceEnterDuration: item.presenceEnterDuration,
  983. presenceExitDuration: item.presenceExitDuration,
  984. trackPresence: Number(item.isTracking),
  985. excludeFalling: Number(item.isFalling),
  986. }
  987. })
  988. console.log('当前所有区块配置:', blockData)
  989. try {
  990. const res = roomApi.saveRoomInfo({
  991. roomId: deviceRoomId.value,
  992. devId: props.devId,
  993. furnitures: mapCanvasList.value
  994. .filter((item) => item.type !== 'radar')
  995. .map((item) => {
  996. return {
  997. name: item.name,
  998. type: item.type as FurnitureType,
  999. width: item.width,
  1000. length: item.height,
  1001. top: item.top,
  1002. left: item.left,
  1003. rotate: item.rotate as 0 | 90 | 180 | 270,
  1004. x: item?.x || 0,
  1005. y: item?.y || 0,
  1006. }
  1007. }),
  1008. subRegions: blockData,
  1009. })
  1010. console.log('保存所有配置 成功', res)
  1011. message.success('保存成功')
  1012. emit('success')
  1013. } catch (error) {
  1014. console.error('保存所有配置 失败', error)
  1015. }
  1016. }
  1017. const startResize = (block: BlockItem, e: MouseEvent) => {
  1018. block.isResizing = true
  1019. selectedBlock.value = block
  1020. const startX = e.clientX
  1021. const startY = e.clientY
  1022. const initialWidth = block.width
  1023. const initialHeight = block.height
  1024. const moveHandler = (e: MouseEvent) => {
  1025. const rect = getContainerRect()
  1026. const deltaX = e.clientX - startX
  1027. const deltaY = e.clientY - startY
  1028. // 限制最小尺寸和容器边界
  1029. block.width = Math.max(50, Math.min(initialWidth + deltaX, rect.width - block.x))
  1030. block.height = Math.max(50, Math.min(initialHeight + deltaY, rect.height - block.y))
  1031. // 改变了区块的长款,元素的结束位置也相应变化
  1032. block.stopXx = block.ox + block.width
  1033. block.stopYy = block.oy + block.height
  1034. }
  1035. const upHandler = () => {
  1036. block.isResizing = false
  1037. selectedBlock.value = null
  1038. document.removeEventListener('mousemove', moveHandler)
  1039. document.removeEventListener('mouseup', upHandler)
  1040. }
  1041. document.addEventListener('mousemove', moveHandler)
  1042. document.addEventListener('mouseup', upHandler)
  1043. }
  1044. const blockInputPressEnter = (e: Event, el: BlockItem, attr: string) => {
  1045. if (attr === 'startXx') {
  1046. el.startXx = Number(el[attr as keyof BlockItem])
  1047. el.x = el.startXx + originX
  1048. }
  1049. if (attr === 'stopXx') {
  1050. el.stopXx = Number(el[attr as keyof BlockItem])
  1051. el.width = el.stopXx + originX - el.width
  1052. }
  1053. if (attr === 'startYy') {
  1054. el.startYy = Number(el[attr as keyof BlockItem])
  1055. el.x = el.startYy + originY
  1056. }
  1057. if (attr === 'stopYy') {
  1058. el.stopYy = Number(el[attr as keyof BlockItem])
  1059. el.height = el.stopYy + originY - el.height
  1060. }
  1061. }
  1062. const blockInputBlur = (e: Event, el: BlockItem, attr: string) => {
  1063. console.log('blockInputBlur', e, el, attr)
  1064. if (attr === 'startXx') {
  1065. el.startXx = Number(el[attr as keyof BlockItem])
  1066. el.x = el.startXx + originX
  1067. }
  1068. if (attr === 'stopXx') {
  1069. el.stopXx = Number(el[attr as keyof BlockItem])
  1070. el.width = el.stopXx + originX - el.width
  1071. }
  1072. if (attr === 'startYy') {
  1073. el.startYy = Number(el[attr as keyof BlockItem])
  1074. el.x = el.startYy + originY
  1075. }
  1076. if (attr === 'stopYy') {
  1077. el.stopYy = Number(el[attr as keyof BlockItem])
  1078. el.height = el.stopYy + originY - el.height
  1079. }
  1080. }
  1081. const deleteBlockArea = (id: string) => {
  1082. if (id) {
  1083. blocks.value = blocks.value.filter((item) => item.id !== id)
  1084. selectedBlock.value = null
  1085. }
  1086. }
  1087. </script>
  1088. <style scoped lang="less">
  1089. .viewer {
  1090. padding: 10px;
  1091. min-width: 500px;
  1092. flex-shrink: 0;
  1093. // margin-top: 10px;
  1094. &-header {
  1095. display: flex;
  1096. justify-content: space-between;
  1097. padding-bottom: 20px;
  1098. &-title {
  1099. font-size: 16px;
  1100. font-weight: 600;
  1101. line-height: 24px;
  1102. }
  1103. &-subtitle {
  1104. font-size: 14px;
  1105. color: #666;
  1106. }
  1107. }
  1108. &-content {
  1109. display: flex;
  1110. gap: 20px;
  1111. }
  1112. }
  1113. .mapBox {
  1114. background-color: #e0e0e0;
  1115. background-image:
  1116. linear-gradient(rgba(0, 0, 0, 0.1) 1px, transparent 1px),
  1117. linear-gradient(to right, rgba(0, 0, 0, 0.1) 1px, transparent 1px);
  1118. background-size: 20px 20px;
  1119. position: relative;
  1120. flex-shrink: 0;
  1121. // 添加黑边框
  1122. &::before {
  1123. content: '';
  1124. position: absolute;
  1125. top: -5px;
  1126. left: -5px;
  1127. width: calc(100% + 10px);
  1128. height: calc(100% + 10px);
  1129. border: 5px solid rgba(0, 0, 0, 0.8);
  1130. box-sizing: border-box;
  1131. pointer-events: none;
  1132. }
  1133. }
  1134. .mapConfig {
  1135. background-color: #f5f5f5;
  1136. border-radius: 10px;
  1137. padding: 12px;
  1138. &-header {
  1139. font-size: 14px;
  1140. margin-bottom: 10px;
  1141. font-weight: 600;
  1142. }
  1143. &-item {
  1144. display: flex;
  1145. line-height: 30px;
  1146. &-label {
  1147. color: #888;
  1148. min-width: 80px;
  1149. }
  1150. &-content {
  1151. color: #555;
  1152. min-width: 100px;
  1153. }
  1154. }
  1155. }
  1156. .temp-block {
  1157. position: absolute;
  1158. background: rgba(24, 144, 255, 0.2);
  1159. border: 2px dashed #1890ff;
  1160. }
  1161. .block-item {
  1162. background: rgba(24, 144, 255, 0.1);
  1163. .resize-handle {
  1164. position: absolute;
  1165. right: -4px;
  1166. bottom: -4px;
  1167. width: 15px;
  1168. height: 15px;
  1169. background: #1890ff;
  1170. cursor: nwse-resize;
  1171. font-size: 12px;
  1172. color: #fff;
  1173. display: flex;
  1174. align-items: center;
  1175. justify-content: center;
  1176. }
  1177. }
  1178. .dragging-item {
  1179. opacity: 0.5;
  1180. transition: opacity 0.2s ease;
  1181. }
  1182. </style>