CrontabValidate.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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\validate\crontab;
  20. use app\common\validate\BaseValidate;
  21. use Cron\CronExpression;
  22. /**
  23. * 定时任务验证器
  24. * Class CrontabValidate
  25. * @package app\adminapi\validate\crontab
  26. */
  27. class CrontabValidate extends BaseValidate
  28. {
  29. protected $rule = [
  30. 'name' => 'require',
  31. 'type' => 'require|in:1',
  32. 'command' => 'require',
  33. 'status' => 'require|in:1,2,3',
  34. 'expression' => 'require|checkExpression',
  35. 'id' => 'require',
  36. 'operate' => 'require'
  37. ];
  38. protected $message = [
  39. 'name.require' => '请输入定时任务名称',
  40. 'type.require' => '请选择类型',
  41. 'type.in' => '类型值错误',
  42. 'command.require' => '请输入命令',
  43. 'status.require' => '请选择状态',
  44. 'status.in' => '状态值错误',
  45. 'expression.require' => '请输入运行规则',
  46. 'id.require' => '参数缺失',
  47. 'operate.require' => '请选择操作',
  48. ];
  49. /**
  50. * @notes 添加定时任务场景
  51. * @return CrontabValidate
  52. * @author Tab
  53. * @date 2021/8/17 11:18
  54. */
  55. public function sceneAdd()
  56. {
  57. return $this->remove('id', 'require')->remove('operate', 'require');
  58. }
  59. /**
  60. * @notes 查看定时任务详情场景
  61. * @return CrontabValidate
  62. * @author Tab
  63. * @date 2021/8/17 11:55
  64. */
  65. public function sceneDetail()
  66. {
  67. return $this->only(['id']);
  68. }
  69. /**
  70. * @notes 编辑定时任务
  71. * @return CrontabValidate
  72. * @author Tab
  73. * @date 2021/9/15 15:53
  74. */
  75. public function sceneEdit()
  76. {
  77. return $this->remove('operate', 'require');
  78. }
  79. /**
  80. * @notes 删除定时任务场景
  81. * @return CrontabValidate
  82. * @author Tab
  83. * @date 2021/8/17 14:09
  84. */
  85. public function sceneDelete()
  86. {
  87. return $this->only(['id']);
  88. }
  89. /**
  90. * @notes
  91. * @return CrontabValidate
  92. * @author Tab
  93. * @date 2021/8/17 15:06
  94. */
  95. public function sceneOperate()
  96. {
  97. return $this->only(['id', 'operate']);
  98. }
  99. /**
  100. * @notes 获取规则执行时间场景
  101. * @return CrontabValidate
  102. * @author Tab
  103. * @date 2021/8/17 15:36
  104. */
  105. public function sceneExpression()
  106. {
  107. return $this->only(['expression']);
  108. }
  109. /**
  110. * @notes 校验运行规则
  111. * @param $value
  112. * @param $rule
  113. * @param $data
  114. * @return bool|string
  115. * @author Tab
  116. * @date 2021/8/17 11:42
  117. */
  118. public function checkExpression($value, $rule, $data)
  119. {
  120. if(CronExpression::isValidExpression($value) === false) {
  121. return '定时任务运行规则错误';
  122. }
  123. return true;
  124. }
  125. }