wlzboy
4 天以前 c098f1e3a3e052aa3d65584aae6dc003a70d75ad
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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
<template>
  <view class="attachment-upload-container">
    <view class="detail-section">
      <view class="section-title">
        {{ title }}
        <button class="upload-btn" @click="showUploadDialog" v-if="!readonly">上传附件</button>
      </view>
      <view v-if="attachmentList && attachmentList.length > 0">
        <view class="attachment-item" v-for="(item, index) in attachmentList" :key="item.attachmentId">
          <view class="attachment-thumbnail" @click="viewAttachment(item)">
            <image :src="getImageUrl(item)" mode="aspectFill" class="thumbnail-image"></image>
          </view>
          <view class="attachment-info">
            <view class="attachment-category">
              <text class="category-tag">{{ getCategoryName(item.attachmentCategory) }}</text>
            </view>
            <view class="attachment-meta">
              <text class="upload-time">{{ formatTime(item.uploadTime) }}</text>
            </view>
          </view>
          <view class="attachment-actions">
            <button class="action-btn view-btn" @click="viewAttachment(item)">查看</button>
            <button class="action-btn delete-btn" @click="deleteAttachment(item.attachmentId, index)" v-if="!readonly">删除</button>
          </view>
        </view>
      </view>
      <view v-else class="no-attachment">
        <text>暂无附件</text>
      </view>
    </view>
    
    <!-- 附件上传对话框 -->
    <uni-popup ref="uploadPopup" type="bottom">
      <view class="upload-dialog">
        <view class="dialog-header">
          <text class="dialog-title">上传附件</text>
          <uni-icons type="closeempty" size="24" @click="closeUploadDialog"></uni-icons>
        </view>
        <view class="dialog-content">
          <view class="form-item">
            <view class="form-label">附件分类</view>
            <picker @change="onCategoryChange" :value="selectedCategoryIndex" :range="categoryList" range-key="label">
              <view class="picker-value">
                {{ categoryList[selectedCategoryIndex].label }}
                <uni-icons type="arrowdown" size="16"></uni-icons>
              </view>
            </picker>
          </view>
          <view class="form-item">
            <view class="form-label">选择图片</view>
            <button class="choose-image-btn" @click="chooseImage">
              <uni-icons type="image" size="20"></uni-icons>
              <text class="btn-text">点击选择</text>
            </button>
          </view>
          <view class="preview-area" v-if="tempImagePath">
            <image :src="tempImagePath" mode="aspectFit" class="preview-image"></image>
          </view>
        </view>
        <view class="dialog-footer">
          <button class="cancel-btn" @click="closeUploadDialog">取消</button>
          <button class="confirm-btn" @click="confirmUpload" :disabled="!tempImagePath">确定上传</button>
        </view>
      </view>
    </uni-popup>
  </view>
</template>
 
<script>
  import { getAttachmentList, deleteAttachment } from '@/api/task'
  import { formatDateTime } from '@/utils/common'
  import { getToken } from '@/utils/auth'
  import config from '@/config'
  
  export default {
    name: 'AttachmentUpload',
    props: {
      // 任务ID
      taskId: {
        type: [String, Number],
        required: true
      },
      // 标题
      title: {
        type: String,
        default: '任务附件'
      },
      // 是否自动加载附件列表
      autoLoad: {
        type: Boolean,
        default: true
      },
      // 是否只读模式(禁止上传和删除)
      readonly: {
        type: Boolean,
        default: false
      }
    },
    data() {
      return {
        attachmentList: [],
        categoryList: [
          { label: '知情同意书', value: '1' },
          { label: '病人资料', value: '2' },
          { label: '操作记录', value: '3' },
          { label: '出车前', value: '4' },
          { label: '出车后', value: '5' },
          { label: '系安全带', value: '6' }
        ],
        selectedCategoryIndex: 0,
        tempImagePath: null
      }
    },
    mounted() {
      // 自动加载附件列表
      if (this.autoLoad && this.taskId) {
        this.loadAttachmentList()
      }
    },
    watch: {
      // 监听taskId变化,重新加载附件列表
      taskId(newVal) {
        if (newVal && this.autoLoad) {
          this.loadAttachmentList()
        }
      }
    },
    methods: {
      // 加载附件列表
      loadAttachmentList() {
        if (!this.taskId) {
          return
        }
        
        getAttachmentList(this.taskId).then(response => {
          this.attachmentList = response.data || response || []
          this.$emit('loaded', this.attachmentList)
        }).catch(error => {
          console.error('加载附件列表失败:', error)
          this.$emit('error', error)
        })
      },
      
      // 显示上传对话框
      showUploadDialog() {
        this.selectedCategoryIndex = 0
        this.tempImagePath = null
        this.$refs.uploadPopup.open()
      },
      
      // 关闭上传对话框
      closeUploadDialog() {
        this.$refs.uploadPopup.close()
      },
      
      // 分类选择变化
      onCategoryChange(e) {
        this.selectedCategoryIndex = e.detail.value
      },
      
      // 选择图片
      chooseImage() {
        const that = this
        uni.chooseImage({
          count: 1,
          sizeType: ['compressed'],
          sourceType: ['album', 'camera'],
          success: function(res) {
            that.tempImagePath = res.tempFilePaths[0]
          },
          fail: function(err) {
            console.error('选择图片失败:', err)
            that.$modal.showToast('选择图片失败')
          }
        })
      },
      
      // 确认上传
      confirmUpload() {
        if (!this.tempImagePath) {
          this.$modal.showToast('请先选择图片')
          return
        }
        
        const that = this
        const category = this.categoryList[this.selectedCategoryIndex].value
        
        // 统一直接上传到后端服务器
        uni.showLoading({
          title: '上传中...'
        })
        
        uni.uploadFile({
          url: config.baseUrl + '/task/attachment/upload/' + that.taskId,
          filePath: that.tempImagePath,
          name: 'file',
          formData: {
            'category': category
          },
          header: {
            'Authorization': 'Bearer ' + getToken()
          },
          success: function(uploadRes) {
            uni.hideLoading()
            
            if (uploadRes.statusCode === 200) {
              const result = JSON.parse(uploadRes.data)
              if (result.code === 200) {
                that.$modal.showToast('上传成功')
                that.closeUploadDialog()
                that.loadAttachmentList()
                that.$emit('uploaded', result)
              } else {
                that.$modal.showToast(result.msg || '上传失败')
                that.$emit('error', result)
              }
            } else {
              that.$modal.showToast('上传失败')
              that.$emit('error', uploadRes)
            }
          },
          fail: function(err) {
            uni.hideLoading()
            console.error('上传失败:', err)
            that.$modal.showToast('上传失败:' + (err.errMsg || '请检查网络'))
            that.$emit('error', err)
          }
        })
      },
      
      // 查看附件
      viewAttachment(item) {
        // 如果是图片,使用图片预览
        const imageTypes = ['jpg', 'jpeg', 'png', 'gif', 'bmp']
        const fileExt = item.fileName.split('.').pop().toLowerCase()
        
        if (imageTypes.includes(fileExt)) {
          // 优先使用fileUrl字段(后端拼接好的完整URL)
          let imageUrl = item.fileUrl
          // 如果没有fileUrl,则使用下载接口
          if (!imageUrl) {
            imageUrl = config.baseUrl + '/task/attachment/download/' + item.attachmentId
          }
          
          // 微信小程序中预览图片
          // #ifdef MP-WEIXIN
          // 微信小程序需要先下载到本地再预览
          uni.showLoading({ title: '加载中...' })
          uni.downloadFile({
            url: imageUrl,
            success: function(res) {
              uni.hideLoading()
              if (res.statusCode === 200) {
                uni.previewImage({
                  urls: [res.tempFilePath],
                  current: res.tempFilePath
                })
              } else {
                uni.showToast({ title: '加载图片失败', icon: 'none' })
              }
            },
            fail: function() {
              uni.hideLoading()
              uni.showToast({ title: '下载失败', icon: 'none' })
            }
          })
          // #endif
          
          // 非微信小程序环境,直接预览
          // #ifndef MP-WEIXIN
          uni.previewImage({
            urls: [imageUrl],
            current: imageUrl
          })
          // #endif
        } else {
          this.$modal.showToast('仅支持预览图片')
        }
        
        this.$emit('view', item)
      },
      
      // 删除附件
      deleteAttachment(attachmentId, index) {
        const that = this
        this.$modal.confirm('确定要删除该附件吗?').then(() => {
          deleteAttachment(attachmentId).then(response => {
            that.$modal.showToast('删除成功')
            that.attachmentList.splice(index, 1)
            that.$emit('deleted', attachmentId)
          }).catch(error => {
            console.error('删除附件失败:', error)
            that.$modal.showToast('删除失败')
            that.$emit('error', error)
          })
        }).catch(() => {})
      },
      
      // 获取分类名称
      getCategoryName(category) {
        console.log('category', category)
        const item = this.categoryList.find(c => c.value === category)
        return item ? item.label : '未分类'
      },
      
      // 格式化时间
      formatTime(time) {
        if (!time) return ''
        return formatDateTime(time, 'YYYY-MM-DD HH:mm')
      },
      
      // 格式化文件大小
      formatFileSize(size) {
        if (!size) return '0B'
        if (size < 1024) return size + 'B'
        if (size < 1024 * 1024) return (size / 1024).toFixed(2) + 'KB'
        return (size / 1024 / 1024).toFixed(2) + 'MB'
      },
      
      // 获取图片URL
      getImageUrl(item) {
        
        // 优先使用fileUrl字段(后端拼接好的完整URL)
        if (item.fileUrl) {
          return item.fileUrl
        }
        // 使用下载接口
        if (item.attachmentId) {
          return config.baseUrl + '/task/attachment/download/' + item.attachmentId
        }
        // 默认占位图
        return '/static/images/default-image.png'
      },
      
      // 刷新附件列表(供外部调用)
      refresh() {
        this.loadAttachmentList()
      }
    }
  }
</script>
 
<style lang="scss" scoped>
  .attachment-upload-container {
    .detail-section {
      background-color: white;
      border-radius: 15rpx;
      padding: 30rpx;
      margin-bottom: 20rpx;
      box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
      
      .section-title {
        font-size: 32rpx;
        font-weight: bold;
        margin-bottom: 20rpx;
        color: #333;
        border-bottom: 1rpx solid #f0f0f0;
        padding-bottom: 10rpx;
        display: flex;
        justify-content: space-between;
        align-items: center;
        
        .upload-btn {
          font-size: 24rpx;
          padding: 8rpx 20rpx;
          background-color: #007AFF;
          color: white;
          border-radius: 8rpx;
          border: none;
        }
      }
      
      .no-attachment {
        text-align: center;
        padding: 40rpx 0;
        color: #999;
        font-size: 28rpx;
      }
      
      .attachment-item {
        display: flex;
        align-items: center;
        padding: 20rpx;
        margin-bottom: 15rpx;
        background-color: #f9f9f9;
        border-radius: 10rpx;
        
        &:last-child {
          margin-bottom: 0;
        }
        
        .attachment-thumbnail {
          width: 120rpx;
          height: 120rpx;
          border-radius: 8rpx;
          overflow: hidden;
          margin-right: 20rpx;
          flex-shrink: 0;
          background-color: #f0f0f0;
          
          .thumbnail-image {
            width: 100%;
            height: 100%;
          }
        }
        
        .attachment-info {
          flex: 1;
          margin-right: 20rpx;
          
          .attachment-category {
            margin-bottom: 8rpx;
            
            .category-tag {
              display: inline-block;
              padding: 4rpx 12rpx;
              background-color: #007AFF;
              color: white;
              font-size: 22rpx;
              border-radius: 4rpx;
            }
          }
          
          .attachment-meta {
            font-size: 24rpx;
            color: #999;
            
            .upload-time {
              margin-right: 20rpx;
            }
          }
        }
        
        .attachment-actions {
          display: flex;
          flex-direction: column;
          gap: 10rpx;
          
          .action-btn {
            padding: 8rpx 20rpx;
            font-size: 24rpx;
            border-radius: 6rpx;
            border: none;
            
            &.view-btn {
              background-color: #007AFF;
              color: white;
            }
            
            &.delete-btn {
              background-color: #ff3b30;
              color: white;
            }
          }
        }
      }
    }
    
    .upload-dialog {
      background-color: white;
      border-radius: 20rpx 20rpx 0 0;
      padding: 30rpx;
      
      .dialog-header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 30rpx;
        
        .dialog-title {
          font-size: 32rpx;
          font-weight: bold;
          color: #333;
        }
      }
      
      .dialog-content {
        .form-item {
          margin-bottom: 30rpx;
          
          .form-label {
            font-size: 28rpx;
            color: #333;
            margin-bottom: 15rpx;
          }
          
          .picker-value {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 20rpx;
            background-color: #f5f5f5;
            border-radius: 10rpx;
            font-size: 28rpx;
            color: #333;
          }
          
          .choose-image-btn {
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 30rpx;
            background-color: #f5f5f5;
            border-radius: 10rpx;
            border: 2rpx dashed #ccc;
            color: #666;
            font-size: 28rpx;
            
            .btn-text {
              margin-left: 10rpx;
            }
          }
        }
        
        .preview-area {
          margin-top: 20rpx;
          
          .preview-image {
            width: 100%;
            height: 400rpx;
            border-radius: 10rpx;
          }
        }
      }
      
      .dialog-footer {
        display: flex;
        gap: 20rpx;
        margin-top: 30rpx;
        
        button {
          flex: 1;
          height: 80rpx;
          border-radius: 10rpx;
          font-size: 30rpx;
          border: none;
        }
        
        .cancel-btn {
          background-color: #f5f5f5;
          color: #666;
        }
        
        .confirm-btn {
          background-color: #007AFF;
          color: white;
          
          &:disabled {
            background-color: #ccc;
          }
        }
      }
    }
  }
</style>