fix(接口定义): 修复定时任务选择cron表达式无法选择年的缺陷

--bug=1022084 --user=王孝刚 【接口测试】github#21420,接口定时同步 : Cron表达式-自动生成-设置了年,
保存的时候提示错误,不设置年就正常 https://www.tapd.cn/55049933/s/1328305
This commit is contained in:
wxg0103 2023-01-30 13:47:52 +08:00 committed by wxg0103
parent 0ef9f20aba
commit 4c27ef2c48
1 changed files with 30 additions and 28 deletions

View File

@ -7,21 +7,23 @@ import parser from "cron-parser";
* @return True is expression is valid * @return True is expression is valid
*/ */
export function cronValidate(cronExpression ){ export function cronValidate(cronExpression ){
try {
parser.parseExpression(cronExpression);
} catch (e) {
return false;
}
if (!cronExpression) { if (!cronExpression) {
return false; return false;
} }
//alert("校验函数的开始!"); //alert("校验函数的开始!");
var cronParams = cronExpression.split(" "); let cronParams = cronExpression.split(" ");
if (cronParams.length < 6 || cronParams.length > 7) { if (cronParams.length < 6 || cronParams.length > 7) {
return false; return false;
} }
let cronValue = cronExpression;
if(cronExpression.split(" ").length === 7){
cronValue = cronExpression.substring(0, cronExpression.lastIndexOf(" "));
}
try {
parser.parseExpression(cronValue);
} catch (e) {
return false;
}
//CronTrigger cronTrigger = new CronTrigger(); //CronTrigger cronTrigger = new CronTrigger();
//cronTrigger.setCronExpression( cronExpression ); //cronTrigger.setCronExpression( cronExpression );
@ -81,15 +83,15 @@ function checkSecondsField(secondsField) {
function checkField(secondsField, minimal, maximal) { function checkField(secondsField, minimal, maximal) {
if (secondsField.indexOf("-") > -1 ) { if (secondsField.indexOf("-") > -1 ) {
var startValue = secondsField.substring(0, secondsField.indexOf( "-" )); let startValue = secondsField.substring(0, secondsField.indexOf( "-" ));
var endValue = secondsField.substring(secondsField.indexOf( "-" ) + 1); let endValue = secondsField.substring(secondsField.indexOf( "-" ) + 1);
if (!(checkIntValue(startValue, minimal, maximal, true) && checkIntValue(endValue, minimal, maximal, true))) { if (!(checkIntValue(startValue, minimal, maximal, true) && checkIntValue(endValue, minimal, maximal, true))) {
return false; return false;
} }
try { try {
var startVal = parseInt(startValue, 10); let startVal = parseInt(startValue, 10);
var endVal = parseInt(endValue, 10); let endVal = parseInt(endValue, 10);
//return endVal > startVal; //return endVal > startVal;
return true; return true;
} catch (e) { } catch (e) {
@ -102,13 +104,13 @@ function checkField(secondsField, minimal, maximal) {
} else if (secondsField.indexOf( "*" ) != -1) { } else if (secondsField.indexOf( "*" ) != -1) {
return true; return true;
} else { } else {
return checkIntValue(secondsField, minimal, maximal); return checkIntValue(secondsField, minimal, maximal,true);
} }
} }
function checkIntValue(value, minimal, maximal, checkExtremity) { function checkIntValue(value, minimal, maximal, checkExtremity) {
try { try {
var val = parseInt(value, 10); let val = parseInt(value, 10);
//判断是否为整数 //判断是否为整数
if (value == val) { if (value == val) {
if (checkExtremity) { if (checkExtremity) {
@ -220,11 +222,11 @@ function checkYearField(yearField) {
function checkFieldWithLetter(value, letter, minimalBefore, maximalBefore, function checkFieldWithLetter(value, letter, minimalBefore, maximalBefore,
minimalAfter, maximalAfter) { minimalAfter, maximalAfter) {
var canBeAlone = false; let canBeAlone = false;
var canHaveIntBefore = false; let canHaveIntBefore = false;
var canHaveIntAfter = false; let canHaveIntAfter = false;
var mustHaveIntBefore = false; let mustHaveIntBefore = false;
var mustHaveIntAfter = false; let mustHaveIntAfter = false;
if (letter == "L") { if (letter == "L") {
canBeAlone = true; canBeAlone = true;
@ -248,8 +250,8 @@ function checkFieldWithLetter(value, letter, minimalBefore, maximalBefore,
mustHaveIntAfter = true; mustHaveIntAfter = true;
} }
var beforeLetter = ""; let beforeLetter = "";
var afterLetter = ""; let afterLetter = "";
if (value.indexOf(letter) >= 0 ) { if (value.indexOf(letter) >= 0 ) {
beforeLetter = value.substring( 0, value.indexOf(letter)); beforeLetter = value.substring( 0, value.indexOf(letter));
@ -301,9 +303,9 @@ function checkFieldWithLetter(value, letter, minimalBefore, maximalBefore,
} */ } */
function checkIncrementField(value, minimal, maximal) { function checkIncrementField(value, minimal, maximal) {
var start = value.substring(0, value.indexOf("/")); let start = value.substring(0, value.indexOf("/"));
var increment = value.substring(value.indexOf("/") + 1); let increment = value.substring(value.indexOf("/") + 1);
if (!("*" == start)) { if (!("*" == start)) {
return checkIntValue(start, minimal, maximal, true) && checkIntValue(increment, minimal, maximal, false); return checkIntValue(start, minimal, maximal, true) && checkIntValue(increment, minimal, maximal, false);
@ -315,16 +317,16 @@ function checkIncrementField(value, minimal, maximal) {
function checkListField(value, minimal, maximal ) { function checkListField(value, minimal, maximal ) {
var st = value.split(","); let st = value.split(",");
var values = new Array(st.length); let values = new Array(st.length);
for(var j = 0; j < st.length; j++) { for(let j = 0; j < st.length; j++) {
values[j] = st[j]; values[j] = st[j];
} }
for (var i= 0; i < values.length; i++) { for (let i= 0; i < values.length; i++) {
var currentValue = values[i]; let currentValue = values[i];
if (!checkIntValue(currentValue, minimal, maximal, true)) { if (!checkIntValue(currentValue, minimal, maximal, true)) {
return false; return false;