gaoluyang
7 天以前 6b35989783d91899169f89e21a7d3734d8cadc1d
src/views/qualityManagement/finalInspection/components/formDia.vue
@@ -4,6 +4,7 @@
        v-model="dialogFormVisible"
        :title="operationType === 'add' ? '新增出厂检验' : '编辑出厂检验'"
        width="70%"
            draggable
        @close="closeDia"
    >
      <el-form :model="form" label-width="140px" label-position="top" :rules="rules" ref="formRef">
@@ -31,7 +32,10 @@
        <el-row :gutter="30">
          <el-col :span="12">
            <el-form-item label="单位:" prop="unit">
              <el-input v-model="form.unit" placeholder="请输入" clearable/>
              <el-select v-model="form.unit" placeholder="请选择" clearable style="width: 100%">
                <el-option label="箱" value="箱" />
                <el-option label="提" value="提" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
@@ -51,7 +55,8 @@
          </el-col>
               <el-col :span="12">
                  <el-form-item label="检验员:" prop="checkName">
                     <el-select v-model="form.checkName"               filterable
                     <el-select v-model="form.checkName"
                                     filterable
                                     default-first-option
                                     :reserve-keyword="false" placeholder="请选择" clearable>
                        <el-option v-for="item in userList" :key="item.nickName" :label="item.nickName"
@@ -98,7 +103,7 @@
</template>
<script setup>
import {ref, computed} from "vue";
import {ref, computed, reactive, toRefs, getCurrentInstance} from "vue";
import dayjs from "dayjs";
import useUserStore from "@/store/modules/user.js";
import {getOptions} from "@/api/procurementManagement/procurementLedger.js";
@@ -183,22 +188,26 @@
   let userLists = await userListNoPage();
   userList.value = userLists.data;
   form.value = {}
  getProductOptions();
  if (operationType.value === 'edit') {
    form.value = {...row}
      currentProductId.value = row.productId || 0
      // 加载当前产品下的规格列表,并根据后端返回的 model 反查并设置 modelId,实现规格下拉框反显
      if (currentProductId.value) {
         modelList({ id: currentProductId.value }).then((res) => {
            modelOptions.value = res;
            const target = res.find((item) => item.model === row.model);
            if (target) {
               form.value.modelId = target.id;
            }
         });
      }
      getQualityInspectParamList(row.id)
    currentProductId.value = row.productId || 0
    // 加载当前产品下的规格列表,并根据后端返回的 model 反查并设置 modelId,实现规格下拉框反显
    if (currentProductId.value) {
      modelList({ id: currentProductId.value }).then((res) => {
        modelOptions.value = res;
        const target = res.find((item) => item.model === row.model);
        if (target) {
          form.value.modelId = target.id;
        }
      });
    }
    getQualityInspectParamList(row.id)
  } else {
    // 新增时设置默认值
    form.value.unit = "箱"; // 单位默认为箱
    form.value.checkResult = "合格"; // 检验结果默认为合格
  }
  await getProductOptions();
   // 默认检验员为当前登录人,检测日期默认为当天(空时填充)
   if (currentUserName.value && !form.value.checkName) {
      form.value.checkName = currentUserName.value;
@@ -207,10 +216,52 @@
      form.value.checkTime = getToday();
   }
}
const getProductOptions = () => {
  productTreeList({productName: '质量'}).then((res) => {
    productOptions.value = convertIdToValue(res);
  });
const findNodeByLabel = (nodes, label) => {
  for (let i = 0; i < nodes.length; i++) {
    // 先尝试精确匹配
    if (nodes[i].label === label) {
      return nodes[i].value; // 找到节点,返回该节点的value
    }
    // 如果精确匹配失败,尝试模糊匹配(包含关键词)
    if (nodes[i].label && nodes[i].label.includes(label)) {
      return nodes[i].value;
    }
    if (nodes[i].children && nodes[i].children.length > 0) {
      const foundValue = findNodeByLabel(nodes[i].children, label);
      if (foundValue) {
        return foundValue; // 在子节点中找到,返回该节点的value
      }
    }
  }
  return null; // 没有找到节点,返回null
};
const getProductOptions = async () => {
  const res = await productTreeList({productName: '质量'});
  productOptions.value = convertIdToValue(res);
  // 新增模式下,默认选择"成品检验"
  if (operationType.value === 'add') {
    const finishedProductId = findNodeByLabel(productOptions.value, '成品检验');
    if (finishedProductId) {
      form.value.productId = finishedProductId;
      // 找到对应的节点,使用实际的 label 作为 productName
      const findNode = (nodes, value) => {
        for (let i = 0; i < nodes.length; i++) {
          if (nodes[i].value === value) {
            return nodes[i];
          }
          if (nodes[i].children && nodes[i].children.length > 0) {
            const found = findNode(nodes[i].children, value);
            if (found) return found;
          }
        }
        return null;
      };
      const node = findNode(productOptions.value, finishedProductId);
      form.value.productName = node ? node.label : '成品检验';
      getModels(finishedProductId);
    }
  }
};
const getModels = (value) => {
   currentProductId.value = value
@@ -286,12 +337,18 @@
}
const getList = () => {
   qualityInspectDetailByProductId(currentProductId.value).then(res => {
      tableData.value = res.data;
      tableData.value = res.data.map(item => ({
         ...item,
         testValue: item.testValue ?? 0
      }));
   })
}
const getQualityInspectParamList = (id) => {
   qualityInspectParamInfo(id).then(res => {
      tableData.value = res.data;
      tableData.value = res.data.map(item => ({
         ...item,
         testValue: item.testValue ?? 0
      }));
   })
}
// 关闭弹框