ExportCurrentInfo.js 3.2 KB
Newer Older
徐立's avatar
徐立 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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
import fetch from 'dva/fetch';
import React from 'react';
import ReactDOM from 'react-dom';
import { Select, Button, Modal, Input, Transfer, Row, Col, Form, message, notification } from 'antd';
import { connect } from 'dva';
import { getToken } from '../utils/token'
import config from '../config/config';

import QueryItem from './QueryItem';
import OrderItem from './OrderItem';

import FormdataWrapper from '../utils/object-to-formdata-custom';
import ButtonDiy from './ButtonDiy/ButtonDiy';
const Option = Select.Option;
var keyX = 1;

function swapArray(arr, index1, index2) {
  arr[index1] = arr.splice(index2, 1, arr[index1])[0];
  return arr;
}

/**
 *
 * 2019/02/21  修改导出方式为fetch
 * 增加 props.fileName 设置导出文件名称
 * mustQuerys 设置搜索条件 demo :
 *  mustQuerys={[{
                hql: 'bean.adminStatus',
                x: '=',
                v: "pass",
                c: 'com.zysoft.app.zydxl.entity.base.BaseImportantWatch$Status',
                notes: 'com.zysoft.app.zydxl.entity.base.BaseImportantWatch$Status',
              }]}
 */

const FormItem = Form.Item;
@connect(({ DataObj, loading }) => ({
  DataObj,
  loading: loading.models.DataObj,
}))
@Form.create()
export default class ExportCurrentInfo extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
    
      confirmLoading: false,
    };
  }

  exportData = () => {
 
      let divElement = document.getElementById('downloadDiv');
      let downloadUrl = config.httpServer + '/DataObjApi/exportCurrent?';
      const token = getToken() != null && getToken() != 'null' ? getToken() : '0000';
      downloadUrl = `${downloadUrl}token=${token}`;
      let param = {
        dataObjId:this.props.objId,
        query:this.props.query,
        custom:this.props.custom,
        sql:this.props.sql,

      };
    
   
      this.downloadFile(downloadUrl, param);
    

  };

  downloadFile(url, params) {
    this.setState({ confirmLoading: true });

    fetch(url, {
      method: 'POST',
      body: FormdataWrapper(params),
    }).then(res => {
      if (res.status != '200') {
        return res.json();
      } else {
        return res.blob();
      }
    }).then(data => {
      if (data instanceof Blob) {
        let a = document.createElement('a');
        let url = window.URL.createObjectURL(data);
        let filename = (this.props.fileName ? this.props.fileName : '导出文件') + '.xlsx';
        a.href = url;
        a.download = filename;
        a.click();
        window.URL.revokeObjectURL(url);
        a = null;
      } else {
        notification.error({
          message: `文件导出错误`,
          description: data.errMsg,
        });
      }

    }).catch(err => {
      notification.error({
        message: `网络请求超时`,
      });
    }).finally(() => {
        this.setState({ confirmLoading: false });
      });
  }

 

  render() {

    return (
      <span>
         <ButtonDiy name='导出'
                   type='default'
                   className='defaultBlue'
                   loading={this.state.confirmLoading}
                   handleClick={this.exportData}/>
        <div id='downloadDiv' style={{ display: 'none' }}></div>
       
      </span>
    );
  }
}