From e67160b34db938e487ddf152738fe1b4bdc72b9c Mon Sep 17 00:00:00 2001
From: rain <1679827795@qq.com>
Date: Mon, 28 Apr 2025 15:17:12 +0800
Subject: [PATCH] 机构管理省市级行政区划名称回显

---
 src/views/system/dept.vue |   91 +++++++++++++++++++++++++++++----------------
 1 files changed, 58 insertions(+), 33 deletions(-)

diff --git a/src/views/system/dept.vue b/src/views/system/dept.vue
index e2f6e33..589c603 100644
--- a/src/views/system/dept.vue
+++ b/src/views/system/dept.vue
@@ -482,31 +482,39 @@
 		this.selectionList = [];
 		this.$refs.crud.toggleSelection();
 	  },
-	  beforeOpen(done, type) {
-		if (['add', 'edit'].includes(type)) {
-		  this.initData();
-		}
-		if (['edit', 'view'].includes(type)) {
-		  getDept(this.form.id).then((res) => {
-			const areaCode = res.data.data.areaCode;
-			const areaName = res.data.data.areaName;
-			
-			// 生成完整的行政区划代码
-			const fullAreaCode = this.getFullAreaCode(areaCode);
-			
-			this.form = Object.assign({}, res.data.data, {
-			  hasChildren: this.form.hasChildren,
-			  // 使用完整的行政区划代码
-			  areaCode: fullAreaCode,
-			  areaName: areaName || '',
-			});
-		  });
-		} else if (type === 'add') {
-		  this.form.areaCode = '';
-		  this.form.areaName = '';
-		}
-		done();
-	  },
+// 修改 beforeOpen 方法
+beforeOpen(done, type) {
+  if (['add', 'edit'].includes(type)) {
+    this.initData();
+  }
+  if (['edit', 'view'].includes(type)) {
+    getDept(this.form.id).then((res) => {
+      const data = res.data.data;
+      const areaCode = data.areaCode;
+      const areaName = data.areaName;
+      
+      // 生成完整的行政区划代码(考虑不同级别)
+      const fullAreaCode = this.getFullAreaCode(areaCode);
+      
+      this.form = Object.assign({}, data, {
+        hasChildren: this.form.hasChildren,
+        areaCode: fullAreaCode,
+        areaName: areaName || '',
+      });
+
+      // 调试输出
+      console.log('回显数据:', {
+        originalAreaCode: areaCode,
+        fullAreaCode: fullAreaCode,
+        areaName: areaName
+      });
+    });
+  } else if (type === 'add') {
+    this.form.areaCode = '';
+    this.form.areaName = '';
+  }
+  done();
+},
 	  beforeClose(done) {
 		this.parentId = '';
 		const column = this.findObject(this.option.column, 'parentId');
@@ -559,14 +567,31 @@
 		  resolve(res.data.data);
 		});
 	  },
-	  // 新增:获取完整的行政区划代码
-	  getFullAreaCode(countyCode) {
-		if (!countyCode) return '';
-		// 县级代码是12位,省级代码取前2位,市级代码取前4位
-		const provinceCode = countyCode.substring(0, 2) + '0000000000';
-		const cityCode = countyCode.substring(0, 4) + '00000000';
-		return `${provinceCode},${cityCode},${countyCode}`;
-	  },
+// 修改 getFullAreaCode 方法
+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}`;
+  }
+},
 	},
   };
   </script>

--
Gitblit v1.9.3