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 |  602 ++++++++++++++++++++++++++++++++++++++++-------------
 1 files changed, 449 insertions(+), 153 deletions(-)

diff --git a/src/views/device/addDevice.vue b/src/views/device/addDevice.vue
index 224ed67..8b12544 100644
--- a/src/views/device/addDevice.vue
+++ b/src/views/device/addDevice.vue
@@ -1,20 +1,20 @@
 <template>
     <basic-container>
-        <avue-crud :option="option" :table-loading="loading" :data="data" v-model:page="page" ref="crud" @row-del="rowDel"
-            v-model="form" :permission="permissionList" @row-update="rowUpdate" @row-save="rowSave"
+        <avue-crud :option="option" :table-loading="loading" :data="data" v-model:page="page" ref="crud"
+            @row-del="rowDel" v-model="form" :permission="permissionList" @row-update="rowUpdate" @row-save="rowSave"
             :before-open="beforeOpen" @search-change="searchChange" @search-reset="searchReset"
             @selection-change="selectionChange" @current-change="currentChange" @size-change="sizeChange"
             @refresh-change="refreshChange" @on-load="onLoad">
 
             <template #menu="scope">
-                <el-button type="primary" text icon="el-icon-copy-document" @click.stop="copyInfo(scope.row, scope.index)"
-                    v-if="userInfo.role_name.includes('admin')">复制
+                <el-button type="primary" text icon="el-icon-copy-document"
+                    @click.stop="copyInfo(scope.row, scope.index)" v-if="userInfo.role_name.includes('admin')">复制
                 </el-button>
             </template>
         </avue-crud>
     </basic-container>
 </template>
-  
+
 <script>
 import {
     getList,
@@ -22,21 +22,25 @@
     update,
     add,
     getDetail,
-} from '@/api/device/workspaces';
+} from '@/api/device/workspaces'
 
-import { getLazyTree } from '@/api/base/region';
+import { getList as getSystemNames } from '@/api/system/sysConfig'
+import { getRegionTreeAll } from '@/api/job/task';
 
-import { mapGetters } from 'vuex';
-import { getWebsocketUrl } from '@/websocket/util/config';
-import ConnectWebSocket from '@/websocket';
-import { $Clipboard } from "@smallwei/avue";
+import { getLazyTree } from '@/api/base/region'
+
+import { mapGetters } from 'vuex'
+import { $Clipboard } from "@smallwei/avue"
+
 
 export default {
     components: {
     },
 
-    data() {
+    data () {
         return {
+            xzqhData: [],
+            systemNamesList: [],
             // 用于记录已经创建的websocket,防止重复创建
             webSocketIdSet: new Set(),
             // 用于保存多组websocket
@@ -61,93 +65,110 @@
             },
             selectionList: [],
             option: {
+                addTitle: '设备注册',
                 menuWidth: 300,
                 lazy: true,
                 tree: true,
-                // height: '460',
-                // calcHeight: 32,
-                // 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,
+
+                height: 'auto',
+                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,
@@ -157,148 +178,390 @@
                         ],
                     },
                     {
+                        label: '设备类型',
+                        editDisplay: false,
+                        prop: 'type',
+                        type: 'radio',
+                        labelWidth: 130,
+                        value: 0,
+                        type: 'select',
+                        dicData: [
+                            {
+                                label: '机场',
+                                value: 0
+                            },
+                            {
+                                label: '遥控器',
+                                value: 1
+                            }
+                        ],
+                        rules: [
+                            {
+                                required: true,
+                                message: '请选择设备类型',
+                                trigger: 'change',
+                            },
+                        ],
+                    },
+                    {
+                        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',
 
                     },
+                    
 
                 ],
             },
             data: [],
 
-        };
+        }
     },
     computed: {
         ...mapGetters(['userInfo', 'permission']),
-        permissionList() {
+        permissionList () {
             return {
                 addBtn: this.validData(this.permission.airport_add, false),
                 viewBtn: this.validData(this.permission.airport_view, true),
                 delBtn: this.validData(this.permission.airport_delete, false),
                 editBtn: this.validData(this.permission.airport_edit, false),
-            };
+            }
         },
-        ids() {
-            let ids = [];
+        ids () {
+            let ids = []
             this.selectionList.forEach(ele => {
-                ids.push(ele.id);
-            });
-            return ids.join(',');
+                ids.push(ele.id)
+            })
+            return ids.join(',')
+        },
+        systemNamesDicData() {
+            return this.systemNamesList.map(item => ({
+                label: item.name,
+                value: item.id,
+            }));
         },
     },
-    destroyed() {
+    destroyed () {
         this.websocketMap.forEach((k, v) => {
             if (null != v) {
                 v.close()
                 this.websocketMap.delete(k)
                 this.webSocketIdSet.delete(k)
             }
-        });
+        })
+    },
+    async created() {
+        await this.getRegionTreeList();
+        await this.getSystemNamesList();
     },
     methods: {
-
-        copyInfo(row, index) {
-            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("复制失败");
-            });
+        async getRegionTreeList(code) {
+            try {
+                const res = await getRegionTreeAll({ parentCode: code });
+                return res.data.data; // 返回数据而不是赋值给 this.xzqhData
+            } catch (error) {
+                console.error('获取区域树数据失败:', error);
+                return null;
+            }
         },
-        init() {
-            this.onLoad(this.page);
+        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 '/';
+            }
         },
 
-        handleOpenRemoteDebugging(row) {
-            this.curDeviceInfo = row;
-            this.remoteDebuggingShow = true;
+        copyInfo (row, index) {
+          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}
+            aircrattName:${row.workspace_desc}`
+          $Clipboard({
+            text: params,
+          }).then(() => {
+            this.$message.success("复制成功")
+          }).catch(() => {
+            this.$message.error("复制失败")
+          })
+        },
+        init () {
+            this.onLoad(this.page)
         },
 
-        rowSave(row, done, loading) {
+        handleOpenRemoteDebugging (row) {
+            this.curDeviceInfo = row
+            this.remoteDebuggingShow = true
+        },
+
+        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);
+                    this.onLoad(this.page)
                     this.$message({
                         type: 'success',
                         message: '操作成功!',
-                    });
-                    done();
+                    })
+                    done()
                 },
                 error => {
-                    window.console.log(error);
-                    loading();
+                    window.console.log(error)
+                    loading()
                 }
-            );
+            )
         },
-        rowUpdate(row, index, done, loading) {
-            update(row).then(
+        rowUpdate (row, index, done, loading) {
+            const submitData = {
+                ...row,
+                area_code: row.area_code.split(',').pop(),
+            }
+            update(submitData).then(
                 () => {
-                    this.onLoad(this.page);
+                    this.onLoad(this.page)
                     this.$message({
                         type: 'success',
                         message: '操作成功!',
-                    });
-                    done();
+                    })
+                    done()
                 },
                 error => {
-                    window.console.log(error);
-                    loading();
+                    window.console.log(error)
+                    loading()
                 }
-            );
+            )
         },
-        rowDel(row) {
+        rowDel (row) {
             this.$confirm('确定将选择数据删除?', {
                 confirmButtonText: '确定',
                 cancelButtonText: '取消',
                 type: 'warning',
             })
                 .then(() => {
-                    return remove(row.id);
+                    return remove(row.id)
                 })
                 .then(() => {
-                    this.onLoad(this.page);
+                    this.onLoad(this.page)
                     this.$message({
                         type: 'success',
                         message: '操作成功!',
-                    });
-                });
+                    })
+                })
         },
-        searchReset() {
-            this.query = {};
-            this.onLoad(this.page);
+        searchReset () {
+            this.query = {}
+            this.onLoad(this.page)
         },
-        searchChange(params, done) {
-            this.query = params;
-            this.page.currentPage = 1;
-            this.onLoad(this.page, params);
-            done();
+        searchChange (params, done) {
+            this.query = params
+            this.page.currentPage = 1
+            this.onLoad(this.page, params)
+            done()
         },
-        selectionChange(list) {
-            this.selectionList = list;
+        selectionChange (list) {
+            this.selectionList = list
         },
-        selectionClear() {
-            this.selectionList = [];
-            this.$refs.crud.toggleSelection();
+        selectionClear () {
+            this.selectionList = []
+            this.$refs.crud.toggleSelection()
         },
-        handleDelete() {
+        handleDelete () {
             if (this.selectionList.length === 0) {
-                this.$message.warning('请选择至少一条数据');
-                return;
+                this.$message.warning('请选择至少一条数据')
+                return
             }
             this.$confirm('确定将选择数据删除?', {
                 confirmButtonText: '确定',
@@ -306,77 +569,111 @@
                 type: 'warning',
             })
                 .then(() => {
-                    return remove(this.ids);
+                    return remove(this.ids)
                 })
                 .then(() => {
-                    this.onLoad(this.page);
+                    this.onLoad(this.page)
                     this.$message({
                         type: 'success',
                         message: '操作成功!',
-                    });
-                    this.$refs.crud.toggleSelection();
-                });
+                    })
+                    this.$refs.crud.toggleSelection()
+                })
         },
-        beforeOpen(done, type) {
-            if (['edit', 'view'].includes(type)) {
-                getDetail(this.form.id).then(res => {
-                    this.form = res.data.data;
-                });
+        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}`
             }
-            done();
         },
-        currentChange(currentPage) {
-            this.page.currentPage = currentPage;
+        beforeOpen (done, type) {
+            if (['edit', 'view'].includes(type)) {
+                getDetail(this.form.id).then(async res => {
+                    const data = res.data.data
+                     this.form = {
+                        ...data,
+                        area_code: await this.getFullAreaCode(data.area_code),
+                     }
+                })
+            }
+            done()
         },
-        sizeChange(pageSize) {
-            this.page.pageSize = pageSize;
+        currentChange (currentPage) {
+            this.page.currentPage = currentPage
         },
-        refreshChange() {
-            this.onLoad(this.page, this.query);
+        sizeChange (pageSize) {
+            this.page.pageSize = pageSize
         },
-        onLoad(page, params = {}) {
-            const { releaseTimeRange } = this.query;
+        refreshChange () {
+            this.onLoad(this.page, this.query)
+        },
+        onLoad (page, params = {}) {
+            
+            const { releaseTimeRange } = this.query
             let values = {
                 ...params,
                 ...this.query,
-            };
+            }
             if (releaseTimeRange) {
                 values = {
                     ...values,
                     releaseTime_datege: releaseTimeRange[0],
                     releaseTime_datelt: releaseTimeRange[1],
-                };
-                values.releaseTimeRange = null;
+                }
+                values.releaseTimeRange = null
             }
-            values['type'] = 1;
-            this.loading = true;
-            getList(page.currentPage, page.pageSize, values).then(res => {
-                const data = res.data.data;
-                this.page.total = data.total;
+            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(async res => {
+                const data = res.data.data
+                this.page.total = data.total
                 // data.records.forEach(e => {
                 //     e['hasChildren'] = e.has_children;
                 //     if (e.firmware_status == 4) {
                 //         this.connectWebSocket(e);
                 //     }
                 // });
-                this.data = data.records;
-                this.loading = false;
-                this.selectionClear();
-            });
+                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) {
+        treeLoad (tree, treeNode, resolve) {
             var params = {
                 childSn: tree.child_sn,
-            };
+            }
             getList(1, 10, params).then(res => {
-                const data = res.data.data.records;
-                resolve(data);
-            });
+                const data = res.data.data.records
+                resolve(data)
+            })
         },
     },
-};
+}
 </script>
-  
+
 <style lang="scss" scoped>
 .firmware_status {
     background-color: aqua;
@@ -397,4 +694,3 @@
     }
 }
 </style>
-  
\ No newline at end of file

--
Gitblit v1.9.3