网上找的没有满意的,决定从若依前后端分离其前端vue2中的crontab进行转换,先上效果
若依v2版:
改v3后:
v2转v3没什么难度,其中有大量的将 this.*** 替换为 ***.value,笔者写了个正则替换,希望可以帮助大家
this.(w+) $服务器托管1.value
需要注意的有,在v2中【this.$refs[refName].cycle01 = indexArr[0]】这样写
在v3中要转换一下,在子组件中用【defineExpose】抛出一个setData方法,然后【proxy.$refs[refName].setData(“cycle01”, Number(indexArr[0]))】赋值
其中子组件CrontabSecond.vue的代码,其它分/时/日/月/周/年的类似,参照着改就可以了
每秒,允许的通配符[, - * /] 周期,从 - 秒 从 秒开始,每 秒执行一次 指定 for="item in 60" :key="item" :value="String(item-1)" :label="'第' + String(item-1) + '秒'">第 {{item-1}} 秒 import { ref } from 'vue'; const emits = defineEmits(["update"]); const props = defineProps({ check: { type: Function, require:false }, cron: { type: Object, default: function(){ return {} } }, }); const radioValue = ref(1); const cycle01 = ref(1); const cycle02 = ref(2); const average01 = ref(0); const average02 = ref(1); const checkboxList = ref([]); // 计算两个周期值 const cycleTotal = computed(() => { const c01 = props.check(cycle01.value, 0, 58) const c02 = props.check(cycle02.value, c01 ? c01 + 1 : 1, 59) return c01 + '-' + c02; }) // 计算平均用到的值 const averageTotal = computed(() => { const a01 = props.check(average01.value, 0, 58) const a02 = props.check(average02.value, 1, 59 - a01 || 0) return a01 + '/' + a02; }) // 计算勾选的checkbox值合集 const checkboxString = computed(() => { let str = checkboxList.value.join(); return str == '' ? '*' : str; }) watch(()=>radioValue.value, ()=>{ radioChange(); }) watch(()=>cycleTotal.value, ()=>{ cycleChange(); }) watch(()=>averageTotal.value, ()=>{ averageChange(); }) watch(()=>checkboxString.value, ()=>{ checkboxChange(); }) // 单选按钮值变化时 function radioChange() { switch (radioValue.value) { case 1: emits('update', 'second', '*', 'second'); break; case 2: emits('update', 'second', cycleTotal.value); break; case 3: emits('update', 'second', averageTotal.value); break; case 4: emits('update', 'second', checkboxString.value); break; } } // 周期两个值变化时 function cycleChange() { if (radioValue.value == '2') { emits('update', 'second', cycleTotal.value); } } // 平均两个值变化时 function averageChange() { if (radioValue.value == '3') { emits('update', 'second', averageTotal.value); } } // checkbox值变化时 function checkboxChange() { if (radioValue.value == '4') { emits('update', 'second', checkboxString.value); } } defineExpose({ setData(key, value){ eval(key).value = value } })
关于CrontabResult.vue的组件,转v3时,额外注意数组排序这里就好了,
# 如果改为compare.value会出现bug的 正确为 arr.sort(this.compare) 改为 arr.sort(compare)
贴出核心Crontab.vue的代码,其子组件就不一一贴了,需要的可以自己下若依代码进行转换
CrontabSecond @update="updateCrontabValue" v-model:check="checkNumber" v-model:cron="crontabValueObj" ref="cronsecond" /> CrontabMin @update="updateCrontabValue" v-model:check="checkNumber" v-model:cron="crontabValueObj" ref="cronmin" /> CrontabHour @update="updateCrontabValue" v-model:check="checkNumber" v-model:cron="crontabValueObj" ref="cronhour" /> CrontabDay @update="updateCrontabValue" v-model:check="checkNumber" v-model:cron="crontabValueObj" ref="cronday" /> CrontabMonth @update="updateCrontabValue" v-model:check="checkNumber" v-model:cron="crontabValueObj" ref="cronmonth" /> CrontabWeek @update="updateCrontabValue" v-model:check="checkNumber" v-model:cron="crontabValueObj" ref="cronweek" /> CrontabYear @update="updateCrontabValue" v-model:check="checkNumber" v-model:cron="crontabValueObj" ref="cronyear" />import CrontabSecond fro服务器托管m "./crontab/CrontabSecond.vue"; import CrontabMin from "./crontab/CrontabMin.vue"; import CrontabHour from "./crontab/CrontabHour.vue"; import CrontabDay from "./crontab/CrontabDay.vue"; import CrontabMonth from "./crontab/CrontabMonth.vue"; import CrontabWeek from "./crontab/CrontabWeek.vue"; import CrontabYear from "./crontab/CrontabYear.vue"; import CrontabResult from "./crontab/CrontabResult.vue"; const { proxy } = getCurrentInstance(); const emits = defineEmits(["hide", "fill"]); const props = defineProps({ expression: {type: String, default: ""} }) const tabTitles = ref(["秒", "分钟", "小时", "日", "月", "周", "年"]) const tabActive = ref(0) const crontabValueObj = ref({ second: "*", min: "*", hour: "*", day: "*", month: "*", week: "?", year: "", }) const crontabValueString = computed(() => { let obj = crontabValueObj.value; let str = obj.second + " " + obj.min + " " + obj.hour + " " + obj.day + " " + obj.month + " " + obj.week + (obj.year == "" ? "" : " " + obj.year); return str; }) onMounted(() => { resolveExp(); }) watch(() => props.expression, (v) => { resolveExp(); }); function resolveExp() { // 反解析 表达式 if (props.expression) { let arr = props.expression.split(" "); if (arr.length >= 6) { //6 位以上是合法表达式 let obj = { second: arr[0], min: arr[1], hour: arr[2], day: arr[3], month: arr[4], week: arr[5], year: arr[6] ? arr[6] : "", }; crontabValueObj.value = { ...obj, }; for (let i in obj) { if (obj[i]) changeRadio(i, obj[i]); } } } else { // 没有传入的表达式 则还原 clearCron(); } } // tab切换值 function tabCheck(index) { tabActive.value = index; } // 由子组件触发,更改表达式组成的字段值 function updateCrontabValue(name, value, from) { // "updateCrontabValue", name, value, from; crontabValueObj.value[name] = value; if (from && from !== name) { console.log(`来自组件 ${from} 改变了 ${name} ${value}`); changeRadio(name, value); } } // 赋值到组件 function changeRadio(name, value) { let arr = ["second", "min", "hour", "month"] let refName = "cron" + name let insValue; if (!proxy.$refs[refName]) return; if (arr.includes(name)) { if (value === "*") { insValue = 1; } else if (value.indexOf("-") > -1) { let indexArr = value.split("-"); isNaN(indexArr[0]) ? (proxy.$refs[refName].setData("cycle01", 0)) : (proxy.$refs[refName].setData("cycle01", Number(indexArr[0]))); proxy.$refs[refName].setData("cycle02", Number(indexArr[1])); insValue = 2; } else if (value.indexOf("/") > -1) { let indexArr = value.split("/"); isNaN(indexArr[0]) ? (proxy.$refs[refName].setData("average01", 0)) : (proxy.$refs[refName].setData("average01", Number(indexArr[0]))); proxy.$refs[refName].setData("average02", Number(indexArr[1])); insValue = 3; } else { insValue = 4; let list = value.split(","); for(let item of list){ item = String(item) } proxy.$refs[refName].setData("checkboxList", list); } } else if (name == "day") { if (value === "*") { insValue = 1; } else if (value == "?") { insValue = 2; } else if (value.indexOf("-") > -1) { let indexArr = value.split("-"); isNaN(indexArr[0]) ? (proxy.$refs[refName].setData("cycle01", 0)) : (proxy.$refs[refName].setData("cycle01", Number(indexArr[0]))); proxy.$refs[refName].setData("cycle02", Number(indexArr[1])); insValue = 3; } else if (value.indexOf("/") > -1) { let indexArr = value.split("/"); isNaN(indexArr[0]) ? (proxy.$refs[refName].setData("average01", 0)) : (proxy.$refs[refName].setData("average01", Number(indexArr[0]))); proxy.$refs[refName].setData("average02", Number(indexArr[1])); insValue = 4; } else if (value.indexOf("W") > -1) { let indexArr = value.split("W"); isNaN(indexArr[0]) ? (proxy.$refs[refName].setData("workday", 0)) : (proxy.$refs[refName].setData("workday", Number(indexArr[0]))); insValue = 5; } else if (value === "L") { insValue = 6; } else { let list = value.split(","); for(let item of list){ item = String(item) } proxy.$refs[refName].setData("checkboxList", list); insValue = 7; } } else if (name == "week") { if (value === "*") { insValue = 1; } else if (value == "?") { insValue = 2; } else if (value.indexOf("-") > -1) { let indexArr = value.split("-"); isNaN(indexArr[0]) ? (proxy.$refs[refName].setData("cycle01", "0")) : (proxy.$refs[refName].setData("cycle01", String(indexArr[0]))); proxy.$refs[refName].setData("cycle02", String(indexArr[1])); insValue = 3; } else if (value.indexOf("#") > -1) { let indexArr = value.split("#"); isNaN(indexArr[0]) ? (proxy.$refs[refName].setData("average01", 1)) : (proxy.$refs[refName].setData("average01", Number(indexArr[0]))); proxy.$refs[refName].setData("average02", String(indexArr[1])); insValue = 4; } else if (value.indexOf("L") > -1) { let indexArr = value.split("L"); isNaN(indexArr[0]) ? (proxy.$refs[refName].setData("weekday", "1")) : (proxy.$refs[refName].setData("weekday", String(indexArr[0]))); insValue = 5; } else { let list = value.split(","); for(let item of list){ item = String(item) } proxy.$refs[refName].setData("checkboxList", list); insValue = 6; } } else if (name == "year") { if (value == "") { insValue = 1; } else if (value == "*") { insValue = 2; } else if (value.indexOf("-") > -1) { insValue = 3; } else if (value.indexOf("/") > -1) { insValue = 4; } else { let list = value.split(","); for(let item of list){ item = String(item) } proxy.$refs[refName].setData("checkboxList", list); insValue = 5; } } proxy.$refs[refName].setData("radioValue", insValue); } // 表单选项的子组件校验数字格式(通过-props传递) function
for="item of tabTitles" width="40" :key="item">{{item}} {{crontabValueObj.second}} {{crontabValueObj.min}} {{crontabValueObj.hour}} {{crontabValueObj.day}} {{crontabValueObj.month}} {{crontabValueObj.week}} {{crontabValueObj.year}}
Cron 表达式 {{crontabValueString}}