index.vue 13.7 KB
Newer Older
何虹's avatar
何虹 committed
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 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 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 315 316 317 318 319 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 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 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
<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">
        <p class="showFiletxt" v-show="fileName">{{fileName}}</p>
        <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>
import ucComponent from '../ucClass/uc_component'
import jsoneditor from '../common/jsoneditor'
import commonUtility from '../funTools/commonUtility'
import _ from 'lodash'
export default {
  mixins: [ucComponent],
  components: {
    jsoneditor
  },
  name: 'hsFile',
  props: {
    allSourceData: {
      type: Object,
      default() {
        return {}
      }
    },
    elInfo: {
      type: Object,
      default() {
        return {}
      }
    },
    value: '',
    layout: {},
    jsoneditorCloseAfter: {
      type: Function
    },
    jsoneditorOpenAfter: {
      type: Function
    }
  },
  watch: {
    allSourceData: {
      handler: function(newVal, oldVal) {
        const { config } = newVal
        this.initConfig(config)
      },
      deep: true
    },
    value(val) {
      this.value_inner = val
      this.initData()
    },
    value_inner(val) {
      this.$emit('input', val)
    },
    fileList: {
      handler: function(newVal, oldVal) {
        this.changeBtnStatus(newVal)
      },
      deep: true
    },
    newFileList: {
      handler: function(newVal, oldVal) {
        const data = _.cloneDeep(newVal)
        data.forEach(item => {
          delete item.fileListImage
          delete item.file
          delete item.name
        })
        this.value_inner = JSON.stringify(data)
        // this.changeBtnStatus(data)
      },
      deep: true
    }
  },
  data() {
    return {
      elId: '',
      value_inner: '',
      jsoneditorVisible: false,
      sourceData: [],
      visible_popover: false,
      fileList: [],
      fileListImage: [],
      fileListNoImage: [],
      dialogVisible: false,
      newFileList: [],
      files: [],
      isfullscreen: false,
      multiple: true,
      configData: {
        multiple: true,
        accept: '*',
        appCode: '',
        label: '查看附件'
      },
      appCode: '',
      hideInput: false,
      fileName: ''
    }
  },
  mounted() {
    this.$nextTick(() => {
      const { config } = this.allSourceData
      this.initConfig(config)
    })
    // const exitImage = localStorage['exitImage']
    // if(this.value){
    //   const data=JSON.parse(this.value)
    // }
    // console.log(this.value, 'append-to-body')
    // this.initData()
  },

  methods: {
    changeBtnStatus(data) {
      if (data.length >= 1) {
        this.hideInput = true
      } else {
        this.hideInput = false
      }
    },
    openDialog() {
      this.initData()
    },
    initData() {
      if (this.value) {
        this.newFileList = JSON.parse(this.value)
        const { config } = this.allSourceData
        this.initConfig(config)
        const appCode_ = this.configData.appCode
        const { appCode } = commonUtility.parseUrlQueryBIDyncAppcodePageName()
        const appCode__ = appCode_ || appCode
        this.appCode = appCode__
        this.newFileList.forEach(item => {
          const { NewFileName } = item
          this.$set(
            item,
            'file',
            `fileresourceAPI/file/${appCode__}/${NewFileName}/`
          )
          this.$set(item, 'fileListImage', [
            `fileresourceAPI/file/${appCode__}/${NewFileName}/`
          ])
        })
        // this.changeBtnStatus(this.newFileList)
      }
    },
    initConfig(config = {}) {
      this.configData = Object.assign(this.configData, config)
      const { multiple } = this.configData
      if (!multiple && this.newFileList.length) {
        this.fileName = this.newFileList[0].OldFileName
      }
    },
    handleClose(done) {
      this.$confirm('确认关闭?')
        .then(_ => {
          done()
        })
        .catch(_ => {})
    },
    imgPreview(file) {
      const self = this
      // 看支持不支持FileReader
      if (!file || !window.FileReader) return
      // 创建一个reader
      const reader = new FileReader()
      // 将图片将转成 base64 格式
      reader.readAsDataURL(file)
      // 读取成功后的回调
      reader.onloadend = function() {
        const type = file.type
        if (/^image/.test(type)) {
          self.fileListImage.push(this.result)
        } else {
          self.fileListNoImage.push({
            name: file.name
          })
        }
        self.fileList.push({
          type: file.type,
          NewFileName: file.name,
          OldFileName: file.name,
          file: this.result,
          fileBrod: file,
          previewList: [this.result]
        })
      }
    },
    handleFileChange(e) {
      const inputDOM = document.getElementById('avatar')
      this.file = inputDOM.files[0]
      this.files = Array.from(inputDOM.files)
      // 在获取到文件对象进行预览就行了!
      this.files.forEach(item => {
        this.imgPreview(item)
      })
      // 清除当前input 值
      e.target.value = ''
    },
    deleteCurrImage(image, index) {
      this.fileList.splice(index, 1)
    },
    preview(item, index) {
      const { name } = item
      this.$set(item, 'file', `fileresourceAPI/file/${this.appCode}/${name}/`)
      this.$set(item, 'fileListImage', [
        `fileresourceAPI/file/${this.appCode}/${name}/`
      ])
    },
    deleteFile(item, index) {
      this.fileList.splice(index, 1)
    },
    deleteExitFile(item, index) {
      this.newFileList.splice(index, 1)
    },
    async upload() {
      const formData = new FormData()
      if (!this.fileList.length) {
        this.$message.error('请选择文件!')
        return
      }
      const { multiple } = this.configData
      if (!multiple) {
        if (this.newFileList.length >= 1) {
          this.$message.error('仅支持单文件上传,请手动删除服务器文件后再试!')
          return
        }
      }
      this.fileList.forEach(element => {
        const { NewFileName, fileBrod } = element
        formData.append(NewFileName, fileBrod)
      })
      const newFileList = []
      const res = await this.$API.uploadFile(formData)
      const resData = (res.data || []).filter(item => {
        return item.Sucess
      })
      const resDataError = (res.data || []).filter(item => {
        return !item.Sucess
      })
      let errorStr = ''
      resDataError.forEach(jtem => {
        const { Error, OldFileName } = jtem
        errorStr += `${OldFileName}:${Error}`
      })
      if (errorStr) {
        this.$message.error(errorStr)
      }
      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}/`
          ]
        })
        const targetIndex = this.fileList.findIndex(
          item_ => item_.NewFileName === item.OldFileName
        )
        this.fileList.splice(targetIndex, 1)
      })
      this.newFileList.push(...newFileList)
      if (!multiple && this.newFileList.length) {
        this.fileName = this.newFileList[0].OldFileName
      }
      localStorage['exitImage'] = JSON.stringify(this.newFileList)
    }
  }
}
</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;
}
.showFiletxt{
height: 18px;
}
</style>