From 02409bfbe15f22fc3b5dccadabfd860a660a49d9 Mon Sep 17 00:00:00 2001
From: 罗广辉 <guanghui.luo@foxmail.com>
Date: Sat, 11 Oct 2025 10:37:14 +0800
Subject: [PATCH] feat: 名称修改

---
 src/views/device/addDevice.vue |  394 ++++++++++++++++++++++++++++++++++++++++++++++----------
 1 files changed, 322 insertions(+), 72 deletions(-)

diff --git a/src/views/device/addDevice.vue b/src/views/device/addDevice.vue
index 88a8a89..8b12544 100644
--- a/src/views/device/addDevice.vue
+++ b/src/views/device/addDevice.vue
@@ -24,11 +24,12 @@
     getDetail,
 } from '@/api/device/workspaces'
 
+import { getList as getSystemNames } from '@/api/system/sysConfig'
+import { getRegionTreeAll } from '@/api/job/task';
+
 import { getLazyTree } from '@/api/base/region'
 
 import { mapGetters } from 'vuex'
-import { getWebsocketUrl } from '@/websocket/util/config'
-import ConnectWebSocket from '@/websocket'
 import { $Clipboard } from "@smallwei/avue"
 
 
@@ -38,6 +39,8 @@
 
     data () {
         return {
+            xzqhData: [],
+            systemNamesList: [],
             // 用于记录已经创建的websocket,防止重复创建
             webSocketIdSet: new Set(),
             // 用于保存多组websocket
@@ -62,20 +65,26 @@
             },
             selectionList: [],
             option: {
+                addTitle: '设备注册',
                 menuWidth: 300,
                 lazy: true,
                 tree: true,
 
-                // dialogWidth: 1050,
+                dialogWidth: 1050,
+                searchGutter: 30,
                 tip: false,
                 searchShow: true,
                 searchMenuSpan: 6,
+                searchGutter: 30,
                 border: true,
                 index: true,
+                indexLabel: '序号',
+                indexWidth: 60,
                 viewBtn: true,
                 selection: false,
                 excelBtn: false,
                 addBtn: true,
+                addBtnText: '设备注册',
                 dialogClickModal: false,
                 grid: false,
 
@@ -83,74 +92,83 @@
                 calcHeight: 20,
                 column: [
                     {
-                        label: '工作空间名称',
+                        label: '设备名称',// '工作空间名称',
                         prop: 'workspace_name',
-                        editDisabled: true,
-                        searchLabelWidth: 130,
+                        // editDisabled: true,
+                        editDisplay: false,
+                        labelWidth: 130,
                         search: true,
                         // searchSpan: 4,
                         labelWidth: 130,
                         rules: [
                             {
                                 required: true,
-                                message: '请输入工作空间名称',
+                                message: '请输入设备名称',
                                 trigger: 'blur',
                             },
                         ],
                     },
                     {
-                        label: '工作空间描述',
-                        prop: 'workspace_desc',
-                        labelWidth: 130,
-                        // search: true,
-                        searchSpan: 4,
-                        // editDisabled: true,
-                        rules: [
-                            {
-                                required: true,
-                                message: '请输入工作空间描述',
-                                trigger: 'blur',
-                            },
-                        ],
-                    },
-                    {
-                        label: '平台名称',
+                        label: '系统名称',// '平台名称',
                         prop: 'platform_name',
                         labelWidth: 130,
-                        editDisabled: true,
+                        width: 180,
+                        // editDisabled: true,
+                        editDisplay: false,
+                        type: 'select',
+                        dicData: this.systemNamesDicData,
                         rules: [
                             {
                                 required: false,
-                                message: '请输入平台名称',
+                                message: '请输入系统名称',
                                 trigger: 'blur',
                             },
                         ],
                     },
-
+                    {
+                        label: '所属单位',
+                        prop: 'dept_name',
+                        addDisplay: false,
+                        editDisplay: false,
+                        viewDisplay: false,
+                        labelWidth: 130,
+                        width: 180,
+                        rules: [
+                        {
+                            required: true,
+                            message: '请输入所属单位',
+                            trigger: 'blur',
+                        },
+                        ],
+                    },
                     {
                         label: '所属单位',
                         prop: 'dept_id',
+                        hide: true,
                         type: 'tree',
                         defaultExpandAll: true,
+                        search: true,
+                        searchSpan: 4,
                         labelWidth: 130,
                         dicUrl: '/blade-system/dept/getTree',
                         props: {
-                            label: 'name',
-                            value: 'id',
+                        label: 'name',
+                        value: 'id',
                         },
                         rules: [
-                            {
-                                required: true,
-                                message: '请输入所属单位',
-                                trigger: 'blur',
-                            },
+                        {
+                            required: true,
+                            message: '请输入所属单位',
+                            trigger: 'blur',
+                        },
                         ],
                     },
                     {
                         label: '绑定编号',
                         prop: 'bind_code',
                         labelWidth: 130,
-                        editDisabled: true,
+                        // editDisabled: true,
+                        editDisplay: false,
                         rules: [
                             {
                                 required: false,
@@ -161,10 +179,12 @@
                     },
                     {
                         label: '设备类型',
+                        editDisplay: false,
                         prop: 'type',
                         type: 'radio',
                         labelWidth: 130,
                         value: 0,
+                        type: 'select',
                         dicData: [
                             {
                                 label: '机场',
@@ -184,16 +204,121 @@
                         ],
                     },
                     {
+                        label: '设备位置',// '行政区划',
+                        prop: 'area_name',
+                        // hide: true,
+                        overHidden: true,
+                        addDisplay: false, //新增显示
+                        editDisplay: false, //编辑显示
+                        viewDisplay: true, //查看显示
+                        labelWidth: 130,
+                        width: 200,
+                    },
+                    {
+                        label: '设备位置',
+                        prop: 'area_code',
+                        type: 'cascader',
+                        labelWidth: 130,
+                        searchSpan: 4,
+                        hide: true,
+                        search: true,
+                        // overHidden: true,
+                        editDisplay: true,
+                        viewDisplay: true,
+                        checkStrictly: true,
+
+                        props: {
+                        label: 'title',
+                        value: 'value',
+                        emitPath: false,
+                        multiple: false,
+                        expandTrigger: 'hover',
+                        },
+                        dataType: 'string',
+                        rules: [
+                        {
+                            required: true,
+                            message: '请选择设备位置',
+                            trigger: 'change',
+                        },
+                        ],
+                        lazy: true,
+                        lazyLoad (node, resolve) {
+                        let level = node.level
+                        let list = []
+                        let callback = () => {
+                            resolve(
+                            (list || []).map(ele => ({
+                                ...ele,
+                                value: ele.value,
+                                leaf: level >= 2,
+                            }))
+                            )
+                        }
+
+                        if (level === 0) {
+                            getLazyTree('000000000000').then(res => {
+                            list = res.data.data
+                            callback()
+                            })
+                        } else if (level === 1) {
+                            getLazyTree(node.value).then(res => {
+                            list = res.data.data
+                            callback()
+                            })
+                        } else if (level === 2) {
+                            getLazyTree(node.value).then(res => {
+                            list = res.data.data
+                            callback()
+                            })
+                        } else {
+                            callback()
+                        }
+                        },
+                    },
+                    {
+                        label: '设备描述', // '工作空间描述',
+                        prop: 'workspace_desc',
+                        labelWidth: 130,
+                        searchSpan: 4,
+                        rules: [
+                            {
+                                required: true,
+                                message: '请输入设备描述',
+                                trigger: 'blur',
+                            },
+                            {
+                                max: 30,
+                                message: '设备描述不能超过30个字',
+                                trigger: 'blur',
+                            },
+                            // 如果需要更严格的校验(如中文字符计算),可以使用 validator
+                            {
+                                validator: (rule, value, callback) => {
+                                    if (value && value.length > 30) {
+                                        callback(new Error('设备描述不能超过30个字'));
+                                    } else {
+                                        callback();
+                                    }
+                                },
+                                trigger: 'blur',
+                            },
+                        ],
+                    },
+                    
+                    {
                         label: '创建时间',
                         prop: 'create_time',
                         addDisplay: false,
                         editDisplay: false,
+                        overHidden: true,
                         type: 'date',
                         labelWidth: 130,
                         width: 160,
                         format: 'YYYY-MM-DD HH:mm:ss',
 
                     },
+                    
 
                 ],
             },
@@ -218,6 +343,12 @@
             })
             return ids.join(',')
         },
+        systemNamesDicData() {
+            return this.systemNamesList.map(item => ({
+                label: item.name,
+                value: item.id,
+            }));
+        },
     },
     destroyed () {
         this.websocketMap.forEach((k, v) => {
@@ -228,44 +359,117 @@
             }
         })
     },
+    async created() {
+        await this.getRegionTreeList();
+        await this.getSystemNamesList();
+    },
     methods: {
+        async getRegionTreeList(code) {
+            try {
+                const res = await getRegionTreeAll({ parentCode: code });
+                return res.data.data; // 返回数据而不是赋值给 this.xzqhData
+            } catch (error) {
+                console.error('获取区域树数据失败:', error);
+                return null;
+            }
+        },
+        async getSystemNamesList() {
+            try {
+                const res = await getSystemNames(1, 99);
+                if (res?.data?.data?.records) {
+                    this.systemNamesList = res.data.data.records;
+                    // 找到平台名称列的索引
+                    const columnIndex = this.option.column.findIndex(col => col.prop === 'platform_name');
+                    if (columnIndex !== -1) {
+                        // 直接更新 dicData
+                        this.option.column[columnIndex].dicData = this.systemNamesList.map(item => ({
+                            label: item.name,
+                            value: item.id
+                        }));
+                    }
+                }
+            } catch (error) {
+                console.error('获取系统名称失败:', error);
+                this.systemNamesList = [];
+            }
+        },
+        beforeOpen (type, row, index) {
+            if (type === 'add') {
+                this.form = {
+                    workspace_name: '',
+                    platform_name: '',
+                    dept_name: '',
+                    bind_code: '',
+                    type: 0,
+                    workspace_desc: '',
+                    area_name: '',
+                    area_code: '',
+                }
+            }
+        },
+        async getXZQHList(area_code) {
+            try {
+                const res = await getLazyTree(area_code);
+                return res.data.data; // 返回数据,而不是直接修改 this.xzqhData
+            } catch (error) {
+                console.error('获取行政区划数据失败:', error);
+                return [];
+            }
+        },  
+        findNodeById(tree, id) {
+            if (tree.id === id) {
+                return tree;
+            }
+            if (tree.childrens && tree.childrens.length > 0) {
+                for (const child of tree.childrens) {
+                    const found = this.findNodeById(child, id);
+                    if (found) return found;
+                }
+            }
+            return null;
+        },
+        async fetchAreaName(areaCode) {
+            if (!areaCode) return '/';
+            try {
+                const response = await this.getFullAreaCode(areaCode);
+                const codes = response.split(',');
+                
+                // 获取区域树数据
+                const xzqhData = await this.getRegionTreeList(codes[0]);
+                if (!xzqhData) return '/';
+                
+                const names = [];
+                for (const code of codes) {
+                    const node = this.findNodeById(xzqhData, code);
+                    if (node) {
+                        names.push(node.name);
+                    }
+                }
+
+                return names.join('');
+            } catch (error) {
+                console.error('获取区域名称失败:', error);
+                return '/';
+            }
+        },
 
         copyInfo (row, index) {
-            if (import.meta.env.VITE_APP_ENV === 'development') {
-                // alert('地址'+import.meta.env.VITE_APP_ENV);
-                let params = `
-            host: tcp://139.196.74.78:1883
-            账号:drone  
-            密码:123456  
-            组织id:${row.bing_id}  
-            设备码:${row.bind_code}  
-            机场名称:${row.workspace_name}   
-            aircrattName:${row.workspace_desc}`
-                $Clipboard({
-                    text: params,
-                }).then(() => {
-                    this.$message.success("复制成功")
-                }).catch(() => {
-                    this.$message.error("复制失败")
-                })
-            } else {
-                // alert('地址'+import.meta.env.VITE_APP_ENV);
-                let params = `
-            host: tcp://101.132.85.201:1883 
-            账号:drone  
+          const ipAddress = import.meta.env.VITE_APP_DEVICE_IP
+          let params = `
+            host: tcp://${ipAddress}
+            账号:drone
             密码:drone@123
-            组织id:${row.bing_id}  
-            设备码:${row.bind_code}  
-            机场名称:${row.workspace_name}   
+            组织id:${row.bing_id}
+            设备码:${row.bind_code}
+            机场名称:${row.workspace_name}
             aircrattName:${row.workspace_desc}`
-                $Clipboard({
-                    text: params,
-                }).then(() => {
-                    this.$message.success("复制成功")
-                }).catch(() => {
-                    this.$message.error("复制失败")
-                })
-            }
+          $Clipboard({
+            text: params,
+          }).then(() => {
+            this.$message.success("复制成功")
+          }).catch(() => {
+            this.$message.error("复制失败")
+          })
         },
         init () {
             this.onLoad(this.page)
@@ -277,6 +481,14 @@
         },
 
         rowSave (row, done, loading) {
+            let areaCode = row.area_code
+            if (Array.isArray(areaCode)) {
+                areaCode = areaCode[areaCode.length - 1]
+            } else if (typeof areaCode === 'string' && areaCode.includes(',')) {
+                const codes = areaCode.split(',')
+                areaCode = codes[codes.length - 1]
+            }
+            row.area_code = areaCode || null
             add(row).then(
                 () => {
                     this.onLoad(this.page)
@@ -293,7 +505,11 @@
             )
         },
         rowUpdate (row, index, done, loading) {
-            update(row).then(
+            const submitData = {
+                ...row,
+                area_code: row.area_code.split(',').pop(),
+            }
+            update(submitData).then(
                 () => {
                     this.onLoad(this.page)
                     this.$message({
@@ -364,10 +580,32 @@
                     this.$refs.crud.toggleSelection()
                 })
         },
+        async getFullAreaCode (areaCode) {
+            if (!areaCode) return ''
+
+            const code = areaCode.toString()
+
+            if (code.includes(',')) return code
+
+            if (code.endsWith('0000000000')) {
+                return code
+            } else if (code.endsWith('00000000') && !code.endsWith('0000000000')) {
+                const provinceCode = code.substring(0, 2) + '0000000000'
+                return `${provinceCode},${code}`
+            } else {
+                const provinceCode = code.substring(0, 2) + '0000000000'
+                const cityCode = code.substring(0, 4) + '00000000'
+                return `${provinceCode},${cityCode},${code}`
+            }
+        },
         beforeOpen (done, type) {
             if (['edit', 'view'].includes(type)) {
-                getDetail(this.form.id).then(res => {
-                    this.form = res.data.data
+                getDetail(this.form.id).then(async res => {
+                    const data = res.data.data
+                     this.form = {
+                        ...data,
+                        area_code: await this.getFullAreaCode(data.area_code),
+                     }
                 })
             }
             done()
@@ -382,6 +620,7 @@
             this.onLoad(this.page, this.query)
         },
         onLoad (page, params = {}) {
+            
             const { releaseTimeRange } = this.query
             let values = {
                 ...params,
@@ -395,9 +634,14 @@
                 }
                 values.releaseTimeRange = null
             }
+            console.log(values.area_code,this.query)
             values['type'] = 1
+            if (values.area_code) {
+                values['area_code'] = values.area_code.split(',').pop()
+            }
+            
             this.loading = true
-            getList(page.currentPage, page.pageSize, values).then(res => {
+            getList(page.currentPage, page.pageSize, values).then(async res => {
                 const data = res.data.data
                 this.page.total = data.total
                 // data.records.forEach(e => {
@@ -407,9 +651,15 @@
                 //     }
                 // });
                 this.data = data.records
+                for (const row of this.data) {
+                if (row.area_code) {
+                    row.area_name = await this.fetchAreaName(row.area_code);
+                }
+                }
                 this.loading = false
                 this.selectionClear()
             })
+            
         },
         treeLoad (tree, treeNode, resolve) {
             var params = {
@@ -443,4 +693,4 @@
         border-color: #409eff;
     }
 }
-</style>
\ No newline at end of file
+</style>

--
Gitblit v1.9.3