Let's use AI to Earn!
TypeScript auto-publishdouyindouyin-apielectron-appelectron-reactkuaishoukwaipublishedshipinhaotoolxiaohongshu
feat: 任务周期设置组件
牛文政9dcb17e165e6
diff --git a/src/components/CronSchedule.tsx b/src/components/CronSchedule.tsxnew file mode 100644index 00000000..9cfd7e3e--- /dev/null+++ b/src/components/CronSchedule.tsx@@ -0,0 +1,112 @@+import { useState } from 'react';+import { Select, Input, Button, message, Form } from 'antd';++interface CronScheduleProps {+ onSubmit: (cronExpression: string) => void;+}++const CronSchedule: React.FC<CronScheduleProps> = ({ onSubmit }) => {+ const [selectedType, setSelectedType] = useState('day');+ const [value, setValue] = useState('');+ const [error, setError] = useState('');++ const handleTypeChange = (value: string) => {+ setSelectedType(value);+ setValue(''); // 切换类型时重置输入值+ };++ const handleValueChange = (e: React.ChangeEvent<HTMLInputElement>) => {+ setValue(e.target.value);+ };++ const handleSubmit = () => {+ if (validate()) {+ const cronExpression = `${selectedType}-${value}`;+ onSubmit(cronExpression);+ message.success('定时表达式生成成功');+ }+ };++ const validate = (): boolean => {+ let isValid = true;+ let errorMessage = '';+ const valueInt = parseInt(value, 10);++ switch (selectedType) {+ case 'day':+ if (isNaN(valueInt) || valueInt < 0 || valueInt > 23) {+ errorMessage = '小时必须是0-23的整数';+ isValid = false;+ }+ break;+ case 'week':+ if (isNaN(valueInt) || valueInt < 0 || valueInt > 6) {+ errorMessage = '周必须是0(周日)到6(周六)的整数';+ isValid = false;+ }+ break;+ case 'month':+ if (isNaN(valueInt) || valueInt < 1 || valueInt > 31) {+ errorMessage = '日期必须是1-31的整数';+ isValid = false;+ }+ break;+ default:+ isValid = false;+ }++ setError(errorMessage);+ return isValid;+ };++ return (+ <Form layout="vertical" style={{ width: 300 }}>+ <Form.Item label="周期类型">+ <Select+ value={selectedType}+ onChange={handleTypeChange}+ style={{ width: '100%' }}+ >+ <Select.Option value="day">每天(格式:day-HH)</Select.Option>+ <Select.Option value="week">每周(格式:week-0到6)</Select.Option>+ <Select.Option value="month">+ 每月(格式:month-01到31)+ </Select.Option>+ </Select>+ </Form.Item>++ <Form.Item label="参数设置">+ <Input+ type="number"+ min={selectedType === 'month' ? 1 : 0}+ max={selectedType === 'day' ? 23 : selectedType === 'week' ? 6 : 31}+ value={value}+ onChange={handleValueChange}+ placeholder={+ selectedType === 'day'+ ? '小时(0-23)'+ : selectedType === 'week'+ ? '周几(0=周日)'+ : '日期(1-31)'+ }+ required+ style={{ width: '100%' }}+ />+ {error && <div style={{ color: '#f5222d', fontSize: 12 }}>{error}</div>}+ </Form.Item>++ <Form.Item>+ <Button+ type="primary"+ onClick={handleSubmit}+ disabled={!value || !!error}+ style={{ width: '100%' }}+ >+ 生成定时表达式+ </Button>+ </Form.Item>+ </Form>+ );+};++export default CronSchedule;diff --git a/src/views/reply/components/addAutoRun.tsx b/src/views/reply/components/addAutoRun.tsxindex 18385e68..9bd70c26 100644--- a/src/views/reply/components/addAutoRun.tsx+++ b/src/views/reply/components/addAutoRun.tsx@@ -6,8 +6,9 @@* @Description: 添加自动运行*/import { ipcCreateAutoRunOfReply } from '@/icp/reply';-import { Button, Input, Modal } from 'antd';+import { Modal } from 'antd';import { forwardRef, useImperativeHandle, useState } from 'react';+import CronSchedule from '@/components/CronSchedule';export interface AddAutoRunRef {init: (accountId: number, dataId: string) => Promise<void>;@@ -33,7 +34,9 @@ const Com = forwardRef<AddAutoRunRef>((props: any, ref) => {setIsModalOpen(false);}- async function onFinish() {+ async function handleCron(cron: string) {+ console.log('生成的定时表达式:----', cron);+ setCycleType(cron);const res = await ipcCreateAutoRunOfReply(accountId, dataId, cycleType);console.log('-------- res', res);}@@ -45,24 +48,11 @@ const Com = forwardRef<AddAutoRunRef>((props: any, ref) => {open={isModalOpen}onCancel={handleCancel}footer={null}- width={300}+ width={400}><p>账号ID{accountId}</p>- <p>账号ID{accountId}</p>- <p>- 周期类型 天 day-22 例:每天22时 周 week-2 例:每周周二,周日0 月 month-22- 例:每月22号- </p>-- <Input- onChange={(e) => {- setCycleType(e.target.value);- }}- />- <Button type="primary" onClick={onFinish}>- 创建- </Button>+ <CronSchedule onSubmit={handleCron} /></Modal></>);