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">
@@ -24,16 +25,17 @@
          </el-col>
          <el-col :span="12">
            <el-form-item label="规格型号:" prop="model">
                     <el-select v-model="form.modelId" placeholder="请选择" clearable @change="getProductModel">
                        <el-option v-for="item in modelOptions" :key="item.id" :label="item.model" :value="item.id" />
                     </el-select>
                     <el-input v-model="form.model" placeholder="请输入" clearable/>
            </el-form-item>
          </el-col>
        </el-row>
        <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">
@@ -53,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"
@@ -100,7 +103,9 @@
</template>
<script setup>
import {ref} 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";
import {modelList, productTreeList} from "@/api/basicData/product.js";
import {qualityInspectAdd, qualityInspectUpdate} from "@/api/qualityManagement/rawMaterialInspection.js";
@@ -109,6 +114,9 @@
import {qualityInspectParamInfo} from "@/api/qualityManagement/qualityInspectParam.js";
const { proxy } = getCurrentInstance()
const emit = defineEmits(['close'])
const userStore = useUserStore()
const currentUserName = computed(() => userStore.nickName || userStore.name || "")
const getToday = () => dayjs().format("YYYY-MM-DD")
const dialogFormVisible = ref(false);
const operationType = ref('')
@@ -166,12 +174,7 @@
]);
const tableData = ref([]);
const tableLoading = ref(false);
const userList = ref([
   {nickName: '候盛杰'},
   {nickName: '焦辉'},
   {nickName: '张培'},
   {nickName: '冯灵之'},
]);
const userList = ref([]);
const currentProductId = ref(0);
const modelOptions = ref([]);
@@ -183,29 +186,82 @@
    supplierList.value = res.data;
  });
   let userLists = await userListNoPage();
   // userList.value = userLists.data;
   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;
   }
   if (!form.value.checkTime) {
      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
@@ -281,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
      }));
   })
}
// 关闭弹框