Blame view

src/components/mall/UploadImage.vue 2.49 KB
7f2cddcb   wuxw   开发完成营销功能
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
  <template>
    <div class="upload-image-container">
      <div v-for="(image, index) in images" :key="index" class="image-item">
        <el-image
          :src="image"
          fit="cover"
          style="width: 100px; height: 100px;"
          :preview-src-list="[image]"
        ></el-image>
        <i class="el-icon-delete" @click="removeImage(index)"></i>
      </div>
      <el-upload
        v-if="images.length < maxCount"
        action=""
        :show-file-list="false"
        :before-upload="beforeUpload"
        :http-request="handleUpload"
      >
        <div class="upload-btn">
          <i class="el-icon-plus"></i>
        </div>
      </el-upload>
    </div>
  </template>
  
  <script>
  import { uploadFile } from '@/api/mall/convenienceMenusManageApi'
  
  export default {
    name: 'UploadImage',
    props: {
      maxCount: {
        type: Number,
        default: 1
      }
    },
    data() {
      return {
        images: []
      }
    },
    methods: {
      beforeUpload(file) {
        const isImage = file.type.includes('image/')
        const isLt2M = file.size / 1024 / 1024 < 2
  
        if (!isImage) {
          this.$message.error(this.$t('upload.imageTypeError'))
        }
        if (!isLt2M) {
          this.$message.error(this.$t('upload.imageSizeError'))
        }
        return isImage && isLt2M
      },
      async handleUpload({ file }) {
        try {
          const formData = new FormData()
          formData.append('uploadFile', file)
          const { url } = await uploadFile(formData)
          this.images.push(url)
          this.$emit('change', url)
        } catch (error) {
          this.$message.error(error.message)
        }
      },
      removeImage(index) {
        this.images.splice(index, 1)
        this.$emit('change', this.images[0] || '')
      },
      clear() {
        this.images = []
        this.$emit('change', '')
      }
    }
  }
  </script>
  
  <style lang="scss" scoped>
  .upload-image-container {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
  
    .image-item {
      position: relative;
  
      .el-icon-delete {
        position: absolute;
        top: 5px;
        right: 5px;
        color: #f56c6c;
        cursor: pointer;
        font-size: 16px;
        background: rgba(255, 255, 255, 0.7);
        border-radius: 50%;
        padding: 5px;
      }
    }
  
    .upload-btn {
      width: 100px;
      height: 100px;
      display: flex;
      justify-content: center;
      align-items: center;
      border: 1px dashed #d9d9d9;
      border-radius: 6px;
      cursor: pointer;
      background-color: #fbfdff;
  
      &:hover {
        border-color: #409eff;
      }
  
      i {
        font-size: 28px;
        color: #8c939d;
      }
    }
  }
  </style>