FileTree.vue 11 KB

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