1
yyb
2026-04-29 aec5cbead319feabb2e44ddd5bf99a0af01ca506
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
<template>
  <view class="invoice-add">
    <!-- 使用通用页面头部组件 -->
    <PageHeader :title="pageTitle"
                @back="goBack" />
    <!-- 表单内容 -->
    <u-form @submit="submitForm"
            ref="formRef"
            label-width="110"
            input-align="right"
            error-message-align="right">
      <!-- 基本信息 -->
      <view class="form-section">
        <u-form-item label="待生产数量"
                     prop="planQuantity"
                     required>
          <u-input v-model="form.planQuantity"
                   placeholder="自动填充"
                   disabled />
        </u-form-item>
        <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 v-if="!isStartReport"
                     label="报废数量"
                     prop="scrapQty">
          <u-input v-model="form.scrapQty"
                   placeholder="请输入"
                   type="number" />
        </u-form-item>
        <u-form-item label="班组信息"
                     prop="schedulingUserId"
                     required>
          <u-input v-model="form.userName"
                   placeholder="请选择生产人"
                   readonly
                   @click="openProducerPicker"
                   suffix-icon="arrow-down" />
        </u-form-item>
      </view>
      <!-- 使用FooterButtons组件 -->
      <FooterButtons @cancel="goBack"
                     @confirm="submitForm"
                     :loading="submitting" />
      <!-- 为底部按钮留出空间 -->
      <view style="height: 80px;"></view>
    </u-form>
    <!-- 生产人选择器 -->
    <up-action-sheet :show="showProducerPicker"
                     :actions="producerList"
                     title="选择生产人"
                     @select="onProducerConfirm"
                     @close="showProducerPicker = false" />
  </view>
</template>
 
<script setup>
  import { computed, ref, nextTick } from "vue";
  import { onLoad } from "@dcloudio/uni-app";
  import FooterButtons from "@/components/FooterButtons.vue";
 
  const showToast = message => {
    uni.showToast({
      title: message,
      icon: "none",
    });
  };
  import { addProductMain ,getReportState} from "@/api/productionManagement/productionReporting";
  import { getInfo } from "@/api/login";
  import { userListNoPageByTenantId } from "@/api/system/user";
 
  // 表单引用
  const formRef = ref();
 
  // 表单数据
  let form = ref({
    planQuantity: "",
    quantity: "",
    scrapQty: "",
    userName: "",
    workOrderId: "",
    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 "生产报工";
  });
 
  // 生产人选择器状态
  const showProducerPicker = ref(false);
  const producerList = ref([]);
 
  // 打开生产人选择器
  const openProducerPicker = async () => {
    if (producerList.value.length === 0) {
      // 如果列表为空,先加载用户列表
      try {
        const res = await userListNoPageByTenantId();
        const users = res.data || [];
        // 转换为 action-sheet 需要的格式
        producerList.value = users.map(user => ({
          name: user.nickName || user.userName,
          value: user.userId,
        }));
      } catch (error) {
        console.error("加载用户列表失败:", error);
        showToast("加载用户列表失败");
        return;
      }
    }
    showProducerPicker.value = true;
  };
 
  // 生产人选择确认
  const onProducerConfirm = e => {
    form.value.schedulingUserId = e.value;
    form.value.userName = e.name;
    form.value.userId = e.value; // 同时更新 userId
    showProducerPicker.value = false;
  };
 
  // 提交状态
  const submitting = ref(false);
 
  // 返回上一页
  const goBack = () => {
    uni.navigateBack();
  };
  // 提交表单
  const submitForm = async () => {
    submitting.value = true;
    const planQuantity = Number(form.value.planQuantity) || 0;
    if (!form.value.schedulingUserId) {
      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;
      }
    }
 
    // 准备提交数据,确保数量字段为数字类型
    const submitData = {
      ...form.value,
      quantity,
      scrapQty,
      planQuantity,
    };
    console.log(submitData, "submitData");
 
    addProductMain(submitData).then(res => {
      if (res.code === 200) {
        showToast("报工成功");
        submitting.value = false;
        setTimeout(() => {
          goBack();
        }, 1000);
      } else {
        showToast(res.msg || "报工失败");
        submitting.value = false;
      }
    });
  };
 
  // 页面加载时初始化数据
  onLoad(options => {
    console.log(options, "options");
    // 如果没有 orderRow 参数,说明是从首页直接跳转,需要用户手动选择订单
    if (!options.orderRow) {
      console.log("从首页跳转,无订单数据");
      getInfo().then(res => {
        // 默认使用当前登录用户
        form.value.userId = res.user.userId;
        // 回显展示名称优先使用 nickName;若后端不返回则兜底用 userName
        // 避免使用可能为 id 的 userName 字段导致“回显的是 id”
        form.value.userName = res.user.nickName || res.user.userName;
        form.value.schedulingUserId = res.user.userId;
      });
      return;
    }
    try {
      // 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.workOrderId || "";
 
      // 若扫码结果已携带 todayReportState,则先本地渲染标题/显隐
      if (orderRow.todayReportState != null) {
        reportState.value = { state: Number(orderRow.todayReportState) };
      }
      getInfo().then(res => {
        // 默认使用当前登录用户,但允许用户修改
        form.value.userId = res.user.userId;
        // 回显展示名称优先使用 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);
      });
    } catch (error) {
      console.error("订单解析失败:", error);
      showToast("订单解析失败");
      goBack();
      return;
    }
  });
</script>
 
<style scoped lang="scss">
  @import "@/static/scss/form-common.scss";
</style>