import React from "react"; import { Button, Progress } from "antd"; import "./export-frame.css"; import Super from "../tmpl/superagent"; import Units from "../units"; import { ButtonConfig } from "../tmpl/interface"; import { LocaleContext } from "../locale/LocaleProvider"; interface SelectCodeExportFrameProps { serverKey?: string; fileExport: ButtonConfig; codes: string[]; title?: string; } interface SelectCodeExportFrameStat { interrupted?: boolean; exportId?: string; exportMode?: string; percent?: number; exportStatus?: string; exportingMsg?: string; } export default class SelectCodeExportFrame extends React.PureComponent< SelectCodeExportFrameProps, SelectCodeExportFrameStat > { static defaultProps = { }; state = { interrupted: false, exportId: undefined, percent: 0, exportStatus: "ready", exportingMsg: "", }; static contextType = LocaleContext; context: React.ContextType; componentWillUnmount() { console.log("销毁"); } timerID = undefined; setInerval = (interval) => { if (this.timerID) { clearInterval(this.timerID); } this.timerID = interval; }; shouldComponentUpdate(prevProps, prevState) { if (prevProps && prevProps.codes && this.props.codes) { if (this.props.codes != prevProps.codes) { this.setState({ exportStatus: 'ready', percent: 0, exportingMsg: "" }) } } return true; } handleStart = () => { const { serverKey, codes, fileExport } = this.props; this.setState({ exportStatus: "ready" }); const { getServiceLangStr } = this.context; Super.super({ url: `/v3/export/ltmpl-data-filedata`, serverKey, method: "POST", data: { language: getServiceLangStr(), codes, sourceId: fileExport.id, }, }).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 { serverKey, fileExport } = this.props; const { exportId } = this.state; const hydrocarbonToken = Units.serverHydrocarbonToken(serverKey); Units.downloadFile( Units.joinPath( Units.api(serverKey), `/v3/export/download?exportId=${exportId}&@token=${hydrocarbonToken}&@programToken=${Units.programCode( serverKey )}&@applicationToken=${Units.applicationCode(serverKey)}` ), fileExport.title ); }; handleCancel = () => { this.setState({ percent: 0, exportStatus: "stop", exportId: undefined, }); }; render() { const { } = this.props; const { exportStatus, percent, exportingMsg, } = this.state; const { translate } = this.context; return (
{exportStatus == "ready" ? ( <> ) : exportStatus == "exporting" ? ( ) : ( )} {exportStatus == "success" ? ( ) : ( "" )} {percent > 0 ? ( ) : ( "" )}

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

); } }