index.js 4.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
import fetch from 'dva/fetch';
import React from 'react';
import {
  Form,
  notification,
} from 'antd';
import { connect } from 'dva';
import { getToken } from '../../utils/token';
import config from '@/webPublic/one_stop_public/config';
import FormdataWrapper from '../../utils/object-to-formdata-custom';
import ButtonDiy from '../ButtonDiy/ButtonDiy';
12
import {  giveFilePostDataInfoForTrue } from '@/webPublic/one_stop_public/Base16';
13
import SelectModal from '@/webPublic/one_stop_public/App/ExportCurrentInfo/SelectModal';
14
import { getHeaders } from '@/webPublic/zyd_public/utils/utils';
钟是志's avatar
钟是志 committed
15
import { getTransformApi } from '@/webPublic/one_stop_public/2022beidianke/localstorageTransform';
16 17 18 19 20 21 22

const getFileName = (fileName = '导出文件', ext = 'xlsx') => {
  fileName = fileName.replaceAll('.', '');
  ext = ext.replaceAll('.', '');
  return fileName + '.' + ext;
}

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
/**
 *
 * 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',
              }]}
 */



@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,
      showSelectModal: false,
    };
  }

  changeShowSelectModal = () => {
    this.setState({
      showSelectModal: !this.state.showSelectModal,
    })
  }

  exportData = () => {
60
    let downloadUrl = config.httpServer + '/DataObjApi/exportCurrent';
61 62 63 64 65 66
    let param = {
      dataObjId: this.props.objId,
      query: this.props.query,
      custom: this.props.custom,
      sql: this.props.sql,
      index: this.props.index,
67
      // token: getToken(),
68 69 70 71 72 73 74 75 76 77 78 79 80 81
    };
    if(this.props.openSelectFieldsModal){ // 26598 自定义数据导出---导出字段调整,,,注意数据权限

      this.setState({
        showSelectModal: {
          param,
          downloadUrl,
        }
      });
      return false;
    }
    this.downloadFile(downloadUrl, param);
  };

钟是志's avatar
钟是志 committed
82
  downloadFile = async (url, params) => {
83 84
    this.setState({ confirmLoading: true });
    let newApi = giveFilePostDataInfoForTrue(params, url);
85 86 87
    if(newApi && newApi.datas){
      newApi.datas.token = getToken();
    }
钟是志's avatar
钟是志 committed
88 89
    const { transformApi, headersApi } = await getTransformApi(newApi.url);
    fetch(transformApi, {
90
      method: 'POST',
91
      body: FormdataWrapper(newApi.datas),
钟是志's avatar
钟是志 committed
92
      ...getHeaders('', headersApi),
93 94 95 96 97 98 99 100 101 102 103 104
    })
      .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);
105
          let filename = getFileName(this.props.fileName, this.props.ext);
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
          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 });
      });
  }

128 129 130 131 132 133 134 135
  checkDownload = () => {
    if(this.props.beforeDownloadFile){
      this.props.beforeDownloadFile(this.exportData);
    }else{
      this.exportData();
    }
  }

136 137 138 139 140 141 142 143 144 145
  render() {
    const btn = {
      name: '导出',
      type: 'default',
      className: 'defaultBlue',
      ...(this.props.btn ? this.props.btn : {}),
    };
    const { custom, index, openSelectFieldsModal } = this.props;
    const { showSelectModal } = this.state;
    return (
146
      <span data-cell-comname={'export'}>
147
				<ButtonDiy {...btn} loading={this.state.confirmLoading} handleClick={this.checkDownload} />
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
        {
          openSelectFieldsModal &&
          <SelectModal custom={custom}
                       index={index}
                       changeShow={this.changeShowSelectModal}
                       show={showSelectModal}
                       downloadFile={this.downloadFile.bind(this)}

          />
        }

				<div id="downloadDiv" style={{ display: 'none' }} />
			</span>
    );
  }
}