无人机管理后台前端(已迁走)
rain
2025-04-14 52810073efa553cb733a0d22e603596ed98f67f8
机构管理的查看编辑行政区划回显
1 files modified
58 ■■■■ changed files
src/views/system/dept.vue 58 ●●●● patch | view | raw | blame | history
src/views/system/dept.vue
@@ -48,7 +48,7 @@
        </template>
        <!-- 自定义 areaCode 的显示模板,确保显示单一值 -->
        <template #areaCode="{ row }">
          {{ row.areaName || row.areaCode }}
          <span>{{ row.areaName }}</span>
        </template>
      </avue-crud>
    </basic-container>
@@ -391,19 +391,15 @@
        }
        console.log('--- Start rowUpdate ---');
        
        // 处理 areaCode,确保只取最后一个值
        let areaCode = this.form.areaCode;
        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.areaCode = areaCode || null; // 确保非空
        // 提交时只使用县级代码
        const submitData = {
          ...row,
          areaCode: row.areaCode.split(',').pop() // 取最后一个代码(县级)
        };
        
        console.log('Row data before submit:', row);
        
        update(row).then(
        update(submitData).then(
          (res) => {
            console.log('Update response:', res.data);
            this.$message({
@@ -493,19 +489,19 @@
          getDept(this.form.id).then((res) => {
            const areaCode = res.data.data.areaCode;
            const areaName = res.data.data.areaName;
            let finalAreaCode = areaCode;
            if (typeof areaCode === 'string' && areaCode.includes(',')) {
              const codes = areaCode.split(',');
              finalAreaCode = codes[codes.length - 1]; // 取最后一个值
            }
            // 生成完整的行政区划代码
            const fullAreaCode = this.getFullAreaCode(areaCode);
            this.form = Object.assign({}, res.data.data, {
              hasChildren: this.form.hasChildren,
              areaCode: finalAreaCode ? String(finalAreaCode) : null, // 确保是字符串
              areaName: areaName || '', // 回显区域名称
              // 使用完整的行政区划代码
              areaCode: fullAreaCode,
              areaName: areaName || '',
            });
          });
        } else if (type === 'add') {
          this.form.areaCode = null;
          this.form.areaCode = '';
          this.form.areaName = '';
        }
        done();
@@ -537,19 +533,13 @@
          console.log('onLoad response:', res.data);
          // 处理 areaCode,确保是单一字符串
          const data = (res.data.data || []).map(item => {
            let areaCode = item.areaCode;
            // 如果是数组,取最后一个值
            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];
            }
            const fullAreaCode = this.getFullAreaCode(item.areaCode);
            return {
              ...item,
              areaCode: areaCode ? String(areaCode) : '' // 确保是字符串
              // 保存完整的行政区划代码
              areaCode: fullAreaCode,
              // 使用原始的县级代码作为实际值
              originalAreaCode: item.areaCode
            };
          });
          this.data = data;
@@ -568,6 +558,14 @@
          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}`;
      },
    },
  };
  </script>