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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<template>
  <div>
    <el-upload
      class="upload-demo"
      :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-preview="handlePreview"
      :on-remove="handleRemove"
      :before-upload="beforeUpload"
      :on-success="handleUploadSuccess"
      :headers="headers"
      :drag="settings.drag"
    >
      <template v-if="settings.drag">
        <i class="el-icon-upload" />
        <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
      </template>
 
      <el-button v-if="!settings.drag" size="small" type="primary">{{ defaultSettings.title }}</el-button>
      <div v-if="defaultSettings.tip" slot="tip" class="el-upload__tip">{{ defaultSettings.tip }}</div>
      <div v-else slot="tip" class="el-upload__tip">只能上传{{ defaultSettings.num }}个格式为[{{ defaultSettings.accept }}]的文件,且不超过{{ defaultSettings.max/1024 }}M
      </div>
 
    </el-upload>
    <div id="uploadPreviewImages" style="display:none;">
      <span
        v-for="(src,index) in fileList"
        :key="index"
      >
        <img
          v-if="checkImg(src.name)"
          class="v-img"
          :src="src.url"
          :alt="src.name"
          style="width: 100px;height: 100px;"
        >
      </span>
    </div>
  </div>
</template>
 
<script>
import { getToken } from '@/utils/auth'
import { sessionToken } from '@/settings'
import { getFtpUrl } from '@/utils/base'
import Viewer from 'viewerjs'
import 'viewerjs/dist/viewer.css'
let viewer = null
export default {
  props: {
    values: {
      type: Array,
      default: function() {
        return []
      }
    },
    settings: {
      type: Object,
      default: function() {
        return {}
      }
    }
  },
  data() {
    return {
      headers: {
 
      },
      showList: [], // 用于详情展示
      fileList: [],
      defaultSettings: {
        title: '点击上传',
        max: 1024 * 100, // 最大大小,单位kb
        num: 1, // 支持上传图片个数
        accept: '.jpg,.jpeg,.png,.bmp,.pdf,.doc,.docx,.xls,.xlsx,.zip,.apk', // 限制格式
        tip: '', // 提示 默认:`只能上传${this.defaultSettings.num}个${this.defaultSettings.accept}文件,且不超过${this.defaultSettings.max}kb`
        uploadUrl: '', // 上传路径
        multiple: true, // 是否支持批量上传
        disabled: false, // 是否禁用
        type: 'picture' // text/picture
      }
    }
  },
  watch: {
    values(v) {
      this.fileList = Object.assign([], v)
      this.showList = Object.assign([], v)
      this.$nextTick(() => {
        this.initPreviewImg()
      })
    }
  },
  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)
    // console.log(this.defaultSettings)
    // this.$nextTick(() => {
    //   new Viewer(document.getElementsByClassName('el-upload-list'))
    // })
  },
  mounted() {
    this.initPreviewImg()
    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
        }
      }
 
      this.$nextTick(() => {
        this.initPreviewImg()
      })
    },
 
    handleExceed(files, fileList) {
      this.$message.warning('最多上传' + this.defaultSettings.num + '个文件')
    },
    beforeUpload(file) {
      // console.log('upload before')
      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 < this.defaultSettings.max
      if (!isImg) {
        this.$message.error('上传文件格式只能是 ' + this.defaultSettings.accept + ' 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传文件大小不能超过 ' + this.defaultSettings.max / 1024 + 'M!' + '当前文件大小:' + parseInt(file.size / 1024 / 1024) + 'M')
      }
      return isImg && isLt2M && isRepeat
    },
    handleUploadSuccess(response, file, fileList) {
      // console.log('upload sucess')
      // console.log(response.data)
      response.data.forEach(f => {
        this.fileList.push({
          uid: file.uid,
          name: f.name,
          url: this.getUrl(f.path),
          path: f.path,
          attSize: f.attSize || null,
          fileTime: f.fileTime || null
        })
      })
      this.$nextTick(() => {
        this.initPreviewImg()
      })
    },
    getContent() {
      return this.fileList
    },
    handlePreview(file) {
      if (!this.checkImg(file.name)) {
        return false
      }
      let index = 0
      for (let i = 0; i < this.fileList.length; i++) {
        const f = this.fileList[i]
        if (this.checkImg(f.name)) {
          if (file.uid == f.uid) {
            break
          }
          index++
        }
      }
      // this.fileList.forEach((f, i) => {
      //   if (file.uid == f.uid) {
      //     index = i
      //   }
      // })
      // document.querySelector('#uploadPreviewImages').children[0].click()
      viewer.view(index)
    },
    /*
     * @Author : liu.q [916000612@qq.com]
     * @Date : 2019-05-26 14:48
     * @Description : 初始化
     */
    initPreviewImg() {
      if (viewer != null) {
        viewer.destroy()
      }
      const ViewerDom = document.querySelector('#uploadPreviewImages')
      viewer = new Viewer(ViewerDom, {
 
      })
      // console.log(this.fileList)
    },
    /*
     * @Author : liu.q [916000612@qq.com]
     * @Date : 2019-05-26 14:48
     * @Description : 校验图片
     */
    checkImg(name) {
      const suffix = name.substring(name.lastIndexOf('.'), name.length)
      const imgArray = ['.jpg', '.jpeg', '.png', '.bmp']
      if (imgArray.indexOf(suffix) < 0) {
        return false
      }
      return true
    },
    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>