spring
2026-07-31 a8cc9d60a80afd7362e1d0fd31fb4307a6e04e2e
src/pages/production/twist/receive/steelCore/form.vue
@@ -21,7 +21,7 @@
      />
      <wd-input
        v-model="model.monofilamentNumber"
        label="样品编号"
        label="盘号"
        label-width="100px"
        prop="monofilamentNumber"
        clearable
@@ -33,7 +33,7 @@
        label-width="100px"
        prop="amount"
        clearable
        placeholder="请输入长度"
        placeholder="请输入数量"
      />
      <wd-input
        v-model="model.weight"
@@ -60,7 +60,6 @@
<script lang="ts" setup>
import { onMounted, watch } from "vue";
import useFormData from "@/hooks/useFormData";
import TwistApi from "@/api/product/twist";
import ManageApi from "@/api/product/manage";
import { useToast } from "wot-design-uni";
@@ -80,14 +79,11 @@
});
const emits = defineEmits(["refresh"]);
const paramsId = ref();
const editId = ref(); // 编辑时的ID
const allListData = ref<any[]>([]); // 存储完整列表数据
const toast = useToast();
const { form: model } = useFormData({
  diskMaterial: undefined, // 芯线类型
  model: undefined, // 规格型号
  monofilamentNumber: undefined, // 样品编号
  monofilamentNumber: undefined, // 盘号
  amount: undefined, // 数量
  weight: undefined, // 重量
  supplier: undefined, // 厂家
@@ -150,105 +146,28 @@
  { immediate: true }
);
// 新增提交
// 收集表单数据(不调 API,返回给父组件处理)
const collectData = () => {
  return {
    diskMaterial: model.diskMaterial,
    model: model.model,
    monofilamentNumber: model.monofilamentNumber,
    amount: model.amount,
    weight: model.weight,
    supplier: model.supplier,
    type: model.type,
  };
};
// 新增提交(兼容旧模式,父组件可调用)
const submit = async () => {
  const currentWireId = props.wireId || paramsId.value;
  const { code } = await TwistApi.addStrandedWireDish([
    {
      wireId: currentWireId,
      ...model,
    },
  ]);
  if (code == 200) {
    toast.success("新增成功");
    emits("refresh");
    return true;
  }
  return false;
  return collectData();
};
// 编辑提交(也走新增接口,提交整个列表)
// 编辑提交(兼容旧模式,不调API,返回表单数据)
const submitEdit = async (list?: any[], id?: number) => {
  const currentList = list || allListData.value;
  const currentId = id || editId.value;
  if (!currentId) {
    toast.error("缺少记录ID");
    return false;
  }
  // 更新列表中对应的数据项
  const updatedList = currentList.map((item) => {
    if (item.id === currentId) {
      // 保留原有数据,然后更新修改的字段
      const updatedItem = {
        ...item, // 先保留原有的所有数据
        diskMaterial: model.diskMaterial,
        model: model.model,
        monofilamentNumber: model.monofilamentNumber,
        amount: model.amount,
        weight: model.weight,
        supplier: model.supplier,
        type: model.type,
      };
      return updatedItem;
    }
    return item;
  });
  // 提交整个列表
  const { code } = await TwistApi.addStrandedWireDish(updatedList);
  if (code == 200) {
    toast.success("更新成功");
    return true;
  }
  return false;
  return collectData();
};
// 设置表单数据(用于编辑时回显)
const setFormData = (list: any[], currentEditId: number) => {
  // 安全检查:确保list是数组
  if (!Array.isArray(list)) {
    return;
  }
  // 存储完整列表数据
  allListData.value = list;
  editId.value = currentEditId;
  // 找到当前编辑项并回显到表单
  const currentItem = list.find((item) => item.id === currentEditId);
  if (currentItem) {
    model.diskMaterial = currentItem.diskMaterial;
    model.model = currentItem.model;
    model.monofilamentNumber = currentItem.monofilamentNumber;
    model.amount = currentItem.amount;
    model.weight = currentItem.weight;
    model.supplier = currentItem.supplier;
    model.type = currentItem.type || "钢芯";
    // 设置芯线类型的回显值
    diskMaterialValue.value = currentItem.diskMaterial || "";
  }
};
// 监听编辑数据变化,自动回显
watch(
  () => props.editData,
  (newData) => {
    if (newData && props.mode === "edit") {
      model.diskMaterial = newData.diskMaterial || "";
      model.model = newData.model || "";
      model.monofilamentNumber = newData.monofilamentNumber || "";
      model.amount = newData.amount || "";
      model.weight = newData.weight || "";
      model.supplier = newData.supplier || "";
      model.type = newData.type || "钢芯";
      diskMaterialValue.value = newData.diskMaterial || "";
    }
  },
  { immediate: true, deep: true }
);
// 重置表单数据
const resetFormData = () => {
@@ -268,7 +187,7 @@
    model.diskMaterial = data.diskMaterial || "";
    model.model = data.model || "";
    model.monofilamentNumber = data.monofilamentNumber || "";
    model.amount = data.oneLength || data.amount || "";
    model.amount = data.amount || data.oneLength || "";
    model.weight = data.weight || "";
    model.supplier = data.supplier || "";
    model.type = data.type || "钢芯";
@@ -276,9 +195,30 @@
  }
};
onLoad((options: any) => {
  paramsId.value = options.id;
});
// 设置表单数据(用于编辑时回显,兼容 edit.vue)
const setFormData = (item: any) => {
  if (item) {
    model.diskMaterial = item.diskMaterial || "";
    model.model = item.model || "";
    model.monofilamentNumber = item.monofilamentNumber || "";
    model.amount = item.amount || "";
    model.weight = item.weight || "";
    model.supplier = item.supplier || "";
    model.type = item.type || "钢芯";
    diskMaterialValue.value = item.diskMaterial || "";
  }
};
// 监听编辑数据变化,自动回显
watch(
  () => props.editData,
  (newData) => {
    if (newData && props.mode === "edit") {
      setFormData(newData);
    }
  },
  { immediate: true, deep: true }
);
onMounted(async () => {
  await loadDiskMaterialDict();
@@ -288,6 +228,7 @@
defineExpose({
  submit,
  submitEdit,
  collectData,
  setFormData,
  resetFormData,
  fillFormData,