result.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. <template>
  2. <div class="popup-result">
  3. <p class="title">最近5次运行时间</p>
  4. <ul class="popup-result-scroll">
  5. <template v-if='isShow'>
  6. <li v-for='item in resultList' :key="item">{{item}}</li>
  7. </template>
  8. <li v-else>计算结果中...</li>
  9. </ul>
  10. </div>
  11. </template>
  12. <script>
  13. export default {
  14. data() {
  15. return {
  16. dayRule: '',
  17. dayRuleSup: '',
  18. dateArr: [],
  19. resultList: [],
  20. isShow: false
  21. }
  22. },
  23. name: 'crontab-result',
  24. methods: {
  25. // 表达式值变化时,开始去计算结果
  26. expressionChange() {
  27. // 计算开始-隐藏结果
  28. this.isShow = false;
  29. // 获取规则数组[0秒、1分、2时、3日、4月、5星期、6年]
  30. let ruleArr = this.$options.propsData.ex.split(' ');
  31. // 用于记录进入循环的次数
  32. let nums = 0;
  33. // 用于暂时存符号时间规则结果的数组
  34. let resultArr = [];
  35. // 获取当前时间精确至[年、月、日、时、分、秒]
  36. let nTime = new Date();
  37. let nYear = nTime.getFullYear();
  38. let nMonth = nTime.getMonth() + 1;
  39. let nDay = nTime.getDate();
  40. let nHour = nTime.getHours();
  41. let nMin = nTime.getMinutes();
  42. let nSecond = nTime.getSeconds();
  43. // 根据规则获取到近100年可能年数组、月数组等等
  44. this.getSecondArr(ruleArr[0]);
  45. this.getMinArr(ruleArr[1]);
  46. this.getHourArr(ruleArr[2]);
  47. this.getDayArr(ruleArr[3]);
  48. this.getMonthArr(ruleArr[4]);
  49. this.getWeekArr(ruleArr[5]);
  50. this.getYearArr(ruleArr[6], nYear);
  51. // 将获取到的数组赋值-方便使用
  52. let sDate = this.dateArr[0];
  53. let mDate = this.dateArr[1];
  54. let hDate = this.dateArr[2];
  55. let DDate = this.dateArr[3];
  56. let MDate = this.dateArr[4];
  57. let YDate = this.dateArr[5];
  58. // 获取当前时间在数组中的索引
  59. let sIdx = this.getIndex(sDate, nSecond);
  60. let mIdx = this.getIndex(mDate, nMin);
  61. let hIdx = this.getIndex(hDate, nHour);
  62. let DIdx = this.getIndex(DDate, nDay);
  63. let MIdx = this.getIndex(MDate, nMonth);
  64. let YIdx = this.getIndex(YDate, nYear);
  65. // 重置月日时分秒的函数(后面用的比较多)
  66. const resetSecond = function () {
  67. sIdx = 0;
  68. nSecond = sDate[sIdx]
  69. }
  70. const resetMin = function () {
  71. mIdx = 0;
  72. nMin = mDate[mIdx]
  73. resetSecond();
  74. }
  75. const resetHour = function () {
  76. hIdx = 0;
  77. nHour = hDate[hIdx]
  78. resetMin();
  79. }
  80. const resetDay = function () {
  81. DIdx = 0;
  82. nDay = DDate[DIdx]
  83. resetHour();
  84. }
  85. const resetMonth = function () {
  86. MIdx = 0;
  87. nMonth = MDate[MIdx]
  88. resetDay();
  89. }
  90. // 如果当前年份不为数组中当前值
  91. if (nYear !== YDate[YIdx]) {
  92. resetMonth();
  93. }
  94. // 如果当前月份不为数组中当前值
  95. if (nMonth !== MDate[MIdx]) {
  96. resetDay();
  97. }
  98. // 如果当前“日”不为数组中当前值
  99. if (nDay !== DDate[DIdx]) {
  100. resetHour();
  101. }
  102. // 如果当前“时”不为数组中当前值
  103. if (nHour !== hDate[hIdx]) {
  104. resetMin();
  105. }
  106. // 如果当前“分”不为数组中当前值
  107. if (nMin !== mDate[mIdx]) {
  108. resetSecond();
  109. }
  110. // 循环年份数组
  111. goYear: for (let Yi = YIdx; Yi < YDate.length; Yi++) {
  112. let YY = YDate[Yi];
  113. // 如果到达最大值时
  114. if (nMonth > MDate[MDate.length - 1]) {
  115. resetMonth();
  116. continue;
  117. }
  118. // 循环月份数组
  119. goMonth: for (let Mi = MIdx; Mi < MDate.length; Mi++) {
  120. // 赋值、方便后面运算
  121. let MM = MDate[Mi];
  122. MM = MM < 10 ? '0' + MM : MM;
  123. // 如果到达最大值时
  124. if (nDay > DDate[DDate.length - 1]) {
  125. resetDay();
  126. if (Mi == MDate.length - 1) {
  127. resetMonth();
  128. continue goYear;
  129. }
  130. continue;
  131. }
  132. // 循环日期数组
  133. goDay: for (let Di = DIdx; Di < DDate.length; Di++) {
  134. // 赋值、方便后面运算
  135. let DD = DDate[Di];
  136. let thisDD = DD < 10 ? '0' + DD : DD;
  137. // 如果到达最大值时
  138. if (nHour > hDate[hDate.length - 1]) {
  139. resetHour();
  140. if (Di == DDate.length - 1) {
  141. resetDay();
  142. if (Mi == MDate.length - 1) {
  143. resetMonth();
  144. continue goYear;
  145. }
  146. continue goMonth;
  147. }
  148. continue;
  149. }
  150. // 判断日期的合法性,不合法的话也是跳出当前循环
  151. if (this.checkDate(YY + '-' + MM + '-' + thisDD + ' 00:00:00') !== true && this.dayRule !== 'workDay' && this.dayRule !== 'lastWeek' && this.dayRule !== 'lastDay') {
  152. resetDay();
  153. continue goMonth;
  154. }
  155. // 如果日期规则中有值时
  156. if (this.dayRule == 'lastDay') {
  157. // 如果不是合法日期则需要将前将日期调到合法日期即月末最后一天
  158. if (this.checkDate(YY + '-' + MM + '-' + thisDD + ' 00:00:00') !== true) {
  159. while (DD > 0 && this.checkDate(YY + '-' + MM + '-' + thisDD + ' 00:00:00') !== true) {
  160. DD--;
  161. thisDD = DD < 10 ? '0' + DD : DD;
  162. }
  163. }
  164. } else if (this.dayRule == 'workDay') {
  165. // 校验并调整如果是2月30号这种日期传进来时需调整至正常月底
  166. if (this.checkDate(YY + '-' + MM + '-' + thisDD + ' 00:00:00') !== true) {
  167. while (DD > 0 && this.checkDate(YY + '-' + MM + '-' + thisDD + ' 00:00:00') !== true) {
  168. DD--;
  169. thisDD = DD < 10 ? '0' + DD : DD;
  170. }
  171. }
  172. // 获取达到条件的日期是星期X
  173. let thisWeek = this.formatDate(new Date(YY + '-' + MM + '-' + thisDD + ' 00:00:00'), 'week');
  174. // 当星期日时
  175. if (thisWeek == 1) {
  176. // 先找下一个日,并判断是否为月底
  177. DD++;
  178. thisDD = DD < 10 ? '0' + DD : DD;
  179. // 判断下一日已经不是合法日期
  180. if (this.checkDate(YY + '-' + MM + '-' + thisDD + ' 00:00:00') !== true) {
  181. DD -= 3;
  182. }
  183. } else if (thisWeek == 7) {
  184. // 当星期6时只需判断不是1号就可进行操作
  185. if (this.dayRuleSup !== 1) {
  186. DD--;
  187. } else {
  188. DD += 2;
  189. }
  190. }
  191. } else if (this.dayRule == 'weekDay') {
  192. // 如果指定了是星期几
  193. // 获取当前日期是属于星期几
  194. let thisWeek = this.formatDate(new Date(YY + '-' + MM + '-' + DD + ' 00:00:00'), 'week');
  195. // 校验当前星期是否在星期池(dayRuleSup)中
  196. if (this.dayRuleSup.indexOf(thisWeek) < 0) {
  197. // 如果到达最大值时
  198. if (Di == DDate.length - 1) {
  199. resetDay();
  200. if (Mi == MDate.length - 1) {
  201. resetMonth();
  202. continue goYear;
  203. }
  204. continue goMonth;
  205. }
  206. continue;
  207. }
  208. } else if (this.dayRule == 'assWeek') {
  209. // 如果指定了是第几周的星期几
  210. // 获取每月1号是属于星期几
  211. let thisWeek = this.formatDate(new Date(YY + '-' + MM + '-' + DD + ' 00:00:00'), 'week');
  212. if (this.dayRuleSup[1] >= thisWeek) {
  213. DD = (this.dayRuleSup[0] - 1) * 7 + this.dayRuleSup[1] - thisWeek + 1;
  214. } else {
  215. DD = this.dayRuleSup[0] * 7 + this.dayRuleSup[1] - thisWeek + 1;
  216. }
  217. } else if (this.dayRule == 'lastWeek') {
  218. // 如果指定了每月最后一个星期几
  219. // 校验并调整如果是2月30号这种日期传进来时需调整至正常月底
  220. if (this.checkDate(YY + '-' + MM + '-' + thisDD + ' 00:00:00') !== true) {
  221. while (DD > 0 && this.checkDate(YY + '-' + MM + '-' + thisDD + ' 00:00:00') !== true) {
  222. DD--;
  223. thisDD = DD < 10 ? '0' + DD : DD;
  224. }
  225. }
  226. // 获取月末最后一天是星期几
  227. let thisWeek = this.formatDate(new Date(YY + '-' + MM + '-' + thisDD + ' 00:00:00'), 'week');
  228. // 找到要求中最近的那个星期几
  229. if (this.dayRuleSup < thisWeek) {
  230. DD -= thisWeek - this.dayRuleSup;
  231. } else if (this.dayRuleSup > thisWeek) {
  232. DD -= 7 - (this.dayRuleSup - thisWeek)
  233. }
  234. }
  235. // 判断时间值是否小于10置换成“05”这种格式
  236. DD = DD < 10 ? '0' + DD : DD;
  237. // 循环“时”数组
  238. goHour: for (let hi = hIdx; hi < hDate.length; hi++) {
  239. let hh = hDate[hi] < 10 ? '0' + hDate[hi] : hDate[hi]
  240. // 如果到达最大值时
  241. if (nMin > mDate[mDate.length - 1]) {
  242. resetMin();
  243. if (hi == hDate.length - 1) {
  244. resetHour();
  245. if (Di == DDate.length - 1) {
  246. resetDay();
  247. if (Mi == MDate.length - 1) {
  248. resetMonth();
  249. continue goYear;
  250. }
  251. continue goMonth;
  252. }
  253. continue goDay;
  254. }
  255. continue;
  256. }
  257. // 循环"分"数组
  258. goMin: for (let mi = mIdx; mi < mDate.length; mi++) {
  259. let mm = mDate[mi] < 10 ? '0' + mDate[mi] : mDate[mi];
  260. // 如果到达最大值时
  261. if (nSecond > sDate[sDate.length - 1]) {
  262. resetSecond();
  263. if (mi == mDate.length - 1) {
  264. resetMin();
  265. if (hi == hDate.length - 1) {
  266. resetHour();
  267. if (Di == DDate.length - 1) {
  268. resetDay();
  269. if (Mi == MDate.length - 1) {
  270. resetMonth();
  271. continue goYear;
  272. }
  273. continue goMonth;
  274. }
  275. continue goDay;
  276. }
  277. continue goHour;
  278. }
  279. continue;
  280. }
  281. // 循环"秒"数组
  282. goSecond: for (let si = sIdx; si <= sDate.length - 1; si++) {
  283. let ss = sDate[si] < 10 ? '0' + sDate[si] : sDate[si];
  284. // 添加当前时间(时间合法性在日期循环时已经判断)
  285. if (MM !== '00' && DD !== '00') {
  286. resultArr.push(YY + '-' + MM + '-' + DD + ' ' + hh + ':' + mm + ':' + ss)
  287. nums++;
  288. }
  289. // 如果条数满了就退出循环
  290. if (nums == 5) break goYear;
  291. // 如果到达最大值时
  292. if (si == sDate.length - 1) {
  293. resetSecond();
  294. if (mi == mDate.length - 1) {
  295. resetMin();
  296. if (hi == hDate.length - 1) {
  297. resetHour();
  298. if (Di == DDate.length - 1) {
  299. resetDay();
  300. if (Mi == MDate.length - 1) {
  301. resetMonth();
  302. continue goYear;
  303. }
  304. continue goMonth;
  305. }
  306. continue goDay;
  307. }
  308. continue goHour;
  309. }
  310. continue goMin;
  311. }
  312. } //goSecond
  313. } //goMin
  314. }//goHour
  315. }//goDay
  316. }//goMonth
  317. }
  318. // 判断100年内的结果条数
  319. if (resultArr.length == 0) {
  320. this.resultList = ['没有达到条件的结果!'];
  321. } else {
  322. this.resultList = resultArr;
  323. if (resultArr.length !== 5) {
  324. this.resultList.push('最近100年内只有上面' + resultArr.length + '条结果!')
  325. }
  326. }
  327. // 计算完成-显示结果
  328. this.isShow = true;
  329. },
  330. // 用于计算某位数字在数组中的索引
  331. getIndex(arr, value) {
  332. if (value <= arr[0] || value > arr[arr.length - 1]) {
  333. return 0;
  334. } else {
  335. for (let i = 0; i < arr.length - 1; i++) {
  336. if (value > arr[i] && value <= arr[i + 1]) {
  337. return i + 1;
  338. }
  339. }
  340. }
  341. },
  342. // 获取"年"数组
  343. getYearArr(rule, year) {
  344. this.dateArr[5] = this.getOrderArr(year, year + 100);
  345. if (rule !== undefined) {
  346. if (rule.indexOf('-') >= 0) {
  347. this.dateArr[5] = this.getCycleArr(rule, year + 100, false)
  348. } else if (rule.indexOf('/') >= 0) {
  349. this.dateArr[5] = this.getAverageArr(rule, year + 100)
  350. } else if (rule !== '*') {
  351. this.dateArr[5] = this.getAssignArr(rule)
  352. }
  353. }
  354. },
  355. // 获取"月"数组
  356. getMonthArr(rule) {
  357. this.dateArr[4] = this.getOrderArr(1, 12);
  358. if (rule.indexOf('-') >= 0) {
  359. this.dateArr[4] = this.getCycleArr(rule, 12, false)
  360. } else if (rule.indexOf('/') >= 0) {
  361. this.dateArr[4] = this.getAverageArr(rule, 12)
  362. } else if (rule !== '*') {
  363. this.dateArr[4] = this.getAssignArr(rule)
  364. }
  365. },
  366. // 获取"日"数组-主要为日期规则
  367. getWeekArr(rule) {
  368. // 只有当日期规则的两个值均为“”时则表达日期是有选项的
  369. if (this.dayRule == '' && this.dayRuleSup == '') {
  370. if (rule.indexOf('-') >= 0) {
  371. this.dayRule = 'weekDay';
  372. this.dayRuleSup = this.getCycleArr(rule, 7, false)
  373. } else if (rule.indexOf('#') >= 0) {
  374. this.dayRule = 'assWeek';
  375. let matchRule = rule.match(/[0-9]{1}/g);
  376. this.dayRuleSup = [Number(matchRule[1]), Number(matchRule[0])];
  377. this.dateArr[3] = [1];
  378. if (this.dayRuleSup[1] == 7) {
  379. this.dayRuleSup[1] = 0;
  380. }
  381. } else if (rule.indexOf('L') >= 0) {
  382. this.dayRule = 'lastWeek';
  383. this.dayRuleSup = Number(rule.match(/[0-9]{1,2}/g)[0]);
  384. this.dateArr[3] = [31];
  385. if (this.dayRuleSup == 7) {
  386. this.dayRuleSup = 0;
  387. }
  388. } else if (rule !== '*' && rule !== '?') {
  389. this.dayRule = 'weekDay';
  390. this.dayRuleSup = this.getAssignArr(rule)
  391. }
  392. }
  393. },
  394. // 获取"日"数组-少量为日期规则
  395. getDayArr(rule) {
  396. this.dateArr[3] = this.getOrderArr(1, 31);
  397. this.dayRule = '';
  398. this.dayRuleSup = '';
  399. if (rule.indexOf('-') >= 0) {
  400. this.dateArr[3] = this.getCycleArr(rule, 31, false)
  401. this.dayRuleSup = 'null';
  402. } else if (rule.indexOf('/') >= 0) {
  403. this.dateArr[3] = this.getAverageArr(rule, 31)
  404. this.dayRuleSup = 'null';
  405. } else if (rule.indexOf('W') >= 0) {
  406. this.dayRule = 'workDay';
  407. this.dayRuleSup = Number(rule.match(/[0-9]{1,2}/g)[0]);
  408. this.dateArr[3] = [this.dayRuleSup];
  409. } else if (rule.indexOf('L') >= 0) {
  410. this.dayRule = 'lastDay';
  411. this.dayRuleSup = 'null';
  412. this.dateArr[3] = [31];
  413. } else if (rule !== '*' && rule !== '?') {
  414. this.dateArr[3] = this.getAssignArr(rule)
  415. this.dayRuleSup = 'null';
  416. } else if (rule == '*') {
  417. this.dayRuleSup = 'null';
  418. }
  419. },
  420. // 获取"时"数组
  421. getHourArr(rule) {
  422. this.dateArr[2] = this.getOrderArr(0, 23);
  423. if (rule.indexOf('-') >= 0) {
  424. this.dateArr[2] = this.getCycleArr(rule, 24, true)
  425. } else if (rule.indexOf('/') >= 0) {
  426. this.dateArr[2] = this.getAverageArr(rule, 23)
  427. } else if (rule !== '*') {
  428. this.dateArr[2] = this.getAssignArr(rule)
  429. }
  430. },
  431. // 获取"分"数组
  432. getMinArr(rule) {
  433. this.dateArr[1] = this.getOrderArr(0, 59);
  434. if (rule.indexOf('-') >= 0) {
  435. this.dateArr[1] = this.getCycleArr(rule, 60, true)
  436. } else if (rule.indexOf('/') >= 0) {
  437. this.dateArr[1] = this.getAverageArr(rule, 59)
  438. } else if (rule !== '*') {
  439. this.dateArr[1] = this.getAssignArr(rule)
  440. }
  441. },
  442. // 获取"秒"数组
  443. getSecondArr(rule) {
  444. this.dateArr[0] = this.getOrderArr(0, 59);
  445. if (rule.indexOf('-') >= 0) {
  446. this.dateArr[0] = this.getCycleArr(rule, 60, true)
  447. } else if (rule.indexOf('/') >= 0) {
  448. this.dateArr[0] = this.getAverageArr(rule, 59)
  449. } else if (rule !== '*') {
  450. this.dateArr[0] = this.getAssignArr(rule)
  451. }
  452. },
  453. // 根据传进来的min-max返回一个顺序的数组
  454. getOrderArr(min, max) {
  455. let arr = [];
  456. for (let i = min; i <= max; i++) {
  457. arr.push(i);
  458. }
  459. return arr;
  460. },
  461. // 根据规则中指定的零散值返回一个数组
  462. getAssignArr(rule) {
  463. let arr = [];
  464. let assiginArr = rule.split(',');
  465. for (let i = 0; i < assiginArr.length; i++) {
  466. arr[i] = Number(assiginArr[i])
  467. }
  468. arr.sort(this.compare)
  469. return arr;
  470. },
  471. // 根据一定算术规则计算返回一个数组
  472. getAverageArr(rule, limit) {
  473. let arr = [];
  474. let agArr = rule.split('/');
  475. let min = Number(agArr[0]);
  476. let step = Number(agArr[1]);
  477. while (min <= limit) {
  478. arr.push(min);
  479. min += step;
  480. }
  481. return arr;
  482. },
  483. // 根据规则返回一个具有周期性的数组
  484. getCycleArr(rule, limit, status) {
  485. // status--表示是否从0开始(则从1开始)
  486. let arr = [];
  487. let cycleArr = rule.split('-');
  488. let min = Number(cycleArr[0]);
  489. let max = Number(cycleArr[1]);
  490. if (min > max) {
  491. max += limit;
  492. }
  493. for (let i = min; i <= max; i++) {
  494. let add = 0;
  495. if (status == false && i % limit == 0) {
  496. add = limit;
  497. }
  498. arr.push(Math.round(i % limit + add))
  499. }
  500. arr.sort(this.compare)
  501. return arr;
  502. },
  503. // 比较数字大小(用于Array.sort)
  504. compare(value1, value2) {
  505. if (value2 - value1 > 0) {
  506. return -1;
  507. } else {
  508. return 1;
  509. }
  510. },
  511. // 格式化日期格式如:2017-9-19 18:04:33
  512. formatDate(value, type) {
  513. // 计算日期相关值
  514. let time = typeof value == 'number' ? new Date(value) : value;
  515. let Y = time.getFullYear();
  516. let M = time.getMonth() + 1;
  517. let D = time.getDate();
  518. let h = time.getHours();
  519. let m = time.getMinutes();
  520. let s = time.getSeconds();
  521. let week = time.getDay();
  522. // 如果传递了type的话
  523. if (type == undefined) {
  524. return Y + '-' + (M < 10 ? '0' + M : M) + '-' + (D < 10 ? '0' + D : D) + ' ' + (h < 10 ? '0' + h : h) + ':' + (m < 10 ? '0' + m : m) + ':' + (s < 10 ? '0' + s : s);
  525. } else if (type == 'week') {
  526. // 在quartz中 1为星期日
  527. return week + 1;
  528. }
  529. },
  530. // 检查日期是否存在
  531. checkDate(value) {
  532. let time = new Date(value);
  533. let format = this.formatDate(time)
  534. return value === format;
  535. }
  536. },
  537. watch: {
  538. 'ex': 'expressionChange'
  539. },
  540. props: ['ex'],
  541. mounted: function () {
  542. // 初始化 获取一次结果
  543. this.expressionChange();
  544. }
  545. }
  546. </script>