Blame view

src/components/oa/uploadImageUrl.vue 3.32 KB
0bf7e6a5   wuxw   加入问卷功能代码
1
2
3
  <template>
    <div class="upload-image-container">
      <div v-for="(image, index) in photos" :key="index" class="image-item">
a99eb7a5   wuxw   开发完成办公下功能
4
5
        <el-image 
          :src="image" 
d4a6b78f   wuxw   OA 中考勤功能开发完成
6
7
8
9
          fit="cover" 
          style="width: 100px; height: 100px;"
          :preview-src-list="[image]"
        ></el-image>
a99eb7a5   wuxw   开发完成办公下功能
10
        <i 
d4a6b78f   wuxw   OA 中考勤功能开发完成
11
12
13
          class="el-icon-delete remove-icon" 
          @click="handleRemoveImage(image)"
        ></i>
0bf7e6a5   wuxw   加入问卷功能代码
14
      </div>
d4a6b78f   wuxw   OA 中考勤功能开发完成
15
  
a99eb7a5   wuxw   开发完成办公下功能
16
      <div 
d4a6b78f   wuxw   OA 中考勤功能开发完成
17
18
19
20
        v-if="photos.length < imageCount" 
        class="upload-button" 
        @click="triggerUpload"
      >
0bf7e6a5   wuxw   加入问卷功能代码
21
        <i class="el-icon-plus"></i>
a99eb7a5   wuxw   开发完成办公下功能
22
      </div>
d4a6b78f   wuxw   OA 中考勤功能开发完成
23
  
a99eb7a5   wuxw   开发完成办公下功能
24
25
      <input 
        type="file" 
d4a6b78f   wuxw   OA 中考勤功能开发完成
26
        ref="fileInput" 
a99eb7a5   wuxw   开发完成办公下功能
27
        accept="image/*" 
d4a6b78f   wuxw   OA 中考勤功能开发完成
28
29
30
        hidden 
        @change="handleFileChange"
      >
0bf7e6a5   wuxw   加入问卷功能代码
31
32
33
34
    </div>
  </template>
  
  <script>
d4a6b78f   wuxw   OA 中考勤功能开发完成
35
  import { uploadFile } from '@/api/oa/addAttendanceClassesStaffApi'
0bf7e6a5   wuxw   加入问卷功能代码
36
37
38
39
40
41
42
  import { getCommunityId } from '@/api/community/communityApi'
  
  export default {
    name: 'UploadImageUrl',
    props: {
      imageCount: {
        type: Number,
d4a6b78f   wuxw   OA 中考勤功能开发完成
43
        default: 1
0bf7e6a5   wuxw   加入问卷功能代码
44
45
46
47
      }
    },
    data() {
      return {
d4a6b78f   wuxw   OA 中考勤功能开发完成
48
49
50
        photos: [],
        photosUrl: [],
        communityId: getCommunityId()
0bf7e6a5   wuxw   加入问卷功能代码
51
52
      }
    },
0bf7e6a5   wuxw   加入问卷功能代码
53
    methods: {
d4a6b78f   wuxw   OA 中考勤功能开发完成
54
      triggerUpload() {
a99eb7a5   wuxw   开发完成办公下功能
55
56
        this.$refs.fileInput.click()
      },
d4a6b78f   wuxw   OA 中考勤功能开发完成
57
58
59
60
      async handleFileChange(event) {
        const file = event.target.files[0]
        if (!file) return
  
a99eb7a5   wuxw   开发完成办公下功能
61
62
63
        if (file.size > 2 * 1024 * 1024) {
          this.$message.error(this.$t('uploadImage.imageSizeLimit'))
          return
0bf7e6a5   wuxw   加入问卷功能代码
64
        }
d4a6b78f   wuxw   OA 中考勤功能开发完成
65
66
  
        // 预览图片
a99eb7a5   wuxw   开发完成办公下功能
67
68
69
        const reader = new FileReader()
        reader.onload = (e) => {
          this.photos.push(e.target.result)
0bf7e6a5   wuxw   加入问卷功能代码
70
        }
a99eb7a5   wuxw   开发完成办公下功能
71
        reader.readAsDataURL(file)
d4a6b78f   wuxw   OA 中考勤功能开发完成
72
73
  
        // 上传图片
0bf7e6a5   wuxw   加入问卷功能代码
74
75
        try {
          const formData = new FormData()
d4a6b78f   wuxw   OA 中考勤功能开发完成
76
          formData.append('uploadFile', file)
a99eb7a5   wuxw   开发完成办公下功能
77
          formData.append('communityId', this.communityId)
d4a6b78f   wuxw   OA 中考勤功能开发完成
78
79
80
  
          const res = await uploadFile(formData)
          this.photosUrl.push(res.data)
a99eb7a5   wuxw   开发完成办公下功能
81
          this.$emit('notifyUploadImage', this.photosUrl)
0bf7e6a5   wuxw   加入问卷功能代码
82
        } catch (error) {
d4a6b78f   wuxw   OA 中考勤功能开发完成
83
          this.$message.error(this.$t('uploadImage.uploadFailed'))
0bf7e6a5   wuxw   加入问卷功能代码
84
        }
d4a6b78f   wuxw   OA 中考勤功能开发完成
85
86
  
        event.target.value = null
0bf7e6a5   wuxw   加入问卷功能代码
87
      },
d4a6b78f   wuxw   OA 中考勤功能开发完成
88
89
90
91
92
93
      handleRemoveImage(image) {
        const index = this.photos.indexOf(image)
        if (index > -1) {
          this.photos.splice(index, 1)
          this.photosUrl.splice(index, 1)
          this.$emit('notifyUploadImage', this.photosUrl)
a99eb7a5   wuxw   开发完成办公下功能
94
        }
a99eb7a5   wuxw   开发完成办公下功能
95
      },
d4a6b78f   wuxw   OA 中考勤功能开发完成
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
      clearImages() {
        this.photos = []
        this.photosUrl = []
      },
      setImages(images) {
        this.clearImages()
        images.forEach(img => {
          if (img.indexOf('base64,') > -1) {
            this.photos.push(img)
            return
          }
          // 处理其他类型的图片URL
          this.photos.push(img)
          this.photosUrl.push({ url: img })
        })
0bf7e6a5   wuxw   加入问卷功能代码
111
112
113
114
115
116
117
118
119
120
      }
    }
  }
  </script>
  
  <style lang="scss" scoped>
  .upload-image-container {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
d4a6b78f   wuxw   OA 中考勤功能开发完成
121
  
0bf7e6a5   wuxw   加入问卷功能代码
122
123
    .image-item {
      position: relative;
d4a6b78f   wuxw   OA 中考勤功能开发完成
124
      margin-right: 10px;
0bf7e6a5   wuxw   加入问卷功能代码
125
    }
d4a6b78f   wuxw   OA 中考勤功能开发完成
126
127
128
129
130
131
132
133
134
135
136
137
138
  
    .remove-icon {
      position: absolute;
      top: -10px;
      right: -10px;
      color: #f56c6c;
      cursor: pointer;
      font-size: 16px;
      background: white;
      border-radius: 50%;
      padding: 2px;
    }
  
0bf7e6a5   wuxw   加入问卷功能代码
139
    .upload-button {
a99eb7a5   wuxw   开发完成办公下功能
140
141
      width: 100px;
      height: 100px;
d4a6b78f   wuxw   OA 中考勤功能开发完成
142
143
      border: 1px dashed #d9d9d9;
      border-radius: 6px;
0bf7e6a5   wuxw   加入问卷功能代码
144
145
146
      display: flex;
      justify-content: center;
      align-items: center;
0bf7e6a5   wuxw   加入问卷功能代码
147
      cursor: pointer;
d4a6b78f   wuxw   OA 中考勤功能开发完成
148
149
150
151
      color: #8c939d;
      font-size: 28px;
      background-color: #fbfdff;
  
0bf7e6a5   wuxw   加入问卷功能代码
152
153
      &:hover {
        border-color: #409eff;
0bf7e6a5   wuxw   加入问卷功能代码
154
155
156
157
      }
    }
  }
  </style>