CrontabLogic.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeadmin快速开发前后端分离管理后台(PHP版)
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
  8. // | github下载:https://github.com/likeshop-github/likeadmin
  9. // | 访问官网:https://www.likeadmin.cn
  10. // | likeadmin团队 版权所有 拥有最终解释权
  11. // +----------------------------------------------------------------------
  12. // | author: likeadminTeam
  13. // +----------------------------------------------------------------------
  14. namespace app\adminapi\logic\crontab;
  15. use app\common\enum\CrontabEnum;
  16. use app\common\logic\BaseLogic;
  17. use app\common\model\Crontab;
  18. use Cron\CronExpression;
  19. /**
  20. * 定时任务逻辑层
  21. * Class CrontabLogic
  22. * @package app\adminapi\logic\crontab
  23. */
  24. class CrontabLogic extends BaseLogic
  25. {
  26. /**
  27. * @notes 添加定时任务
  28. * @param $params
  29. * @return bool
  30. * @author 段誉
  31. * @date 2022/3/29 14:41
  32. */
  33. public static function add($params)
  34. {
  35. try {
  36. $params['remark'] = $params['remark'] ?? '';
  37. $params['params'] = $params['params'] ?? '';
  38. $params['last_time'] = time();
  39. Crontab::create($params);
  40. return true;
  41. } catch (\Exception $e) {
  42. self::setError($e->getMessage());
  43. return false;
  44. }
  45. }
  46. /**
  47. * @notes 查看定时任务详情
  48. * @param $params
  49. * @return array
  50. * @author 段誉
  51. * @date 2022/3/29 14:41
  52. */
  53. public static function detail($params)
  54. {
  55. $field = 'id,name,type,type as type_desc,command,params,status,status as status_desc,expression,remark';
  56. $crontab = Crontab::field($field)->findOrEmpty($params['id']);
  57. if ($crontab->isEmpty()) {
  58. return [];
  59. }
  60. return $crontab->toArray();
  61. }
  62. /**
  63. * @notes 编辑定时任务
  64. * @param $params
  65. * @return bool
  66. * @author 段誉
  67. * @date 2022/3/29 14:42
  68. */
  69. public static function edit($params)
  70. {
  71. try {
  72. $params['remark'] = $params['remark'] ?? '';
  73. $params['params'] = $params['params'] ?? '';
  74. Crontab::update($params);
  75. return true;
  76. } catch (\Exception $e) {
  77. self::setError($e->getMessage());
  78. return false;
  79. }
  80. }
  81. /**
  82. * @notes 删除定时任务
  83. * @param $params
  84. * @return bool
  85. * @author 段誉
  86. * @date 2022/3/29 14:42
  87. */
  88. public static function delete($params)
  89. {
  90. try {
  91. Crontab::destroy($params['id']);
  92. return true;
  93. } catch (\Exception $e) {
  94. self::setError($e->getMessage());
  95. return false;
  96. }
  97. }
  98. /**
  99. * @notes 操作定时任务
  100. * @param $params
  101. * @return bool
  102. * @author 段誉
  103. * @date 2022/3/29 14:42
  104. */
  105. public static function operate($params)
  106. {
  107. try {
  108. $crontab = Crontab::findOrEmpty($params['id']);
  109. if ($crontab->isEmpty()) {
  110. throw new \Exception('定时任务不存在');
  111. }
  112. switch ($params['operate']) {
  113. case 'start';
  114. $crontab->status = CrontabEnum::START;
  115. break;
  116. case 'stop':
  117. $crontab->status = CrontabEnum::STOP;
  118. break;
  119. }
  120. $crontab->save();
  121. return true;
  122. } catch (\Exception $e) {
  123. self::setError($e->getMessage());
  124. return false;
  125. }
  126. }
  127. /**
  128. * @notes 获取规则执行时间
  129. * @param $params
  130. * @return array|string
  131. * @author 段誉
  132. * @date 2022/3/29 14:42
  133. */
  134. public static function expression($params)
  135. {
  136. try {
  137. $cron = new CronExpression($params['expression']);
  138. $result = $cron->getMultipleRunDates(5);
  139. $result = json_decode(json_encode($result), true);
  140. $lists = [];
  141. foreach ($result as $k => $v) {
  142. $lists[$k]['time'] = $k + 1;
  143. $lists[$k]['date'] = str_replace('.000000', '', $v['date']);
  144. }
  145. $lists[] = ['time' => 'x', 'date' => '……'];
  146. return $lists;
  147. } catch (\Exception $e) {
  148. return $e->getMessage();
  149. }
  150. }
  151. }