BomTree.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. <template>
  2. <div>
  3. <el-input v-model="deptName" class="mb-20px" clearable placeholder="请输入BOM名称">
  4. <template #prefix>
  5. <Icon icon="ep:search" />
  6. </template>
  7. </el-input>
  8. </div>
  9. <div ref="treeContainer" class="tree-container">
  10. <el-tree
  11. ref="treeRef"
  12. :data="bomList"
  13. :expand-on-click-node="false"
  14. :filter-node-method="filterNode"
  15. :props="defaultProps"
  16. default-expand-all
  17. highlight-current
  18. node-key="id"
  19. @node-click="handleNodeClick"
  20. @node-contextmenu="handleRightClick"
  21. style="height: 33em"
  22. draggable
  23. :allow-drop="allowDrop"
  24. @node-drop="handleNodeDrop"
  25. @node-drag-start="handleDragStart"
  26. @node-drag-over="handleDragOver"
  27. @node-drag-end="handleDragEnd"
  28. >
  29. <template #default="{ node}">
  30. <div style="display: flex; justify-content: space-between;align-items: center;width: 100%">
  31. <div>
  32. <el-icon style="vertical-align: middle"><Folder /></el-icon> <!-- 文件夹图标 -->
  33. <span style="vertical-align: middle;margin-left: 3px">{{ node.data.name }}</span>
  34. </div>
  35. </div>
  36. </template>
  37. </el-tree>
  38. <div v-if="dragTip.visible" class="drag-tip" :style="dragTip.style">
  39. 移动到 {{ dragTip.text }}
  40. </div>
  41. </div>
  42. <div v-show="menuVisible" ref="contextMenuRef" class="custom-menu" :style="{ left: menuX + 'px', top: menuY + 'px' }">
  43. <ul>
  44. <li style="border-bottom: 1px solid #ccc;" @click="handleMenuClick('add')">新增子节点</li>
  45. <li style="border-bottom: 1px solid #ccc;" @click="handleMenuClick('edit')">编辑节点</li>
  46. <li @click="handleMenuClick('delete')">删除节点</li>
  47. <li @click="handleMenuClick('copy')">复制</li>
  48. </ul>
  49. </div>
  50. <Dialog v-model="dialogVisible" :title="dialogTitle" style="width: 40em">
  51. <el-form
  52. ref="formRef"
  53. v-loading="formLoading"
  54. :model="formData"
  55. :rules="formRules"
  56. label-width="80px"
  57. >
  58. <el-form-item label="资料分类名称" prop="name" label-width="110px">
  59. <el-input v-model="formData.name" placeholder="请输入资料分类名称" />
  60. </el-form-item>
  61. <el-form-item label="显示排序" prop="sort" label-width="110px">
  62. <el-input-number v-model="formData.sort" :min="0" controls-position="right" />
  63. </el-form-item>
  64. <el-form-item label="备注" prop="remark" label-width="110px">
  65. <el-input
  66. v-model="formData.remark"
  67. maxlength="11"
  68. placeholder="请输入备注"
  69. type="textarea"
  70. />
  71. </el-form-item>
  72. </el-form>
  73. <template #footer>
  74. <el-button type="primary" @click="submitForm">确 定</el-button>
  75. <el-button @click="dialogVisible = false">取 消</el-button>
  76. </template>
  77. </Dialog>
  78. </template>
  79. <script lang="ts" setup>
  80. import { ElTree, FormRules } from 'element-plus'
  81. import { IotInfoApi, IotInfoVO } from '@/api/pms/iotinfo'
  82. import * as InfoClassifyApi from '@/api/pms/info'
  83. import { defaultProps, handleTree } from '@/utils/tree'
  84. import { CommonStatusEnum } from '@/utils/constants'
  85. import {IotInfoClassifyApi} from "@/api/pms/info";
  86. import { Document, Folder, Search } from '@element-plus/icons-vue'
  87. import * as BomApi from '@/api/pms/bom'
  88. import {IotDeviceApi} from "@/api/pms/device";
  89. const { t } = useI18n() // 国际化
  90. const message = useMessage() // 消息弹窗
  91. const dialogVisible = ref(false) // 弹窗的是否展示
  92. const dialogTitle = ref('') // 弹窗的标题
  93. const formRef = ref() // 搜索的表单
  94. const openForm = (type: string, id?: number) => {
  95. formRef.value.open(type, id)
  96. }
  97. const handleDragStart = (node) => {
  98. highlightedNode.value = null;
  99. dragTip.value.visible = true;
  100. };
  101. const highlightedNode = ref(null);
  102. const handleDragOver = (dragNode, targetNode, event) => {
  103. // 高亮目标节点
  104. highlightedNode.value = targetNode;
  105. // 更新提示位置和内容
  106. dragTip.value.text = targetNode.label;
  107. dragTip.value.style = {
  108. left: `${event.clientX + 15}px`,
  109. top: `${event.clientY}px`
  110. };
  111. };
  112. const handleDragEnd = () => {
  113. highlightedNode.value = null;
  114. dragTip.value.visible = false;
  115. };
  116. const dragTip = ref({
  117. visible: false,
  118. text: '',
  119. style: {}
  120. });
  121. const handleNodeDrop = async (draggingNode, dropNode, dropType, event) => {
  122. // 获取拖拽后的新父节点ID和目标位置
  123. const newParentId = dropNode.data.id;
  124. const targetIndex = dropNode.parent.childNodes.findIndex(n => n.data.id === dropNode.data.id);
  125. // 更新本地数据
  126. draggingNode.data.parentId = newParentId;
  127. console.log(JSON.stringify(draggingNode.data))
  128. await InfoClassifyApi.IotInfoClassifyApi.removeIotInfoClassify(draggingNode.data)
  129. };
  130. const allowDrop = (draggingNode, dropNode, type) => {
  131. // 禁止拖拽到非文件夹节点内部
  132. //if (dropNode.data.type === 'dept') return false;
  133. // 限制最大层级(如不超过3层)
  134. return true
  135. };
  136. // 动态高度计算
  137. const treeContainer = ref(null);
  138. const setHeight = () => {
  139. if (!treeContainer.value) return;
  140. const windowHeight = window.innerHeight;
  141. const containerTop = treeContainer.value.offsetTop;
  142. treeContainer.value.style.height = `${windowHeight *0.50}px`; // 60px 底部预留
  143. };
  144. onMounted(async () => {
  145. await getTree()
  146. setHeight();
  147. if (bomList.value.length > 0) {
  148. emits('success', bomList.value[0].id)
  149. }
  150. window.addEventListener('resize', setHeight);
  151. });
  152. onUnmounted(() => {
  153. window.removeEventListener('resize', setHeight);
  154. });
  155. const formLoading = ref(false) // 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用
  156. const formType = ref('') // 表单的类型:create - 新增;update - 修改
  157. const formData = ref({
  158. id: undefined,
  159. parentId: undefined,
  160. deviceId: undefined,
  161. name: undefined,
  162. sort: undefined,
  163. status: CommonStatusEnum.ENABLE,
  164. remark: undefined
  165. })
  166. const formRules = reactive<FormRules>({
  167. name: [{ required: true, message: '资料分类名称不能为空', trigger: 'blur' }],
  168. sort: [{ required: true, message: '显示排序不能为空', trigger: 'blur' }],
  169. status: [{ required: true, message: '状态不能为空', trigger: 'blur' }]
  170. })
  171. const submitForm = async () => {
  172. // 校验表单
  173. if (!formRef) return
  174. const valid = await formRef.value.validate()
  175. if (!valid) return
  176. // 提交请求
  177. formLoading.value = true
  178. try {
  179. const data = formData.value as unknown as InfoClassifyApi.IotInfoClassifyVO
  180. if (formType.value === 'create') {
  181. data.parentId = nodeInfo.value.id
  182. data.deviceId = props.deviceId
  183. await InfoClassifyApi.IotInfoClassifyApi.createIotInfoClassify(data)
  184. message.success(t('common.createSuccess'))
  185. } else {
  186. await InfoClassifyApi.IotInfoClassifyApi.updateIotInfoClassify(data)
  187. message.success(t('common.updateSuccess'))
  188. }
  189. dialogVisible.value = false
  190. } finally {
  191. await getTree()
  192. formLoading.value = false
  193. }
  194. }
  195. /** 重置表单 */
  196. const resetForm = () => {
  197. formData.value = {
  198. id: undefined,
  199. title: '',
  200. parentId: undefined,
  201. name: undefined,
  202. sort: undefined,
  203. leaderUserId: undefined,
  204. phone: undefined,
  205. email: undefined,
  206. status: CommonStatusEnum.ENABLE
  207. }
  208. formRef.value?.resetFields()
  209. }
  210. defineOptions({ name: 'BomTree' })
  211. const props = defineProps({
  212. deviceId: { type: Number, required: true }
  213. })
  214. const deptName = ref('')
  215. const nodeInfo = ref({})
  216. const bomList = ref<Tree[]>([]) // 树形结构
  217. const treeRef = ref<InstanceType<typeof ElTree>>()
  218. const menuVisible = ref(false)
  219. const menuX = ref(0)
  220. const menuY = ref(0)
  221. const contextMenuRef = ref(null) // 弹窗DOM引用
  222. let selectedNode = null
  223. const handleRightClick = (event, node, data) => {
  224. nodeInfo.value = node;
  225. console.log(JSON.stringify(nodeInfo.value))
  226. event.preventDefault()
  227. menuX.value = event.clientX
  228. menuY.value = event.clientY
  229. selectedNode = data
  230. // menuVisible.value = true
  231. }
  232. const handleMenuClick = async (action) => {
  233. switch (action) {
  234. case 'add':
  235. dialogVisible.value = true
  236. dialogTitle.value = '新增资料分类'
  237. formType.value = 'create'
  238. resetForm()
  239. break
  240. case 'edit':
  241. resetForm()
  242. dialogVisible.value = true
  243. dialogTitle.value = '编辑资料分类'
  244. formType.value = 'update'
  245. formData.value = nodeInfo.value
  246. console.log(JSON.stringify(formData.value))
  247. break
  248. case 'delete':
  249. // 删除的二次确认
  250. await message.delConfirm()
  251. // 发起删除
  252. await IotInfoClassifyApi.deleteIotInfoClassify(nodeInfo.value.id)
  253. message.success(t('common.delSuccess'))
  254. // 刷新列表
  255. await getTree()
  256. break
  257. }
  258. menuVisible.value = false
  259. }
  260. const queryParams = reactive({
  261. pageNo: 1,
  262. pageSize: 10,
  263. name: undefined,
  264. status: undefined,
  265. deviceCategoryId: undefined,
  266. })
  267. /** 获得部门树 */
  268. const getTree = async () => {
  269. IotDeviceApi.getIotDevice(props.deviceId).then((re) => {
  270. queryParams.deviceCategoryId = re.assetClass
  271. BomApi.getBomPage(queryParams).then((res) => {
  272. console.log(JSON.stringify(res))
  273. // bomList.value = []
  274. // bomList.value.push(...handleTree(res))
  275. bomList.value = handleTree(res)
  276. })
  277. })
  278. }
  279. /** 基于名字过滤 */
  280. const filterNode = (name: string, data: Tree) => {
  281. if (!name) return true
  282. return data.name.includes(name)
  283. }
  284. /** 处理部门被点击 */
  285. const handleNodeClick = async (row: { [key: string]: any }) => {
  286. console.log(row)
  287. emits('node-click', row)
  288. }
  289. const emits = defineEmits(['node-click','success'])
  290. /** 监听deptName */
  291. watch(deptName, (val) => {
  292. treeRef.value!.filter(val)
  293. })
  294. const handleClickOutside = (event) => {
  295. if (
  296. menuVisible.value &&
  297. contextMenuRef.value &&
  298. !contextMenuRef.value.contains(event.target)
  299. ) {
  300. menuVisible.value = false
  301. }
  302. }
  303. watch(menuVisible, (visible) => {
  304. if (visible) {
  305. document.addEventListener('click', handleClickOutside)
  306. } else {
  307. document.removeEventListener('click', handleClickOutside)
  308. }
  309. })
  310. onBeforeUnmount(() => {
  311. document.removeEventListener('click', handleClickOutside)
  312. })
  313. /** 初始化 */
  314. // onMounted(async () => {
  315. // await getTree()
  316. // })
  317. </script>
  318. <style lang="scss" scoped>
  319. .custom-menu {
  320. position: fixed;
  321. background: white;
  322. border: 1px solid #ccc;
  323. box-shadow: 2px 2px 5px rgba(0, 0, 0, 0.1);
  324. z-index: 1000;
  325. }
  326. .custom-menu ul {
  327. list-style: none;
  328. padding: 0;
  329. margin: 0;
  330. }
  331. .custom-menu li {
  332. padding: 8px 20px;
  333. cursor: pointer;
  334. font-size: 14px;
  335. margin: 5px
  336. }
  337. .custom-menu li:hover {
  338. background: #77a0ec;
  339. }
  340. .tree-container {
  341. overflow-y: auto;
  342. min-width: 100%;
  343. border: 1px solid #e4e7ed;
  344. border-radius: 4px;
  345. }
  346. /* 自定义滚动条(可选) */
  347. .tree-container::-webkit-scrollbar {
  348. width: 6px;
  349. }
  350. .tree-container::-webkit-scrollbar-thumb {
  351. background: #c0c4cc;
  352. border-radius: 3px;
  353. }
  354. .highlight {
  355. background-color: #f0f9ff;
  356. color: #409eff;
  357. padding: 2px 5px;
  358. border-radius: 3px;
  359. }
  360. /* 拖拽提示框样式 */
  361. .drag-tip {
  362. position: fixed;
  363. background: #fff;
  364. border: 1px solid #409eff;
  365. padding: 5px 10px;
  366. border-radius: 4px;
  367. box-shadow: 0 2px 8px rgba(0,0,0,0.1);
  368. z-index: 9999;
  369. }
  370. /* 调整拖拽指示线样式 */
  371. :deep(.el-tree-node__content) .el-tree-node__expand-icon {
  372. position: relative;
  373. }
  374. :deep(.el-tree-node__drop-indicator) {
  375. background-color: #409eff;
  376. height: 2px;
  377. }
  378. /* 禁止拖拽到内部时的缩进 */
  379. :deep(.is-drop-inner) .el-tree-node__drop-indicator {
  380. background-color: #409eff;
  381. height: 100%;
  382. width: 4px;
  383. left: -8px !important;
  384. }
  385. </style>