batchImport.vue 6.8 KB
<template>
  <el-dialog title="批量导入" width="800px" :visible="visible" :close-on-click-modal="false" :close-on-press-escape="!importing" :show-close="!importing" :before-close="handleClose" @update:visible="handleDialogVisible">
    <div class="import-content">
      <div class="form-item">
        <span class="label">导入文件:</span>
        <div class="file-area">
          <el-button size="small" icon="el-icon-plus" :disabled="importing" @click="$refs.fileInput.click()">导入文件</el-button>
          <input ref="fileInput" class="file-input" type="file" accept=".xlsx,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" :disabled="importing" @change="selectFile" />
          <a class="template-link" :href="templateUrl" download>模板下载</a>
          <p v-if="file.name" class="file-name">
            {{ file.name }}
            <el-button type="text" :disabled="importing" icon="el-icon-close" @click="removeFile"></el-button>
          </p>
        </div>
      </div>
      <p v-if="importing" class="importing-tip">导入中,请稍候...</p>
      <div v-if="hasResult" class="result">
        <p>共导入{{ result.totalCount }}条,成功{{ result.successCount }}条,失败{{ result.failCount }}条</p>
        <el-button v-if="result.failCount > 0" type="text" @click="downloadErrorFile">导出错误文件</el-button>
      </div>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button v-if="importing" disabled>导入中,请稍候...</el-button>
      <template v-else>
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="startImport">确定</el-button>
      </template>
    </span>
  </el-dialog>
</template>

<script>
const TEMPLATE_URL = 'https://img02.glsx.com.cn/weapp/resource/saas/scrm/xlsx/核赔订单导入模板.xlsx'

export default {
  name: 'ClaimsBatchImport',
  props: { visible: { type: Boolean, default: false } },
  data() {
    return {
      file: {},
      importing: false,
      hasResult: false,
      templateUrl: TEMPLATE_URL,
      result: { totalCount: 0, successCount: 0, failCount: 0, batchId: '' }
    }
  },
  methods: {
    selectFile(event) {
      const file = event.target.files[0]
      if (!file) return
      if (file.name.substring(file.name.lastIndexOf('.')).toLowerCase() !== '.xlsx') {
        this.$message({
          message: '仅支持上传xlsx格式的文件',
          type: 'error',
          customClass: 'message-Z-zinde'
        })
        this.$refs.fileInput.value = ''
        return
      }
      this.file = file
      this.clearResult()
    },
    removeFile() {
      if (this.importing) return
      this.clearFile()
      this.clearResult()
    },
    clearFile() {
      this.file = {}
      if (this.$refs.fileInput) this.$refs.fileInput.value = ''
    },
    clearResult() {
      this.hasResult = false
      this.result = { totalCount: 0, successCount: 0, failCount: 0, batchId: '' }
    },
    startImport() {
      if (!this.file.size) {
        return this.$message({
          message: '请上传文件',
          type: 'error',
          customClass: 'message-Z-zinde'
        })
      }
      const reader = new FileReader()
      this.importing = true
      reader.readAsDataURL(this.file)
      reader.onload = event => {
        const base64 = event.target.result.split(',')[1]
        const fileType = this.file.name.split('.').pop()
        this.$apis.claims
          .batchImportGHClaims({ file: `${fileType}@${base64}` })
          .then(res => {
            if (res.code !== 0 && res.code !== '0') {
              this.$message({
                message: res.message || '导入失败,请检查文件后重试',
                type: 'error',
                customClass: 'message-Z-zinde'
              })
              return
            }
            this.setResult(res.data || {})
            if (this.result.successCount > 0) this.$emit('success')
          })
          .catch(() => {})
          .finally(() => {
            this.importing = false
            this.clearFile()
          })
      }
      reader.onerror = () => {
        this.importing = false
        this.clearFile()
        this.$message({
          message: '文件读取失败,请重新上传',
          type: 'error',
          customClass: 'message-Z-zinde'
        })
      }
    },
    setResult(res) {
      const successCount = Number(res.successCount || res.successNum || res.successfulCount || 0)
      const failCount = Number(res.failCount || res.failedCount || res.failNum || 0)
      this.result = {
        totalCount: Number(res.totalCount || res.importCount || res.total || successCount + failCount),
        successCount,
        failCount,
        batchId: res.batchId || res.batchID || ''
      }
      this.hasResult = true
    },
    downloadErrorFile() {
      if (!this.result.batchId) {
        return this.$message({
          message: '未获取到导入批次,请联系管理员',
          type: 'error',
          customClass: 'message-Z-zinde'
        })
      }
      this.$apis.claims
        .exportBatchImportError({ batchId: this.result.batchId })
        .then(res => {
          if (res.code !== 0 && res.code !== '0') {
            return this.$message({
              message: res.message || '错误文件导出失败',
              type: 'error',
              customClass: 'message-Z-zinde'
            })
          }
          const data = res.data || {}
          const fileUrl = typeof data === 'string' ? data : data.fileUrl || data.url || data.downloadUrl || data.exportUrl || ''
          if (!fileUrl) {
            return this.$message({
              message: '未获取到错误文件,请联系管理员',
              type: 'error',
              customClass: 'message-Z-zinde'
            })
          }
          window.location.href = fileUrl
        })
        .catch(() => {})
    },
    handleClose(done) {
      if (this.importing) return
      this.clearFile()
      this.clearResult()
      this.$emit('update:visible', false)
      if (done) done()
    },
    handleDialogVisible(visible) {
      if (!visible) this.handleClose()
    }
  }
}
</script>

<style lang="scss" scoped>
.import-content {
  min-height: 150px;
  padding: 12px 20px;
  .form-item {
    display: flex;
    align-items: flex-start;
    .label {
      width: 88px;
      line-height: 32px;
      text-align: right;
    }
    .file-area {
      position: relative;
      .file-input {
        display: none;
      }
      .template-link {
        margin-left: 12px;
        line-height: 32px;
        color: #5850ff;
      }
      .file-name {
        margin: 8px 0 0;
        color: #666;
      }
    }
  }
  .importing-tip {
    margin: 24px 0 0 88px;
    color: #999;
  }
  .result {
    margin: 50px 0 0 88px;
    text-align: left;
    p {
      display: inline-block;
      margin-right: 12px;
    }
  }
}
</style>