importRecord.vue 5.27 KB
<template>
  <GLDialog
      mode="mid"
      class="muti"
      title="导入记录"
      :before-close="handleClose"
      @onOk="handleConfirm"
      @onCancel="handleClose"
      :visible.sync="show"
      :modal-append-to-body="true"
  >
    <el-form
        ref="recordRef"
        label-width="154px"
        :rules="configRules"
        :model="recordForm"
    >
      <el-form-item
          label="记录类型:"
          prop="importType"
          class="m-t-20"
      >
        <el-radio-group
            :size="size"
            v-model="recordForm.importType"
        >
          <el-radio :label="4">提现记录</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item
          label="导入链接:"
          prop="exportUrl"
          class="m-t-20"
      >
        <div class="activity-link">
          <div class="upload-btn">
            <el-button size="small">
              <i class="el-icon-plus"></i>
              上传文件
            </el-button>
            <input
                type="file"
                style="opacity: 0"
                ref="file"
                accept="*"
                @change="uploadFile"
            />
          </div>
<!--          下载模板-->
          <a :href="downloadLink" target="_blank">{{ downloadText }}</a>
          <template v-if="recordForm.exportUrl">
            <div class="file-info flex">
              {{ fileName }}
              <div class="el-icon-success"></div>
            </div>
          </template>
          <template v-if="fileError">
            <div class="file-info flex error">
              <a
                  :href="fileError"
                  target="_blank"
              >下载失败列表</a>
              <div class="el-icon-error"></div>
            </div>
          </template>
        </div>
      </el-form-item>
    </el-form>
  </GLDialog>
</template>

<script type='text/ecmascript-6'>
import commonFun from '../common/fun.js'

export default {
  name: '',
  props: {
    show: Boolean,
  },
  data() {
    return {
      formItemWidth: 'width: 284px;',
      size: 'small',
      channelList: [],
      recordForm: {
        importType: 4,
        exportUrl: ''
      },
      configRules: {
        importType: [
          {
            required: true,
            trigger: 'change'
          }
        ],
        exportUrl: [
          {
            required: true,
            message: '请导入链接',
          }
        ],
      },
      fileName: '',
      fileError: ''
    }
  },
  mounted() {
  },
  computed:{
    downloadLink(){
      const links = {
        '4': 'https://img01.glsx.com.cn/weapp/resource/saas/scrm/xlsx/提现记录.xlsx'
      }
      return links[this.recordForm.importType] || ''
    },
    downloadText(){
      const typeMap = {
        '4': '下载提现记录模板'
      }
      return typeMap[this.recordForm.importType] || '下载模板';
    }
  },
  methods: {
    handleClose() {
      this.recordForm = this.$options.data.call(this).recordForm
      this.fileError = ''
      this.$refs.recordRef.resetFields()
      this.$emit('update:show', false)
    },
    handleConfirm() {
      this.$refs.recordRef.validate(async (valid) => {
        if (valid) {
          try {
            const {recordForm} = this,
                res = await this.$apis.redenvelope.importRedEnvelopeRecord(recordForm)
            if (res) {
              this.$message({
                message: '导入成功',
                type: 'success'
              })
              this.handleClose()
              this.$parent.handleSearch()
            } else throw '导入失败'
          } catch (error) {
            this.$message.error(error)
          }
        }
      })
    },
    async uploadFile(e) {
      try {
        const {url, name} = await commonFun.uploadFile(e, this),
            {importType} = this.recordForm
        this.$refs.file.value = ''
        const res = await this.$apis.redenvelope.checkImport({importType, exportUrl: url})
        if (res) {
          if (res.totalCount == 0 || res.failCount > 0) {
            this.fileError = res.errorUrl
            this.$message.error('红包记录导入失败')
            this.recordForm.exportUrl = ''
            return
          }
          this.$message({
            message: '导入成功',
            type: 'success'
          })
          this.fileError = ''
          this.recordForm.exportUrl = url
          this.fileName = name
        }
      } catch (error) {
        console.log(error)
        if (error) this.$message.error(error)
      }
    }
  }
}
</script>

<style scoped lang='scss'>
.activity-link {
  display: flex;
  width: 500px;

  .upload-btn {
    position: relative;
    width: 110px;
    height: 32px;

    .el-button--small {
      width: 100%;
      height: 100%;
    }

    input {
      cursor: pointer;
      position: absolute;
      height: 100%;
      width: 100%;
      top: 0px;
      left: 0px;
      font-size: 0px;
    }
  }

  a,
  .error {
    margin-left: 8px;
    color: #5850ff;
    font-size: 14px;
    cursor: pointer;
  }

  .file-info {
    margin-left: 8px;

    .el-icon-success {
      margin-left: 8px;
      color: #67c23a;
    }

    .el-icon-error {
      margin-left: 8px;
      color: red;
    }
  }
}

.muti {
  /deep/ .el-form-item__content {
    width: auto;
  }
}
</style>