import * as React from 'react'; import { Icon, Tabs, Progress, Modal, Card } from 'antd'; import { dateFormat } from '../utils/utils'; import { message } from 'antd'; import { cancelTask } from '../api/index'; import { RuningTaskData, FinishTaskData } from '../modal'; const { TabPane } = Tabs; // const statusMap = [{ // key: 4, // label: '取消' // }, { // key: 5, // label: '失败' // }, { // key: 6, // label: '超时' // }, { // key: 7, // label: '成功' // }]; // const operateTypeMap = [{ // key: 0, // label: '无操作' // }, { // key: 1, // label: '查看' // }, { // key: 2, // label: '下载' // }] interface SiderListProps { /** 对话框是否隐藏*/ showBar: boolean; /** 账号*/ employee_id: number; /** 收起*/ closeBar: Function; className?: string; fetchAllData: Function; data: { running_tasks: RuningTaskData[]; finish_tasks: FinishTaskData[] } } interface RuningTaskProps { key: string | number; data: RuningTaskData; /** 取消回调*/ cancelTask?: (...args: any[]) => any | PromiseLike; } interface FinishTaskProps { key: string | number; data: FinishTaskData; } function showMore(msg: string) { Modal.info({ title: '错误信息', width: 800, okText: null, maskClosable: true, content:

{msg}

}); } const RuningTask = (props: RuningTaskProps) => { const { data, cancelTask} = props; const { name, description, progress, allow_cancel, create_time, all_time, status } = data; const percent = progress ? progress * 100 : 0; const now = Math.floor(Date.now() / 1000); const time = now - create_time; let timeNotice = ''; if (time < 60) { timeNotice = '刚刚创建' } else if(time < 3600) { const minutes = Math.floor(time / 60); timeNotice = `${minutes}分钟前创建` } else { const hours = Math.floor(time / 3600); timeNotice = `${hours}小时前创建` } const leftTime = Math.floor(all_time * ( 1 - progress) ); const leftTimeNotice = `预计剩余 ${leftTime} 秒` return (

{name}

{description}

{ allow_cancel && ( status == 3 ? 取消中... : 取消 ) }
{timeNotice}
{leftTimeNotice}
) } const FinishTask = (props: FinishTaskProps) => { const { data } = props; const { name, description, status, oper_type, url, message, create_time, end_time } = data; function getTit(status: number) { switch(status) { case 4: return (

{name} 取消

) case 5: return (

{name} 失败

) case 6: return (

{name} 超时

) case 7: return (

{name} 成功

) } } function getLink(oper_type: number, url: string) { if ( oper_type == 0 || !url ) { return '' } else { if(oper_type == 1) { return 查看 } else if( oper_type == 2 ) { return 下载 } else { return '' } } } const durationTime = end_time - create_time; let durationTimeStr = ''; if (durationTime < 60) { durationTimeStr = `${durationTimeStr} 秒` } else if(durationTime < 3600) { const minutes = Math.floor(durationTime / 60); durationTimeStr = `${minutes} 分钟` } else { const hours = Math.floor(durationTime / 3600); durationTimeStr = `${hours} 小时` } return (
{ getTit(status) }

{description} {getLink(oper_type, url)}

{ status == 5 &&

{message} { message.length > 8 && { showMore(message) }} href='javascript:void(0)'>显示更多 }

}
{dateFormat(end_time * 1000, '{y}-{m}-{d} {h}:{i}:{s}')}
{`执行 ${durationTimeStr}`}
) } type InitialState = { data: { running_tasks: RuningTaskData[]; finish_tasks: FinishTaskData[] }; } class SiderList extends React.Component { static defaultProps = { classPrefix: 'tabs', onChange: () => {} } state: InitialState = { data: { running_tasks: [], finish_tasks: [] } } async handleCancel(id: number) { const { fetchAllData } = this.props; const { employee_id } = this.props; const { code } = await cancelTask({employee_id, task_id: id}, { callback: () => { message.success('任务取消成功!'); }, errorCb: (msg: string) => { message.error(msg || '任务取消失败!'); } }) if ( code === 200 ) { fetchAllData() } } render() { const { closeBar, className, data } = this.props; // console.log(loading) const { running_tasks, finish_tasks } = data; return

我的后台任务

{closeBar()}} type="close"/>
{ running_tasks.length > 0 ? running_tasks.map(task => ( { this.handleCancel(task.id) }} > )):
{/* 暂无数据 */} 暂无数据 {/* 暂无数据 */}

没有进行中的任务
可以查看已结束的任务

}
{ finish_tasks.map(task => ( )) }
} } export default SiderList;