YLouie
2025-10-23 8460eac918d8c8e825a0f78ac0af1c03cad8fd49
src/pages/production/twist/receive/steelCore/form.vue
@@ -2,26 +2,26 @@
  <wd-form ref="form" :model="model" class="relative form_box">
    <wd-cell-group :border="true">
      <wd-input
        v-model="model.steelCoreName"
        label="钢芯名称"
        v-model="model.model"
        label="规格型号"
        label-width="100px"
        prop="steelCoreName"
        prop="model"
        clearable
        placeholder="请输入钢芯名称"
        placeholder="请输入规格型号"
      />
      <wd-input
        v-model="model.plateNo"
        label="盘号"
        v-model="model.monofilamentNumber"
        label="样品编号"
        label-width="100px"
        prop="plateNo"
        prop="monofilamentNumber"
        clearable
        placeholder="请输入盘号"
      />
      <wd-input
        v-model="model.length"
        label="长度"
        v-model="model.amount"
        label="数量"
        label-width="100px"
        prop="length"
        prop="amount"
        clearable
        placeholder="请输入长度"
      />
@@ -34,10 +34,10 @@
        placeholder="请输入重量"
      />
      <wd-input
        v-model="model.manufacturers"
        v-model="model.supplier"
        label="厂家"
        label-width="100px"
        prop="manufacturers"
        prop="supplier"
        clearable
        placeholder="请输入厂家"
      />
@@ -47,19 +47,117 @@
<script lang="ts" setup>
import useFormData from "@/hooks/useFormData";
import TwistApi from "@/api/product/twist";
import { useToast } from "wot-design-uni";
const emits = defineEmits(["refresh"]);
const paramsId = ref();
const editId = ref(); // 编辑时的ID
const allListData = ref<any[]>([]); // 存储完整列表数据
const toast = useToast();
const { form: model } = useFormData({
  steelCoreName: undefined, // 钢芯名称
  plateNo: undefined, // 盘号
  length: undefined, // 长度
  model: undefined, // 规格型号
  monofilamentNumber: undefined, // 样品编号
  amount: undefined, // 数量
  weight: undefined, // 重量
  manufacturers: undefined, // 厂家
  supplier: undefined, // 厂家
  type: "钢芯",
});
// 新增提交
const submit = async () => {
  const { code } = await TwistApi.addStrandedWireDish([
    {
      wireId: paramsId.value,
      ...model,
    },
  ]);
  if (code == 200) {
    toast.success("新增成功");
    emits("refresh");
    return true;
  }
  return false;
};
// 编辑提交(也走新增接口,提交整个列表)
const submitEdit = async () => {
  if (!editId.value) {
    toast.error("缺少记录ID");
    return false;
  }
  console.log("submitEdit - 编辑前的列表:", allListData.value);
  console.log("submitEdit - 当前表单数据:", model);
  console.log("submitEdit - 编辑的ID:", editId.value);
  // 更新列表中对应的数据项
  const updatedList = allListData.value.map((item) => {
    if (item.id === editId.value) {
      // 保留原有数据,然后更新修改的字段
      const updatedItem = {
        ...item, // 先保留原有的所有数据
        model: model.model,
        monofilamentNumber: model.monofilamentNumber,
        amount: model.amount,
        weight: model.weight,
        supplier: model.supplier,
        type: model.type,
      };
      console.log("submitEdit - 更新后的项:", updatedItem);
      return updatedItem;
    }
    return item;
  });
  console.log("submitEdit - 提交的完整列表:", updatedList);
  // 提交整个列表
  const { code } = await TwistApi.addStrandedWireDish(updatedList);
  if (code == 200) {
    toast.success("更新成功");
    return true;
  }
  return false;
};
// 设置表单数据(用于编辑时回显)
const setFormData = (list: any[], currentEditId: number) => {
  // 安全检查:确保list是数组
  if (!Array.isArray(list)) {
    console.error("setFormData: list 参数不是数组", list);
    return;
  }
  // 存储完整列表数据
  allListData.value = list;
  editId.value = currentEditId;
  // 找到当前编辑项并回显到表单
  const currentItem = list.find((item) => item.id === currentEditId);
  if (currentItem) {
    model.model = currentItem.model;
    model.monofilamentNumber = currentItem.monofilamentNumber;
    model.amount = currentItem.amount;
    model.weight = currentItem.weight;
    model.supplier = currentItem.supplier;
    model.type = currentItem.type || "钢芯";
  }
};
onLoad((options: any) => {
  paramsId.value = options.id;
});
defineExpose({
  submit,
  submitEdit,
  setFormData,
});
</script>
<style lang="scss" scoped>
.form_box {
}
.submit_btn {
  position: absolute;
  bottom: 0;