当前位置: 首页 > news >正文

吉林网站建设司兰州快速seo整站优化招商

吉林网站建设司,兰州快速seo整站优化招商,怎么在网上卖自己的产品,新一代php+mysql+dreamweaver网站建设典型案例前言 最近在写的一个分布式调度系统,后端同学需要让我传入cron表达式,给调度接口传参。我去了学习了解了cron表达式的用法,发现有3个通用的表达式刚好符合我们的需求: 需求 每天 xx 的时间: 0 11 20 * * ? 上面是…

前言

最近在写的一个分布式调度系统,后端同学需要让我传入cron表达式,给调度接口传参。我去了学习了解了cron表达式的用法,发现有3个通用的表达式刚好符合我们的需求:

需求

  1. 每天 xx 的时间:

0 11 20 * * ?

上面是每天20:11的cron表达式

  1. 每周的 xxx 星期 的 xxx 时间

0 14 20 * * WED,THU

上面是 每周星期三,星期四20:14的cron表达式

  1. 每周的 xxx号 的 xxx时间

0 15 20 3,7 * ?

上面是 每月的3,7号20:15的cron表达式

这三个表达式刚好符合我们的需求,并且每个符号表达的意思也很直观。那么,话不多说,直接开写!

环境

  • react

    • 我的版本:“react”: “18.2.0”,用的函数组件+react hooks
  • moment

    • npm install moment
  • semi-design(组件库)

    • npm install semi-design
  • typescript

实现

数据

utils下创建cron.ts,对组件所用到的数据进行统一的管理:

export const dayOfTheWeekData = [{ key: 'MON', label: '星期一' },{ key: 'TUE', label: '星期二' },{ key: 'WED', label: '星期三' },{ key: 'THU', label: '星期四' },{ key: 'FRI', label: '星期五' },{ key: 'SAT', label: '星期六' },{ key: 'SUN', label: '星期天' }
];export const dayOfTheWeekOption = [{ key: '1', label: '星期一' },{ key: '2', label: '星期二' },{ key: '3', label: '星期三' },{ key: '4', label: '星期四' },{ key: '5', label: '星期五' },{ key: '6', label: '星期六' },{ key: '7', label: '星期天' }
];export const monthOption = [{ key: '1', label: '一月' },{ key: '2', label: '二月' },{ key: '3', label: '三月' },{ key: '4', label: '四月' },{ key: '5', label: '五月' },{ key: '6', label: '六月' },{ key: '7', label: '七月' },{ key: '8', label: '八月' },{ key: '9', label: '九月' },{ key: '10', label: '十月' },{ key: '11', label: '十一月' },{ key: '12', label: '十二月' }
];//获取dayOfTheMonthOption的每月对象
function getDayOfTheMonthOption() {const days = [];for (let i = 1; i < 32; i += 1) {days.push({ key: i.toString(), label: i.toString().concat('号') });}return days;
}export const dayOfTheMonthOption = getDayOfTheMonthOption();

组件

到了组件的具体实现,个人感觉我写的注释挺全的,就单挑几个核心重点讲下:

时间转换函数(handleTimeChange)

  //时间选择函数const handleTimeChange = (time: moment.Moment | null) => {setSelectTime(time);if (!time) return;const currentCron = expression ? expression.split(' ') : [];const [seconds, , , dayOfMonth, month1, dayOfWeek] = currentCron;const minutes = moment(time).minutes().toString(); //获取分钟const hours = moment(time).hours().toString(); //获取小时let result = null;if (!Number.isNaN(Number(hours)) && !Number.isNaN(Number(minutes))) {const minutesAndHour = seconds.concat(space).concat(minutes).concat(space).concat(hours).concat(space);if (defaultTimeType === 'everyDay') result = minutesAndHour.concat('* * ?');if (defaultTimeType !== 'everyDay')result = minutesAndHour.concat(dayOfMonth).concat(space).concat(month1).concat(space).concat(dayOfWeek);}if (result) onChange?.(result);setExpression(result);};
  1. 使用moment函数将time转成数字类型
    1. minutes = moment(time).minutes().toString(); //获取分钟
    2. hours = moment(time).hours().toString(); //获取小时
  2. 获取时间cron字符串minutesAndHour:
const minutesAndHour = seconds.concat(space).concat(minutes).concat(space).concat(hours).concat(space);
  1. 拼接得到完整的cron表达式:
    1. defaultTimeType === ‘everyDay’

result = minutesAndHour.concat(‘* * ?’);

  1. defaultTimeType !== ‘everyDay’
result = minutesAndHour.concat(dayOfMonth).concat(space).concat(month1).concat(space).concat(dayOfWeek);

日期转换函数(handleSelectChange)

setSelectedValue(data);
const selectValues = data.join(',');
const currentCron = expression ? expression.split(' ') : [];
const [seconds, minutes, hours, dayOfMonth, month1, dayOfWeek] = currentCron;
let result = '';
if (defaultTimeType === 'everyWeek') {result = seconds.concat(space).concat(minutes).concat(space).concat(hours).concat(space).concat(dayOfMonth).concat(space).concat(month1).concat(space).concat(selectValues);
}
if (defaultTimeType === 'everyMonth') {result = seconds.concat(space).concat(minutes).concat(space).concat(hours).concat(space).concat(data.length ? selectValues : '*').concat(space).concat(month1).concat(space).concat(dayOfWeek);
}
if (selectTime) onChange?.(result);
setExpression(result);
  1. defaultTimeType === ‘everyWeek’
result = seconds.concat(space).concat(minutes).concat(space).concat(hours).concat(space).concat(dayOfMonth).concat(space).concat(month1).concat(space).concat(selectValues);
  1. defaultTimeType === ‘everyMonth’
result = seconds.concat(space).concat(minutes).concat(space).concat(hours).concat(space).concat(data.length ? selectValues : '*').concat(space).concat(month1).concat(space).concat(dayOfWeek);

组件全部代码(CronInput)

import { ConfigProvider, TimePicker } from '@douyinfe/semi-ui';
import { Fragment, useState } from 'react';
import { Select } from '@douyinfe/semi-ui';
import moment from 'moment';
//引入数据
import { dayOfTheMonthOption, dayOfTheWeekData } from '@/utils/cron';const { Option } = Select;
const format = 'HH:mm';
const defaultCron = '0 * * * * ?';
const space = ' '; //空格
//类型选择
const timeTypes = [{ key: 'everyDay', label: '每天' },{ key: 'everyWeek', label: '每周' },{ key: 'everyMonth', label: '每月' }
];interface Props {onChange?: (cron?: string) => void;
}
const CronInput: React.FC<Props> = ({ onChange }) => {const [defaultTimeType, setDefaultTimeType] = useState(timeTypes[0].key); //选择类型const [selectedValue, setSelectedValue] = useState<[]>([]); //日期,多选数组const [selectTime, setSelectTime] = useState<any>(null); //时间const [expression, setExpression] = useState<string | null>(defaultCron); //bzd//类型选择函数const handleTimeTypeChange = (selectValue: string) => {setDefaultTimeType(selectValue);setSelectTime(null);setSelectedValue([]);setExpression(defaultCron);};//时间选择函数const handleTimeChange = (time: moment.Moment | null) => {setSelectTime(time);if (!time) return;const currentCron = expression ? expression.split(' ') : [];const [seconds, , , dayOfMonth, month1, dayOfWeek] = currentCron;const minutes = moment(time).minutes().toString(); //获取分钟const hours = moment(time).hours().toString(); //获取小时let result = null;if (!Number.isNaN(Number(hours)) && !Number.isNaN(Number(minutes))) {const minutesAndHour = seconds.concat(space).concat(minutes).concat(space).concat(hours).concat(space);if (defaultTimeType === 'everyDay') result = minutesAndHour.concat('* * ?');if (defaultTimeType !== 'everyDay')result = minutesAndHour.concat(dayOfMonth).concat(space).concat(month1).concat(space).concat(dayOfWeek);}if (result) onChange?.(result);setExpression(result);};const handleSelectChange = (data: []) => {setSelectedValue(data);const selectValues = data.join(',');const currentCron = expression ? expression.split(' ') : [];const [seconds, minutes, hours, dayOfMonth, month1, dayOfWeek] = currentCron;let result = '';if (defaultTimeType === 'everyWeek') {result = seconds.concat(space).concat(minutes).concat(space).concat(hours).concat(space).concat(dayOfMonth).concat(space).concat(month1).concat(space).concat(selectValues);}if (defaultTimeType === 'everyMonth') {result = seconds.concat(space).concat(minutes).concat(space).concat(hours).concat(space).concat(data.length ? selectValues : '*').concat(space).concat(month1).concat(space).concat(dayOfWeek);}if (selectTime) onChange?.(result);setExpression(result);};const RenderSelect = ({placeholder,data = []}: {placeholder: string;data: { key: string; label: string }[];}) => {return (<Fragment><Selectmultipleplaceholder={placeholder}onChange={(val: any) => handleSelectChange(val)}style={{ marginRight: '16px', width: 'auto' }}value={selectedValue}>{data.map((item: { key: string; label: string }) => (<Option key={item.key} value={item.key}>{item.label}</Option>))}</Select><ConfigProvider><TimePickervalue={selectTime && moment(selectTime, format).toDate()}format={format}placeholder="请选择时间"onChange={(val: any) => handleTimeChange(val)}/></ConfigProvider></Fragment>);};return (<><div className={'cron'}><Select// role="cron-type"style={{ marginRight: '16px', width: 'auto' }}placeholder="请选择类型"onChange={(val: any) => handleTimeTypeChange(val)}value={defaultTimeType}>{timeTypes.map((item) => (<Option key={item.key} value={item.key}>{' '}{item.label}</Option>))}</Select>{defaultTimeType === 'everyDay' && (<ConfigProvider><TimePickervalue={selectTime && moment(selectTime, format).toDate()}format={format}placeholder="请选择时间"onChange={(val: any) => handleTimeChange(val)}/></ConfigProvider>)}{defaultTimeType === 'everyWeek' && (<RenderSelect data={dayOfTheWeekData} placeholder="请选择星期" />)}{defaultTimeType === 'everyMonth' && (<RenderSelect data={dayOfTheMonthOption} placeholder="请选择日期" />)}</div></>);
};export default CronInput;

使用与效果

使用

使用方法很简单,接收onChange传来的cron表达式即可:

const App: FC<IProps> = (props) => {const { datas = [] } = props;let [value, setValue] = useState<string>();return (<div><CronInput onChange={(cron) => setValue(cron)} /><div>{value}</div></div>);
};

效果

  1. 每天

image.png

  1. 每周

image.png

  1. 每月

image.png


文章转载自:
http://dinncoferret.ssfq.cn
http://dinncoirredeemable.ssfq.cn
http://dinncouncompanionable.ssfq.cn
http://dinncohairtrigger.ssfq.cn
http://dinncoaccurately.ssfq.cn
http://dinncoempaistic.ssfq.cn
http://dinncoyikes.ssfq.cn
http://dinncojeu.ssfq.cn
http://dinncowaggle.ssfq.cn
http://dinncothermophilic.ssfq.cn
http://dinncolatecomer.ssfq.cn
http://dinncooutgeneral.ssfq.cn
http://dinncobarnaby.ssfq.cn
http://dinncoobsolesce.ssfq.cn
http://dinncosomal.ssfq.cn
http://dinncobrooklyn.ssfq.cn
http://dinncopreggers.ssfq.cn
http://dinncoshillelah.ssfq.cn
http://dinncolymphocytotic.ssfq.cn
http://dinncowhalecalf.ssfq.cn
http://dinncodemetrius.ssfq.cn
http://dinncosonneteer.ssfq.cn
http://dinncoextasy.ssfq.cn
http://dinncoTRUE.ssfq.cn
http://dinncoazo.ssfq.cn
http://dinncokantianism.ssfq.cn
http://dinncoheedless.ssfq.cn
http://dinncobuqsha.ssfq.cn
http://dinncorink.ssfq.cn
http://dinncoexpunge.ssfq.cn
http://dinncoeffectuate.ssfq.cn
http://dinncofrankly.ssfq.cn
http://dinncovitellogenesis.ssfq.cn
http://dinncoclangour.ssfq.cn
http://dinncosharleen.ssfq.cn
http://dinnconanking.ssfq.cn
http://dinncoissuer.ssfq.cn
http://dinncominirecession.ssfq.cn
http://dinncofat.ssfq.cn
http://dinncostomacher.ssfq.cn
http://dinncojurisprudent.ssfq.cn
http://dinncobemud.ssfq.cn
http://dinncodivertingly.ssfq.cn
http://dinncoreoccupy.ssfq.cn
http://dinncopupilage.ssfq.cn
http://dinncocoxcomb.ssfq.cn
http://dinncorapt.ssfq.cn
http://dinncoextractive.ssfq.cn
http://dinncostreamside.ssfq.cn
http://dinncoautochory.ssfq.cn
http://dinncomaulers.ssfq.cn
http://dinncopremedical.ssfq.cn
http://dinncoliberality.ssfq.cn
http://dinncobymotive.ssfq.cn
http://dinncoflix.ssfq.cn
http://dinncosuperior.ssfq.cn
http://dinncoirma.ssfq.cn
http://dinncoartie.ssfq.cn
http://dinncocausally.ssfq.cn
http://dinncoadventist.ssfq.cn
http://dinncohemoprotein.ssfq.cn
http://dinncoinsistency.ssfq.cn
http://dinncofalconine.ssfq.cn
http://dinncocryptogam.ssfq.cn
http://dinncoxiii.ssfq.cn
http://dinncointuitionalism.ssfq.cn
http://dinncolych.ssfq.cn
http://dinncospeedlight.ssfq.cn
http://dinncohylotheism.ssfq.cn
http://dinncodarwinist.ssfq.cn
http://dinncoheroin.ssfq.cn
http://dinncoirak.ssfq.cn
http://dinncorowan.ssfq.cn
http://dinncorhonchus.ssfq.cn
http://dinncohade.ssfq.cn
http://dinncosuperintendent.ssfq.cn
http://dinncomaudlin.ssfq.cn
http://dinncoruritanian.ssfq.cn
http://dinncoesme.ssfq.cn
http://dinncodft.ssfq.cn
http://dinncofolkway.ssfq.cn
http://dinncobony.ssfq.cn
http://dinncofruitlessly.ssfq.cn
http://dinncopaleogeophysics.ssfq.cn
http://dinncoboottree.ssfq.cn
http://dinncoredline.ssfq.cn
http://dinncohecate.ssfq.cn
http://dinncoanesthesia.ssfq.cn
http://dinncotortuosity.ssfq.cn
http://dinncohomonuclear.ssfq.cn
http://dinncoodeon.ssfq.cn
http://dinncopantler.ssfq.cn
http://dinncoprofilometer.ssfq.cn
http://dinncodeaf.ssfq.cn
http://dinncotriceratops.ssfq.cn
http://dinncohypopyon.ssfq.cn
http://dinncoepisternum.ssfq.cn
http://dinncoglyphography.ssfq.cn
http://dinncomatric.ssfq.cn
http://dinncomessin.ssfq.cn
http://www.dinnco.com/news/140331.html

相关文章:

  • 做视频网站需要哪些证地推拉新app推广平台有哪些
  • 南宁营销型网站建设哪家好象山seo外包服务优化
  • 怎么用网站挂QQ湖北seo推广
  • 做网站公司找哪家seo顾问能赚钱吗
  • 小鸡a做爰片免费网站百度seo培训要多少钱
  • 想要网站推广页面头条号权重查询
  • 如何为网站做面包屑导航优化大师是什么软件
  • 小学网站建设方案网络营销心得体会1000字
  • 网站建设音乐插件怎么弄seo外链平台热狗
  • 盐田区住房和建设局网站18种最有效推广的方式
  • 天眼查 企业查询网页seo软件工具
  • 长春火车站需要核酸检测报告吗seo外链建设的方法有
  • 嘉兴 企业网站 哪家如何建立网页
  • 杭州手机网站制作电脑公司关键词优化排名软件怎么样
  • 网站建设 资讯动态互联网推广好做吗
  • 2008年做的网站怎么创建网站教程
  • 做网站注册什么性质的公司林云seo博客
  • 南通网站建设教程北京seo教师
  • 买保险网站如何用google搜索产品关键词
  • 织梦html5网站模板seo关键词排名优化要多少钱
  • 广西汽车网网站建设廊坊快速优化排名
  • 佛山网站建设怎么做网络广告销售
  • 绵阳专门做网站的公司做网站的步骤
  • 重庆潼南网站建设报价防疫测温健康码核验一体机
  • 网站初期吸引用户注册免费优化推广网站的软件
  • 网站上的付费文章怎么做网店运营推广登录入口
  • wordpress与知更鸟区别seo方法
  • 虎门响应式网站建设软文推广平台排名
  • 网站恶意点击东莞seo建站优化哪里好
  • 软件公司网站建设最新的域名网站