step-2.vue 7.34 KB
<template>
  <div class="step-2-box">
    <div class="title">待交接资产列表</div>
    <d2-table
      ref="table"
      :table-data="listData"
      :table-params="tableParams"
      :hasPage="false"
    >
      <template #statusHtml="scope">
        <span
          class="circle"
          :style="{
              background:CODE_STATUS_COLOR[scope.scope[scope.prop]]
            }"
        ></span>
        {{CODE_STATUS[scope.scope[scope.prop]] || "-" }}
      </template>
      <template #auth-button="scope">
        <template v-if="scope.scope.signCustodyStatus">
          <template v-if="scope.scope.proofUrls.length < 5">
            <el-button
              type="text"
              class="upload"
            >上传凭证
              <input
                type="file"
                :id="'fileUpload' + scope.index"
                class="import-input"
                accept=".pdf,image/jpeg,image/png,image/bmp"
                @change="(e) => importFile(e, scope.index)"
              />
            </el-button>
          </template>
          <template v-for="(i,index) in scope.scope.proofUrls">
            <div
              class="btn-list"
              :key="index"
            >
              <template v-if="i">
                <el-button
                  type="text"
                  @click="view(scope.scope.proofUrls, i)"
                >查看凭证</el-button>
                <el-button
                  type="text"
                  @click="del(scope.index, index)"
                >删除</el-button>
              </template>
            </div>
          </template>
        </template>
        <template v-else>
          -
        </template>
      </template>
    </d2-table>
    <div class="block"></div>
    <el-image-viewer
      v-if="imgViewerVisible"
      :on-close="closeImgViewer"
      :url-list="currentImgUrls"
      :initial-index="currentIndex"
    />
  </div>
</template>

<script type='text/ecmascript-6'>
import GlOssApi from "glsk-oss-jssdk"
import { baseConfig } from "@/main"

export default {
  name: 'Step-2',
  props: {
    tableData: {
      type: Array,
      default: () => []
    }
  },
  components: {
    "el-image-viewer": () => import("element-ui/packages/image/src/image-viewer")
  },
  data () {
    return {
      currentImgUrl: '',
      imgViewerVisible: false,
      GlOssApi: null,
      CODE_STATUS_COLOR: {
        true: "#2CCA74",
        false: "#EB4E4E"
      },
      CODE_STATUS: {
        true: "已签协议",
        false: "未签署"
      },
      tableParams: {
        btnId: new Date().getTime() + parseInt(Math.random() * 1000),
        listName: "records",
        mutiSelectUniqueKey: "evalOrderNo",
        mutiSelectName: "contentIndex",
        tableOption: {
          width: "150"
        },
        tableColumns: [
          {
            label: "评估单号",
            prop: "evalOrderNo",
            minWidth: 157
          },
          {
            label: "门店",
            prop: "sellerMerchantName",
            minWidth: 157
          },
          {
            label: "车型",
            prop: "carModel",
            minWidth: 261
          },
          {
            label: "车牌",
            prop: "licensePlate",
            minWidth: 110
          },
          {
            label: "协议状态",
            prop: "signCustodyStatus",
            minWidth: 110,
            addHtml: 'statusHtml'
          },
        ]
      },
      listData: [],
      currentIndex: 0,
      currentImgUrls: []
    }
  },
  watch: {
    tableData: {
      handler (val) {
        this.imgViewerVisible = false
        this.currentImgUrls = []
        this.listData = val || []
        this.GlOssApi = new GlOssApi({
          baseConfig
        })
      },
      immediate: true,
      deep: true
    }
  },

  mounted () {
  },
  methods: {
    filterArr () {
      const arr = []
      if (this.$refs.table.mutiSelectArr.contentIndex?.length) this.$refs.table.mutiSelectArr.contentIndex.forEach(item => arr.push({
        evalOrderNo: item.evalOrderNo,
        proofUrls: item.proofUrls,
        signCustodyStatus: item.signCustodyStatus
      }))
      return arr
    },
    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)
    },
    importFile (e, index) {
      const fileInfo = e.srcElement.files[0],
        fileName = fileInfo.name,
        size = e.target.files[0].size / 1024 / 1024,
        extension = fileName.split('.').pop().toLowerCase()
      if (!['jpg', 'jpeg', 'png', 'bmp', 'webp', 'pdf'].includes(extension)) {
        return this.notice.errorTips("只允许上传图片/PDF")
      }
      console.log(fileInfo)
      if (size < 10) {
        const success = (res) => {
          console.log(res, '===')
          console.log("上传文件成功")
          this.notice.okTips("上传文件成功")
          let { proofUrls } = this.listData[index]
          proofUrls.push(res.aliyunAddress)
          console.log(proofUrls, '===')
          this.$set(this.listData[index], proofUrls, proofUrls)
        }
        const fail = (res) => {
          this.notice.errorTips("上传文件失败")
        }
        this.handleOssUpload(
          fileInfo,
          fileName,
          success,
          fail
        )
      } else {
        this.notice.errorTips("上传文件不可超过10M")
      }
      e.target.value = ""
    },
    del (index, subIndex) {
      console.log(index, subIndex)
      this.listData[index].proofUrls.splice(subIndex, 1)
    },
    view (list, url) {
      let type = this.getFileTypeFromUrl(url)
      if (type === 'image') {
        const urls = list.filter(i => this.getFileTypeFromUrl(i) == 'image'),
          index = urls.findIndex(i => i === url)
        this.currentImgUrls = urls
        this.currentIndex = index
        this.imgViewerVisible = true
      } else window.open(url)
    },
    getFileTypeFromUrl (url) {
      if (!url) return 'file'
      const pathname = url.split('?')[0]
      const extension = pathname.split('.').pop()?.toLowerCase() || ''
      const imageExtensions = ['jpg', 'jpeg', 'png', 'bmp', 'webp']
      if (imageExtensions.includes(extension)) return 'image'
      if (/x-oss-process=image\/format,\w+/.test(url)) return 'image'
      return 'file'
    },
    closeImgViewer () {
      this.imgViewerVisible = false
    },
  }
}
</script>

<style scoped lang='scss'>
.step-2-box {
  .title {
    height: 50px;
    color: #333333;
    font-size: 16px;
    font-weight: 500;
    line-height: 50px;
  }
  .upload {
    position: relative;
    .import-input {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      opacity: 0;
    }
  }
  .btn-list {
    margin-top: 2px;
  }
}
.circle {
  display: inline-block;
  border-radius: 50%;
  width: 6px;
  height: 6px;
  margin-right: 8px;
  vertical-align: middle;
}
::v-deep .el-image-viewer__wrapper {
  width: 100vw;
  height: 100vh;
  .el-image-viewer__close {
    color: #fff;
  }
}
.block {
  height: 24px;
}
</style>