doum
昨天 4bb5e34bc9d6e0332e96e90036ba187ff17df57f
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
<template>
  <div>
    <el-upload
      :action="uploadImgUrl"
      :data="uploadData"
      list-type="picture-card"
      :file-list="fileList"
      accept=".jpg,.png"
      :before-upload="beforeUpload"
      :on-success="uploadSuccess"
      :on-preview="handlePreview"
      :on-remove="handleRemove"
      :on-error="fail"
    >
      <i class="el-icon-plus icon"></i>
    </el-upload>
   <el-image-viewer
      v-if="showViewer"
      :on-close="closeViewer"
      :initialIndex="tempIndex"
      :url-list="srcList"
      :z-index="3000"
    />
  </div>
 
</template>
 
<script>
import ElImageViewer from 'element-ui/packages/image/src/image-viewer'
export default {
  components: {
    ElImageViewer
  },
  props: {
    fileList: {
      type: Array,
      default: () => []
    },
    maxNum: {
      type: Number,
      default: () => null
    },
    uploadData: Object
  },
  data () {
    return {
      uploadImgUrl: process.env.VUE_APP_API_PREFIX + '/public/upload',
      realList: [],
      // srcList: [],
      tempIndex: 0,
      showViewer: false
    }
  },
  computed:{
    srcList(){
      return this.fileList.map(item => { return item.url })
    }
  },
  watch: {
  },
  methods: {
    handlePreview(file) {
      // console.log('预览文件:', file,this.fileList);
      this.tempIndex = this.srcList.findIndex(item => item == file.url)
      this.showViewer = true
    },
    beforeUpload (file) {
      this.$emit('beginUpload')
      const isJPGOrPNG = file.type === 'image/jpeg' || file.type === 'image/png'
      const isLt2M = file.size / 1024 / 1024 < 1; // 500kb
      if (!isJPGOrPNG) {
        this.$message.error('上传头像图片只能是 JPG/PNG 格式!');
        return false
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 500KB!');
        return false
      }
      return true
    },
    // 上传图片成功
    uploadSuccess (res, file, fileList) {
      this.$emit('endUpload')
      console.log('上传成功1:',fileList);
      if (res.code === 200) {
        this.fileList.push({
          fileurl: res.data.imgaddr,
          name: res.data.originname,
          url: res.data.url
        })
        console.log('上传成功2:', this.fileList);
      } else {
        this.$message.error(res.msg || '上传失败')
      }
    },
    fail (err, file, fileList) {
      this.$emit('endUpload')
      this.$message.error('上传失败')
    },
    closeViewer () {
      this.showViewer = false
    },
    handleRemove (file) {
      const tempIndex = this.fileList.findIndex(item => item.url === file.url)
      if(tempIndex >= 0){
        this.fileList.splice(tempIndex, 1)
      }
    }
  }
}
</script>
 
<style lang="scss" scoped>
::v-deep .el-upload--picture-card{
  width: 90px !important;
  height: 90px !important;
}
::v-deep .el-upload-list__item {
  width: 90px !important;
  height: 90px !important;
}
.icon {
  -webkit-transform: translate(-50%,-50%);
  -ms-transform: translate(-50%,-50%);
  transform: translate(0%, -85%);
}
::v-deep .el-upload-list__item {
  width: 90px !important;
  height: 90px !important;
}
</style>