shikeying
2023-04-07 c192f834c4e092bc7c0f2722c343c25c1be619ab
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
<template>
  <div>
    <el-upload
      class="upload-demo"
      drag
      :action="defaultSettings.uploadUrl"
      :multiple="defaultSettings.multiple"
      name="file"
      :accept="defaultSettings.accept"
      :file-list="showList"
      :list-type="defaultSettings.type"
      :limit="defaultSettings.num"
      :disabled="defaultSettings.disabled"
      :on-exceed="handleExceed"
      :on-remove="handleRemove"
      :before-upload="beforeUpload"
      :on-success="handleUploadSuccess"
      :headers="headers"
    >
      <i class="el-icon-upload" />
      <div class="el-upload__text">将文件拖到此处,或<em>{{ defaultSettings.title }}</em></div>
      <div slot="tip" class="el-upload__tip">
        只能上传{{ defaultSettings.num }}个,且格式为[{{ defaultSettings.accept }}]的文件,
        且不超过{{ defaultSettings.max }}MB
      </div>
 
    </el-upload>
  </div>
</template>
 
<script>
import { getToken } from '@/utils/auth'
import { sessionToken } from '@/settings'
import { getFtpUrl } from '@/utils/base'
 
export default {
  props: {
    values: {
      type: Array,
      default: function() {
        return []
      }
    },
    settings: {
      type: Object,
      default: function() {
        return {}
      }
    }
  },
  data() {
    return {
      headers: {
 
      },
      showList: [], // 用于详情展示
      fileList: [],
      defaultSettings: {
        title: '点击上传',
        max: 100, // 最大大小,单位MB
        num: 1, // 支持上传文件个数
        accept: '.jpg,.png,.pdf,.doc,.docx,.xls,.xlsx,.zip,.txt,.rar,.ppt,.pptx,.xlt', // 限制格式
        tip: '', // 提示 默认:`只能上传${this.defaultSettings.num}个${this.defaultSettings.accept}文件,且不超过${this.defaultSettings.max}MB`
        uploadUrl: '', // 上传路径
        multiple: true, // 是否支持批量上传
        disabled: false, // 是否禁用
        type: 'text' // text/picture
      }
    }
  },
  watch: {
    values(v) {
      this.fileList = Object.assign([], v)
      this.showList = Object.assign([], v)
      this.showList.forEach(obj => {
        obj.url = this.getUrl(obj.path)
      })
      this.fileList.forEach(obj => {
        obj.url = this.getUrl(obj.path)
      })
    }
  },
  created() {
    this.showList = Object.assign([], this.values)
    this.fileList = Object.assign([], this.values)
 
    this.showList.forEach(obj => {
      obj.url = this.getUrl(obj.path)
    })
    this.fileList.forEach(obj => {
      obj.url = this.getUrl(obj.path)
    })
 
    this.defaultSettings = Object.assign(this.defaultSettings, this.settings)
  },
  mounted() {
    this.headers[sessionToken] = getToken()
  },
  methods: {
    handleRemove(file, fileList) {
      for (let i = 0; i < this.fileList.length; i++) {
        const img = this.fileList[i]
        if (file.uid == img.uid) {
          this.fileList.splice(i, 1)
          break
        }
      }
    },
 
    handleExceed(files, fileList) {
      this.$message.warning('最多上传' + this.defaultSettings.num + '个文件')
    },
    beforeUpload(file) {
      let isRepeat = true
      for (let i = 0; i < this.fileList.length; i++) {
        const f = this.fileList[i]
        if (file.name == f.name) {
          isRepeat = false
          break
        }
      }
 
      if (!isRepeat) {
        this.$message.error('该文件已存在!')
      }
      const isImg = this.defaultSettings.accept.split(',').indexOf(file.name.substr(file.name.lastIndexOf('.'), file.name.length).toLocaleString()) > -1
      const isLt2M = file.size / 1024 / 1024 < this.defaultSettings.max
      if (!isImg) {
        this.$message.error('上传文件格式只能是 ' + this.defaultSettings.accept + ' 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传文件大小不能超过 ' + this.defaultSettings.max + 'MB')
      }
      return isImg && isLt2M && isRepeat
    },
    handleUploadSuccess(response, file, fileList) {
      if (response.code == 10000) {
        response.data.forEach(f => {
          this.fileList.push({
            uid: file.uid,
            name: f.name,
            url: this.getUrl(f.path),
            path: f.path
          })
        })
      } else {
        // 上传失败,把显示的上传文件List中相应失败的文件剔除
        this.showList = this.fileList
        console.log('上传失败!')
        this.$message.warning('上传失败!')
      }
    },
    getContent() {
      return this.fileList
    },
    getUrl(path) {
      if (path.substr(0, 7).toLowerCase() == 'http://' || path.substr(0, 8).toLowerCase() == 'https://') {
        return path
      } else {
        return getFtpUrl() + path
      }
    }
  }
}
</script>
 
<style scoped>
  /* 图片预览层级*/
  .viewer-container {
    z-index: 90709102!important;
  }
 
</style>