截流自动化的商城平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CrontabValidate.php 1.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. namespace app\admin\validate\system;
  3. use think\Validate;
  4. use Cron\CronExpression;
  5. class CrontabValidate extends Validate
  6. {
  7. protected $rule = [
  8. 'id' => 'require',
  9. 'name' => 'require',
  10. 'type' => 'require|in:1,2',
  11. 'command' => 'require',
  12. 'status' => 'require|in:1,2',
  13. 'expression' => 'expression',
  14. ];
  15. protected $message = [
  16. 'expression.expression'=>'定时任务规则设置错误',
  17. ];
  18. /**
  19. * 添加
  20. */
  21. public function sceneAdd()
  22. {
  23. $this->remove('id');
  24. }
  25. /**
  26. * 命令验证
  27. * @param $password
  28. * @param $other
  29. * @param $data
  30. * @return bool|mixed
  31. */
  32. protected function expression($expression, $other, $data)
  33. {
  34. if ($data['type'] == 2) {
  35. return true;
  36. }
  37. if (empty($expression)) {
  38. return '定时任务的规则不能为空';
  39. }
  40. if (CronExpression::isValidExpression($expression) === false) {
  41. return false;
  42. }
  43. $cron_expression = CronExpression::factory($expression);
  44. try {
  45. $cron_expression->getMultipleRunDates(1);
  46. } catch (Exception $e) {
  47. return false;
  48. }
  49. return true;
  50. }
  51. }