CrontabLogic.php 4.8 KB

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