ImportLocationConfigDialog.vue 3.12 KB
Newer Older
YazhouChen's avatar
YazhouChen committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
<template>
  <!--导入文件对话框-->
  <el-dialog v-dialogDrag style="font-size: 10px;" width='450px'
     :title="$t('LocationConfigurationManagement.dialogLocationImport')"
             @close="close"
     :visible.sync="dialog_importFile"
     :before-upload="beforeUpload">
    <el-upload
      action="doUpload"
      :limit="1"
      :before-upload="beforeUpload">
      <el-button slot="trigger" size="small" type="primary">{{$t('common.selectFileBtn')}}</el-button>
      <div slot="tip" class="el-upload__tip">{{$t('common.importMessage')}}</div>
      <div slot="tip" class="el-upload-list__item-name">{{fileName}}</div>
    </el-upload>
    <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitUpload()">{{$t('common.ok')}}</el-button>
       <el-button @click="close">{{$t('common.cancel')}}</el-button>
      </span>
  </el-dialog>

</template>

<script>
  import LocationService from '@/domain/services/LocationService.js'
  import HelperUtil from '@/utils/HelperUtil'
    export default {
        name: "ImportLocationConfigDialog",
        props: ['command'],
        data:function(){
          return {
            dialog_importFile:true,
            fileName:'',
            files:'',
          }
        },

        methods:{
          //关闭
          close() {
            this.command.done()
          },

          //上传函数
          submitUpload: function () {
            let _this = this
            if (_this.fileName == "") {
              //请选择要上传的文件
              _this.InfoTip.warningTip(_this, HelperUtil.getCheckStatusCodeObjectByCode(_this.successCode.CHOOSE_UPLOADFILE_CODE))
              return false
            }
            let fileFormData = new FormData();
            fileFormData.append('file', _this.files, _this.files.name);
            LocationService.uploadLocation(fileFormData).then(result => {
              //成功
              _this.InfoTip.successTip(_this,HelperUtil.getStatusCodeObjectByCode(_this.successCode.OTHERS_CODE));
              _this.close()
            }).catch(err => {
              //失败
              _this.InfoTip.errorTip(_this, err)
              // this.command.done()
            })

          },

          //上传之前检查
          beforeUpload(file) {
            this.files = file;
            const extension = file.name.split('.')[1] === 'xls'
            const extension2 = file.name.split('.')[1] === 'xlsx'
            const isLt2M = file.size / 1024 / 1024 < 5
            if (!extension && !extension2) {
              //上传文件只能是 xls、xlsx格式!
              this.InfoTip.warningTip(this, HelperUtil.getCheckStatusCodeObjectByCode(this.successCode.UPLOADFILE_FORMAT_CODE))
              return
            }
            if (!isLt2M) {
              //上传文件大小不能超过 5MB!
              this.InfoTip.warningTip(this, HelperUtil.getCheckStatusCodeObjectByCode(this.successCode.UPLOADFILE_SIZE_CODE))
              return
            }
            this.fileName = file.name;
            return false // 返回false不会自动上传
          },
        },
    }
</script>

<style scoped>

</style>