yyb
2026-04-29 6e39775c07de5b9051cef1626a6e975b3a26ebfa
更新生产报告表单,以动态处理报告状态并相应调整输入字段
已修改1个文件
121 ■■■■ 文件已修改
src/pages/productionManagement/productionReport/index.vue 121 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/pages/productionManagement/productionReport/index.vue
@@ -1,7 +1,7 @@
<template>
  <view class="invoice-add">
    <!-- 使用通用页面头部组件 -->
    <PageHeader title="生产报工"
    <PageHeader :title="pageTitle"
                @back="goBack" />
    <!-- 表单内容 -->
    <u-form @submit="submitForm"
@@ -18,14 +18,16 @@
                   placeholder="自动填充"
                   disabled />
        </u-form-item>
        <u-form-item label="本次生产数量"
        <u-form-item v-if="!isStartReport"
                     label="本次生产数量"
                     prop="quantity"
                     required>
          <u-input v-model="form.quantity"
                   placeholder="请输入"
                   type="number" />
        </u-form-item>
        <u-form-item label="报废数量"
        <u-form-item v-if="!isStartReport"
                     label="报废数量"
                     prop="scrapQty">
          <u-input v-model="form.scrapQty"
                   placeholder="请输入"
@@ -58,7 +60,7 @@
</template>
<script setup>
  import { ref, nextTick } from "vue";
  import { computed, ref, nextTick } from "vue";
  import { onLoad } from "@dcloudio/uni-app";
  import FooterButtons from "@/components/FooterButtons.vue";
@@ -68,7 +70,7 @@
      icon: "none",
    });
  };
  import { addProductMain } from "@/api/productionManagement/productionReporting";
  import { addProductMain ,getReportState} from "@/api/productionManagement/productionReporting";
  import { getInfo } from "@/api/login";
  import { userListNoPageByTenantId } from "@/api/system/user";
@@ -85,6 +87,20 @@
    productProcessRouteItemId: "",
    userId: "",
    schedulingUserId: "",
  });
  // 报工状态(来自 reportState 接口)
  const reportState = ref({});
  // 开始报工:隐藏“本次生产数量/报废数量”
  const isStartReport = computed(() => Number(reportState.value?.state) === 1);
  const pageTitle = computed(() => {
    const state = Number(reportState.value?.state);
    if (state === 1) return "开始报工";
    if (state === 2) return "结束报工";
    if (state === 3) return "报工完成";
    return "生产报工";
  });
  // 生产人选择器状态
@@ -130,38 +146,47 @@
  // 提交表单
  const submitForm = async () => {
    submitting.value = true;
    // 校验表单
    if (!form.value.quantity) {
      submitting.value = false;
      showToast("请输入本次生产数量");
      return;
    }
    const planQuantity = Number(form.value.planQuantity) || 0;
    if (!form.value.schedulingUserId) {
      submitting.value = false;
      showToast("请选择生产人");
      return;
    }
    // 转换为数字进行比较
    const quantity = Number(form.value.quantity) || 0;
    const scrapQty = Number(form.value.scrapQty) || 0;
    const planQuantity = Number(form.value.planQuantity);
    // 验证生产数量和报废数量的和不能超过待生产数量
    if (quantity + scrapQty > planQuantity) {
      submitting.value = false;
      showToast("生产数量和报废数量的和不能超过待生产数量");
      return;
    // 开始报工:不需要填写数量/报废数量
    let quantity = 0;
    let scrapQty = 0;
    if (!isStartReport.value) {
      // 校验表单
      if (!form.value.quantity) {
        submitting.value = false;
        showToast("请输入本次生产数量");
        return;
      }
      // 转换为数字进行比较
      quantity = Number(form.value.quantity) || 0;
      scrapQty = Number(form.value.scrapQty) || 0;
      // 验证生产数量和报废数量的和不能超过待生产数量
      if (quantity + scrapQty > planQuantity) {
        submitting.value = false;
        showToast("生产数量和报废数量的和不能超过待生产数量");
        return;
      }
      if (quantity > planQuantity) {
        submitting.value = false;
        showToast("本次生产数量不能大于待生产数量");
        return;
      }
    }
    if (quantity > planQuantity) {
      submitting.value = false;
      showToast("本次生产数量不能大于待生产数量");
      return;
    }
    // 准备提交数据,确保数量字段为数字类型
    const submitData = {
      ...form.value,
      quantity: Number(form.value.quantity),
      scrapQty: Number(form.value.scrapQty) || 0,
      planQuantity: Number(form.value.planQuantity) || 0,
      quantity,
      scrapQty,
      planQuantity,
    };
    console.log(submitData, "submitData");
@@ -188,25 +213,57 @@
      getInfo().then(res => {
        // 默认使用当前登录用户
        form.value.userId = res.user.userId;
        form.value.userName = res.user.userName;
        // 回显展示名称优先使用 nickName;若后端不返回则兜底用 userName
        // 避免使用可能为 id 的 userName 字段导致“回显的是 id”
        form.value.userName = res.user.nickName || res.user.userName;
        form.value.schedulingUserId = res.user.userId;
      });
      return;
    }
    try {
      const orderRow = JSON.parse(options.orderRow);
      // options.orderRow 来自路由参数,可能是 encodeURIComponent 后的字符串
      const orderRowRaw =
        typeof options.orderRow === "string" ? options.orderRow : String(options.orderRow);
      let orderRowStr = orderRowRaw;
      try {
        orderRowStr = decodeURIComponent(orderRowRaw);
      } catch (e) {
        // 若已经是未编码的 JSON,则无需解码
      }
      const orderRow = JSON.parse(orderRowStr);
      console.log("构造的orderRow:", orderRow);
      console.log(orderRow, "orderRow======########");
      // 确保 planQuantity 转换为字符串,以便在 u-input 中正确显示
      form.value.planQuantity = orderRow.planQuantity != null ? String(orderRow.planQuantity) : "";
      form.value.productProcessRouteItemId = orderRow.productProcessRouteItemId || "";
      form.value.workOrderId = orderRow.id || "";
      form.value.workOrderId = orderRow.workOrderId || "";
      // 若扫码结果已携带 todayReportState,则先本地渲染标题/显隐
      if (orderRow.todayReportState != null) {
        reportState.value = { state: Number(orderRow.todayReportState) };
      }
      getInfo().then(res => {
        // 默认使用当前登录用户,但允许用户修改
        form.value.userId = res.user.userId;
        form.value.userName = res.user.userName;
        // 回显展示名称优先使用 nickName;若后端不返回则兜底用 userName
        form.value.userName = res.user.nickName || res.user.userName;
        form.value.schedulingUserId = res.user.userId;
      });
      // 根据扫码传入的 orderRow 获取报工状态
      getReportState(orderRow)
        .then(res => {
          if (res.code === 200) {
            reportState.value = res.data || {};
          } else {
            // 状态获取失败时,不影响页面正常录入(兜底显示输入项)
            console.warn("getReportState 返回非200:", res);
          }
        })
        .catch(err => {
          console.error("获取报工状态失败:", err);
        });
      // 使用 nextTick 确保 DOM 更新
      nextTick(() => {
        console.log("form.value after assignment:", form.value);