import React from "react"; import { Button, Radio, Divider, InputNumber, Checkbox, Progress } from "antd"; import "./export-frame.css"; import Super from "../tmpl/superagent"; import Units from "../units"; import { PageInfo, QueryKey } from "../tmpl/interface"; import { LocaleContext } from "../locale/LocaleProvider"; const RadioGroup = Radio.Group; export type ExportStatus = "ready" | "exporting" | "success" | "fail" | "stop"; export type ExportMode = "current" | "page" | "all"; export type ExportType = "ltmpl-data-excel" | "ltmpl-data-filedata"; interface ExportFrameProps { defaultExportMode?: string; serverKey?: string; sourceId?: string; queryKey: QueryKey; currentPage?: PageInfo; title: string; type: ExportType; exportModes: ExportMode[]; } interface ExportFrameStat { interrupted?: boolean; disabledPageNoInput?: boolean; disabledCurrentPageRadio?: boolean; disabledAllPageRadio?: boolean; exportId?: string; startPageNo?: number; endPageNo?: number; exportMode?: string; percent?: number; exportStatus?: string; exportingMsg?: string; } export default class ExportFrame extends React.PureComponent< ExportFrameProps, ExportFrameStat > { static defaultProps = { defaultExportMode: "all", type: "ltmpl-data-excel", exportModes: ["current", "page", "all"], }; state = { interrupted: false, disabledPageNoInput: false, disabledCurrentPageRadio: false, disabledAllPageRadio: false, exportId: undefined, startPageNo: 1, endPageNo: 10, exportMode: "all", percent: 0, exportStatus: "ready", exportingMsg: "", }; static contextType = LocaleContext; context: React.ContextType; componentWillUnmount() { console.log("销毁"); } onChangeRadio = (e) => { this.setState({ exportMode: e.target.value, startPageNo: 1, endPageNo: 10, }); }; timerID = undefined; setInerval = (interval) => { if (this.timerID) { clearInterval(this.timerID); } this.timerID = interval; }; shouldComponentUpdate(prevProps, prevState) { if (prevProps && prevProps.queryKey && this.props.queryKey) { if (this.props.queryKey?.key != prevProps.queryKey?.key) { this.setState({ exportStatus: 'ready', percent: 0, exportingMsg: "", exportMode: "all" }) } } return true; } handleStart = () => { const { queryKey, currentPage, title, serverKey, type, sourceId } = this.props; const { startPageNo, endPageNo, exportMode } = this.state; this.setState({ exportStatus: "ready", }); const { getServiceLangStr } = this.context; Super.super({ url: `/v3/export/${type}`, serverKey, method: "GET", query: { sourceId, language: getServiceLangStr(), scope: exportMode, withDetail: false, queryKey: queryKey.key, startPageNo: exportMode == "page" ? startPageNo : currentPage?.pageNo, endPageNo: exportMode == "page" ? endPageNo : currentPage?.pageNo, }, }).then((res) => { if (res.exportId) { this.setState({ exportId: res.exportId, exportStatus: "exporting", }); this.statusOut(res.exportId); this.setInerval(setInterval(() => this.statusOut(res.exportId), 2000)); } else { this.setState({ exportStatus: "fail", }); } }); }; statusOut = (exportId) => { const { serverKey } = this.props; const { exportStatus } = this.state; Super.super( { url: `/v3/export/status`, serverKey, query: { interrupted: exportStatus == "stop", exportId, }, method: "GET", }, "", "none" ).then((res) => { if (res.status == "error") { clearInterval(this.timerID); console.log(res.statusMsg, this.timerID); this.setState({ exportingMsg: res.statusMsg, exportStatus: "stop", percent: 0, }); } else if (res.breaked) { clearInterval(this.timerID); this.setState({ exportingMsg: "已终止", exportStatus: "stop", percent: 0, }); } else { this.setState({ exportingMsg: res.statusMsg, percent: Math.floor((res.current / res.totalCount) * 100), }); } if (exportStatus == "stop") { clearInterval(this.timerID); } if (res.completed === true) { clearInterval(this.timerID); this.setState({ exportStatus: "success", exportingMsg: res.statusMsg, percent: Math.floor((res.current / res.totalCount) * 100), }); } }); }; download = () => { const { title, serverKey } = this.props; const { exportId } = this.state; const hydrocarbonToken = Units.hydrocarbonToken(); Units.downloadFile( Units.joinPath( Units.api(serverKey), `/v3/export/download?exportId=${exportId}&@token=${hydrocarbonToken}&@programToken=${Units.programCode( serverKey )}&@applicationToken=${Units.applicationCode(serverKey)}` ), title ); }; handleCancel = () => { this.setState({ percent: 0, exportStatus: "stop", exportId: undefined, }); }; render() { const { defaultExportMode, currentPage, exportModes } = this.props; const { startPageNo, endPageNo, disabledPageNoInput, exportMode, exportStatus, percent, exportingMsg, } = this.state; const { translate } = this.context; return (
{ <> {exportModes.includes("current") ? {translate("${当前页}")} : null} {exportModes.includes("page") ? {translate("${多页}")} : null} {exportModes.includes("all") ? {translate("${全部}")} : null} } {exportMode == "page" ? (
{translate("${页号}:")} this.setState({ startPageNo: v1 })} disabled={disabledPageNoInput} /> - this.setState({ endPageNo: v2 })} disabled={disabledPageNoInput} />
) : ( "" )}
{exportStatus == "ready" ? ( <> ) : exportStatus == "exporting" ? ( ) : ( )} {exportStatus == "success" ? ( ) : ( "" )} {percent > 0 ? ( ) : ( "" )}

{translate("${" + exportingMsg + "}")}

); } }