无人机管理后台前端(已迁走)
rjg
2025-04-14 9e7b9c04609ba8595ea45eea732b312fcb387151
src/views/system/dept.vue
@@ -48,7 +48,7 @@
      </template>
      <!-- 自定义 areaCode 的显示模板,确保显示单一值 -->
      <template #areaCode="{ row }">
        {{ 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({
@@ -492,18 +488,21 @@
      if (['edit', 'view'].includes(type)) {
        getDept(this.form.id).then((res) => {
         const areaCode = res.data.data.areaCode;
         let finalareaCode = areaCode;
         if (typeof areaCode === 'string' && areaCode.includes(',')) {
           const codes = areaCode.split(',');
           finalareaCode = codes[codes.length - 1]; // 取最后一个值
         }
         const areaName = res.data.data.areaName;
         // 生成完整的行政区划代码
         const fullAreaCode = this.getFullAreaCode(areaCode);
         this.form = Object.assign({}, res.data.data, {
           hasChildren: this.form.hasChildren,
           areaCode: finalareaCode ? String(finalareaCode) : null // 确保是字符串
           // 使用完整的行政区划代码
           areaCode: fullAreaCode,
           areaName: areaName || '',
         });
        });
      } else if (type === 'add') {
        this.form.areaCode = null;
        this.form.areaCode = '';
        this.form.areaName = '';
      }
      done();
     },
@@ -534,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;
@@ -565,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>