gaoluyang
2026-09-20 e449a5408265e4bd1f6c66f5be28a42efac444ee
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
283
284
285
286
287
288
289
290
<template>
  <view class="account-detail">
    <PageHeader :title="pageTitle"
                @back="goBack" />
    <view class="form-container">
      <up-form ref="formRef"
               :model="form"
               :rules="rules"
               label-width="110"
               input-align="right"
               error-message-align="right">
        <u-cell-group title="报告信息"
                      class="form-section">
          <up-form-item label="产品名称"
                        prop="productName"
                        required
                        border-bottom>
            <up-input v-model="form.productName"
                      placeholder="请输入产品名称"
                      clearable />
          </up-form-item>
          <up-form-item label="产品型号"
                        prop="productModel"
                        border-bottom>
            <up-input v-model="form.productModel"
                      placeholder="请输入产品型号"
                      clearable />
          </up-form-item>
          <up-form-item label="报告编号"
                        prop="reportNo"
                        required
                        border-bottom>
            <up-input v-model="form.reportNo"
                      placeholder="请输入报告编号"
                      clearable />
          </up-form-item>
          <up-form-item label="报告类型"
                        prop="reportType"
                        border-bottom>
            <FormPicker v-model="form.reportType"
                        type="select"
                        title="选择报告类型"
                        :options="reportTypeOptions"
                        placeholder="请选择报告类型" />
          </up-form-item>
          <up-form-item label="检测单位"
                        prop="testingAgency"
                        border-bottom>
            <up-input v-model="form.testingAgency"
                      placeholder="请输入检测单位"
                      clearable />
          </up-form-item>
        </u-cell-group>
 
        <u-cell-group title="有效期"
                      class="form-section">
          <up-form-item label="报告日期"
                        prop="reportDate"
                        required
                        border-bottom>
            <FormPicker v-model="form.reportDate"
                        type="date"
                        placeholder="请选择报告日期"
                        @change="calculateExpiration" />
          </up-form-item>
          <up-form-item label="有效期(月)"
                        prop="validityPeriod"
                        required
                        border-bottom>
            <up-input v-model="form.validityPeriod"
                      type="number"
                      placeholder="1~120,默认 12"
                      clearable
                      @blur="calculateExpiration" />
          </up-form-item>
          <up-form-item label="到期时间"
                        prop="expirationTime"
                        required
                        border-bottom>
            <up-input v-model="form.expirationTime"
                      placeholder="系统自动计算"
                      disabled />
          </up-form-item>
          <up-form-item label="备注"
                        prop="remark">
            <up-textarea v-model="form.remark"
                         placeholder="请输入备注内容"
                         auto-height
                         maxlength="500" />
          </up-form-item>
        </u-cell-group>
 
        <u-cell-group title="附件"
                      class="form-section">
          <view class="upload-wrap">
            <CommonUpload v-model="form.attachmentFiles"
                          accept="all"
                          :max-count="5" />
          </view>
        </u-cell-group>
      </up-form>
    </view>
 
    <FooterButtons :loading="loading"
                   confirmText="保存"
                   @cancel="goBack"
                   @confirm="handleSubmit" />
  </view>
</template>
 
<script setup>
  import { computed, ref } from "vue";
  import { onLoad } from "@dcloudio/uni-app";
  import PageHeader from "@/components/PageHeader.vue";
  import FooterButtons from "@/components/FooterButtons.vue";
  import FormPicker from "@/components/FormPicker.vue";
  import CommonUpload from "@/components/CommonUpload.vue";
  import { formatDateToYMD } from "@/utils/ruoyi";
  import { toFileList, toUrlString } from "@/utils/file";
  import { loadDetailData } from "@/utils/detailCache";
  import { addMonths } from "@/utils/warning";
  import {
    addInspectionReport,
    getInspectionReport,
    updateInspectionReport,
  } from "@/api/qualityManagement/inspectionReport";
 
  const formRef = ref();
  const loading = ref(false);
  const reportId = ref("");
 
  const reportTypeOptions = [
    { name: "型式试验报告", value: "型式试验报告" },
    { name: "出厂检验报告", value: "出厂检验报告" },
    { name: "委托检验报告", value: "委托检验报告" },
  ];
 
  const form = ref({
    id: undefined,
    productName: "",
    productModel: "",
    reportNo: "",
    reportType: "",
    testingAgency: "",
    reportDate: "",
    validityPeriod: 12,
    expirationTime: "",
    remark: "",
    attachmentFiles: [],
  });
 
  const rules = {
    productName: [
      { required: true, message: "请输入产品名称", trigger: "blur" },
    ],
    reportNo: [{ required: true, message: "请输入报告编号", trigger: "blur" }],
    reportDate: [
      { required: true, message: "请选择报告日期", trigger: "change" },
    ],
    validityPeriod: [
      { required: true, message: "请输入有效期", trigger: "blur" },
    ],
    expirationTime: [
      { required: true, message: "到期时间不能为空", trigger: "change" },
    ],
  };
 
  const pageTitle = computed(() => (reportId.value ? "编辑报告" : "新增报告"));
 
  // 到期时间 = 报告日期 + 有效期月,只读
  const calculateExpiration = () => {
    const months = Number(form.value.validityPeriod);
    if (form.value.reportDate && Number.isFinite(months) && months > 0) {
      form.value.expirationTime = addMonths(form.value.reportDate, months);
    } else {
      form.value.expirationTime = "";
    }
  };
 
  const goBack = () => uni.navigateBack();
 
  const loadDetail = async () => {
    let data = null;
    try {
      data = await loadDetailData({
        cacheKey: "inspectionReportDetail",
        id: reportId.value,
        fetcher: getInspectionReport,
      });
    } catch (error) {
      data = null;
    }
    if (!data) {
      uni.showToast({ title: "未找到报告信息", icon: "none" });
      return;
    }
 
    form.value = {
      id: data.id,
      productName: data.productName || "",
      productModel: data.productModel || "",
      reportNo: data.reportNo || "",
      reportType: data.reportType || "",
      testingAgency: data.testingAgency || "",
      reportDate: (data.reportDate || "").slice(0, 10),
      validityPeriod: data.validityPeriod ?? 12,
      expirationTime: (data.expirationTime || "").slice(0, 10),
      remark: data.remark || "",
      attachmentFiles: toFileList(data.attachmentUrls),
    };
  };
 
  const handleSubmit = async () => {
    const valid = await formRef.value.validate().catch(() => false);
    if (!valid) return;
 
    calculateExpiration();
    const payload = {
      id: form.value.id,
      productName: form.value.productName,
      productModel: form.value.productModel,
      reportNo: form.value.reportNo,
      reportType: form.value.reportType,
      testingAgency: form.value.testingAgency,
      reportDate: form.value.reportDate,
      validityPeriod: Number(form.value.validityPeriod),
      expirationTime: form.value.expirationTime,
      remark: form.value.remark,
      attachmentUrls: toUrlString(form.value.attachmentFiles),
    };
 
    loading.value = true;
    const action = reportId.value ? updateInspectionReport : addInspectionReport;
    action(payload)
      .then(() => {
        uni.showToast({ title: "保存成功", icon: "success" });
        setTimeout(() => uni.navigateBack(), 1000);
      })
      .catch(() => {
        uni.showToast({ title: "保存失败", icon: "none" });
      })
      .finally(() => {
        loading.value = false;
      });
  };
 
  onLoad(options => {
    if (options?.id) {
      reportId.value = options.id;
      form.value.id = options.id;
      loadDetail();
    } else {
      form.value.reportDate = formatDateToYMD(Date.now());
      calculateExpiration();
    }
  });
</script>
 
<style scoped lang="scss">
  @import "@/static/scss/form-common.scss";
 
  .account-detail {
    min-height: 100vh;
    background: #f8f9fa;
    padding-bottom: 100px;
  }
 
  .form-container {
    padding: 12px 12px 0;
  }
 
  .form-section {
    margin-bottom: 12px;
    border-radius: 12px;
    overflow: hidden;
    box-shadow: 0 2px 10px rgba(15, 35, 95, 0.05);
  }
 
  .upload-wrap {
    padding: 12px;
  }
 
  :deep(.u-cell-group__title) {
    padding: 14px 18px 10px !important;
    font-size: 15px !important;
    font-weight: 600 !important;
    color: #22324d !important;
    background: #f8fbff !important;
  }
</style>