Browse Source

feat: 添加公司名称格式化功能,支持从组织结构中获取顶层公司名

xieyong 3 hours ago
parent
commit
8e200faafc
1 changed files with 46 additions and 0 deletions
  1. 46 0
      src/views/system/user/index.vue

+ 46 - 0
src/views/system/user/index.vue

@@ -203,6 +203,22 @@
             showOverflowTooltip: true,
             showOverflowTooltip: true,
             minWidth: 110
             minWidth: 110
           },
           },
+          {
+            prop: 'groupName',
+            label: '公司',
+            // sortable: 'custom',
+            showOverflowTooltip: true,
+            minWidth: 110,
+            formatter: (row) => {
+              if (!row.groupRoleList || !row.groupRoleList.length) return '';
+              const names = row.groupRoleList.map((item) => {
+                if (item.groupName) return item.groupName;
+                if (item.groupId) return this.companyMap[item.groupId] || '';
+                return '';
+              });
+              return names.filter(Boolean).join('、');
+            }
+          },
           {
           {
             prop: 'name',
             prop: 'name',
             label: '姓名',
             label: '姓名',
@@ -271,6 +287,8 @@
         // 是否显示导入弹窗
         // 是否显示导入弹窗
         showImport: false,
         showImport: false,
         organizationList: [],
         organizationList: [],
+        // 组织 id -> 顶层公司名 的扁平映射,给 table formatter 查公司名用
+        companyMap: {},
         pageSize: this.$store.state.tablePageSize,
         pageSize: this.$store.state.tablePageSize,
         cacheKeyUrl: '763607fc-system-user'
         cacheKeyUrl: '763607fc-system-user'
       };
       };
@@ -294,6 +312,34 @@
               idField: 'id',
               idField: 'id',
               parentIdField: 'parentId'
               parentIdField: 'parentId'
             });
             });
+            // 构建 id -> parentId 的映射,再为每个 id 算出"顶层公司名"
+            const parentMap = {};
+            const idToName = {};
+            _list.forEach((item) => {
+              parentMap[item.id] = item.parentId;
+              idToName[item.id] = item.name;
+            });
+            const findRootName = (id, visited) => {
+              visited = visited || new Set();
+              if (visited.has(id)) return ''; // 防成环
+              visited.add(id);
+              const parentId = parentMap[id];
+              // 顶层判断——parentId 为空 / 0 / 或不在我们这张表里
+              if (
+                parentId == null ||
+                parentId === '' ||
+                parentId === 0 ||
+                !parentMap.hasOwnProperty(parentId)
+              ) {
+                return idToName[id] || '';
+              }
+              return findRootName(parentId, visited);
+            };
+            const companyMap = {};
+            _list.forEach((item) => {
+              companyMap[item.id] = findRootName(item.id);
+            });
+            this.companyMap = companyMap;
           })
           })
           .catch((e) => {
           .catch((e) => {
             this.loading = false;
             this.loading = false;