ExportCurrentInfo.js 3.1 KB
Newer Older
徐立's avatar
徐立 committed
1 2 3
import fetch from 'dva/fetch';
import React from 'react';
import ReactDOM from 'react-dom';
4 5 6 7 8 9 10 11 12 13 14 15
import {
	Select,
	Button,
	Modal,
	Input,
	Transfer,
	Row,
	Col,
	Form,
	message,
	notification,
} from 'antd';
徐立's avatar
徐立 committed
16
import { connect } from 'dva';
17
import { getToken } from '../utils/token';
chscls@163.com's avatar
chscls@163.com committed
18
import config from '@/webPublic/one_stop_public/config';
徐立's avatar
徐立 committed
19 20 21 22 23 24 25 26 27 28

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) {
29 30
	arr[index1] = arr.splice(index2, 1, arr[index1])[0];
	return arr;
徐立's avatar
徐立 committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
}

/**
 *
 * 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 }) => ({
49 50
	DataObj,
	loading: loading.models.DataObj,
徐立's avatar
徐立 committed
51 52 53
}))
@Form.create()
export default class ExportCurrentInfo extends React.Component {
54 55 56 57 58 59
	constructor(props) {
		super(props);
		this.state = {
			confirmLoading: false,
		};
	}
徐立's avatar
徐立 committed
60

61 62 63 64 65 66 67 68 69 70 71 72 73 74
	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,
			index: this.props.index,
		};
		this.downloadFile(downloadUrl, param);
	};
徐立's avatar
徐立 committed
75

76 77
	downloadFile(url, params) {
		this.setState({ confirmLoading: true });
徐立's avatar
徐立 committed
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
		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 : '导出文件.') + (this.props.ext || '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 });
			});
	}
徐立's avatar
徐立 committed
117

118 119 120 121 122 123 124 125 126 127 128 129 130 131
	render() {
		const btn = {
			name: '导出',
			type: 'default',
			className: 'defaultBlue',
			...(this.props.btn ? this.props.btn : {}),
		};
		return (
			<span>
				<ButtonDiy {...btn} loading={this.state.confirmLoading} handleClick={this.exportData} />
				<div id="downloadDiv" style={{ display: 'none' }} />
			</span>
		);
	}
徐立's avatar
徐立 committed
132
}