userImport.vue 3.54 KB
<template>
  <div class="app-content" style="height:75vh;overflow: auto;">

    <div class="submit-btn upload-header">
      <FileUpload @beforeUpload="beforeUpload" @cancel="onCancel" />
    </div>
    <a-divider style="height: 1px; background-color: #e8e8e8;" />
    <div class="upload-table">
      <a-spin :spinning="loading" style="width: 100%;height: 100%;">
        <a-table :dataSource="tableData" :columns="columns" rowKey="id" :pagination="false">
        </a-table>
      </a-spin>
    </div>
    <div class="upload-bottom" style="text-align:center;padding:6px 0">
      <a-button type="primary" @click="dataImport" :disabled="disabled">导入</a-button>
    </div>

  </div>
</template>
<script>
import { isEmptyParams, readExcelFile, personGender, personBirthday, checkIdentitytionId, checkEmail, checkPhone } from "@/views/utils/common"
import moment from "moment"

export default {
  name: "userImport",
  data () {
    return {
      columns: [],
      columnsName: [],
      tableData: [],
      disabled: true,
      loading: false,
      fileName: "",
    };
  },
  created () {

  },
  methods: {
    beforeUpload (file) {
      this.loading = true
      this.columns = []
      this.tableData = []
      this.columnsName = []
      this.fileName = file.name
      let list = []
      readExcelFile(file, 0).then((data) => {
        if (data) {
          list = data
          if (list.length > 0) {
            for (var key in list[0]) {
              if (!this.columnsName.includes(key)) {
                this.columnsName.push(key)
                let columns = { title: key, dataIndex: key, align: 'center' }
                this.columns.push(columns)
              } else {
                alert('存在重复的列,请检查!')
                this.columns = []
                this.tableData = []
                break
              }
            }
          }
          if (this.columns.length > 0) {
            this.tableData = list
            this.disabled = false
          }
          this.loading = false
        } else this.loading = false
      })

      // //读取文件数据
      // list.forEach(e => {
      //   let gender = personGender(e.证件号)
      //   let birthday = personBirthday(e.证件号) + ' 00:00:00'
      //   let expert = { personName: e.姓名, certId: e.证件号, sex: gender, birthday: moment(birthday).format('YYYY-MM-DD HH:mm:ss'), mobile: e.手机号, email: e.邮箱, specName: e.评审专业, titleName: e.职称, unitName: e.工作单位 }
      //   this.tableData.push(expert)
      //   if (this.tableData.length > 0)
      //     this.disabled = false
      // })
      return false
    },
    dataImport () {
      if (this.tableData.length == 0) {
        this.$message.error('请选择Excel!')
        return
      }
      this.loading = true
      this.tableData.forEach(e => {
        if (e.birthday)
          e.birthday = e.birthday + ' 00:00:00'
      })
      this.$api.applicant.userImport(this.tableData).then(({ data = {} }) => {
        if (data) {
          this.$message.success('成功!')
          this.tableData = data
        }
        this.loading = false
      })
    },
    onCancel () {
      this.loading = true
      this.columns = []
      this.tableData = []
      this.columnsName = []
      this.loading = false
      this.fileName = ""
    }
  },
};
</script>
<style scoped lang="less">
::-webkit-scrollbar {
  width: 8px;
  height: 8px;
}
.upload-header {
  height: 30px;
}
.upload-table {
  min-height: 150px;
  max-height: calc(100% - 90px);
  overflow-y: auto;
}
.upload-bottom {
  height: 42px;
}
</style>