fuliqi
2024-01-24 29c1e7eb5ac16e90d8991a86c1c071bc312ec8d9
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
<template>
  <div>
    <el-upload :action="action" :headers="headers" list-type="picture-card" :accept="accept"
               :limit="limitNumber" :file-list="fileList" :before-upload="beforeAvatarUpload"
               :on-preview="handlePictureCardPreview" :on-change="handleChange"
               :class="{disabled:uploadDisabled,imageSize:imgSize}" :data="{
              path: uploadPath,
              bizCode: businessId
            }" :on-remove="handleRemove" :on-success="handleSuccess" :on-error="handleError">
      <i class="el-icon-plus"></i>
      <div slot="tip" v-if="imageSize" class="el-upload__tip" style="color:#909399;">
        请上传{{imageSize}}尺寸大小的图片</div>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible" :append-to-body="isAppendTobody">
      <img width="100%" :src="dialogImageUrl" alt />
    </el-dialog>
  </div>
</template>
<script>
import {
  getUuid
} from '@/api/sm'
import Cookie from 'js-cookie'
// 跨域认证信息 header 名
const xsrfHeaderName = 'Authorization'
export default {
  props: {
    limitNumber: {
      type: Number,
      default: 5
    },
    fileList: {
      type: Array,
      default: function() {
        return []
      }
    },
    isAppendTobody: {
      type: Boolean,
      default: false
    },
    uploadPath: {
      type: String,
      default: 'product'
    },
    /**
     * 设置上传图片的大小(提示)
     */
    imageSize: {
      type: String,
      default: null
    },
    /**
     * 是否要隐藏上传图片文件筐
     */
    hideAddUpload: {
      type: Boolean,
      default: false
    },
    /**
     * 给图片设置样式(加class)
     */
    imgSize: {
      type: Boolean,
      default: false
    },
    // 图片接收类型
    accept: {
      type: String,
      default: '.jpg,.jpeg,.png,gif'
    }
  },
  watch: {
    fileList: {
      handler: function(newValue, oldValue) {
        if (newValue.length === this.limitNumber) {
          this.uploadDisabled = true
        }
      },
      immediate: true
    }
 
  },
  data() {
    return {
      action: `${process.env.VUE_APP_CURRENTMODE === 'development' ? '/api/' : process.env.VUE_APP_API_BASE_URL}lbcloud-file/file/upload/file`,
      headers: {
        Authorization: 'Bearer ' + Cookie.get(xsrfHeaderName)
      },
      dialogVisible: false,
      dialogImageUrl: null,
      uploadDisabled: false,
      businessId: null
    }
  },
  created() {
    this.businessId = getUuid().split('-').join('')
  },
  mounted() {
    if (this.hideAddUpload) {
      this.uploadDisabled = this.hideAddUpload
    }
  },
  methods: {
    handleError(e, file, fileList) {
      this.$message.error('上传文件失败!请重新上传')
      this.uploadDisabled = false
    },
    /**
     * 设置上传图片大小
     */
    beforeAvatarUpload(file) {
      const nameType = file.name.substring(file.name.lastIndexOf('.') + 1)
      const fileType = this.accept.toLowerCase().includes(nameType.toLowerCase())
      const imgSize = file.type.includes('image')
      const videoSize = file.type.includes('video')
      if (!fileType) {
        this.$message.error(`上传失败!仅支持${this.accept}格式,请重新上传`)
        return false
      }
      if (imgSize) {
        const fileSize = file.size / 1024 / 1024 < 5
        if (!fileSize) {
          this.$message.error('上传失败!图片大小不能超多 5MB!请重新上传')
          return false
        }
      }
      if (videoSize) {
        const fileSize = file.size / 1024 / 1024 < 300
        if (!fileSize) {
          this.$message.error('上传失败!视频大小不能超多 300MB!请重新上传')
          return false
        }
      }
      return true
    },
    /**
       *上传图片
       */
    handleSuccess(res, file) {
      this.$emit('handle-success', res.data)
    },
    /**
       * 预览
       */
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    },
    /**
     * 限制文件上传个数
     */
    handleChange(file, fileList) {
      if (fileList.length === this.limitNumber) {
        this.uploadDisabled = true
      }
    },
    /**
     * 删除图片
     */
    handleRemove(file, fileList) {
      this.$emit('handle-remove', file, JSON.parse(JSON.stringify(fileList)))
      if (fileList.length < this.limitNumber) {
        this.uploadDisabled = false
      }
    }
  }
}
</script>
<style lang="scss">
.imageSize {
  .el-upload--picture-card,
  .el-upload-list__item {
    width: 320px;
    height: 180px;
  }
  .el-icon-plus {
    line-height: 180px;
  }
}
.el-form-item__content .el-upload-list__item {
  max-width: 100%;
}
</style>