Blame view

ant-design-vue-jeecg/src/components/jeecg/JUpload.vue 14 KB
肖超群 authored
1
2
3
4
<template>
  <div :id="containerId" style="position: relative">

    <!--  ---------------------------- begin 图片左右换位置 ------------------------------------- -->
肖超群 authored
5
6
7
8
9
10
11
12
13
14
    <div class="movety-container" :style="{top:top+'px',left:left+'px',display:moveDisplay}"
         style="padding:0 8px;position: absolute;z-index: 91;height: 32px;width: 104px;text-align: center;">
      <div :id="containerId+'-mover'" :class="showMoverTask?'uploadty-mover-mask':'movety-opt'"
           style="margin-top: 12px">
        <a @click="moveLast" style="margin: 0 5px;">
          <a-icon type="arrow-left" style="color: #fff;font-size: 16px"/>
        </a>
        <a @click="moveNext" style="margin: 0 5px;">
          <a-icon type="arrow-right" style="color: #fff;font-size: 16px"/>
        </a>
肖超群 authored
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
      </div>
    </div>
    <!--  ---------------------------- end 图片左右换位置 ------------------------------------- -->

    <a-upload
      name="file"
      :multiple="multiple"
      :action="uploadAction"
      :headers="headers"
      :data="{'biz':bizPath}"
      :fileList="fileList"
      :beforeUpload="doBeforeUpload"
      @change="handleChange"
      :disabled="disabled"
      :returnUrl="returnUrl"
      :listType="complistType"
      @preview="handlePreview"
      :class="{'uploadty-disabled':disabled}">
      <template>
        <div v-if="isImageComp">
肖超群 authored
35
          <a-icon type="plus"/>
肖超群 authored
36
37
38
          <div class="ant-upload-text">{{ text }}</div>
        </div>
        <a-button v-else-if="buttonVisible">
肖超群 authored
39
40
          <a-icon type="upload"/>
          {{ text }}
肖超群 authored
41
42
43
44
        </a-button>
      </template>
    </a-upload>
    <a-modal :visible="previewVisible" :footer="null" @cancel="handleCancel">
肖超群 authored
45
      <img alt="example" style="width: 100%" :src="previewImage"/>
肖超群 authored
46
47
48
49
50
51
    </a-modal>
  </div>
</template>

<script>
肖超群 authored
52
53
54
import Vue from 'vue'
import {ACCESS_TOKEN} from "@/store/mutation-types"
import {getFileAccessHttpUrl} from '@/api/manage';
肖超群 authored
55
肖超群 authored
56
57
58
59
60
61
62
63
64
65
const FILE_TYPE_ALL = "all"
const FILE_TYPE_IMG = "image"
const FILE_TYPE_TXT = "file"
const uidGenerator = () => {
  return '-' + parseInt(Math.random() * 10000 + 1, 10);
}
const getFileName = (path) => {
  if (path.lastIndexOf("\\") >= 0) {
    let reg = new RegExp("\\\\", "g");
    path = path.replace(reg, "/");
肖超群 authored
66
  }
肖超群 authored
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
  return path.substring(path.lastIndexOf("/") + 1);
}
export default {
  name: 'JUpload',
  data() {
    return {
      uploadAction: window._CONFIG['domianURL'] + "/sys/common/upload",
      headers: {},
      fileList: [],
      newFileList: [],
      uploadGoOn: true,
      previewVisible: false,
      //---------------------------- begin 图片左右换位置 -------------------------------------
      previewImage: '',
      containerId: '',
      top: '',
      left: '',
      moveDisplay: 'none',
      showMoverTask: false,
      moverHold: false,
      currentImg: ''
      //---------------------------- end 图片左右换位置 -------------------------------------
肖超群 authored
89
    }
肖超群 authored
90
91
92
93
94
95
  },
  props: {
    text: {
      type: String,
      required: false,
      default: "点击上传"
肖超群 authored
96
    },
肖超群 authored
97
98
99
100
    fileType: {
      type: String,
      required: false,
      default: FILE_TYPE_ALL
肖超群 authored
101
    },
肖超群 authored
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
    /*这个属性用于控制文件上传的业务路径*/
    bizPath: {
      type: String,
      required: false,
      default: "temp"
    },
    value: {
      type: [String, Array],
      required: false
    },
    // update-begin- --- author:wangshuai ------ date:20190929 ---- for:Jupload组件增加是否能够点击
    disabled: {
      type: Boolean,
      required: false,
      default: false
    },
    // update-end- --- author:wangshuai ------ date:20190929 ---- for:Jupload组件增加是否能够点击
    //此属性被废弃了
    triggerChange: {
      type: Boolean,
      required: false,
      default: false
    },
    /**
     * update -- author:lvdandan -- date:20190219 -- for:Jupload组件增加是否返回url,
     * true:仅返回url
     * false:返回fileName filePath fileSize
     */
    returnUrl: {
      type: Boolean,
      required: false,
      default: true
    },
    number: {
      type: Number,
      required: false,
      default: 0
    },
    buttonVisible: {
      type: Boolean,
      required: false,
      default: true
    },
    multiple: {
      type: Boolean,
      default: true
    },
    beforeUpload: {
      type: Function
    },
  },
  watch: {
    value: {
      immediate: true,
      handler() {
        let val = this.value
        if (val instanceof Array) {
          if (this.returnUrl) {
            this.initFileList(val.join(','))
肖超群 authored
161
          } else {
肖超群 authored
162
            this.initFileListArr(val);
肖超群 authored
163
          }
肖超群 authored
164
165
        } else {
          this.initFileList(val)
肖超群 authored
166
167
        }
      }
肖超群 authored
168
169
170
171
172
    }
  },
  computed: {
    isImageComp() {
      return this.fileType === FILE_TYPE_IMG
肖超群 authored
173
    },
肖超群 authored
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
    complistType() {
      return this.fileType === FILE_TYPE_IMG ? 'picture-card' : 'text'
    }
  },
  created() {
    const token = Vue.ls.get(ACCESS_TOKEN);
    //---------------------------- begin 图片左右换位置 -------------------------------------
    this.headers = {"X-Access-Token": token};
    this.containerId = 'container-ty-' + new Date().getTime();
    //---------------------------- end 图片左右换位置 -------------------------------------
  },

  methods: {
    initFileListArr(val) {
      if (!val || val.length == 0) {
        this.fileList = [];
        return;
肖超群 authored
191
      }
肖超群 authored
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
      let fileList = [];
      for (var a = 0; a < val.length; a++) {
        let url = getFileAccessHttpUrl(val[a].filePath);
        fileList.push({
          uid: uidGenerator(),
          name: val[a].fileName,
          status: 'done',
          url: url,
          response: {
            status: "history",
            message: val[a].filePath
          }
        })
      }
      this.fileList = fileList
肖超群 authored
207
    },
肖超群 authored
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
    initFileList(paths) {
      if (!paths || paths.length == 0) {
        //return [];
        // update-begin- --- author:os_chengtgen ------ date:20190729 ---- for:issues:326,Jupload组件初始化bug
        this.fileList = [];
        return;
        // update-end- --- author:os_chengtgen ------ date:20190729 ---- for:issues:326,Jupload组件初始化bug
      }
      let fileList = [];
      let arr = paths.split(",")
      for (var a = 0; a < arr.length; a++) {
        let url = getFileAccessHttpUrl(arr[a]);
        fileList.push({
          uid: uidGenerator(),
          name: getFileName(arr[a]),
          status: 'done',
          url: url,
          response: {
            status: "history",
            message: arr[a]
          }
        })
      }
      this.fileList = fileList
肖超群 authored
232
    },
肖超群 authored
233
234
235
236
237
238
239
    handlePathChange() {
      let uploadFiles = this.fileList
      let path = ''
      if (!uploadFiles || uploadFiles.length == 0) {
        path = ''
      }
      let arr = [];
肖超群 authored
240
肖超群 authored
241
242
243
244
245
      for (var a = 0; a < uploadFiles.length; a++) {
        // update-begin-author:lvdandan date:20200603 for:【TESTA-514】【开源issue】多个文件同时上传时,控制台报错
        if (uploadFiles[a].status === 'done') {
          arr.push(uploadFiles[a].response.message)
        } else {
肖超群 authored
246
247
          return;
        }
肖超群 authored
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
        // update-end-author:lvdandan date:20200603 for:【TESTA-514】【开源issue】多个文件同时上传时,控制台报错
      }
      if (arr.length > 0) {
        path = arr.join(",")
      }
      this.$emit('change', path);
    },
    doBeforeUpload(file) {
      this.uploadGoOn = true
      var fileType = file.type;
      if (this.fileType === FILE_TYPE_IMG) {
        if (fileType.indexOf('image') < 0) {
          this.$message.warning('请上传图片');
          this.uploadGoOn = false
          return false;
肖超群 authored
263
        }
肖超群 authored
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
      }
      // 扩展 beforeUpload 验证
      if (typeof this.beforeUpload === 'function') {
        return this.beforeUpload(file)
      }
      return true
    },
    handleChange(info) {
      console.log("--文件列表改变--")
      if (!info.file.status && this.uploadGoOn === false) {
        info.fileList.pop();
      }
      let fileList = info.fileList
      if (info.file.status === 'done') {
        if (this.number > 0) {
          fileList = fileList.slice(-this.number);
肖超群 authored
280
        }
肖超群 authored
281
282
283
284
285
        if (info.file.response.success) {
          fileList = fileList.map((file) => {
            if (file.response) {
              let reUrl = file.response.message;
              file.url = getFileAccessHttpUrl(reUrl);
肖超群 authored
286
            }
肖超群 authored
287
288
            return file;
          });
肖超群 authored
289
        }
肖超群 authored
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
        //this.$message.success(`${info.file.name} 上传成功!`);
      } else if (info.file.status === 'error') {
        this.$message.error(`${info.file.name} 上传失败.`);
      } else if (info.file.status === 'removed') {
        this.handleDelete(info.file)
      }
      this.fileList = fileList
      if (info.file.status === 'done' || info.file.status === 'removed') {
        //returnUrl为true时仅返回文件路径
        if (this.returnUrl) {
          this.handlePathChange()
        } else {
          //returnUrl为false时返回文件名称、文件路径及文件大小
          this.newFileList = [];
          for (var a = 0; a < fileList.length; a++) {
            // update-begin-author:lvdandan date:20200603 for:【TESTA-514】【开源issue】多个文件同时上传时,控制台报错
            if (fileList[a].status === 'done') {
              var fileJson = {
                fileName: fileList[a].name,
                filePath: fileList[a].response.message,
                fileSize: fileList[a].size
              };
              this.newFileList.push(fileJson);
            } else {
              return;
肖超群 authored
315
            }
肖超群 authored
316
            // update-end-author:lvdandan date:20200603 for:【TESTA-514】【开源issue】多个文件同时上传时,控制台报错
肖超群 authored
317
          }
肖超群 authored
318
          this.$emit('change', this.newFileList);
肖超群 authored
319
        }
肖超群 authored
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
      }
    },
    handleDelete(file) {
      //如有需要新增 删除逻辑
      console.log(file)
    },
    handlePreview(file) {
      if (this.fileType === FILE_TYPE_IMG) {
        this.previewImage = file.url || file.thumbUrl;
        this.previewVisible = true;
      } else {
        location.href = file.url
      }
    },
    handleCancel() {
      this.previewVisible = false;
    },
    //---------------------------- begin 图片左右换位置 -------------------------------------
    moveLast() {
      //console.log(ev)
      //console.log(this.fileList)
      //console.log(this.currentImg)
      let index = this.getIndexByUrl();
      if (index == 0) {
        this.$message.warn('未知的操作')
      } else {
        let curr = this.fileList[index].url;
        let last = this.fileList[index - 1].url;
        let arr = []
        for (let i = 0; i < this.fileList.length; i++) {
          if (i == index - 1) {
            arr.push(curr)
          } else if (i == index) {
            arr.push(last)
          } else {
            arr.push(this.fileList[i].url)
肖超群 authored
356
357
          }
        }
肖超群 authored
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
        this.currentImg = last
        this.$emit('change', arr.join(','))
      }
    },
    moveNext() {
      let index = this.getIndexByUrl();
      if (index == this.fileList.length - 1) {
        this.$message.warn('已到最后~')
      } else {
        let curr = this.fileList[index].url;
        let next = this.fileList[index + 1].url;
        let arr = []
        for (let i = 0; i < this.fileList.length; i++) {
          if (i == index + 1) {
            arr.push(curr)
          } else if (i == index) {
            arr.push(next)
          } else {
            arr.push(this.fileList[i].url)
肖超群 authored
377
378
          }
        }
肖超群 authored
379
380
        this.currentImg = next
        this.$emit('change', arr.join(','))
肖超群 authored
381
382
      }
    },
肖超群 authored
383
384
385
386
387
    getIndexByUrl() {
      for (let i = 0; i < this.fileList.length; i++) {
        if (this.fileList[i].url === this.currentImg || encodeURI(this.fileList[i].url) === this.currentImg) {
          return i;
        }
肖超群 authored
388
      }
肖超群 authored
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
      return -1;
    }
  },
  mounted() {
    const moverObj = document.getElementById(this.containerId + '-mover');
    if (moverObj) {
      moverObj.addEventListener('mouseover', () => {
        this.moverHold = true
        this.moveDisplay = 'block';
      });
      moverObj.addEventListener('mouseout', () => {
        this.moverHold = false
        this.moveDisplay = 'none';
      });
    }
肖超群 authored
404
肖超群 authored
405
406
407
408
409
410
411
412
413
414
415
416
417
    let picList = document.getElementById(this.containerId) ? document.getElementById(this.containerId).getElementsByClassName('ant-upload-list-picture-card') : [];
    if (picList && picList.length > 0) {
      picList[0].addEventListener('mouseover', (ev) => {
        ev = ev || window.event;
        let target = ev.target || ev.srcElement;
        if ('ant-upload-list-item-info' == target.className) {
          this.showMoverTask = false
          let item = target.parentElement
          this.left = item.offsetLeft
          this.top = item.offsetTop + item.offsetHeight - 50;
          this.moveDisplay = 'block';
          this.currentImg = target.getElementsByTagName('img')[0].src
        }
肖超群 authored
418
肖超群 authored
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
      });

      picList[0].addEventListener('mouseout', (ev) => {
        ev = ev || window.event;
        let target = ev.target || ev.srcElement;
        //console.log('移除',target)
        if ('ant-upload-list-item-info' == target.className) {
          this.showMoverTask = true
          setTimeout(() => {
            if (this.moverHold === false)
              this.moveDisplay = 'none';
          }, 100)
        }
        if ('ant-upload-list-item ant-upload-list-item-done' == target.className || 'ant-upload-list ant-upload-list-picture-card' == target.className) {
          this.moveDisplay = 'none';
        }
      })
      //---------------------------- end 图片左右换位置 -------------------------------------
肖超群 authored
437
    }
肖超群 authored
438
439
440
441
  },
  model: {
    prop: 'value',
    event: 'change'
肖超群 authored
442
  }
肖超群 authored
443
}
肖超群 authored
444
445
446
</script>

<style lang="less">
肖超群 authored
447
.uploadty-disabled {
肖超群 authored
448
  .ant-upload-list-item {
肖超群 authored
449
    .anticon-close {
肖超群 authored
450
451
      display: none;
    }
肖超群 authored
452
453

    .anticon-delete {
肖超群 authored
454
455
456
457
      display: none;
    }
  }
}
肖超群 authored
458
459
460
461
462
463
464
465
466
467
468

//---------------------------- begin 图片左右换位置 -------------------------------------
.uploadty-mover-mask {
  background-color: rgba(0, 0, 0, 0.5);
  opacity: .8;
  color: #fff;
  height: 28px;
  line-height: 28px;
}

//---------------------------- end 图片左右换位置 -------------------------------------
肖超群 authored
469
</style>