sugangqiang 2 жил өмнө
parent
commit
4d41fe9fcf

+ 30 - 0
app/command/RegisterMessenger.php

@@ -0,0 +1,30 @@
+<?php
+
+declare (strict_types=1);
+
+namespace app\command;
+
+use think\console\Command;
+use think\console\Input;
+use think\console\input\Argument;
+use think\console\input\Option;
+use think\console\Output;
+use think\facade\Db;
+
+/**
+ * 注册短信提醒后台审核
+ */
+class RegisterMessenger extends Command {
+
+    protected function configure() {
+        // 指令配置
+        $this->setName('RegisterMessenger')
+                ->setDescription('Auto send msg');
+    }
+
+    protected function execute(Input $input, Output $output) {
+        //短信加入队列
+        queue("app\job\Messenger", ["type" => "msg", "msg" => 777, "status" => 1]);
+    }
+
+}

+ 5 - 6
app/command/Solver.php

@@ -10,6 +10,7 @@ use think\console\input\Argument;
 use think\console\input\Option;
 use think\console\Output;
 use think\facade\Db;
+use app\common\api\XwCrontab;
 
 /**
  * 主要处理短信的命令行程序
@@ -28,11 +29,13 @@ class Solver extends Command {
         $lockFile = fopen($lockFileName, "a");
         if (flock($lockFile, LOCK_EX | LOCK_NB)) {//文件锁(独占)
             set_time_limit(0);
+            $root = app()->getRootPath();
             $schedules = Db::table("new_schedule")->select();
             foreach ($schedules as $schedule) {
                 //检查时间到了没
-                if ($this->checkTime($schedule["time"])) {
-                    queue("app\job\Messenger", []); //任务加入队列待执行
+                if(XwCrontab::check(time(), $schedule["timeStr"])){
+                    exec("php {$root}think " . $schedule["action"]);
+                    //queue("app\job\Messenger", []); //任务加入队列待执行
                 }
             }
             flock($lockFile, LOCK_UN);
@@ -41,8 +44,4 @@ class Solver extends Command {
         }
     }
 
-    private function checkTime() {
-        return false;
-    }
-
 }

+ 135 - 0
app/common/api/XwCrontab.php

@@ -0,0 +1,135 @@
+<?php
+
+namespace app\common\api;
+
+class XwCrontab {
+
+    /**
+     * 检查某时间($time)是否符合某个corntab时间计划($str_cron)
+     *
+     * @param int    $time     时间戳
+     * @param string $str_cron corntab的时间计划,如,"30 2 * * 1-5"
+     *
+     * @return bool/string 出错返回string(错误信息)
+     */
+    static public function check($time, $str_cron) {
+        //完整的crontab表达式,去掉秒后再来判断
+        $tmp = explode(" ", $str_cron);
+        array_shift($tmp);
+        $format_time = self::format_timestamp($time);
+        $format_cron = self::format_crontab(implode(" ", $tmp));
+        if (!is_array($format_cron)) {
+            return $format_cron;
+        }
+        return self::format_check($format_time, $format_cron);
+    }
+
+    /**
+     * 使用格式化的数据检查某时间($format_time)是否符合某个corntab时间计划($format_cron)
+     *
+     * @param array $format_time self::format_timestamp()格式化时间戳得到
+     * @param array $format_cron self::format_crontab()格式化的时间计划
+     *
+     * @return bool
+     */
+    static public function format_check(array $format_time, array $format_cron) {
+        return (!$format_cron[0] || in_array($format_time[0], $format_cron[0])) && (!$format_cron[1] || in_array($format_time[1], $format_cron[1])) && (!$format_cron[2] || in_array($format_time[2], $format_cron[2])) && (!$format_cron[3] || in_array($format_time[3], $format_cron[3])) && (!$format_cron[4] || in_array($format_time[4], $format_cron[4]))
+        ;
+    }
+
+    /**
+     * 格式化时间戳,以便比较
+     *
+     * @param int $time 时间戳
+     *
+     * @return array
+     */
+    static public function format_timestamp($time) {
+        return explode('-', date('i-G-j-n-w', $time));
+    }
+
+    /**
+     * 格式化crontab时间设置字符串,用于比较
+     *
+     * @param string $str_cron crontab的时间计划字符串,如"15 3 * * *"
+     *
+     * @return array/string 正确返回数组,出错返回字符串(错误信息)
+     */
+    static public function format_crontab($str_cron) {
+        //格式检查
+        $str_cron = trim($str_cron);
+        $reg = '#^((\*(/\d+)?|((\d+(-\d+)?)(?3)?)(,(?4))*))( (?2)){4}$#';
+        if (!preg_match($reg, $str_cron)) {
+            return '格式错误';
+        }
+
+        try {
+            //分别解析分、时、日、月、周
+            $arr_cron = array();
+            $parts = explode(' ', $str_cron);
+            $arr_cron[0] = self::parse_cron_part($parts[0], 0, 59); //分
+            $arr_cron[1] = self::parse_cron_part($parts[1], 0, 23); //时
+            $arr_cron[2] = self::parse_cron_part($parts[2], 1, 31); //日
+            $arr_cron[3] = self::parse_cron_part($parts[3], 1, 12); //月
+            $arr_cron[4] = self::parse_cron_part($parts[4], 0, 6); //周(0周日)
+        } catch (Exception $e) {
+            return $e->getMessage();
+        }
+
+        return $arr_cron;
+    }
+
+    /**
+     * 解析crontab时间计划里一个部分(分、时、日、月、周)的取值列表
+     * @param string $part  时间计划里的一个部分,被空格分隔后的一个部分
+     * @param int    $f_min 此部分的最小取值
+     * @param int    $f_max 此部分的最大取值
+     *
+     * @return array 若为空数组则表示可任意取值
+     * @throws Exception
+     */
+    static protected function parse_cron_part($part, $f_min, $f_max) {
+        $list = array();
+
+        //处理"," -- 列表
+        if (false !== strpos($part, ',')) {
+            $arr = explode(',', $part);
+            foreach ($arr as $v) {
+                $tmp = self::parse_cron_part($v, $f_min, $f_max);
+                $list = array_merge($list, $tmp);
+            }
+            return $list;
+        }
+
+        //处理"/" -- 间隔
+        $tmp = explode('/', $part);
+        $part = $tmp[0];
+        $step = isset($tmp[1]) ? $tmp[1] : 1;
+
+        //处理"-" -- 范围
+        if (false !== strpos($part, '-')) {
+            list($min, $max) = explode('-', $part);
+            if ($min > $max) {
+                throw new Exception('使用"-"设置范围时,左不能大于右');
+            }
+        } elseif ('*' == $part) {
+            $min = $f_min;
+            $max = $f_max;
+        } else {//数字
+            $min = $max = $part;
+        }
+
+        //空数组表示可以任意值
+        if ($min == $f_min && $max == $f_max && $step == 1) {
+            return $list;
+        }
+
+        //越界判断
+        if ($min < $f_min || $max > $f_max) {
+            throw new Exception('数值越界。应该:分0-59,时0-59,日1-31,月1-12,周0-6');
+        }
+
+        return $max - $min > $step ? range($min, $max, $step) : array((int) $min);
+    }
+
+}

+ 2 - 1
config/console.php

@@ -6,6 +6,7 @@
 return [
     // 指令定义
     'commands' => [
-        "Solver" => "app\command\Solver"
+        "Solver" => "app\command\Solver",
+        "RegisterMessenger" => "app\command\RegisterMessenger", //注册短信提醒后台审核
     ],
 ];