settlementDialog.vue 13.7 KB
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
<!-- 财务结算 -->
<template>
  <baseDialog
    :dialog-config="dialogConfig"
    :is-show.sync="visible"
    :btn-loding="btnLoding"
    class="accountDailog"
    @handleClick="handleAddConfirm"
  >
    <div class="reject-dialog">
      <glsx-combi-form
        class="edit-form-container"
        ref="editFormRef"
        label-width="150px"
        default-width="350px"
        :form-ref="editChildRef"
        :form-obj="infoForm"
        :form-data="editFormData"
        :form-rules="formRules"
      >
        <template slot="attachmentListHtml" slot-scope="scope">
          <el-upload
            action=""
            accept=".png,.jpg"
            class="upload-file"
            drag
            :show-file-list="false"
            :file-list="uploadFileList"
            :limit="5"
            :auto-upload="false"
            :on-remove="importFileChange"
            :on-change="
              (file, fileList) => handleFileChange(file, fileList, scope.scope)
            "
            :on-exceed="hanldeOnExceed"
            multiple
          >
            <i class="el-icon-upload"></i>
            <div class="el-upload__text">点击或将文件拖拽到此区域上传</div>
            <div class="item-tips text-tips">支持格式:.png .jpg</div>
            <div class="item-tips">单个文件不能超过10M,最多上传5个文件</div>
          </el-upload>
          <!-- 文件列表区域 -->
          <div class="file-box">
            <div
              v-for="(subItem, subIndex) in infoForm.attachmentList"
              :key="subIndex"
            >
              <div
                class="file-item"
                v-if="!filterEmpty(subItem.uploadProgress) || subItem.fileUrl"
              >
                <div class="file-content" @click="handleFileView(subItem)">
                  <el-progress
                    define-back-color="#F5F7FA"
                    :text-inside="true"
                    text-color="#333333"
                    :show-text="false"
                    :stroke-width="26"
                    stroke-linecap="butt"
                    :percentage="subItem.uploadProgress"
                  ></el-progress>
                  <div class="file-name">{{ subItem.fileName }}</div>
                </div>
                <i
                  v-if="!subItem.fileUrl"
                  class="el-icon-close del-icon"
                  @click="doCancel(scope.scope, subIndex)"
                ></i>
                <i
                  v-if="subItem.fileUrl"
                  class="el-icon-delete"
                  @click="handleFileDelete(scope.scope, subIndex)"
                ></i>
              </div>
            </div>
          </div>
        </template>
      </glsx-combi-form>
      <span :style="{ 'margin-left': '150px' }"
        >结算完成后,退款单据正式完成。</span
      >
    </div>
    <el-image-viewer
      v-if="imgViewerVisible"
      :on-close="closeImgViewer"
      :url-list="imageFileArr"
      :initial-index="initialIndex"
    />
  </baseDialog>
</template>

<script>
import { financialSettlement } from "@api/refundExamine"
import { uploadFile } from "@/api/sys.base"
import { deepClone } from "@/common/util"
import { baseConfig } from "@/main"
import GlOssApi from "glsk-oss-jssdk"
export default {
  name: "SettlementDialog",
  components: {
    "el-image-viewer": () =>
      import("element-ui/packages/image/src/image-viewer")
  },
  data() {
    var checkPhoto = (rule, value, callback) => {
      // if (this.infoForm.attachmentList.length === 0) {
      //   this.$message({
      //     message: '请上传结算凭证',
      //     type: 'error'
      //   })
      // } else {
      callback()
      // }
    }
    return {
      userInfo: this.$store.state.d2admin.user.info,
      infoForm: {
        attachmentList: [], // 结算凭证
        remark: ""
      },
      dialogConfig: {
        title: "财务结算",
        width: "600px"
      },
      editChildRef: "editChildRef",
      formRules: {
        // attachmentList: [{ required: true, message: '请上传结算凭证', trigger: 'change' }]
        attachmentList: [
          { required: false, validator: checkPhoto, trigger: "change" }
        ]
      },
      editFormData: [
        {
          label: "结算凭证",
          name: "attachmentList",
          addHtml: "attachmentListHtml"
        },
        {
          label: "备注",
          name: "remark",
          type: "input",
          maxlength: 50,
          inputType: "textarea",
          showLimit: true,
          rows: 4
        }
      ],
      baseData: {},
      uploadFileList: [],
      imageFileArr: [],
      imgViewerVisible: false,
      initialIndex: 0,
      btnLoding: false
    }
  },

  props: {
    settlementVisible: {
      type: Boolean,
      default: false
    },
    paramsObj: {
      type: Object,
      default: {}
    }
  },
  watch: {
    "infoForm.attachmentList": {
      handler(newVal) {
        if (newVal) {
          this.uploadFileList = newVal.filter(
            (v) => !this.filterEmpty(v.uploadProgress) || v.fileUrl
          ) // 文件列表
          const imageFileArr = newVal.filter(
            (v) =>
              v.fileUrl &&
              (v.fileUrl.includes(".png") || v.fileUrl.includes("jpg"))
          )
          this.imageFileArr = imageFileArr.map((item) => {
            return item.fileUrl
          })
        }
      },
      deep: true
    }
  },
  computed: {
    visible: {
      get() {
        return this.settlementVisible
      },
      set(newVal) {
        this.$emit("dialogClose", {
          flag: newVal,
          attrName: "settlementVisible"
        })
      }
    }
  },

  mounted() {
    this.GlOssApi = new GlOssApi({
      baseConfig
    })
  },

  methods: {
    // 获取文件
    importFileChange(file, fileList) {
      this.uploadFileList = fileList
      this.infoForm.attachmentList = deepClone(fileList)
      console.log(fileList, "fileList")
    },
    handleFileChange(file, fileList, targetItem) {
      // setTimeout(() => {
      // var reader = new FileReader()
      const fileInfo = file.raw
      const fileName = fileInfo.name
      if (!fileInfo.name.includes(".png") && !fileInfo.name.includes(".jpg")) {
        this.$message({
          message: "文件格式不支持上传",
          type: "error"
        })
        return false
      }
      // 默认10MB
      const limitSize = 10
      const isLt = fileInfo.size / 1024 / 1024 < limitSize
      if (!isLt) {
        this.$message({
          message: "上传文件大小不能超过" + limitSize + "MB!",
          type: "error"
        })
        this.uploadFileList = this.uploadFileList.filter((item) => {
          return item.size / 1024 / 1024 < 10
        })
        return false
      }
      // reader.readAsDataURL(fileInfo)
      // reader.onload = (e) => {
      //   // const _attachmentList = this.infoForm.attachmentList
      //   const _formData = this.infoForm
      //   _formData[targetItem.name].push({
      //     fileUrl: "",
      //     uploadProgress: 0,
      //     fileName: fileInfo.name?.substring(0, 128),
      //     fileSize: fileInfo.size,
      //     fileType: 2, // 文件类型(1.申请附件,2.结算凭证)
      //     cancelAxios: {}
      //   })
      //   const index = _formData[targetItem.name].length - 1
      //   const fileData = e.target.result
      //   const base64 =
      //     fileInfo.name.split(".")[fileInfo.name.split(".").length - 1] +
      //     "@" +
      //     fileData.split(",")[1]
      //   // const requestEvent = fileInfo.type == 'application/pdf' ? uploadPDF : uploadExcel
      //   const fileTypeKey = {
      //     "image/jpeg": "image",
      //     "image/png": "image"
      //   }
      //   // this.btnLoding = true
      //   uploadFile({
      //     file: base64,
      //     fileType: fileTypeKey[fileInfo.type],
      //     onProcess: (e) => {
      //       // 上传进度
      //       const { loaded, total } = e
      //       const uploadPrecent = ((loaded / total) * 100) | 0
      //       _formData[targetItem.name][index].uploadProgress = uploadPrecent
      //     },
      //     cancelFun: _formData[targetItem.name][index].cancelAxios // 取消上传
      //   })
      //     .then((res) => {
      //       _formData[targetItem.name][index].fileUrl = res // 文件地址
      //     })
      //     .catch(() => {
      //       // this.btnLoding = false
      //       this.clearItem(targetItem, index) // 上传失败将内容清空 不删除数组 防止其它上传进度 找不到对应下标
      //     })
      //   console.log("_formData[targetItem.name]", _formData[targetItem.name])
      // }
      const _formData = this.infoForm
      _formData[targetItem.name].push({
        fileUrl: "",
        uploadProgress: 0,
        fileName: fileInfo.name?.substring(0, 128),
        fileSize: fileInfo.size,
        fileType: 2, // 文件类型(1.申请附件,2.结算凭证)
        cancelAxios: {}
      })
      const index = _formData[targetItem.name].length - 1
      const success = (res) => {
        _formData[targetItem.name][index].fileUrl = res.aliyunAddress // 文件地址
      }
      const fail = (err) => {
        this.clearItem(targetItem, index) // 上传失败将内容清空 不删除数组 防止其它上传进度 找不到对应下标
      }
      this.handleOssUpload(fileInfo, fileName, success, fail)
    },
    handleOssUpload(file, fileName, success, fail) {
      const bucketName = "jinrong-huidan-test" // 桶名
      const fileType = fileName.substr(fileName.lastIndexOf(".") + 1)
      this.GlOssApi.multipartUpload(file, success, fail, bucketName, fileType)
    },
    hanldeOnExceed(files, fileList) {
      this.$message({
        message: "最多可上传5个文件",
        type: "error"
      })
    },
    // 删除文件
    handleFileDelete(targetItem, subIndex) {
      this.clearItem(targetItem, subIndex)
      // console.log(subIndex)
      // this.infoForm.attachmentList.splice(subIndex, 1)
    },
    // 预览文件
    handleFileView(item) {
      if (!item.fileUrl) return
      if (item?.fileUrl.includes(".pdf")) {
        // pdf文件直接打开新窗口预览
        window.open(item.fileUrl)
      } else {
        this.imgViewerVisible = true
        this.imageFileArr.forEach((val, index) => {
          if (val == item.fileUrl) this.initialIndex = index
        })
      }
    },
    closeImgViewer() {
      this.imgViewerVisible = false
    },
    // 取消上传
    doCancel(targetItem, subIndex) {
      const _formData = this.infoForm
      const cancelUploadObj = _formData[targetItem.name][subIndex].cancelAxios
      cancelUploadObj && cancelUploadObj.cancel && cancelUploadObj.cancel()
      this.clearItem(targetItem, subIndex)
    },
    filterEmpty(data) {
      return (data ?? "") === ""
    },
    // 清空上传文件数组项内容
    clearItem(targetItem, subIndex) {
      this.$set(this.infoForm[targetItem.name][subIndex], "fileUrl", "")
      this.$set(this.infoForm[targetItem.name][subIndex], "uploadProgress", "")
      // this.infoForm[targetItem.name].splice(subIndex, 1)
    },

    handleAddConfirm() {
      this.$refs.editFormRef.$refs[this.editChildRef].validate((valid) => {
        if (valid) {
          const params = deepClone(this.infoForm)
          params.id = this.paramsObj.id
          const attachmentList = params.attachmentList
          for (var i = 0; i < attachmentList.length; i++) {
            if (attachmentList[i].fileUrl == "") {
              attachmentList.splice(i, 1)
              i-- // 需要将i减1,因为数组长度已经减少了1
            }
          }
          // return console.log('params', params)

          financialSettlement(params).then((res) => {
            this.notice.okTips("操作成功")
            this.visible = true
          })
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.reject-dialog {
  margin: 65px 0;

  .reject-input {
    ::v-deep .el-textarea__inner {
      height: 70px;
    }
  }

  .el-textarea .el-input__count {
    right: 90px;
    bottom: -10px;
    font-size: 12px;
    color: #999999;
  }

  .tips {
    color: #999999;
  }
}

.upload-file {
  ::v-deep .el-upload-dragger {
    display: flex;
    flex-flow: column;
    justify-content: center;
    align-items: center;
    width: 300px;
    height: 160px;
    background: #f7f7f7;

    .el-icon-upload {
      margin: 0;
    }

    .el-upload__text {
      font-size: 14px;
      color: #000000;
    }

    .item-tips {
      font-size: 12px;
      color: #999999;
      height: 22px;
      line-height: 22px;
    }

    .text-tips {
      font-size: 14px;
    }
  }
}

.file-box {
  width: 350px;

  .file-item {
    margin-top: 6px;
    position: relative;
    box-sizing: border-box;
    display: flex;
    justify-content: space-between;
    align-items: center;
    cursor: pointer;

    ::v-deep .el-progress-bar {
      width: 320px;
      background: #ffffff;
      border: 1px solid #f4f6fc;
      border-radius: 4px;

      .el-progress-bar__outer {
        background: transparent;
        border-radius: 4px;

        .el-progress-bar__inner {
          background: #f5f7fa;
          border-radius: 4px;
        }
      }
    }

    ::v-deep .el-image-viewer__wrapper {
      width: 100vw;
      height: 100vh;
    }

    .file-name {
      position: absolute;
      left: 12px;
      top: 6px;
      transform: translateY(-50%);
      color: #333333;
      font-size: 14px;
      max-width: 300px;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }

    .image-file {
      z-index: -1;
    }
  }

  .file-item:first-child {
    margin-top: 15px;

    .file-name {
      position: absolute;
      left: 12px;
      top: 15px;
      transform: translateY(-50%);
      color: #333333;
      font-size: 14px;
    }
  }
}
</style>