churf
2023-09-18 4b4e1b54cae72088fa60978e0192fe57f986e6f3
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
185
186
<template>
  <div>
    <el-upload
      ref="upload"
      :class="{disButtonSty: noneBtnImg}"
      :headers="headers"
      :action="imagesUploadApi + '?name=' + new Date().getTime()"
      :file-list="fileList"
      list-type="picture-card"
      :disabled="disabled"
      :auto-upload="false"
      :before-upload="beforeAvatarUpload"
      :on-change="handleChange"
      :on-preview="handlePictureCardPreview"
      :on-remove="handleRemove"
      :on-success="handleSuccess"
      :on-error="handleError"
    >
      <i slot="default" class="el-icon-plus" />
    </el-upload>
    <!-- 放大事件显示框 -->
    <el-dialog :visible.sync="dialogVisible" :modal="false">
      <img width="100%" :src="dialogImageUrl" alt="">
    </el-dialog>
  </div>
</template>
 
<script>
import { mapGetters } from 'vuex'
import { getToken } from '@/utils/auth'
export default {
  name: 'ImageUpload',
  props: {
    imageUrl: {
      type: String,
      default() {
        return null
      }
    },
    limit: {
      type: Number,
      default() {
        return 1
      }
    },
    disabled: {
      type: Boolean,
      default() {
        return false
      }
    }
  },
  data() {
    return {
      // 控制图片上传按钮是否隐藏
      noneBtnImg: false,
      // 请求头
      headers: { 'Authorization': getToken() },
      // 放大事件显示框图片路径
      dialogImageUrl: '',
      // 放大事件显示框显示状态
      dialogVisible: false,
      // 图片墙列表
      fileList: []
    }
  },
  computed: {
    ...mapGetters([
      'imagesUploadApi',
      'baseApi'
    ])
  },
  mounted() {
    // 判断图片路径参数是否为空
    if (this.imageUrl !== null) {
      // 图片路径数组
      const imageUrlList = this.imageUrl.split(',')
      // 循环
      for (let i = 0; i < imageUrlList.length; i++) {
        // 临时集合
        const itemOption = {}
        // 临时集合添加图片路径参数
        itemOption.url = imageUrlList[i].indexOf('https:') === -1 ? this.baseApi + imageUrlList[i] : imageUrlList[i]
        // 图片墙列表添加临时集合
        this.fileList.push(itemOption)
        // 判断图片列表数量
        if (this.fileList.length >= this.limit) {
          // 上传按钮隐藏
          this.noneBtnImg = true
        }
      }
    }
  },
  methods: {
    // 提交前校验
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpg'
      const isJPEG = file.type === 'image/jpeg'
      const isPNG = file.type === 'image/png'
      const isGIF = file.type === 'image/gif'
      if (!isJPG && !isJPEG && !isPNG && !isGIF) {
        this.$message.error('上传头像图片只能是 JPG/JPEG/PNG/GIF 格式!')
      }
      return isJPG || isJPEG || isPNG || isGIF
    },
    // 改变事件
    handleChange(file, fileList) {
      // 图片墙列表赋值
      this.fileList = fileList
      // 判断图片列表数量
      if (fileList.length >= this.limit) {
        // 上传按钮隐藏
        this.noneBtnImg = true
      }
    },
    // 放大事件
    handlePictureCardPreview(file) {
      // 放大事件显示框图片路径赋值
      this.dialogImageUrl = file.url
      // 放大事件显示框显示
      this.dialogVisible = true
    },
    // 移除事件
    handleRemove(file, fileList) {
      // 图片墙列表赋值
      this.fileList = fileList
      // 判断图片列表数量
      if (fileList.length < this.limit) {
        // 上传按钮隐藏
        this.noneBtnImg = false
      }
    },
    // 成功事件
    handleSuccess(response, file, fileList) {
      // 循环图片墙列表
      for (let i = 0; i < fileList.length; i++) {
        // 判断图片状态
        if (fileList[i].status === 'ready' || fileList[i].status === 'uploading') {
          // 终止
          return
        }
        // 判断循环是否结束
        if (i === fileList.length - 1) {
          // 向父页面发送参数:“imageInfo”是参数名,“fileList”是参数值。
          this.$emit('imageInfo', fileList)
        }
      }
    },
    // 失败事件
    handleError(err) {
      console.log(err)
    },
    // 上传图片
    submit() {
      // 判断图片墙长度
      if (this.fileList.length === 0) {
        // 向父页面发送参数:“imageInfo”是参数名,“this.fileList”是参数值。
        this.$emit('imageInfo', this.fileList)
        // 终止
        return
      }
      // 循环图片墙列表
      for (let i = 0; i < this.fileList.length; i++) {
        // 判断图片状态
        if (this.fileList[i].status === 'ready') {
          // 将指定组件别名进行提交
          this.$refs.upload.submit()
          // 终止
          return
        }
        // 判断循环是否结束
        if (i === this.fileList.length - 1) {
          // 向父页面发送参数:“imageInfo”是参数名,“this.fileList”是参数值。
          this.$emit('imageInfo', this.fileList)
        }
      }
    }
  }
}
</script>
 
<style>
.disButtonSty .el-upload--picture-card{
  display: none;   /* 上传按钮隐藏 */
}
</style>