自己封装的cron表达式组件

news/2025/2/26 9:12:41/文章来源:https://www.cnblogs.com/sour0202/p/18737744

1.新建一个cron定时器组件

<template><div class="layout"><!-- <el-popoverv-model="visible"placement="bottom"width="500"trigger="click"> --><div class="form"><el-row><el-col :span="8"><el-selectv-model="type"placeholder="请选择类型"style="width: 112px"@change="typeChange"><el-option label="每天" value="每天" /><el-option label="每周" value="每周" /><el-option label="每月" value="每月" /><el-option label="固定" value="固定" /></el-select></el-col><el-col :span="8"><el-selectv-show="type == '每周'"v-model="week"value-key="cron"placeholder="请选择星期"style="width: 112px"><el-optionv-for="(item, index) in weekOption":key="item.cron":label="item.title":value="item"/></el-select></el-col><el-col :span="8"><el-selectv-show="type == '每月'"v-model="month"value-key="cron"placeholder="请选择日期"style="width: 112px"><el-optionv-for="(item, index) in monthOption":key="item.cron":label="item.title":value="item"/></el-select></el-col><el-col :span="8"><el-time-pickerv-if="type !== '固定'"v-model="time"placeholder="选择时间"value-format="HH:mm:ss"style="width: 100%"/></el-col><el-col :span="8"><el-time-pickerv-if="type == '固定'"v-model="fixed"placeholder="选择时间"value-format="HH:mm:ss"style="width: 100%"/></el-col></el-row><div class="footer"><!-- <el-button size="mini" type="text" @click="handleClose">取消</el-button> --><el-button type="primary" size="mini" @click="handleSummit">确定</el-button></div></div><!-- <el-buttonslot="reference"v-model="value"type="primary">生成 cron</el-button> --><!-- <el-input slot="reference" v-model="value" /> --><!-- </el-popover> --></div>
</template>
<script>
export default {props: {timeCronStr: {type: String,default: ''}},data() {return {visible: false,value: '',type: '每天', // 天\周\月week: null, // 星期几month: null, // 几号fixed: '', //固定time: '', // 时间weekOption: [{title: '星期一',value: '星期一',cron: 2},{title: '星期二',value: '星期二',cron: 3},{title: '星期三',value: '星期三',cron: 4},{title: '星期四',value: '星期四',cron: 5},{title: '星期五',value: '星期五',cron: 6},{title: '星期六',value: '星期六',cron: 7},{title: '星期日',value: '星期日',cron: 1}],monthOption: []};},created() {this.initData();},mounted() {},methods: {initData() {const arr = [];for (let i = 1; i < 32; i++) {arr.push({title: i + '号',value: i + '号',cron: i});}this.monthOption = arr;if (this.timeCronStr) {this.value = this.timeCronStr;const valueArr = this.timeCronStr.split(',');const valueArrLen = valueArr.length;this.type = valueArr[0];this.time = valueArr[valueArrLen - 1];if (valueArrLen > 2) {// 说明是每月 或 每周if (valueArr[1].indexOf('星期') > -1) {// 每周this.weekOption.map((v) => {if (v.title === valueArr[1]) {this.week = v;}});} else {this.monthOption.map((v) => {if (v.title === valueArr[1]) {this.month = v;}});}}}},typeChange(t) {if (t === '每周' && !this.week) {this.week = this.weekOption[0];}if (t === '每月' && !this.month) {this.month = this.monthOption[0];}},handleSummit() {// if (this.time == '') {//   this.$message({//     message: '请选择时间!',//     type: 'warning'//   });//   return;// }const dataArr = [];let timeCron;const clockCornArr = this.time.split(':').reverse();const clockCornArr2 = this.fixed.split(':').reverse();// if (this.type === '每天') {//   dataArr.push(this.type, this.time);//   timeCron = clockCornArr.join(' ') + ' * * ?';// } else {//   if (this.type === '每月') {//     dataArr.push(this.type, this.month.title, this.time);//     timeCron = clockCornArr.join(' ') + ' ' + this.month.cron + ' * ?';//   } else {//     // 每周//     dataArr.push(this.type, this.week.title, this.time);//     timeCron = clockCornArr.join(' ') + ' ? * ' + this.week.cron;//   }// }// 29 29 16 * * ? * 按天// 19 30 16 * * 2,3 * 按周// 44 30 16 2,4,21 * ? * 按月if (this.type === '每天') {dataArr.push(this.type, this.time);timeCron = clockCornArr.join(' ') + ' * * ? *';// timeCron = clockCornArr.join(' ') + ' * * ? ';} else if (this.type === '每月') {dataArr.push(this.type, this.month.title, this.time);timeCron = clockCornArr.join(' ') + ' ' + this.month.cron + ' * ? *';// timeCron = clockCornArr.join(' ') + ' ' + this.month.cron + ' * ? ';} else if (this.type === '每周') {dataArr.push(this.type, this.week.title, this.time);// timeCron =//   clockCornArr.join(' ') +//   ' * * ' +//   (this.week.cron - 1) +//   '' +//   this.week.cron +//   ' * ';timeCron = clockCornArr.join(' ') + ' ? * ' + this.week.cron;// timeCron = clockCornArr.join(' ') + ' ? * ' + this.week.cron + ' * ';// timeCron = clockCornArr.join(' ') + ' ? * ' + this.week.cron;} else if (this.type === '固定') {console.log(this.fixed, '固定');dataArr.push(this.type, this.fixed);timeCron = clockCornArr2.join(' ') + ' * * ? *';// timeCron = clockCornArr2.join(' ') + ' * * ?';}// this.value = dataArr.join(',');// console.log('clockCornArr', clockCornArr);this.visible = false;// this.$emit('change', this.value, dataArr, timeCron); // 每月,1号,14:52:36 和 36 52 14 1 * ?this.$emit('change', timeCron); // 每月,1号,14:52:36 和 36 52 14 1 * ?}}
};
</script>
<style lang="scss" scoped>
.layout {// display: flex;padding: 10px;text-align: center;
}
.form {padding: 12px;height: 150px;
}
.footer {text-align: right;margin-top: 50px;
}
</style>

2.页面引用 并使用组件

import Vcrontab from '@/components/Vcrontab';<el-dialogwidth="25%"title="Cron表达式生成器":visible.sync="openCron"append-to-bodydestroy-on-closeclass="scrollbar"><!-- @change="changeVal" --><Vcrontab @change="changeVal" /></el-dialog>

3.页面中方法

changeVal(timeCron) {// console.log(timeCron, '子传父');svCornEdit({ cronExpression: timeCron }).then((res) => {console.log(res);this.openCron = false;this.getBase();});},/** cron表达式按钮操作 */handleCron() {this.openCron = true;},// cron表达式转时间cronChangeDate(str) {var toDate = {};if (!str) {toDate.loopType = '单次循环';} else {var result = str.split(' ').join('');var nArr = str.split(' ');var countData = this.getPlaceholderCount(result);// console.log(countData, 'countData');if (countData.count1 === 1 && countData.count2 === 1) {// 只有一个'?'一个'*'则是按周循环toDate.loopType = '周';var keys = nArr[5];var en2cnMap = {1: '周日',2: '周一',3: '周二',4: '周三',5: '周四',6: '周五',7: '周六'};var cnKeys = keys.split(',').map(function (key, idx) {return en2cnMap[key];});toDate.loopValue = cnKeys.join(',');} else if (countData.count1 + countData.count2 === 3) {toDate.loopType = '月';var mot = [];var mkeys = nArr[3].split(',');for (var i = 0; i < mkeys.length; i++) {let mo = mkeys[i] + '号';mot.push(mo);}toDate.loopValue = mot.join(',');} else {toDate.loopType = '日';}toDate.loopTime = nArr[2] + ':' + nArr[1] + ':' + nArr[0];}let data = '';if (!toDate.loopValue) {data = '每' + toDate.loopType + ' ' + toDate.loopTime;} else {data ='每' +toDate.loopType +' ' +toDate.loopValue +' ' +toDate.loopTime;}console.log(data, 'data[[[[]]]]');this.cronZ = data;// return data;},// 统计字符串中包含某个字符的个数getPlaceholderCount(strSource) {var count1 = 0; // ?的个数var count2 = 0; // *的个数strSource.replace(/\*|\?/g, function (m, i) {if (m === '?') {count1++;} else if (m === '*') {count2++;}});var obj = {};obj.count1 = count1; // ?obj.count2 = count2; // *// console.log(obj, 'obj');return obj; //返回一个对象,根据需要得到想要的值},getBase() {homeBaseData().then((res) => {this.baseData = res.data;let cron = res.data.severCorn;return this.cronChangeDate(cron);});},

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/889965.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

毕设的踩坑之路

main()函数 在main()函数中调用 QMessageBox 之前一定要创建 QApplication 对象, 使用 QWidget 之前要创建 QApplication 对象. 不然会程序崩溃. 下面是笔者原先的代码: 之前是因为数据库等一切正常, 所以没有触发到连接数据库失败的 QMessageBox 消息. 后来有一次连接的时候数…

表格内cron表达式转成需要的时间格式

1.表格内添加 :formatter=""<el-table-columnlabel="监视周期"align="center"prop="corn":formatter="cornFormat"/>2.方法// 频率corn转时间格式cornFormat(row, column) {let str = row.corn;// let str = row.corn…

盲派八字推理 API 数据接口

盲派八字推理 API 数据接口 AI / 八字命理 基于八字命理分析 命理分析 / 八字运势。1. 产品功能基于八字命理以及盲派八字理论解析个人运势; 提供详细的大运分析,覆盖学业、婚姻、财运、健康等领域; 分析个人运势,包括学业、婚姻、财运、健康等方面的解读; 智能解读人生关…

2.25 CW 模拟赛 T4. 博弈论

前言 本来并不打算补这个题, 但是发现想拿到更好的分数, 应该对这题有更多的分析 至于 \(\rm{T3}\) , 除了让我知道分拆数类型的状态压缩 \(\rm{dp}\) 还可以乱搞, 没有什么意义, 现在也不太可能拥有这个水平 思路题意 给定一个序列A and B\textrm{A and B}A and B 每次可以在序…

20220226

沪锌 形态待验证 胶系看之前分析即可。后市看方向 17600-18200 https://www.cnblogs.com/lyonlee/p/18729353

2. 在Linux 当中安装 Nginx(13步) 下载安装启动(详细说明+附加详细截图说明)

2. 在Linux 当中安装 Nginx(13步) 下载&安装&启动(详细说明+附加详细截图说明) @目录2. 在Linux 当中安装 Nginx(13步) 下载&安装&启动(详细说明+附加详细截图说明)1. 在 Linxu 下安装 Nginx 的详细步骤2. 最后:1. 在 Linxu 下安装 Nginx 的详细步骤 Nginx 官…

AI应用实战课学习总结(11)用RNN做时序预测

本文介绍了RNN循环神经网络的基本概念 和 各种神经网络(DNN、CNN、RNN)的对比,最后介绍了如何基于RNN来做时序预测的案例。大家好,我是Edison。 最近入坑黄佳老师的《AI应用实战课》,记录下我的学习之旅,也算是总结回顾。 今天是我们的第11站,一起了解RNN循环神经网络的…

微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章

2025年2月25日,.NET团队在博客上宣布了.NET 10 Preview 1的正式发布,文章参见:https://devblogs.microsoft.com/dotnet/dotnet-10-preview-1/,标志着.NET框架在跨平台开发领域迈出了重要一步。这一新版本在.NET Runtime、SDK、库、C#、ASP.NET Core、Blazor以及.NET MAUI等…

推荐一款最新开源,基于AI人工智能UI自动化测试工具!支持自然语言编写脚本!

随着互联网技术的飞速发展,Web应用越来越普及,前端页面也越来越复杂。为了确保产品质量,UI自动化测试成为了开发过程中不可或缺的一环。然而,传统的UI自动化测试工具往往存在学习成本高、维护困难等问题。特别是UI 自动化脚本里往往到处都是选择器,比如 #ids、data-test、…

无需破解,国际版也很香!

随着短视频平台的迅速崛起,各类短视频内容如搞笑片段、生活窍门、旅行日志和教学视频层出不穷,几乎覆盖了所有主题和风格。大众对视频质量的要求日益提高,因此选择一款专业的视频编辑软件显得尤为重要。用户可以利用多层时间线进行剪辑,添加特效与转场,调整色彩和音频,甚…

读DAMA数据管理知识体系指南05数据治理(中)

数据治理需支持业务战略,明确治理责任,评估能力,制定战略框架,参与变革与问题管理,确保法规遵从,通过监控和评估提升数据质量和治理效果。1. 规划组织的数据治理 1.1. 数据治理工作必须支持业务战略和目标1.1.1. 一个组织的业务战略和目标影响着组织的数据战略,以及数据…