.no-vue3-cron-div { .language { position: absolute; r..._vue3 cron组件">
当前位置:   article > 正文

vue3Cron表达式组件

vue3 cron组件

npm安装no-vue3-cron引入报错,就直接把代码拿来自己改了

no-vue3-cron仓库地址:https://github.com/wuchuanpeng/no-vue3-cron

vue-cron.vue

  1. <style lang="scss">
  2. .no-vue3-cron-div {
  3. .language {
  4. position: absolute;
  5. right: 25px;
  6. z-index: 1;
  7. }
  8. .el-tabs {
  9. box-shadow: none;
  10. }
  11. .tabBody {
  12. overflow: auto;
  13. .el-row {
  14. margin: 20px 0;
  15. .long {
  16. .el-select {
  17. width: 200px;
  18. }
  19. }
  20. .el-input-number {
  21. width: 120px;
  22. }
  23. }
  24. }
  25. .myScroller {
  26. &::-webkit-scrollbar {
  27. /*滚动条整体样式*/
  28. width: 5px; /*高宽分别对应横竖滚动条的尺寸*/
  29. height: 1px;
  30. }
  31. &::-webkit-scrollbar-thumb {
  32. /*滚动条里面小方块*/
  33. border-radius: 10px;
  34. background-color: skyblue;
  35. background-image: -webkit-linear-gradient(
  36. 45deg,
  37. rgba(255, 255, 255, 0.2) 25%,
  38. transparent 25%,
  39. transparent 50%,
  40. rgba(255, 255, 255, 0.2) 50%,
  41. rgba(255, 255, 255, 0.2) 75%,
  42. transparent 75%,
  43. transparent
  44. );
  45. }
  46. &::-webkit-scrollbar-track {
  47. /*滚动条里面轨道*/
  48. box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
  49. background: #ededed;
  50. border-radius: 10px;
  51. }
  52. }
  53. .bottom {
  54. width: 100%;
  55. margin-top: 5px;
  56. display: flex;
  57. align-items: center;
  58. justify-content: space-around;
  59. .value {
  60. float: left;
  61. font-size: 14px;
  62. vertical-align: middle;
  63. span:nth-child(1) {
  64. color: red
  65. }
  66. }
  67. }
  68. }
  69. </style>
  70. <template>
  71. <div class="no-vue3-cron-div">
  72. <el-button
  73. class="language"
  74. type="text"
  75. @click="state.language = state.language === 'en' ? 'cn' : 'en'"
  76. >{{ state.language === 'en' ? 'cn' : 'en' }}</el-button
  77. >
  78. <el-tabs type="border-card">
  79. <el-tab-pane>
  80. <template #label>
  81. <span><i class="el-icon-date"></i> {{ state.text.Seconds.name }}</span>
  82. </template>
  83. <div class="tabBody myScroller" :style="{'max-height': maxHeight}">
  84. <el-row>
  85. <el-radio v-model="state.second.cronEvery" label="1">{{
  86. state.text.Seconds.every
  87. }}
  88. </el-radio>
  89. </el-row>
  90. <el-row>
  91. <el-radio v-model="state.second.cronEvery" label="2"
  92. >{{ state.text.Seconds.interval[0] }}
  93. <el-input-number
  94. v-model="state.second.incrementIncrement"
  95. :min="1"
  96. :max="60"
  97. ></el-input-number>
  98. {{ state.text.Seconds.interval[1] || "" }}
  99. <el-input-number
  100. v-model="state.second.incrementStart"
  101. :min="0"
  102. :max="59"
  103. ></el-input-number>
  104. {{ state.text.Seconds.interval[2] || "" }}
  105. </el-radio>
  106. </el-row>
  107. <el-row>
  108. <el-radio class="long" v-model="state.second.cronEvery" label="3"
  109. >{{ state.text.Seconds.specific }}
  110. <el-select :style="{width: '100%'}"
  111. multiple
  112. v-model="state.second.specificSpecific"
  113. >
  114. <el-option
  115. v-for="(val, index) in 60"
  116. :key="index"
  117. :value="val - 1"
  118. >{{ val - 1 }}
  119. </el-option
  120. >
  121. </el-select>
  122. </el-radio>
  123. </el-row>
  124. <el-row>
  125. <el-radio v-model="state.second.cronEvery" label="4"
  126. >{{ state.text.Seconds.cycle[0] }}
  127. <el-input-number
  128. v-model="state.second.rangeStart"
  129. :min="1"
  130. :max="60"
  131. ></el-input-number>
  132. {{ state.text.Seconds.cycle[1] || "" }}
  133. <el-input-number
  134. v-model="state.second.rangeEnd"
  135. :min="0"
  136. :max="59"
  137. ></el-input-number>
  138. {{ state.text.Seconds.cycle[2] || "" }}
  139. </el-radio>
  140. </el-row>
  141. </div>
  142. </el-tab-pane>
  143. <el-tab-pane>
  144. <template #label>
  145. <span><i class="el-icon-date"></i> {{ state.text.Minutes.name }}</span>
  146. </template>
  147. <div class="tabBody myScroller" :style="{'max-height': maxHeight}">
  148. <el-row>
  149. <el-radio v-model="state.minute.cronEvery" label="1">{{
  150. state.text.Minutes.every
  151. }}
  152. </el-radio>
  153. </el-row>
  154. <el-row>
  155. <el-radio v-model="state.minute.cronEvery" label="2"
  156. >{{ state.text.Minutes.interval[0] }}
  157. <el-input-number
  158. v-model="state.minute.incrementIncrement"
  159. :min="1"
  160. :max="60"
  161. ></el-input-number>
  162. {{ state.text.Minutes.interval[1] }}
  163. <el-input-number
  164. v-model="state.minute.incrementStart"
  165. :min="0"
  166. :max="59"
  167. ></el-input-number>
  168. {{ state.text.Minutes.interval[2] || "" }}
  169. </el-radio>
  170. </el-row>
  171. <el-row>
  172. <el-radio class="long" v-model="state.minute.cronEvery" label="3"
  173. >{{ state.text.Minutes.specific }}
  174. <el-select :style="{width: '100%'}"
  175. multiple
  176. v-model="state.minute.specificSpecific"
  177. >
  178. <el-option
  179. v-for="(val, index) in 60"
  180. :key="index"
  181. :value="val - 1"
  182. >{{ val - 1 }}
  183. </el-option
  184. >
  185. </el-select>
  186. </el-radio>
  187. </el-row>
  188. <el-row>
  189. <el-radio v-model="state.minute.cronEvery" label="4"
  190. >{{ state.text.Minutes.cycle[0] }}
  191. <el-input-number
  192. v-model="state.minute.rangeStart"
  193. :min="1"
  194. :max="60"
  195. ></el-input-number>
  196. {{ state.text.Minutes.cycle[1] }}
  197. <el-input-number
  198. v-model="state.minute.rangeEnd"
  199. :min="0"
  200. :max="59"
  201. ></el-input-number>
  202. {{ state.text.Minutes.cycle[2] }}
  203. </el-radio>
  204. </el-row>
  205. </div>
  206. </el-tab-pane>
  207. <el-tab-pane>
  208. <template #label>
  209. <span><i class="el-icon-date"></i> {{ state.text.Hours.name }}</span>
  210. </template>
  211. <div class="tabBody myScroller" :style="{'max-height': maxHeight}">
  212. <el-row>
  213. <el-radio v-model="state.hour.cronEvery" label="1">{{
  214. state.text.Hours.every
  215. }}
  216. </el-radio>
  217. </el-row>
  218. <el-row>
  219. <el-radio v-model="state.hour.cronEvery" label="2"
  220. >{{ state.text.Hours.interval[0] }}
  221. <el-input-number
  222. v-model="state.hour.incrementIncrement"
  223. :min="0"
  224. :max="23"
  225. ></el-input-number>
  226. {{ state.text.Hours.interval[1] }}
  227. <el-input-number
  228. v-model="state.hour.incrementStart"
  229. :min="0"
  230. :max="23"
  231. ></el-input-number>
  232. {{ state.text.Hours.interval[2] }}
  233. </el-radio>
  234. </el-row>
  235. <el-row>
  236. <el-radio class="long" v-model="state.hour.cronEvery" label="3"
  237. >{{ state.text.Hours.specific }}
  238. <el-select :style="{width: '100%'}" multiple v-model="state.hour.specificSpecific">
  239. <el-option
  240. v-for="(val, index) in 24"
  241. :key="index"
  242. :value="val - 1"
  243. >{{ val - 1 }}
  244. </el-option
  245. >
  246. </el-select>
  247. </el-radio>
  248. </el-row>
  249. <el-row>
  250. <el-radio v-model="state.hour.cronEvery" label="4"
  251. >{{ state.text.Hours.cycle[0] }}
  252. <el-input-number
  253. v-model="state.hour.rangeStart"
  254. :min="0"
  255. :max="23"
  256. ></el-input-number>
  257. {{ state.text.Hours.cycle[1] }}
  258. <el-input-number
  259. v-model="state.hour.rangeEnd"
  260. :min="0"
  261. :max="23"
  262. ></el-input-number>
  263. {{ state.text.Hours.cycle[2] }}
  264. </el-radio>
  265. </el-row>
  266. </div>
  267. </el-tab-pane>
  268. <el-tab-pane>
  269. <template #label>
  270. <span><i class="el-icon-date"></i> {{ state.text.Day.name }}</span>
  271. </template>
  272. <div class="tabBody myScroller" :style="{'max-height': maxHeight}">
  273. <el-row>
  274. <el-radio v-model="state.day.cronEvery" label="1">{{
  275. state.text.Day.every
  276. }}
  277. </el-radio>
  278. </el-row>
  279. <el-row>
  280. <el-radio v-model="state.day.cronEvery" label="2"
  281. >{{ state.text.Day.intervalWeek[0] }}
  282. <el-input-number
  283. v-model="state.week.incrementIncrement"
  284. :min="1"
  285. :max="7"
  286. ></el-input-number>
  287. {{ state.text.Day.intervalWeek[1] }}
  288. <el-select :style="{width: '100%'}" v-model="state.week.incrementStart">
  289. <el-option
  290. v-for="(val, index) in 7"
  291. :key="index"
  292. :label="state.text.Week[val - 1]"
  293. :value="val"
  294. ></el-option>
  295. </el-select>
  296. {{ state.text.Day.intervalWeek[2] }}
  297. </el-radio>
  298. </el-row>
  299. <el-row>
  300. <el-radio v-model="state.day.cronEvery" label="3"
  301. >{{ state.text.Day.intervalDay[0] }}
  302. <el-input-number
  303. v-model="state.day.incrementIncrement"
  304. :min="1"
  305. :max="31"
  306. ></el-input-number>
  307. {{ state.text.Day.intervalDay[1] }}
  308. <el-input-number
  309. v-model="state.day.incrementStart"
  310. :min="1"
  311. :max="31"
  312. ></el-input-number>
  313. {{ state.text.Day.intervalDay[2] }}
  314. </el-radio>
  315. </el-row>
  316. <el-row>
  317. <el-radio class="long" v-model="state.day.cronEvery" label="4"
  318. >{{ state.text.Day.specificWeek }}
  319. <el-select :style="{width: '100%'}" multiple v-model="state.week.specificSpecific">
  320. <el-option
  321. v-for="(val, index) in 7"
  322. :key="index"
  323. :label="state.text.Week[val - 1]"
  324. :value="
  325. ['SUN', 'MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT'][val - 1]
  326. "
  327. ></el-option>
  328. </el-select>
  329. </el-radio>
  330. </el-row>
  331. <el-row>
  332. <el-radio class="long" v-model="state.day.cronEvery" label="5"
  333. >{{ state.text.Day.specificDay }}
  334. <el-select :style="{width: '100%'}" multiple v-model="state.day.specificSpecific">
  335. <el-option
  336. v-for="(val, index) in 31"
  337. :key="index"
  338. :value="val"
  339. >{{ val }}
  340. </el-option
  341. >
  342. </el-select>
  343. </el-radio>
  344. </el-row>
  345. <el-row>
  346. <el-radio v-model="state.day.cronEvery" label="6">{{
  347. state.text.Day.lastDay
  348. }}
  349. </el-radio>
  350. </el-row>
  351. <el-row>
  352. <el-radio v-model="state.day.cronEvery" label="7">{{
  353. state.text.Day.lastWeekday
  354. }}
  355. </el-radio>
  356. </el-row>
  357. <el-row>
  358. <el-radio v-model="state.day.cronEvery" label="8"
  359. >{{ state.text.Day.lastWeek[0] }}
  360. <el-select :style="{width: '100%'}" v-model="state.week.cronLastSpecificDomDay">
  361. <el-option
  362. v-for="(val, index) in 7"
  363. :key="index"
  364. :label="state.text.Week[val - 1]"
  365. :value="val"
  366. ></el-option>
  367. </el-select>
  368. {{ state.text.Day.lastWeek[1] || "" }}
  369. </el-radio>
  370. </el-row>
  371. <el-row>
  372. <el-radio v-model="state.day.cronEvery" label="9">
  373. <el-input-number
  374. v-model="state.day.cronDaysBeforeEomMinus"
  375. :min="1"
  376. :max="31"
  377. ></el-input-number>
  378. {{ state.text.Day.beforeEndMonth[0] }}
  379. </el-radio>
  380. </el-row>
  381. <el-row>
  382. <el-radio v-model="state.day.cronEvery" label="10"
  383. >{{ state.text.Day.nearestWeekday[0] }}
  384. <el-input-number
  385. v-model="state.day.cronDaysNearestWeekday"
  386. :min="1"
  387. :max="31"
  388. ></el-input-number>
  389. {{ state.text.Day.nearestWeekday[1] }}
  390. </el-radio>
  391. </el-row>
  392. <el-row>
  393. <el-radio v-model="state.day.cronEvery" label="11"
  394. >{{ state.text.Day.someWeekday[0] }}
  395. <el-input-number
  396. v-model="state.week.cronNthDayNth"
  397. :min="1"
  398. :max="5"
  399. ></el-input-number>
  400. <el-select :style="{width: '100%'}" v-model="state.week.cronNthDayDay">
  401. <el-option
  402. v-for="(val, index) in 7"
  403. :key="index"
  404. :label="state.text.Week[val - 1]"
  405. :value="val"
  406. ></el-option>
  407. </el-select>
  408. {{ state.text.Day.someWeekday[1] }}
  409. </el-radio>
  410. </el-row>
  411. </div>
  412. </el-tab-pane>
  413. <el-tab-pane>
  414. <template #label>
  415. <span><i class="el-icon-date"></i> {{ state.text.Month.name }}</span>
  416. </template>
  417. <div class="tabBody myScroller" :style="{'max-height': maxHeight}">
  418. <el-row>
  419. <el-radio v-model="state.month.cronEvery" label="1">{{
  420. state.text.Month.every
  421. }}
  422. </el-radio>
  423. </el-row>
  424. <el-row>
  425. <el-radio v-model="state.month.cronEvery" label="2"
  426. >{{ state.text.Month.interval[0] }}
  427. <el-input-number
  428. v-model="state.month.incrementIncrement"
  429. :min="0"
  430. :max="12"
  431. ></el-input-number>
  432. {{ state.text.Month.interval[1] }}
  433. <el-input-number
  434. v-model="state.month.incrementStart"
  435. :min="0"
  436. :max="12"
  437. ></el-input-number>
  438. </el-radio>
  439. </el-row>
  440. <el-row>
  441. <el-radio class="long" v-model="state.month.cronEvery" label="3"
  442. >{{ state.text.Month.specific }}
  443. <el-select :style="{width: '100%'}" multiple v-model="state.month.specificSpecific">
  444. <el-option
  445. v-for="(val, index) in 12"
  446. :key="index"
  447. :label="val"
  448. :value="val"
  449. ></el-option>
  450. </el-select>
  451. </el-radio>
  452. </el-row>
  453. <el-row>
  454. <el-radio v-model="state.month.cronEvery" label="4"
  455. >{{ state.text.Month.cycle[0] }}
  456. <el-input-number
  457. v-model="state.month.rangeStart"
  458. :min="1"
  459. :max="12"
  460. ></el-input-number>
  461. {{ state.text.Month.cycle[1] }}
  462. <el-input-number
  463. v-model="state.month.rangeEnd"
  464. :min="1"
  465. :max="12"
  466. ></el-input-number>
  467. </el-radio>
  468. </el-row>
  469. </div>
  470. </el-tab-pane>
  471. <el-tab-pane>
  472. <template #label>
  473. <span><i class="el-icon-date"></i> {{ state.text.Year.name }}</span>
  474. </template>
  475. <div class="tabBody myScroller" :style="{'max-height': maxHeight}">
  476. <el-row>
  477. <el-radio v-model="state.year.cronEvery" label="1">{{
  478. state.text.Year.every
  479. }}
  480. </el-radio>
  481. </el-row>
  482. <el-row>
  483. <el-radio v-model="state.year.cronEvery" label="2"
  484. >{{ state.text.Year.interval[0] }}
  485. <el-input-number
  486. v-model="state.year.incrementIncrement"
  487. :min="1"
  488. :max="99"
  489. ></el-input-number>
  490. {{ state.text.Year.interval[1] }}
  491. <el-input-number
  492. v-model="state.year.incrementStart"
  493. :min="2018"
  494. :max="2118"
  495. ></el-input-number>
  496. </el-radio>
  497. </el-row>
  498. <el-row>
  499. <el-radio class="long" v-model="state.year.cronEvery" label="3"
  500. >{{ state.text.Year.specific }}
  501. <el-select :style="{width: '100%'}"
  502. filterable
  503. multiple
  504. v-model="state.year.specificSpecific"
  505. >
  506. <el-option
  507. v-for="(val, index) in 100"
  508. :key="index"
  509. :label="2017 + val"
  510. :value="2017 + val"
  511. ></el-option>
  512. </el-select>
  513. </el-radio>
  514. </el-row>
  515. <el-row>
  516. <el-radio v-model="state.year.cronEvery" label="4"
  517. >{{ state.text.Year.cycle[0] }}
  518. <el-input-number
  519. v-model="state.year.rangeStart"
  520. :min="2018"
  521. :max="2118"
  522. ></el-input-number>
  523. {{ state.text.Year.cycle[1] }}
  524. <el-input-number
  525. v-model="state.year.rangeEnd"
  526. :min="2018"
  527. :max="2118"
  528. ></el-input-number>
  529. </el-radio>
  530. </el-row>
  531. </div>
  532. </el-tab-pane>
  533. </el-tabs>
  534. <div class="bottom">
  535. <div class="value">
  536. <span>
  537. cron预览:
  538. </span>
  539. <el-tag type="primary">
  540. {{ state.cron }}
  541. </el-tag>
  542. </div>
  543. <div class="buttonDiv">
  544. <el-button type="primary" size="mini" @click.stop="handleChange">{{ state.text.Save }}</el-button>
  545. <el-button type="primary" size="mini" @click.stop="close">{{ state.text.Close }}</el-button>
  546. </div>
  547. </div>
  548. </div>
  549. </template>
  550. <script>
  551. import Language from "./language";
  552. import {watch, reactive, computed, toRefs, defineComponent} from "vue";
  553. export default defineComponent({
  554. name: "noVue3Cron",
  555. props: {
  556. cronValue: String,
  557. i18n: {},
  558. maxHeight: {}
  559. },
  560. setup(props, {emit}) {
  561. const {i18n} = toRefs(props)
  562. const state = reactive({
  563. language: i18n.value,
  564. second: {
  565. cronEvery: "1",
  566. incrementStart: 3,
  567. incrementIncrement: 5,
  568. rangeStart: 0,
  569. rangeEnd: 0,
  570. specificSpecific: [],
  571. },
  572. minute: {
  573. cronEvery: "1",
  574. incrementStart: 3,
  575. incrementIncrement: 5,
  576. rangeStart: 0,
  577. rangeEnd: 0,
  578. specificSpecific: [],
  579. },
  580. hour: {
  581. cronEvery: "1",
  582. incrementStart: 3,
  583. incrementIncrement: 5,
  584. rangeStart: 0,
  585. rangeEnd: 0,
  586. specificSpecific: [],
  587. },
  588. day: {
  589. cronEvery: "1",
  590. incrementStart: 1,
  591. incrementIncrement: 1,
  592. rangeStart: 0,
  593. rangeEnd: 0,
  594. specificSpecific: [],
  595. cronDaysBeforeEomMinus: 0,
  596. cronDaysNearestWeekday: 0,
  597. },
  598. week: {
  599. cronEvery: "1",
  600. incrementStart: 1,
  601. incrementIncrement: 1,
  602. specificSpecific: [],
  603. cronLastSpecificDomDay: 1,
  604. cronNthDayDay: 1,
  605. cronNthDayNth: 1,
  606. },
  607. month: {
  608. cronEvery: "1",
  609. incrementStart: 3,
  610. incrementIncrement: 5,
  611. rangeStart: 0,
  612. rangeEnd: 0,
  613. specificSpecific: [],
  614. },
  615. year: {
  616. cronEvery: "1",
  617. incrementStart: 2017,
  618. incrementIncrement: 1,
  619. rangeStart: 0,
  620. rangeEnd: 0,
  621. specificSpecific: [],
  622. },
  623. output: {
  624. second: "",
  625. minute: "",
  626. hour: "",
  627. day: "",
  628. month: "",
  629. Week: "",
  630. year: "",
  631. },
  632. text: computed(() => Language[state.language || "cn"]),
  633. secondsText: computed(() => {
  634. let seconds = "";
  635. let cronEvery = state.second.cronEvery;
  636. switch (cronEvery.toString()) {
  637. case "1":
  638. seconds = "*";
  639. break;
  640. case "2":
  641. seconds =
  642. state.second.incrementStart +
  643. "/" +
  644. state.second.incrementIncrement;
  645. break;
  646. case "3":
  647. state.second.specificSpecific.map((val) => {
  648. seconds += val + ",";
  649. });
  650. seconds = seconds.slice(0, -1);
  651. break;
  652. case "4":
  653. seconds = state.second.rangeStart + "-" + state.second.rangeEnd;
  654. break;
  655. }
  656. return seconds;
  657. }),
  658. minutesText: computed(() => {
  659. let minutes = "";
  660. let cronEvery = state.minute.cronEvery;
  661. switch (cronEvery.toString()) {
  662. case "1":
  663. minutes = "*";
  664. break;
  665. case "2":
  666. minutes =
  667. state.minute.incrementStart +
  668. "/" +
  669. state.minute.incrementIncrement;
  670. break;
  671. case "3":
  672. state.minute.specificSpecific.map((val) => {
  673. minutes += val + ",";
  674. });
  675. minutes = minutes.slice(0, -1);
  676. break;
  677. case "4":
  678. minutes = state.minute.rangeStart + "-" + state.minute.rangeEnd;
  679. break;
  680. }
  681. return minutes;
  682. }),
  683. hoursText: computed(() => {
  684. let hours = "";
  685. let cronEvery = state.hour.cronEvery;
  686. switch (cronEvery.toString()) {
  687. case "1":
  688. hours = "*";
  689. break;
  690. case "2":
  691. hours =
  692. state.hour.incrementStart + "/" + state.hour.incrementIncrement;
  693. break;
  694. case "3":
  695. state.hour.specificSpecific.map((val) => {
  696. hours += val + ",";
  697. });
  698. hours = hours.slice(0, -1);
  699. break;
  700. case "4":
  701. hours = state.hour.rangeStart + "-" + state.hour.rangeEnd;
  702. break;
  703. }
  704. return hours;
  705. }),
  706. daysText: computed(() => {
  707. let days = "";
  708. let cronEvery = state.day.cronEvery;
  709. switch (cronEvery.toString()) {
  710. case "1":
  711. break;
  712. case "2":
  713. case "4":
  714. case "11":
  715. case "8":
  716. days = "?";
  717. break;
  718. case "3":
  719. days =
  720. state.day.incrementStart + "/" + state.day.incrementIncrement;
  721. break;
  722. case "5":
  723. state.day.specificSpecific.map((val) => {
  724. days += val + ",";
  725. });
  726. days = days.slice(0, -1);
  727. break;
  728. case "6":
  729. days = "L";
  730. break;
  731. case "7":
  732. days = "LW";
  733. break;
  734. case "9":
  735. days = "L-" + state.day.cronDaysBeforeEomMinus;
  736. break;
  737. case "10":
  738. days = state.day.cronDaysNearestWeekday + "W";
  739. break;
  740. }
  741. return days;
  742. }),
  743. weeksText: computed(() => {
  744. let weeks = "";
  745. let cronEvery = state.day.cronEvery;
  746. switch (cronEvery.toString()) {
  747. case "1":
  748. case "3":
  749. case "5":
  750. weeks = "?";
  751. break;
  752. case "2":
  753. weeks =
  754. state.week.incrementStart + "/" + state.week.incrementIncrement;
  755. break;
  756. case "4":
  757. state.week.specificSpecific.map((val) => {
  758. weeks += val + ",";
  759. });
  760. weeks = weeks.slice(0, -1);
  761. break;
  762. case "6":
  763. case "7":
  764. case "9":
  765. case "10":
  766. weeks = "?";
  767. break;
  768. case "8":
  769. weeks = state.week.cronLastSpecificDomDay + "L";
  770. break;
  771. case "11":
  772. weeks = state.week.cronNthDayDay + "#" + state.week.cronNthDayNth;
  773. break;
  774. }
  775. return weeks;
  776. }),
  777. monthsText: computed(() => {
  778. let months = "";
  779. let cronEvery = state.month.cronEvery;
  780. switch (cronEvery.toString()) {
  781. case "1":
  782. months = "*";
  783. break;
  784. case "2":
  785. months =
  786. state.month.incrementStart + "/" + state.month.incrementIncrement;
  787. break;
  788. case "3":
  789. state.month.specificSpecific.map((val) => {
  790. months += val + ",";
  791. });
  792. months = months.slice(0, -1);
  793. break;
  794. case "4":
  795. months = state.month.rangeStart + "-" + state.month.rangeEnd;
  796. break;
  797. }
  798. return months;
  799. }),
  800. yearsText: computed(() => {
  801. let years = "";
  802. let cronEvery = state.year.cronEvery;
  803. switch (cronEvery.toString()) {
  804. case "1":
  805. years = "*";
  806. break;
  807. case "2":
  808. years =
  809. state.year.incrementStart + "/" + state.year.incrementIncrement;
  810. break;
  811. case "3":
  812. state.year.specificSpecific.map((val) => {
  813. years += val + ",";
  814. });
  815. years = years.slice(0, -1);
  816. break;
  817. case "4":
  818. years = state.year.rangeStart + "-" + state.year.rangeEnd;
  819. break;
  820. }
  821. return years;
  822. }),
  823. cron: computed(() => {
  824. return `${state.secondsText || "*"} ${state.minutesText ||
  825. "*"} ${state.hoursText || "*"} ${state.daysText ||
  826. "*"} ${state.monthsText || "*"} ${state.weeksText ||
  827. "?"} ${state.yearsText || "*"}`;
  828. }),
  829. });
  830. watch(() => props.cronValue, (newCron) => {
  831. if(typeof(newCron) !== 'string' || !newCron) return false
  832. let crons = newCron.split(" ");
  833. // 解析seconds
  834. let secondsText = crons[0].trim();
  835. if (secondsText === "*") {
  836. state.second.cronEvery = "1";
  837. }else if (secondsText.includes("/")) {
  838. state.second.cronEvery = "2";
  839. let secondsTexts = secondsText.split("/");
  840. state.second.incrementStart = parseInt(secondsTexts[0])
  841. state.second.incrementIncrement = parseInt(secondsTexts[1])
  842. }else if (secondsText.includes(",") || isFinite(secondsText)) {
  843. state.second.cronEvery = "3";
  844. state.second.specificSpecific = secondsText.split(",").map(item => parseInt(item));
  845. }else if (secondsText.includes("-")) {
  846. state.second.cronEvery = "4";
  847. let secondsTexts = secondsText.split("-");
  848. state.second.rangeStart = parseInt(secondsTexts[0])
  849. state.second.rangeEnd = parseInt(secondsTexts[1])
  850. }
  851. // 解析minutes
  852. let minutesText = crons[1].trim();
  853. if (minutesText === "*") {
  854. state.minute.cronEvery = "1";
  855. }else if (minutesText.includes("/")) {
  856. state.minute.cronEvery = "2";
  857. let minutesTexts = minutesText.split("/");
  858. state.minute.incrementStart = parseInt(minutesTexts[0])
  859. state.minute.incrementIncrement = parseInt(minutesTexts[1])
  860. }else if (minutesText.includes(",") || isFinite(minutesText)) {
  861. state.minute.cronEvery = "3";
  862. state.minute.specificSpecific = minutesText.split(",").map(item => parseInt(item));
  863. }else if (minutesText.includes("-")) {
  864. state.minute.cronEvery = "4";
  865. let minutesTexts = minutesText.split("-");
  866. state.minute.rangeStart = parseInt(minutesTexts[0])
  867. state.minute.rangeEnd = parseInt(minutesTexts[1])
  868. }
  869. // 解析hours
  870. let hoursText = crons[2].trim();
  871. if (hoursText === "*") {
  872. state.hour.cronEvery = "1";
  873. }else if (hoursText.includes("/")) {
  874. state.hour.cronEvery = "2";
  875. let hoursTexts = hoursText.split("/");
  876. state.hour.incrementStart = parseInt(hoursTexts[0])
  877. state.hour.incrementIncrement = parseInt(hoursTexts[1])
  878. }else if (hoursText.includes(",") || isFinite(hoursText)) {
  879. state.hour.cronEvery = "3";
  880. state.hour.specificSpecific = hoursText.split(",").map(item => parseInt(item));
  881. }else if (hoursText.includes("-")) {
  882. state.hour.cronEvery = "4";
  883. let hoursTexts = hoursText.split("-");
  884. state.hour.rangeStart = parseInt(hoursTexts[0])
  885. state.hour.rangeEnd = parseInt(hoursTexts[1])
  886. }
  887. // 解析days weeks
  888. let daysText = crons[3].trim();
  889. let weeksText = crons[5].trim();
  890. if (daysText.includes("/")) {
  891. state.day.cronEvery = "3";
  892. let daysTexts = daysText.split("/");
  893. state.day.incrementStart = parseInt(daysTexts[0])
  894. state.day.incrementIncrement = parseInt(daysTexts[1])
  895. }else if (daysText.includes(",") || isFinite(daysText)) {
  896. state.day.cronEvery = "5";
  897. state.day.specificSpecific = daysText.split(",").map(item => parseInt(item));
  898. }else if (daysText === "L") {
  899. state.day.cronEvery = "6";
  900. }else if (daysText === "LW") {
  901. state.day.cronEvery = "7";
  902. }else if (daysText.startsWith("L-")) {
  903. state.day.cronEvery = "9";
  904. state.day.cronDaysBeforeEomMinus = parseInt(daysText.replaceAll("L-", ""))
  905. }else if (daysText.endsWith("W")) {
  906. state.day.cronEvery = "10";
  907. state.day.cronDaysNearestWeekday = parseInt(daysText.replaceAll("W", ""))
  908. }else if (daysText === "?") {
  909. if (weeksText.includes("/")) {
  910. state.day.cronEvery = "2";
  911. let weeksTexts = weeksText.split("/");
  912. state.week.incrementStart = parseInt(weeksTexts[0])
  913. state.week.incrementIncrement = parseInt(weeksTexts[1])
  914. }else if (weeksText.includes(",") || isFinite(weeksText)) {
  915. state.day.cronEvery = "4";
  916. state.week.specificSpecific = weeksText.split(",").map(item => item);
  917. }else if (weeksText.includes("#")) {
  918. state.day.cronEvery = "11";
  919. let weeksTexts = weeksText.split("#");
  920. state.week.cronNthDayDay = parseInt(weeksTexts[0])
  921. state.week.cronNthDayNth = parseInt(weeksTexts[1])
  922. }else if (weeksText.endsWith("L")) {
  923. state.day.cronEvery = "8";
  924. state.week.cronLastSpecificDomDay = parseInt(weeksText.replaceAll("L", ""))
  925. }
  926. }else {
  927. state.day.cronEvery = "1";
  928. }
  929. // 解析months
  930. let monthsText = crons[4].trim();
  931. if (monthsText === "*") {
  932. state.month.cronEvery = "1";
  933. }else if (monthsText.includes("/")) {
  934. state.month.cronEvery = "2";
  935. let monthsTexts = monthsText.split("/");
  936. state.month.incrementStart = parseInt(monthsTexts[0])
  937. state.month.incrementIncrement = parseInt(monthsTexts[1])
  938. }else if (monthsText.includes(",") || isFinite(monthsText)) {
  939. state.month.cronEvery = "3";
  940. state.month.specificSpecific = monthsText.split(",").map(item => parseInt(item));
  941. }else if (monthsText.includes("-")) {
  942. state.month.cronEvery = "4";
  943. let monthsTexts = monthsText.split("-");
  944. state.month.rangeStart = parseInt(monthsTexts[0])
  945. state.month.rangeEnd = parseInt(monthsTexts[1])
  946. }
  947. // 解析years
  948. let yearsText = crons[6].trim();
  949. if (yearsText === "*") {
  950. state.year.cronEvery = "1";
  951. }else if (yearsText.includes("/")) {
  952. state.year.cronEvery = "2";
  953. let yearsTexts = yearsText.split("/");
  954. state.year.incrementStart = parseInt(yearsTexts[0])
  955. state.year.incrementIncrement = parseInt(yearsTexts[1])
  956. }else if (yearsText.includes(",") || isFinite(yearsText)) {
  957. state.year.cronEvery = "3";
  958. state.year.specificSpecific = yearsText.split(",").map(item => parseInt(item));
  959. }else if (yearsText.includes("-")) {
  960. state.year.cronEvery = "4";
  961. let yearsTexts = yearsText.split("-");
  962. state.year.rangeStart = parseInt(yearsTexts[0])
  963. state.year.rangeEnd = parseInt(yearsTexts[1])
  964. }
  965. }, {
  966. immediate: true
  967. })
  968. const getValue = () => {
  969. return state.cron
  970. }
  971. const close = () => {
  972. emit('close')
  973. }
  974. const handleChange = () => {
  975. emit('change', state.cron)
  976. close()
  977. }
  978. const rest = (data) => {
  979. for (let i in data) {
  980. if (data[i] instanceof Object) {
  981. this.rest(data[i]);
  982. } else {
  983. switch (typeof data[i]) {
  984. case "object":
  985. data[i] = [];
  986. break;
  987. case "string":
  988. data[i] = "";
  989. break;
  990. }
  991. }
  992. }
  993. }
  994. return {
  995. state,
  996. getValue,
  997. close,
  998. handleChange,
  999. rest
  1000. }
  1001. },
  1002. });
  1003. </script>

使用

  1. <el-input v-model="formData.paramExpression"
  2. :disabled="disabledStatus"
  3. placeholder="请输入定时表达式" >
  4. <template #append>
  5. <el-popover :visible="cronPopover" width="700px" trigger="manual">
  6. <vue-cron
  7. :value="formData.paramExpression"
  8. @change="changeCron"
  9. @close="cronPopover=false"
  10. max-height="400px"
  11. i18n="cn"
  12. ></vue-cron>
  13. <template #reference>
  14. <el-button @click="cronPopover = !cronPopover">设置定时表达式</el-button>
  15. </template>
  16. </el-popover>
  17. </template>
  18. </el-input>
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/410658
推荐阅读
相关标签
  

闽ICP备14008679号