Blame view

src/components/market/UploadImage.vue 2.48 KB
b25b036d   wuxw   v1.9 优化日期
1
  <template>
41ad7911   wuxw   开发 admin 营销功能
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
    <div class="upload-image-wrapper">
      <div v-for="(image, index) in images" :key="index" class="image-item">
        <el-image
          style="width: 100px; height: 100px"
          :src="image"
          fit="cover"
          :preview-src-list="images">
        </el-image>
        <i class="el-icon-delete" @click="removeImage(index)"></i>
      </div>
      <div 
        v-if="images.length < imageCount" 
        class="upload-button" 
        @click="triggerUpload">
        <i class="el-icon-plus"></i>
      </div>
      <input 
        type="file" 
        ref="fileInput" 
        accept="image/*" 
        style="display: none" 
        @change="handleFileChange">
    </div>
  </template>
  
  <script>
  export default {
    name: 'UploadImage',
    props: {
      imageCount: {
        type: Number,
        default: 1
      },
      defaultImages: {
        type: Array,
        default: () => []
      }
    },
    data() {
      return {
        images: []
      }
    },
    watch: {
      defaultImages: {
        immediate: true,
        handler(val) {
          if (val && val.length > 0) {
            this.images = [...val]
          }
        }
      }
    },
    methods: {
      triggerUpload() {
        this.$refs.fileInput.click()
      },
      handleFileChange(event) {
        const files = event.target.files
        if (!files || files.length === 0) return
        
        const file = files[0]
        if (file.size > 2 * 1024 * 1024) {
          this.$message.error(this.$t('uploadImage.sizeLimit'))
          return
        }
  
        const reader = new FileReader()
        reader.onload = (e) => {
          this.images.push(e.target.result)
          this.$emit('change', [...this.images])
        }
        reader.readAsDataURL(file)
        event.target.value = null
      },
      removeImage(index) {
        this.images.splice(index, 1)
        this.$emit('change', [...this.images])
      },
      setImages(images) {
        this.images = [...images]
      },
      clear() {
        this.images = []
      }
    }
  }
  </script>
  
  <style scoped>
  .upload-image-wrapper {
    display: flex;
    flex-wrap: wrap;
  }
  
  .image-item {
    position: relative;
    margin-right: 10px;
    margin-bottom: 10px;
  }
  
  .image-item .el-icon-delete {
    position: absolute;
    top: -10px;
    right: -10px;
    font-size: 18px;
    color: #F56C6C;
    cursor: pointer;
    background: #fff;
    border-radius: 50%;
  }
  
  .upload-button {
    width: 100px;
    height: 100px;
    line-height: 100px;
    text-align: center;
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    color: #8c939d;
    font-size: 28px;
  }
  
  .upload-button:hover {
    border-color: #409EFF;
  }
  </style>