You need to sign in or sign up before continuing.
index.vue 13.6 KB
Newer Older
何虹's avatar
何虹 committed
1 2 3 4 5 6 7 8 9 10 11 12
<template>
  <div
    ref="box"
    :id="elId"
  >
    <!-- <div @click="dialogVisible=true">
      附件📎

    </div> -->
    <!-- <el-link el-icon-view @click="dialogVisible=true"> 附件📎</el-link> -->
    <el-link @click="dialogVisible=true">
      <div class="showTxt">
何虹's avatar
何虹 committed
13 14 15 16
        <p
          class="showFiletxt"
          v-show="fileName"
        >{{fileName}}</p>
何虹's avatar
何虹 committed
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
        <div>{{configData.label}}
          <i class="el-icon-s-operation el-icon--right"></i>
        </div>
      </div>
    </el-link>
    <el-dialog
      title="附件上传预览"
      :visible.sync="dialogVisible"
      width="80%"
      center
      :before-close="handleClose"
      :fullscreen='isfullscreen'
      :append-to-body='true'
      @open='openDialog'
    >
      <div class="file_imags">
        <div class="header_box">
          <div
            v-show="!hideInput"
            class="file"
          >
            选择文件
            <input
              type="file"
              id="avatar"
              name="avatar"
              :accept="configData.accept"
              :multiple='configData.multiple'
              @change='handleFileChange'
            />
          </div>
          <div v-show="hideInput">
            仅支持单文件上传,若需继续,请删除已有文件~
          </div>
          <div>
            <el-button
              size="mini"
              type="danger"
              @click="upload"
            >上传到服务器</el-button>
            <!-- <div  @click="isfullscreen=!isfullscreen">全屏</div> -->
            <i
              @click="isfullscreen=!isfullscreen"
              class="el-icon-full-screen fullScreen"
            ></i>
          </div>
        </div>
        <div class="imgBox">
          <div class="fileTypeItem">
            <div class="title_type">
              <i class="el-icon-house"></i>待上传文件:
            </div>
            <div class="allbox">
              <div
                class="file_item"
                v-for="(item,index) in  fileList"
                :key='index'
              >
                <div class="indexTitle">
                  <label class="indexText">{{index+1}}</label>
                  <div class="fileName">{{item.NewFileName}}</div>
                </div>
                <div class="image_tool">
                  <el-image
                    v-if="item.file"
                    style="width: 100px; height: 100px"
                    :src='item.file'
                    :preview-src-list="item.previewList"
                  >
                  </el-image>
                  <div class="tool">
                    <el-button
                      v-show="false"
                      size="mini"
                      @click="preview(item,index)"
                    >预览</el-button>
                    <el-button
                      type="danger"
                      size="mini"
                      slot="reference"
                      @click="deleteCurrImage(index)"
                      icon="el-icon-delete"
                      circle
                    ></el-button>
                  </div>
                </div>

              </div>
            </div>
          </div>
          <div class="fileTypeItem">
            <div class="title_type"><i class="el-icon-thumb"></i>服务器文件:</div>
            <div class="allbox">
              <div
                class="file_item"
                v-for="(item,index) in  newFileList"
                :key='index'
              >
                <div class="indexTitle">
                  <label class="indexText">{{index+1}}</label>
                  <div class="fileName">{{item.OldFileName}}</div>
                </div>
                <div class="image_tool">
                  <el-image
                    v-if="item.file"
                    style="width: 100px; height: 100px"
                    :src='item.file'
                    :preview-src-list="item.fileListImage"
                  >
                  </el-image>
                  <div class="tool">
                    <el-button
                      v-show="false"
                      size="mini"
                      @click="preview(item,index)"
                    >预览</el-button>
                    <el-button
                      type="danger"
                      size="mini"
                      slot="reference"
                      @click="deleteExitFile(index)"
                      icon="el-icon-delete"
                      circle
                    ></el-button>
                  </div>
                </div>

              </div>
            </div>
          </div>

        </div>
      </div>
    </el-dialog>
    <jsoneditor
      v-model='jsoneditorVisible'
      :elInfo='elInfo'
      :layout='layout'
      :jsoneditorCloseAfter='jsoneditorCloseAfter'
      :jsoneditorOpenAfter='jsoneditorOpenAfter'
    >
    </jsoneditor>
  </div>
</template>

<script>
何虹's avatar
何虹 committed
163 164 165 166
import ucComponent from '../ucClass/uc_component'
import jsoneditor from '../common/jsoneditor'
import commonUtility from '../funTools/commonUtility'
import _ from 'lodash'
何虹's avatar
何虹 committed
167 168 169 170 171
export default {
  mixins: [ucComponent],
  components: {
    jsoneditor
  },
何虹's avatar
何虹 committed
172
  name: 'hs-file',
何虹's avatar
何虹 committed
173
  props: {
何虹's avatar
何虹 committed
174 175 176
    uploadFileFun: {
      type: Function
    },
何虹's avatar
何虹 committed
177 178 179
    allSourceData: {
      type: Object,
      default() {
何虹's avatar
何虹 committed
180
        return {}
何虹's avatar
何虹 committed
181 182 183 184 185
      }
    },
    elInfo: {
      type: Object,
      default() {
何虹's avatar
何虹 committed
186
        return {}
何虹's avatar
何虹 committed
187 188
      }
    },
何虹's avatar
何虹 committed
189
    value: '',
何虹's avatar
何虹 committed
190 191 192 193 194 195 196 197 198 199 200
    layout: {},
    jsoneditorCloseAfter: {
      type: Function
    },
    jsoneditorOpenAfter: {
      type: Function
    }
  },
  watch: {
    allSourceData: {
      handler: function(newVal, oldVal) {
何虹's avatar
何虹 committed
201 202
        const { config } = newVal
        this.initConfig(config)
何虹's avatar
何虹 committed
203 204 205 206
      },
      deep: true
    },
    value(val) {
何虹's avatar
何虹 committed
207 208
      this.value_inner = val
      this.initData()
何虹's avatar
何虹 committed
209 210
    },
    value_inner(val) {
何虹's avatar
何虹 committed
211
      this.$emit('input', val)
何虹's avatar
何虹 committed
212 213 214
    },
    fileList: {
      handler: function(newVal, oldVal) {
何虹's avatar
何虹 committed
215
        this.changeBtnStatus(newVal)
何虹's avatar
何虹 committed
216 217 218 219 220
      },
      deep: true
    },
    newFileList: {
      handler: function(newVal, oldVal) {
何虹's avatar
何虹 committed
221
        this.initValue(newVal)
何虹's avatar
何虹 committed
222 223 224 225 226 227
      },
      deep: true
    }
  },
  data() {
    return {
何虹's avatar
何虹 committed
228 229
      elId: '',
      value_inner: '',
何虹's avatar
何虹 committed
230 231 232 233 234 235 236 237 238 239 240 241 242
      jsoneditorVisible: false,
      sourceData: [],
      visible_popover: false,
      fileList: [],
      fileListImage: [],
      fileListNoImage: [],
      dialogVisible: false,
      newFileList: [],
      files: [],
      isfullscreen: false,
      multiple: true,
      configData: {
        multiple: true,
何虹's avatar
何虹 committed
243 244 245
        accept: '*',
        appCode: '',
        label: '查看附件'
何虹's avatar
何虹 committed
246
      },
何虹's avatar
何虹 committed
247
      appCode: '',
何虹's avatar
何虹 committed
248
      hideInput: false,
何虹's avatar
何虹 committed
249 250
      fileName: ''
    }
何虹's avatar
何虹 committed
251 252 253
  },
  mounted() {
    this.$nextTick(() => {
何虹's avatar
何虹 committed
254 255 256
      const { config } = this.allSourceData
      this.initConfig(config)
    })
何虹's avatar
何虹 committed
257 258 259
  },

  methods: {
何虹's avatar
何虹 committed
260
    initValue(newVal) {
何虹's avatar
何虹 committed
261
      const data = _.cloneDeep(newVal)
何虹's avatar
何虹 committed
262
      data.forEach(item => {
何虹's avatar
何虹 committed
263 264 265 266 267
        delete item.fileListImage
        delete item.file
        delete item.name
      })
      this.value_inner = JSON.stringify(data)
何虹's avatar
何虹 committed
268
    },
何虹's avatar
何虹 committed
269 270
    changeBtnStatus(data) {
      if (data.length >= 1) {
何虹's avatar
何虹 committed
271
        this.hideInput = true
何虹's avatar
何虹 committed
272
      } else {
何虹's avatar
何虹 committed
273
        this.hideInput = false
何虹's avatar
何虹 committed
274 275 276
      }
    },
    openDialog() {
何虹's avatar
何虹 committed
277
      this.initData()
何虹's avatar
何虹 committed
278 279
    },
    initData() {
何虹's avatar
何虹 committed
280 281 282
      this.newFileList = this.value ? JSON.parse(this.value) : []
      const { config } = this.allSourceData
      this.initConfig(config)
何虹's avatar
何虹 committed
283
      this.newFileList.forEach(item => {
何虹's avatar
何虹 committed
284
        const { NewFileName } = item
何虹's avatar
何虹 committed
285 286
        this.$set(
          item,
何虹's avatar
何虹 committed
287
          'file',
何虹's avatar
何虹 committed
288
          `fileresourceAPI/file/${this.appCode}/${NewFileName}/`
何虹's avatar
何虹 committed
289 290
        )
        this.$set(item, 'fileListImage', [
何虹's avatar
何虹 committed
291
          `fileresourceAPI/file/${this.appCode}/${NewFileName}/`
何虹's avatar
何虹 committed
292 293
        ])
      })
何虹's avatar
何虹 committed
294 295
    },
    initConfig(config = {}) {
何虹's avatar
何虹 committed
296 297 298 299 300 301
      this.configData = Object.assign(this.configData, config)
      const appCode_ = this.configData.appCode
      const { appCode } = commonUtility.parseUrlQueryBIDyncAppcodePageName()
      const appCode__ = appCode_ || appCode
      this.appCode = appCode__
      const { multiple } = this.configData
何虹's avatar
何虹 committed
302
      if (!multiple && this.newFileList.length) {
何虹's avatar
何虹 committed
303
        this.fileName = this.newFileList[0].OldFileName
何虹's avatar
何虹 committed
304 305 306
      }
    },
    handleClose(done) {
何虹's avatar
何虹 committed
307
      this.$confirm('确认关闭?')
何虹's avatar
何虹 committed
308
        .then(_ => {
何虹's avatar
何虹 committed
309
          done()
何虹's avatar
何虹 committed
310
        })
何虹's avatar
何虹 committed
311
        .catch(_ => {})
何虹's avatar
何虹 committed
312 313
    },
    imgPreview(file) {
何虹's avatar
何虹 committed
314
      const self = this
何虹's avatar
何虹 committed
315
      // 看支持不支持FileReader
何虹's avatar
何虹 committed
316
      if (!file || !window.FileReader) return
何虹's avatar
何虹 committed
317
      // 创建一个reader
何虹's avatar
何虹 committed
318
      const reader = new FileReader()
何虹's avatar
何虹 committed
319
      // 将图片将转成 base64 格式
何虹's avatar
何虹 committed
320
      reader.readAsDataURL(file)
何虹's avatar
何虹 committed
321 322
      // 读取成功后的回调
      reader.onloadend = function() {
何虹's avatar
何虹 committed
323
        const type = file.type
何虹's avatar
何虹 committed
324
        if (/^image/.test(type)) {
何虹's avatar
何虹 committed
325
          self.fileListImage.push(this.result)
何虹's avatar
何虹 committed
326 327 328
        } else {
          self.fileListNoImage.push({
            name: file.name
何虹's avatar
何虹 committed
329
          })
何虹's avatar
何虹 committed
330 331 332 333 334 335 336 337
        }
        self.fileList.push({
          type: file.type,
          NewFileName: file.name,
          OldFileName: file.name,
          file: this.result,
          fileBrod: file,
          previewList: [this.result]
何虹's avatar
何虹 committed
338 339
        })
      }
何虹's avatar
何虹 committed
340 341
    },
    handleFileChange(e) {
何虹's avatar
何虹 committed
342 343 344
      const inputDOM = document.getElementById('avatar')
      this.file = inputDOM.files[0]
      this.files = Array.from(inputDOM.files)
何虹's avatar
何虹 committed
345 346
      // 在获取到文件对象进行预览就行了!
      this.files.forEach(item => {
何虹's avatar
何虹 committed
347 348
        this.imgPreview(item)
      })
何虹's avatar
何虹 committed
349
      // 清除当前input 值
何虹's avatar
何虹 committed
350
      e.target.value = ''
何虹's avatar
何虹 committed
351 352
    },
    deleteCurrImage(image, index) {
何虹's avatar
何虹 committed
353
      this.fileList.splice(index, 1)
何虹's avatar
何虹 committed
354 355
    },
    preview(item, index) {
何虹's avatar
何虹 committed
356 357 358
      const { name } = item
      this.$set(item, 'file', `fileresourceAPI/file/${this.appCode}/${name}/`)
      this.$set(item, 'fileListImage', [
何虹's avatar
何虹 committed
359
        `fileresourceAPI/file/${this.appCode}/${name}/`
何虹's avatar
何虹 committed
360
      ])
何虹's avatar
何虹 committed
361 362
    },
    deleteFile(item, index) {
何虹's avatar
何虹 committed
363
      this.fileList.splice(index, 1)
何虹's avatar
何虹 committed
364 365
    },
    deleteExitFile(item, index) {
何虹's avatar
何虹 committed
366
      this.newFileList.splice(index, 1)
何虹's avatar
何虹 committed
367 368
    },
    async upload() {
何虹's avatar
何虹 committed
369
      const formData = new FormData()
何虹's avatar
何虹 committed
370
      if (!this.fileList.length) {
何虹's avatar
何虹 committed
371 372
        this.$message.error('请选择文件!')
        return
何虹's avatar
何虹 committed
373
      }
何虹's avatar
何虹 committed
374
      const { multiple } = this.configData
何虹's avatar
何虹 committed
375 376
      if (!multiple) {
        if (this.newFileList.length >= 1) {
何虹's avatar
何虹 committed
377 378
          this.$message.error('仅支持单文件上传,请手动删除服务器文件后再试!')
          return
何虹's avatar
何虹 committed
379 380 381
        }
      }
      this.fileList.forEach(element => {
何虹's avatar
何虹 committed
382 383 384 385 386
        const { NewFileName, fileBrod } = element
        formData.append(NewFileName, fileBrod)
      })
      const newFileList = []
      const res = await this.uploadFileFun(formData)
何虹's avatar
何虹 committed
387
      const resData = (res.data || []).filter(item => {
何虹's avatar
何虹 committed
388 389
        return item.Sucess
      })
何虹's avatar
何虹 committed
390
      const resDataError = (res.data || []).filter(item => {
何虹's avatar
何虹 committed
391 392 393
        return !item.Sucess
      })
      let errorStr = ''
何虹's avatar
何虹 committed
394
      resDataError.forEach(jtem => {
何虹's avatar
何虹 committed
395 396 397
        const { Error, OldFileName } = jtem
        errorStr += `${OldFileName}:${Error}`
      })
何虹's avatar
何虹 committed
398
      if (errorStr) {
何虹's avatar
何虹 committed
399
        this.$message.error(errorStr)
何虹's avatar
何虹 committed
400
      }
何虹's avatar
何虹 committed
401
      console.log(this.appCode)
何虹's avatar
何虹 committed
402

何虹's avatar
何虹 committed
403 404 405 406 407 408 409 410
      resData.forEach(item => {
        newFileList.push({
          OldFileName: item.OldFileName,
          NewFileName: item.NewFileName,
          file: `fileresourceAPI/file/${this.appCode}/${item.NewFileName}/`,
          fileListImage: [
            `fileresourceAPI/file/${this.appCode}/${item.NewFileName}/`
          ]
何虹's avatar
何虹 committed
411
        })
何虹's avatar
何虹 committed
412 413
        const targetIndex = this.fileList.findIndex(
          item_ => item_.NewFileName === item.OldFileName
何虹's avatar
何虹 committed
414 415 416 417
        )
        this.fileList.splice(targetIndex, 1)
      })
      this.newFileList.push(...newFileList)
何虹's avatar
何虹 committed
418
      if (!multiple && this.newFileList.length) {
何虹's avatar
何虹 committed
419
        this.fileName = this.newFileList[0].OldFileName
何虹's avatar
何虹 committed
420
      }
何虹's avatar
何虹 committed
421 422 423
      const { uploaded } = this.configData
      if (typeof uploaded === 'function') {
        uploaded(this.newFileList)
何虹's avatar
何虹 committed
424
      }
何虹's avatar
何虹 committed
425
      localStorage['exitImage'] = JSON.stringify(this.newFileList)
何虹's avatar
何虹 committed
426 427
    }
  }
何虹's avatar
何虹 committed
428
}
何虹's avatar
何虹 committed
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
</script>
<style scoped>
.file {
  position: relative;
  display: inline-block;
  background: #d0eeff;
  border: 1px solid #99d3f5;
  border-radius: 4px;
  padding: 4px 12px;
  overflow: hidden;
  color: #1e88c7;
  text-decoration: none;
  text-indent: 0;
  line-height: 20px;
  height: 20px;
}
.file input {
  position: absolute;
  font-size: 100px;
  right: 0;
  top: 0;
  opacity: 0;
}
.file:hover {
  background: #aadffd;
  border-color: #78c3f3;
  color: #004974;
  text-decoration: none;
}
.innerImageBox {
  position: relative;
  margin: 5px;
  border: 1px solid #9e9e9e;
}
.delete_icon {
  position: absolute;
  right: -8px;
  top: -7px;
  color: red;
}
.fileTypeItem {
  margin: 10px;
}
.otherFileBox {
  margin: 5px;
  border: 1px solid #9e9e9e;
  position: relative;
}
.otherFileOutBox {
  display: flex;
}
.imgOutBox {
  display: flex;
  flex-wrap: wrap;
}
.allbox {
  display: flex;
  flex-wrap: wrap;
}
.file_item {
  margin: 10px;
  padding: 5px;
  border: 1px solid gainsboro;
  box-shadow: 1px 4px 5px #888888;
}
.image_tool {
  display: flex;
  justify-content: space-around;
}
.tool {
  display: flex;
  align-items: flex-end;
}
.header_box {
  display: flex;
  justify-content: space-between;
}
.title_type {
  border-bottom: 1px solid #ddd;
}
.title_type > i {
  color: red;
}
.indexTitle {
  display: flex;
}
.fileName {
  width: 140px;
  white-space: nowrap;
  overflow-x: scroll;
}
.fileName::-webkit-scrollbar {
  width: 0px;
  height: 0px;
  /**/
}
.fileName::-webkit-scrollbar-track {
  background: rgb(239, 239, 239);
  border-radius: 2px;
}
.fileName::-webkit-scrollbar-thumb {
  background: #bfbfbf;
  border-radius: 10px;
}
.fileName::-webkit-scrollbar-thumb:hover {
  background: #333;
}
.fileName::-webkit-scrollbar-corner {
  background: #179a16;
}
.fullScreen {
  position: absolute;
  right: 50px;
  top: 25px;
}
.indexText {
  color: red;
  margin-right: 5px;
}
何虹's avatar
何虹 committed
548 549
.showFiletxt {
  height: 18px;
何虹's avatar
何虹 committed
550 551
}
</style>