spring
2025-11-19 af4f45eaa2703ecf991bd10f07f6df179f2677d9
src/pages/production/twist/report/form.vue
@@ -2,103 +2,139 @@
  <wd-form ref="form" :model="model" class="relative form_box">
    <wd-cell-group :border="true">
      <wd-input
        v-model="model.contractNo"
        label="领用杆号"
        label-width="100px"
        prop="contractNo"
        clearable
        placeholder="请输入领用杆号"
      />
      <wd-input
        v-model="model.status"
        label="杆重(kg)"
        label-width="100px"
        prop="status"
        clearable
        placeholder="请输入杆重"
      />
      <wd-input
        v-model="model.clientName"
        label="单丝盘号"
        label-width="100px"
        prop="clientName"
        clearable
        placeholder="请输入单丝盘号"
      />
      <wd-input
        v-model="model.workbench"
        label="实际重量(kg)"
        label-width="100px"
        prop="workbench"
        clearable
        placeholder="请输入实际重量"
      />
      <wd-input
        v-model="model.quality"
        label="盘长(m)"
        label-width="100px"
        prop="quality"
        clearable
        placeholder="请输入盘长"
      />
      <wd-input
        v-model="model.specification"
        label="理论重量(kg)"
        label-width="100px"
        prop="specification"
        clearable
        placeholder="请输入理论重量"
      />
      <wd-input
        v-model="model.disc"
        label="规格型号"
        label-width="100px"
        prop="disc"
        clearable
        placeholder="请输入规格型号"
      />
      <wd-input
        v-model="model.actuallyLength"
        label="实际盘长(m)"
        label="生产长度(m)"
        label-width="100px"
        prop="actuallyLength"
        clearable
        placeholder="请输入实际盘长"
      />
        placeholder="请输入生产长度"
        type="digit"
      >
        <template #label>
          <span style="color: #f56c6c">生产长度(m)</span>
        </template>
      </wd-input>
      <wd-input
        v-model="model.tare"
        label="盘具皮重(kg)"
        label-width="100px"
        prop="tare"
        :disabled="!isFirstReport"
        :clearable="isFirstReport"
        :placeholder="isFirstReport ? '请输入盘具皮重' : '盘具皮重自动带出'"
        type="digit"
      >
        <template #label>
          <span style="color: #f56c6c">盘具皮重(kg)</span>
        </template>
      </wd-input>
    </wd-cell-group>
  </wd-form>
</template>
<script lang="ts" setup>
import { computed, watch } from "vue";
import useFormData from "@/hooks/useFormData";
import { useToast } from "wot-design-uni";
import { useToast, dayjs } from "wot-design-uni";
import TwistApi from "@/api/product/twist";
// 定义 props
const props = defineProps<{
  firstTareValue?: number;
  teamId?: string | number | null;
  isFirstReport?: boolean; // 是否是第一条报工
}>();
// 计算是否是第一条报工
const isFirstReport = computed(() => props.isFirstReport ?? true);
const paramsId = ref();
const toast = useToast();
const { form: model } = useFormData({
  poleNumber: undefined, // 领用杆号
  poleWeight: undefined, // 杆重(kg)
  monofilamentNumber: undefined, // 单丝盘号
  actuallyWeight: undefined, // 实际重量(kg)
  oneLength: undefined, // 盘长(m)
  theoryWeight: undefined, // 理论重量(kg)
  model: undefined, // 规格型号
  actuallyLength: undefined, // 实际盘长(m)
const { form: model, resetForm } = useFormData({
  actuallyLength: undefined, // 生产长度(m)
  tare: undefined, // 盘具皮重(kg)
});
// 主表数据(从父组件传入)
const mainTableData = ref<any>({});
// 设置主表数据
const setMainTableData = (data: any) => {
  mainTableData.value = data;
};
// 监听 firstTareValue 变化,如果不是第一条,自动填充
watch(
  () => props.firstTareValue,
  (newVal) => {
    if (!isFirstReport.value && newVal !== undefined) {
      model.tare = newVal;
    }
  },
  { immediate: true }
);
const submit = async () => {
  const { code } = await TwistApi.addTwistOutput({
  // 获取第一条数据的皮重值,用于后续新增的报工
  const firstTareValue = props.firstTareValue;
  // 如果主表数据未获取,尝试重新获取
  if (!mainTableData.value.model) {
    try {
      const { data } = await TwistApi.getTwistDetailById({
        id: paramsId.value,
      });
      mainTableData.value = {
        model: data.model,
        totalLength: data.totalLength,
        systemNo: data.systemNo,
      };
    } catch (error) {
      console.error("获取主表数据失败:", error);
      toast.error("获取规格型号数据失败,请重试");
      return false;
    }
  }
  // 再次检查主表数据
  if (!mainTableData.value.model) {
    toast.error("规格型号数据未获取,请重试");
    return false;
  }
  const submitData = {
    teamId: props.teamId || null,
    wireId: paramsId.value,
    ...model,
  });
    type: "绞线",
    actuallyLength: model.actuallyLength,
    tare: model.tare || firstTareValue,
    // 从主表获取的字段
    model: mainTableData.value.model, // 规格型号
    oneLength: mainTableData.value.totalLength,
    systemNo: mainTableData.value.systemNo,
    monofilamentNumber: undefined, // 批次号(后端自动生成)
    // 生产日期自动设置为当前时间
    productTime: dayjs().format("YYYY-MM-DD HH:mm:ss"),
  };
  // 调试日志
  console.log("提交数据:", submitData);
  console.log("主表数据:", mainTableData.value);
  const { code } = await TwistApi.addTwistOutput(submitData);
  if (code == 200) {
    toast.success("提交成功");
    resetForm();
    return true;
  } else {
    toast.error("提交失败");
    return false;
  }
};
// 获取表单数据
const getFormData = () => {
  return { ...model };
};
onLoad((options: any) => {
@@ -107,12 +143,12 @@
defineExpose({
  submit,
  getFormData,
  setMainTableData,
});
</script>
<style lang="scss" scoped>
.form_box {
}
.submit_btn {
  position: absolute;
  bottom: 0;