WXL
2026-01-28 bcb9976e7680ce67fadb6fec7ab5fca36552cdbc
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
<template>
  <div class="organ-assessment-form">
    <el-form :model="assessmentData" label-width="120px">
      <el-form-item label="功能状态">
        <el-select v-model="assessmentData.functionStatus" :disabled="readonly">
          <el-option label="正常" value="1" />
          <el-option label="轻度异常" value="2" />
          <el-option label="重度异常" value="3" />
          <el-option label="无法评估" value="4" />
        </el-select>
      </el-form-item>
 
      <el-form-item label="评估意见">
        <el-input
          type="textarea"
          v-model="assessmentData.assessmentOpinion"
          :rows="4"
          :disabled="readonly"
          placeholder="请输入评估意见"
        />
      </el-form-item>
 
      <!-- 附件上传区域 -->
      <el-form-item label="相关附件">
        <div class="attachment-section">
          <div class="attachment-header">
            <i class="el-icon-paperclip"></i>
            <span class="attachment-title">附件上传</span>
            <span class="attachment-tip"
              >支持上传检验报告单等文件 (最多{{ attachmentLimit }}个)</span
            >
          </div>
 
          <!-- 使用 UploadAttachment 组件 -->
          <UploadAttachment
            ref="uploadAttachment"
            :file-list="attachmentFileList"
            :limit="attachmentLimit"
            :accept="attachmentAccept"
            :multiple="true"
            @change="handleAttachmentChange"
            @upload-success="handleUploadSuccess"
            @upload-error="handleUploadError"
            @remove="handleAttachmentRemove"
          />
 
          <!-- 附件列表 -->
          <div
            class="attachment-list"
            v-if="
              assessmentData.attachments &&
                assessmentData.attachments.length > 0
            "
          >
            <div class="list-title">
              已上传附件 ({{ assessmentData.attachments.length }})
            </div>
            <el-table
              :data="assessmentData.attachments"
              style="width: 100%"
              size="small"
            >
              <el-table-column label="文件名" min-width="200">
                <template slot-scope="scope">
                  <i
                    class="el-icon-document"
                    :style="{ color: getFileIconColor(scope.row.fileName) }"
                  ></i>
                  <span class="file-name">{{ scope.row.fileName }}</span>
                </template>
              </el-table-column>
              <el-table-column label="文件类型" width="100">
                <template slot-scope="scope">
                  <el-tag
                    :type="getFileTagType(scope.row.fileName)"
                    size="small"
                  >
                    {{ getFileTypeText(scope.row.fileName) }}
                  </el-tag>
                </template>
              </el-table-column>
              <el-table-column label="上传时间" width="160">
                <template slot-scope="scope">
                  <span>{{ formatDateTime(scope.row.uploadTime) }}</span>
                </template>
              </el-table-column>
              <el-table-column label="文件大小" width="100">
                <template slot-scope="scope">
                  <span>{{ formatFileSize(scope.row.fileSize) }}</span>
                </template>
              </el-table-column>
              <el-table-column label="操作" width="266" fixed="right">
                <template slot-scope="scope">
                  <el-button
                    size="mini"
                    type="primary"
                    @click="handlePreview(scope.row)"
                    :disabled="!isPreviewable(scope.row.fileName)"
                  >
                    预览
                  </el-button>
                  <el-button
                    size="mini"
                    type="success"
                    @click="handleDownload(scope.row)"
                  >
                    下载
                  </el-button>
                  <el-button
                    v-if="!readonly"
                    size="mini"
                    type="danger"
                    @click="handleRemoveAttachment(scope.$index)"
                  >
                    删除
                  </el-button>
                </template>
              </el-table-column>
            </el-table>
          </div>
        </div>
      </el-form-item>
 
      <el-form-item v-if="!readonly">
        <el-button type="primary" @click="handleSave">保存评估</el-button>
        <el-button @click="handleCancel">取消</el-button>
        <el-button
          type="success"
          @click="handleAddAssessment"
          icon="el-icon-plus"
        >
          新增评估记录
        </el-button>
      </el-form-item>
    </el-form>
 
    <!-- 文件预览弹窗 -->
    <FilePreviewDialog
      :visible="previewVisible"
      :file="currentPreviewFile"
      @close="previewVisible = false"
      @download="handleDownload"
    />
  </div>
</template>
 
<script>
import UploadAttachment from "@/components/UploadAttachment";
import FilePreviewDialog from "@/components/FilePreviewDialog";
import dayjs from "dayjs";
 
export default {
  name: "OrganAssessmentForm",
  components: {
    UploadAttachment,
    FilePreviewDialog
  },
  props: {
    organData: {
      type: Object,
      default: () => ({})
    },
    assessmentData: {
      type: Object,
      default: () => ({
        functionStatus: "",
        assessmentOpinion: "",
        attachments: []
      })
    },
    assessmentIndex: {
      type: Number,
      default: 0
    },
    readonly: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      // 预览相关
      previewVisible: false,
      currentPreviewFile: null,
 
      // 附件相关配置
      attachmentLimit: 10,
      attachmentAccept:
        ".pdf,.jpg,.jpeg,.png,.doc,.docx,.xls,.xlsx,.ppt,.pptx,.txt",
      attachmentFileList: []
    };
  },
  watch: {
    organData: {
      handler(newVal, oldVal) {
        // 只有当附件数据真正发生变化时才初始化
        if (
          !oldVal ||
          !oldVal.attachments ||
          JSON.stringify(newVal.attachments) !==
            JSON.stringify(oldVal.attachments)
        ) {
          this.initAttachmentList();
        }
      },
      immediate: true,
      deep: true
    }
  },
  methods: {
    /** 初始化附件列表 */
    initAttachmentList() {
      if (this.organData.attachments && this.organData.attachments.length > 0) {
        this.assessmentData.attachments = [...this.organData.attachments];
        this.attachmentFileList = this.organData.attachments.map(item => ({
          uid: item.id || Math.random(),
          name: item.fileName,
          url: item.path || item.fileUrl,
          status: "success"
        }));
      } else {
        this.assessmentData.attachments = [];
        this.attachmentFileList = [];
      }
    },
    handleSave() {
      const saveData = {
        organData: this.organData,
        assessmentData: this.assessmentData,
        assessmentIndex: this.assessmentIndex
      };
      this.$emit("save", saveData);
    },
 
    handleCancel() {
      this.$emit("cancel");
    },
 
    handleAddAssessment() {
      this.$emit("add-assessment", {
        organData: this.organData,
        currentIndex: this.assessmentIndex
      });
    },
    /** 附件变化处理 */
    handleAttachmentChange(fileList) {
      this.attachmentFileList = fileList;
    },
 
    /** 附件移除处理 */
    handleAttachmentRemove(file) {
      if (file.url) {
        const index = this.assessmentData.attachments.findIndex(
          item => item.path === file.url || item.fileUrl === file.url
        );
        if (index > -1) {
          this.assessmentData.attachments.splice(index, 1);
        }
      }
    },
 
    /** 手动删除附件 */
    handleRemoveAttachment(index) {
      this.assessmentData.attachments.splice(index, 1);
      this.attachmentFileList.splice(index, 1);
      this.$message.success("附件删除成功");
    },
 
    /** 上传成功处理 */
    handleUploadSuccess({ file, fileList, response }) {
      if (response.code === 200) {
        const attachmentObj = {
          id:
            response.data.fileId ||
            Math.random()
              .toString(36)
              .substr(2),
          fileName: file.name,
          path: response.data.fileUrl || file.url,
          fileUrl: response.data.fileUrl || file.url,
          fileType: this.getFileExtension(file.name),
          fileSize: file.size,
          uploadTime: dayjs().format("YYYY-MM-DD HH:mm:ss"),
          assessmentType: this.assessmentType,
          organType: this.organData.organType
        };
 
        this.assessmentData.attachments.push(attachmentObj);
        this.$message.success("文件上传成功");
      }
    },
 
    /** 上传错误处理 */
    handleUploadError({ file, fileList, error }) {
      console.error("附件上传失败:", error);
      this.$message.error("文件上传失败,请重试");
    },
 
    /** 文件预览 */
    handlePreview(file) {
      this.currentPreviewFile = {
        fileName: file.fileName,
        fileUrl: file.path || file.fileUrl,
        fileType: this.getFileType(file.fileName)
      };
      this.previewVisible = true;
    },
 
    /** 文件下载 */
    handleDownload(file) {
      const fileUrl = file.path || file.fileUrl;
      const fileName = file.fileName;
 
      if (fileUrl) {
        const link = document.createElement("a");
        link.href = fileUrl;
        link.download = fileName;
        link.style.display = "none";
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);
        this.$message.success("开始下载文件");
      } else {
        this.$message.warning("文件路径不存在,无法下载");
      }
    },
 
    /** 获取文件类型 */
    getFileType(fileName) {
      if (!fileName) return "other";
 
      const extension = fileName
        .split(".")
        .pop()
        .toLowerCase();
      const imageTypes = ["jpg", "jpeg", "png", "gif", "bmp", "webp"];
      const pdfTypes = ["pdf"];
      const officeTypes = ["doc", "docx", "xls", "xlsx", "ppt", "pptx"];
 
      if (imageTypes.includes(extension)) return "image";
      if (pdfTypes.includes(extension)) return "pdf";
      if (officeTypes.includes(extension)) return "office";
      return "other";
    },
 
    /** 获取文件图标颜色 */
    getFileIconColor(fileName) {
      const type = this.getFileType(fileName);
      const colorMap = {
        image: "#67C23A",
        pdf: "#F56C6C",
        office: "#409EFF",
        other: "#909399"
      };
      return colorMap[type] || "#909399";
    },
 
    /** 获取文件标签类型 */
    getFileTagType(fileName) {
      const type = this.getFileType(fileName);
      const typeMap = {
        image: "success",
        pdf: "danger",
        office: "primary",
        other: "info"
      };
      return typeMap[type] || "info";
    },
 
    /** 获取文件类型文本 */
    getFileTypeText(fileName) {
      const type = this.getFileType(fileName);
      const textMap = {
        image: "图片",
        pdf: "PDF",
        office: "文档",
        other: "其他"
      };
      return textMap[type] || "未知";
    },
 
    /** 检查是否可预览 */
    isPreviewable(fileName) {
      const type = this.getFileType(fileName);
      return ["image", "pdf"].includes(type);
    },
 
    /** 获取文件扩展名 */
    getFileExtension(filename) {
      return filename
        .split(".")
        .pop()
        .toLowerCase();
    },
 
    /** 格式化文件大小 */
    formatFileSize(bytes) {
      if (!bytes || bytes === 0) return "0 B";
      const k = 1024;
      const sizes = ["B", "KB", "MB", "GB"];
      const i = Math.floor(Math.log(bytes) / Math.log(k));
      return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + " " + sizes[i];
    },
 
    /** 日期时间格式化 */
    formatDateTime(dateTime) {
      if (!dateTime) return "";
 
      try {
        const date = new Date(dateTime);
        if (isNaN(date.getTime())) return dateTime;
 
        const year = date.getFullYear();
        const month = String(date.getMonth() + 1).padStart(2, "0");
        const day = String(date.getDate()).padStart(2, "0");
        const hours = String(date.getHours()).padStart(2, "0");
        const minutes = String(date.getMinutes()).padStart(2, "0");
 
        return `${year}-${month}-${day} ${hours}:${minutes}`;
      } catch (error) {
        return dateTime;
      }
    }
  }
};
</script>
 
<style scoped>
.organ-assessment-form {
  padding: 20px;
}
 
.attachment-section {
  border: 1px solid #ebeef5;
  border-radius: 4px;
  padding: 15px;
  background: #fafafa;
}
 
.attachment-header {
  display: flex;
  align-items: center;
  margin-bottom: 16px;
  padding-bottom: 8px;
  border-bottom: 1px solid #ebeef5;
}
 
.attachment-title {
  font-weight: bold;
  margin: 0 8px;
  color: #303133;
}
 
.attachment-tip {
  font-size: 12px;
  color: #909399;
  margin-left: auto;
}
 
.attachment-list {
  margin-top: 16px;
}
 
.list-title {
  font-weight: bold;
  margin-bottom: 12px;
  color: #303133;
  font-size: 14px;
}
 
.file-name {
  font-size: 13px;
  margin-left: 8px;
}
 
::v-deep .el-upload__tip {
  font-size: 12px;
  color: #909399;
  margin-top: 8px;
}
 
::v-deep .el-table .cell {
  padding: 8px 12px;
}
 
::v-deep .el-button--mini {
  padding: 5px 10px;
  font-size: 12px;
}
</style>