Blame view

src/components/upload/UploadImageUrl.vue 3.07 KB
b25b036d   wuxw   v1.9 优化日期
1
  <!-- components/staff/UploadImageUrl.vue -->
a42b3256   wuxw   HC小区管理系统前段vue版正在开发中
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
  <template>
    <div class="upload-image-container">
      <div v-for="(image, index) in photos" :key="index" class="image-item">
        <el-image :src="image" fit="cover" style="width: 100px; height: 100px" :preview-src-list="photos" />
        <i class="el-icon-delete delete-icon" @click="removeImage(index)" />
      </div>
      <div v-if="photos.length < imageCount" class="upload-button" @click="triggerUpload">
        <i class="el-icon-plus" />
      </div>
      <input ref="fileInput" type="file" accept="image/*" hidden @change="handleFileChange" />
    </div>
  </template>
    
  <script>
  import { uploadFile } from '@/api/staff/addStaffApi'
  
  export default {
    name: 'UploadImageUrl',
    props: {
      imageCount: {
        type: Number,
        default: 1
      }
    },
    data() {
      return {
        photos: [],
        photosUrl: []
      }
    },
    methods: {
      triggerUpload() {
        this.$refs.fileInput.click()
      },
      async handleFileChange(event) {
        const file = event.target.files[0]
        if (!file) return
  
        if (file.size > 2 * 1024 * 1024) {
          this.$message.error('图片大小不能超过2MB')
          return
        }
  
        const reader = new FileReader()
        reader.onload = (e) => {
          this.photos.push(e.target.result)
        }
        reader.readAsDataURL(file)
  
        try {
          const formData = new FormData()
          formData.append('uploadFile', file)
cc7e0d1a   wuxw   开发 admin 营销功能
54
          formData.append('communityId', '-1')
a42b3256   wuxw   HC小区管理系统前段vue版正在开发中
55
  
cc7e0d1a   wuxw   开发 admin 营销功能
56
57
58
59
          const { url } = await uploadFile(formData)
  
          this.photosUrl.push(url)
          this.$emit('notifyUploadCoverImage', this.photosUrl)
a42b3256   wuxw   HC小区管理系统前段vue版正在开发中
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
        } catch (error) {
          this.$message.error(error.message || '上传失败')
        }
  
        event.target.value = null
      },
      removeImage(index) {
        this.photos.splice(index, 1)
        this.photosUrl.splice(index, 1)
        this.$emit('notifyUploadCoverImage', this.photosUrl)
      },
      clearImages() {
        this.photos = []
        this.photosUrl = []
      },
      setImages(images) {
        this.clearImages()
        images.forEach(image => {
          if (image.indexOf('base64,') > -1) {
            this.photos.push(image)
            return
          }
          if (image.indexOf('http') > -1) {
            this.photos.push(image)
cc7e0d1a   wuxw   开发 admin 营销功能
84
85
            this.photosUrl.push(image)
           
a42b3256   wuxw   HC小区管理系统前段vue版正在开发中
86
87
            return
          }
cc7e0d1a   wuxw   开发 admin 营销功能
88
          this.photosUrl.push(image)
a42b3256   wuxw   HC小区管理系统前段vue版正在开发中
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
        })
      }
    }
  }
  </script>
    
  <style lang="scss" scoped>
  .upload-image-container {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
  
    .image-item {
      position: relative;
      margin-right: 10px;
      margin-bottom: 10px;
  
      .delete-icon {
        position: absolute;
        top: 5px;
        right: 5px;
        color: #f56c6c;
        font-size: 16px;
        cursor: pointer;
        background: rgba(255, 255, 255, 0.7);
        border-radius: 50%;
        padding: 3px;
      }
    }
  
    .upload-button {
      width: 100px;
      height: 100px;
      border: 1px dashed #dcdfe6;
      display: flex;
      align-items: center;
      justify-content: center;
      cursor: pointer;
      color: #909399;
      font-size: 24px;
  
      &:hover {
        border-color: #409eff;
        color: #409eff;
      }
    }
  }
  </style>