MrShi
4 天以前 be3ec4c1f11a5e090408fcd6f650557651fcf007
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
<template>
  <div class="upload">
    <div class="upload-item" v-for="(item, index) in fileList" :key="index + '_' + item.url">
      <div class="dele" @click="handleDelete(index)">
        <i class="el-icon-delete"></i>
      </div>
      <el-image
        fit="widthFix"
        :preview-src-list="fileList.map(res => res.url)"
        :src="fileList[index].url"
      />
    </div>
    <div class="upload-item" @click="handleClick" v-if="fileList.length < maxCount">
      <i class="el-icon-plus"></i>
    </div>
    <input type="file" accept="image/*" ref="fileInput" style="opacity: 0; position: fixed; top: -100%; left: -100%;" @change="handleChangeFile">
  </div>
</template>
 
<script>
import { upload } from '@/api/system/common'
export default {
  props: {
    fileList: {
      type: Array,
      default: () => []
    },
    uploadData: Object,
    maxCount: {
      type: Number,
      default: 1
    }
  },
  data() {
    return {
      uploadImgUrl: process.env.VUE_APP_API_PREFIX + '/web/public/upload'
    }
  },
  methods: {
    handleDelete (index) {
      this.$emit('deleteRow', index)
    },
    handleClick () {
      this.$refs.fileInput.click()
    },
    handleChangeFile (e) {
      let file = e.target.files[0]
      if (!file) {
        return
      }
      let formData = new FormData()
      formData.append('file', file)
      formData.append('folder', this.uploadData.folder)
      upload(formData)
        .then(res => {
          this.$emit('getFileList', {
            fileurl: res.imgaddr,
            url: res.url
          })
        })
        .catch(e => {
          this.$tip.apiFailed(e)
        })
      }
  }
}
</script>
 
<style lang="scss" scoped>
  .upload {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    .upload-item {
      width: 90px;
      height: 90px;
      display: flex;
      align-items: center;
      justify-content: center;
      flex-direction: column;
      border: 1px solid #D9D9D9;
      border-radius: 4px;
      margin-right: 10px;
      margin-bottom: 10px;
      cursor: pointer;
      position: relative;
      .dele {
        position: absolute;
        top: 0;
        right: 0;
        z-index: 10;
        width: 25px;
        height: 25px;
        line-height: 25px;
        text-align: center;
        background-color: red;
        cursor: pointer;
        .el-icon-delete {
          font-size: 13px;
          color: #fff;
        }
      }
      .el-icon-plus {
        font-size: 24px;
        color: #909399;
      }
    }
  }
</style>