importPay.vue 3.64 KB
<template>
  <el-dialog v-model="show" title="会员上传" width="400px" append-to-body draggable>
    <el-upload
      ref="uploadRef"
      :limit="1"
      accept=".xls"
      :headers="upload.headers"
      :action="upload.url"
      :disabled="upload.isUploading"
      :on-progress="handleFileUploadProgress"
      :on-success="handleFileSuccess"
      :auto-upload="false"
      drag
      name="excel"
    >
      <el-icon class="el-icon--upload"><upload-filled /></el-icon>
      <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
      <template #tip>
        <div class="el-upload__tip text-center">
          <span>仅允许导入xls格式文件。</span>
          <el-link href="/static/excel/member.xls" type="primary" target="_blank" download="会员缴费模版.xls">下载模板</el-link>
        </div>
      </template>
    </el-upload>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="show = false">取 消</el-button>
      </div>
    </template>

    <el-dialog v-model="showDialog" width="400" center title="导入结果">
      <div class="row">
        <span class="">导入成功:{{ form.successCnt }}条</span>
      </div>
      <div v-if="form.failCnt>0" class="row">
        <span class="error">导入失败:{{ form.failCnt }}条</span>
      </div>
      <br>
      <br>
      <div style="text-align: center">
        <el-button type="primary" @click="closeFN">确定</el-button>
        <el-button v-if="form.failCnt>0" type="warning" @click="downloadExcel">导出错误信息</el-button>
      </div>
    </el-dialog>

  </el-dialog>
</template>

<script setup>
import { reactive } from '@vue/runtime-core'
import { getToken } from '@/utils/auth'
import { getCurrentInstance, ref } from 'vue'

const rangeId = ref()
const { proxy } = getCurrentInstance()
const emit = defineEmits(['uploaded'])
const show = ref(false)
const showDialog = ref(false)
const form = ref({
  failCnt: 0,
  successCnt: 0
})
const result = ref(0)
const upload = reactive({
  // 是否禁用上传
  isUploading: false,
  // 设置上传的请求头部
  headers: { Authorization: 'Bearer ' + getToken() },
  // 上传的地址
  url: `${import.meta.env.VITE_APP_BASE_API}/person/payment/addPersonPaymentByImport?rangeId=${rangeId.value}`
})

function submitFileForm() {
  proxy.$refs['uploadRef'].submit()
}

const handleFileUploadProgress = (event, file, fileList) => {
  upload.isUploading = true
}

const handleFileSuccess = (response, file, fileList) => {
  // show.value = false
  upload.isUploading = false
  proxy.$refs['uploadRef'].handleRemove(file)
  try {
    if (response.code == 200) {
      showDialog.value = true
      form.value = response.data
      rangeId.value = response.data.rangeId
    }
    form.value = response.data
    result.value = response.data.result
  } catch (e) {
    console.log(e)
    if (response.code == 500) return proxy.$modal.msgError(response.msg)
  } finally {
    emit('uploaded', rangeId.value)
  }
}


function open(row) {
  rangeId.value = row
  upload.url = `${import.meta.env.VITE_APP_BASE_API}/person/payment/addPersonPaymentByImport?rangeId=${rangeId.value}`
  show.value = true
}

function downloadExcel() {
  proxy.download(
    `/person/payment/exportPersonPaymentByImport`, { info: JSON.stringify(form.value.errs) }, `导入结果${new Date().getTime()}.xlsx`
  )
}

function closeFN() {
  show.value = false
  showDialog.value = false
}

defineExpose({
  open
})

</script>
<style scoped>
.row {
  text-align: center;
  font-size: 18px;
}

.error {
  color: red;
}

.success {
  color: #e6a23c;
}

</style>