CrontabValidate.php 3.5 KB

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