import { forwardRef, useImperativeHandle, useState, useEffect } from 'react'; import { Form, Input, Select, Row, Col, Button, Tabs } from 'antd'; import { useTranslation } from 'react-i18next'; import type { ToolItem, TimeToolModalRef } from '../types' import RbModal from '@/components/RbModal'; import { execute } from '@/api/tools'; const FormItem = Form.Item; const tabKeys = ['currentTime', 'timestampConversion', 'timeFormat'] const formatList = [ { label: 'YYYY-MM-DD HH:mm:ss', value: '%Y-%m-%d %H:%M:%S' }, { label: 'YYYYMMDD_HHmmss', value: '%Y%m%d_%H%M%S' }, { label: 'YYYY年MM月DD日 HH:mm', value: '%Y年%m月%d日 %H:%M' }, { label: 'YYYY-MM-DD HH:mm:ss.SS', value: '%Y-%m-%d %H:%M:%S.%f' }, { label: 'DD/MM/YYYY', value: '%d/%m/%Y' }, { label: 'MM/DD/YYYY', value: '%m/%d/%Y' }, ] interface CurrentTimeObj { datetime: string; iso_format: string; timestamp: string; timestamp_ms: string; } const TimeToolModal = forwardRef((_props, ref) => { const { t } = useTranslation(); const [visible, setVisible] = useState(false); const [form] = Form.useForm<{ timestamp: string; formatType: string; }>(); const [data, setData] = useState({} as ToolItem) const [timeFormat, setTimeFormat] = useState(undefined) const [activeTab, setActiveTab] = useState('currentTime'); const values = Form.useWatch([], form) const [currentTime, setCurrentTime] = useState({} as CurrentTimeObj) const [timestampFormat, setTimestampFormat] = useState(null) const formatTabItems = () => { return tabKeys.map(key => ({ key, label: t(`tool.${key}`), })) } const handleChangeTab = (key: string) => { setActiveTab(key); setTimestampFormat(null) setTimeFormat(undefined) } // 封装取消方法,添加关闭弹窗逻辑 const handleClose = () => { setVisible(false); form.resetFields(); setData({} as ToolItem) setActiveTab('currentTime') }; const handleOpen = (vo: ToolItem) => { setData(vo) setVisible(true); getCurrentTime(vo) }; const getCurrentTime = (vo: ToolItem) => { if (!vo.id) return execute({ tool_id: vo.id, parameters: { operation: 'now', output_format: '%Y-%m-%d %H:%M:%S', } }).then(res => { const response = res as { data: CurrentTimeObj} setCurrentTime(response.data) }) } const handleFormat = () => { const timestamp = form.getFieldValue('timestamp') if (!timestamp || !data.id) return execute({ "tool_id": data.id, "parameters": { "operation": "timestamp_to_datetime", "input_value": timestamp, } }) .then(res => { const response = res as { data: CurrentTimeObj } setTimestampFormat(response.data.datetime) }) } const handleChangeFormatType = () => { if (!data.id) return execute({ tool_id: data.id, parameters: { operation: 'now', output_format: values.formatType, to_timezone: 'UTC' } }).then(res => { const response = res as { data: CurrentTimeObj } console.log('timeFormat', response.data.datetime) setTimeFormat(response.data.datetime) }) } useEffect(() => { if (values?.formatType) { handleChangeFormatType() } }, [values?.formatType]) // 暴露给父组件的方法 useImperativeHandle(ref, () => ({ handleOpen, handleClose })); return (
{/* 当前时间、时间戳转换、时间格式化 */} {/* 当前时间 */} {activeTab === 'currentTime' && <> } {/* 时间戳转换 */} {activeTab === 'timestampConversion' && <> {timestampFormat &&
{t('tool.conversionResult')}
{timestampFormat}
} } {/* 时间格式化 */} {activeTab === 'timeFormat' && <>