吴昊天 2 лет назад
Родитель
Сommit
06e9f13d84

+ 10 - 127
crmeb/app/listener/crontab/SystemCrontabListener.php

@@ -2,17 +2,10 @@
 
 namespace app\listener\crontab;
 
-use app\services\activity\combination\StorePinkServices;
-use app\services\activity\live\LiveGoodsServices;
-use app\services\activity\live\LiveRoomServices;
-use app\services\agent\AgentManageServices;
-use app\services\order\StoreOrderServices;
-use app\services\order\StoreOrderTakeServices;
-use app\services\product\product\StoreProductServices;
-use app\services\system\attachment\SystemAttachmentServices;
+use app\services\system\crontab\CrontabRunServices;
 use app\services\system\crontab\SystemCrontabServices;
 use crmeb\interfaces\ListenerInterface;
-use think\facade\Log;
+use think\helper\Str;
 use Workerman\Crontab\Crontab;
 
 /**
@@ -22,133 +15,23 @@ class SystemCrontabListener implements ListenerInterface
 {
     public function handle($event): void
     {
+        $systemCrontabServices = app()->make(SystemCrontabServices::class);
+        $crontabRunServices = app()->make(CrontabRunServices::class);
+
         //自动写入文件方便检测是否启动定时任务命令
         new Crontab('*/6 * * * * *', function () {
             file_put_contents(root_path() . 'runtime/.timer', time());
         });
 
-        /** @var SystemCrontabServices $systemTimerServices */
-        $systemCrontabServices = app()->make(SystemCrontabServices::class);
         $list = $systemCrontabServices->selectList(['is_del' => 0, 'is_open' => 1])->toArray();
         foreach ($list as &$item) {
+            //转化小驼峰
+            $functionName = Str::camel($item['mark']);
             //获取定时任务时间字符串
             $timeStr = $this->getTimerStr($item);
-            //未支付自动取消订单
-            if ($item['mark'] == 'order_cancel') {
-                new Crontab($timeStr, function () {
-                    try {
-                        app()->make(StoreOrderServices::class)->orderUnpaidCancel();
-                        $this->crontabLog(' 执行未支付自动取消订单');
-                    } catch (\Throwable $e) {
-                        Log::error('自动取消订单失败,失败原因:' . $e->getMessage());
-                    }
-                });
-            }
-            //拼团到期订单处理
-            elseif ($item['mark'] == 'pink_expiration') {
-                new Crontab($timeStr, function () {
-                    try {
-                        app()->make(StorePinkServices::class)->statusPink();
-                        $this->crontabLog(' 执行拼团到期订单处理');
-                    } catch (\Throwable $e) {
-                        Log::error('拼团到期订单处理失败,失败原因:' . $e->getMessage());
-                    }
-                });
-            }
-            //自动解绑上级绑定
-            elseif ($item['mark'] == 'agent_unbind') {
-                new Crontab($timeStr, function () {
-                    try {
-                        app()->make(AgentManageServices::class)->removeSpread();
-                        $this->crontabLog(' 执行自动解绑上级绑定');
-                    } catch (\Throwable $e) {
-                        Log::error('自动解除上级绑定失败,失败原因:' . $e->getMessage());
-                    }
-                });
-            }
-            //更新直播商品状态
-            elseif ($item['mark'] == 'live_product_status') {
-                new Crontab($timeStr, function () {
-                    try {
-                        app()->make(LiveGoodsServices::class)->syncGoodStatus();
-                        $this->crontabLog(' 执行更新直播商品状态');
-                    } catch (\Throwable $e) {
-                        Log::error('更新直播商品状态失败,失败原因:' . $e->getMessage());
-                    }
-                });
-            }
-            //更新直播间状态
-            elseif ($item['mark'] == 'live_room_status') {
-                new Crontab($timeStr, function () {
-                    try {
-                        app()->make(LiveRoomServices::class)->syncRoomStatus();
-                        $this->crontabLog(' 执行更新直播间状态');
-                    } catch (\Throwable $e) {
-                        Log::error('更新直播间状态失败,失败原因:' . $e->getMessage());
-                    }
-                });
-            }
-            //自动收货
-            elseif ($item['mark'] == 'take_delivery') {
-                new Crontab($timeStr, function () {
-                    try {
-                        app()->make(StoreOrderTakeServices::class)->autoTakeOrder();
-                        $this->crontabLog(' 执行自动收货');
-                    } catch (\Throwable $e) {
-                        Log::error('自动收货失败,失败原因:' . $e->getMessage());
-                    }
-                });
-            }
-            //查询预售到期商品自动下架
-            elseif ($item['mark'] == 'advance_off') {
-                new Crontab($timeStr, function () {
-                    try {
-                        app()->make(StoreProductServices::class)->downAdvance();
-                        $this->crontabLog(' 执行预售到期商品自动下架');
-                    } catch (\Throwable $e) {
-                        Log::error('预售到期商品自动下架失败,失败原因:' . $e->getMessage());
-                    }
-                });
-            }
-            //自动好评
-            elseif ($item['mark'] == 'product_replay') {
-                new Crontab($timeStr, function () {
-                    try {
-                        app()->make(StoreOrderServices::class)->autoComment();
-                        $this->crontabLog(' 执行自动好评');
-                    } catch (\Throwable $e) {
-                        Log::error('自动好评失败,失败原因:' . $e->getMessage());
-                    }
-                });
-            }
-            //清除昨日海报
-            elseif ($item['mark'] == 'clear_poster') {
-                new Crontab($timeStr, function () {
-                    try {
-                        app()->make(SystemAttachmentServices::class)->emptyYesterdayAttachment();
-                        $this->crontabLog(' 执行清除昨日海报');
-                    } catch (\Throwable $e) {
-                        Log::error('清除昨日海报失败,失败原因:' . $e->getMessage());
-                    }
-                });
-            }
-            //
-            else {
-
-            }
-        }
-    }
-
-    /**
-     * 定时任务日志
-     * @param $msg
-     */
-    public function crontabLog($msg)
-    {
-        $timer_log_open = config("log.timer_log", false);
-        if ($timer_log_open){
-            $date = date('Y-m-d H:i:s', time());
-            Log::write($date . $msg, 'crontab');
+            new Crontab($timeStr, function () use ($crontabRunServices, $functionName) {
+                $crontabRunServices->$functionName();
+            });
         }
     }
 

+ 194 - 0
crmeb/app/services/system/crontab/CrontabRunServices.php

@@ -0,0 +1,194 @@
+<?php
+
+namespace app\services\system\crontab;
+
+use app\services\activity\combination\StorePinkServices;
+use app\services\activity\live\LiveGoodsServices;
+use app\services\activity\live\LiveRoomServices;
+use app\services\agent\AgentManageServices;
+use app\services\BaseServices;
+use app\services\order\StoreOrderServices;
+use app\services\order\StoreOrderTakeServices;
+use app\services\product\product\StoreProductServices;
+use app\services\system\attachment\SystemAttachmentServices;
+use think\facade\Log;
+
+/**
+ * 执行定时任务
+ * @author 吴汐
+ * @email 442384644@qq.com
+ * @date 2023/03/01
+ */
+class CrontabRunServices extends BaseServices
+{
+    /**
+     * 调用不存在的方法
+     * @param $name
+     * @param $arguments
+     * @return mixed|void
+     * @author 吴汐
+     * @email 442384644@qq.com
+     * @date 2023/03/01
+     */
+    public function __call($name, $arguments)
+    {
+        $this->crontabLog($name . '方法不存在');
+    }
+
+    /**
+     * 定时任务日志
+     * @param $msg
+     */
+    public function crontabLog($msg)
+    {
+        $timer_log_open = config("log.timer_log", false);
+        if ($timer_log_open) {
+            $date = date('Y-m-d H:i:s', time());
+            Log::write($date . $msg, 'crontab');
+        }
+    }
+
+    /**
+     * 未支付自动取消订单
+     * @author 吴汐
+     * @email 442384644@qq.com
+     * @date 2023/03/01
+     */
+    public function orderCancel()
+    {
+        try {
+            app()->make(StoreOrderServices::class)->orderUnpaidCancel();
+            $this->crontabLog(' 执行未支付自动取消订单');
+        } catch (\Throwable $e) {
+            $this->crontabLog('自动取消订单失败,失败原因:' . $e->getMessage());
+        }
+    }
+
+    /**
+     * 拼团到期订单处理
+     * @author 吴汐
+     * @email 442384644@qq.com
+     * @date 2023/03/01
+     */
+    public function pinkExpiration()
+    {
+        try {
+            app()->make(StorePinkServices::class)->statusPink();
+            $this->crontabLog(' 执行拼团到期订单处理');
+        } catch (\Throwable $e) {
+            $this->crontabLog('拼团到期订单处理失败,失败原因:' . $e->getMessage());
+        }
+    }
+
+    /**
+     * 自动解除上级绑定
+     * @author 吴汐
+     * @email 442384644@qq.com
+     * @date 2023/03/01
+     */
+    public function agentUnbind()
+    {
+        try {
+            app()->make(AgentManageServices::class)->removeSpread();
+            $this->crontabLog(' 执行自动解绑上级绑定');
+        } catch (\Throwable $e) {
+            $this->crontabLog('自动解除上级绑定失败,失败原因:' . $e->getMessage());
+        }
+    }
+
+    /**
+     * 更新直播商品状态
+     * @author 吴汐
+     * @email 442384644@qq.com
+     * @date 2023/03/01
+     */
+    public function liveProductStatus()
+    {
+        try {
+            app()->make(LiveGoodsServices::class)->syncGoodStatus();
+            $this->crontabLog(' 执行更新直播商品状态');
+        } catch (\Throwable $e) {
+            $this->crontabLog('更新直播商品状态失败,失败原因:' . $e->getMessage());
+        }
+    }
+
+    /**
+     * 更新直播间状态
+     * @author 吴汐
+     * @email 442384644@qq.com
+     * @date 2023/03/01
+     */
+    public function liveRoomStatus()
+    {
+        try {
+            app()->make(LiveRoomServices::class)->syncRoomStatus();
+            $this->crontabLog(' 执行更新直播间状态');
+        } catch (\Throwable $e) {
+            $this->crontabLog('更新直播间状态失败,失败原因:' . $e->getMessage());
+        }
+    }
+
+    /**
+     * 自动收货
+     * @author 吴汐
+     * @email 442384644@qq.com
+     * @date 2023/03/01
+     */
+    public function takeDelivery()
+    {
+        try {
+            app()->make(StoreOrderTakeServices::class)->autoTakeOrder();
+            $this->crontabLog(' 执行自动收货');
+        } catch (\Throwable $e) {
+            $this->crontabLog('自动收货失败,失败原因:' . $e->getMessage());
+        }
+    }
+
+    /**
+     * 预售到期商品自动下架
+     * @author 吴汐
+     * @email 442384644@qq.com
+     * @date 2023/03/01
+     */
+    public function advanceOff()
+    {
+        try {
+            app()->make(StoreProductServices::class)->downAdvance();
+            $this->crontabLog(' 执行预售到期商品自动下架');
+        } catch (\Throwable $e) {
+            $this->crontabLog('预售到期商品自动下架失败,失败原因:' . $e->getMessage());
+        }
+    }
+
+    /**
+     * 自动好评
+     * @author 吴汐
+     * @email 442384644@qq.com
+     * @date 2023/03/01
+     */
+    public function productReplay()
+    {
+        try {
+            app()->make(StoreOrderServices::class)->autoComment();
+            $this->crontabLog(' 执行自动好评');
+        } catch (\Throwable $e) {
+            $this->crontabLog('自动好评失败,失败原因:' . $e->getMessage());
+        }
+    }
+
+    /**
+     * 清除昨日海报
+     * @author 吴汐
+     * @email 442384644@qq.com
+     * @date 2023/03/01
+     */
+    public function clearPoster()
+    {
+        try {
+            app()->make(SystemAttachmentServices::class)->emptyYesterdayAttachment();
+            $this->crontabLog(' 执行清除昨日海报');
+        } catch (\Throwable $e) {
+            $this->crontabLog('清除昨日海报失败,失败原因:' . $e->getMessage());
+        }
+    }
+}

+ 6 - 53
crmeb/app/services/system/crontab/SystemCrontabServices.php

@@ -14,6 +14,7 @@ use app\services\product\product\StoreProductServices;
 use app\services\system\attachment\SystemAttachmentServices;
 use crmeb\exceptions\AdminException;
 use think\facade\Log;
+use think\helper\Str;
 
 class SystemCrontabServices extends BaseServices
 {
@@ -205,67 +206,19 @@ class SystemCrontabServices extends BaseServices
      */
     public function crontabRun()
     {
+        $crontabRunServices = app()->make(CrontabRunServices::class);
         $time = time();
         file_put_contents(root_path() . 'runtime/.timer', $time); //检测定时任务是否正常
         $list = $this->dao->selectList(['is_open' => 1, 'is_del' => 0])->toArray();
         foreach ($list as $item) {
             if ($item['next_execution_time'] < $time) {
-                if ($item['mark'] == 'order_cancel') {
-                    //未支付自动取消订单
-                    app()->make(StoreOrderServices::class)->orderUnpaidCancel();
-                    $this->crontabLog(' 执行未支付自动取消订单');
-                } elseif ($item['mark'] == 'pink_expiration') {
-                    //拼团到期订单处理
-                    app()->make(StorePinkServices::class)->statusPink();
-                    $this->crontabLog(' 执行拼团到期订单处理');
-                } elseif ($item['mark'] == 'agent_unbind') {
-                    //自动解绑上级绑定
-                    app()->make(AgentManageServices::class)->removeSpread();
-                    $this->crontabLog(' 执行自动解绑上级绑定');
-                } elseif ($item['mark'] == 'live_product_status') {
-                    //更新直播商品状态
-                    app()->make(LiveGoodsServices::class)->syncGoodStatus();
-                    $this->crontabLog(' 执行更新直播商品状态');
-                } elseif ($item['mark'] == 'live_room_status') {
-                    //更新直播间状态
-                    app()->make(LiveRoomServices::class)->syncRoomStatus();
-                    $this->crontabLog(' 执行更新直播间状态');
-                } elseif ($item['mark'] == 'take_delivery') {
-                    //自动收货
-                    app()->make(StoreOrderTakeServices::class)->autoTakeOrder();
-                    $this->crontabLog(' 执行自动收货');
-                } elseif ($item['mark'] == 'advance_off') {
-                    //查询预售到期商品自动下架
-                    app()->make(StoreProductServices::class)->downAdvance();
-                    $this->crontabLog(' 执行预售到期商品自动下架');
-                } elseif ($item['mark'] == 'product_replay') {
-                    //自动好评
-                    app()->make(StoreOrderServices::class)->autoComment();
-                    $this->crontabLog(' 执行自动好评');
-                } elseif ($item['mark'] == 'clear_poster') {
-                    //清除昨日海报
-                    app()->make(SystemAttachmentServices::class)->emptyYesterdayAttachment();
-                    $this->crontabLog(' 执行清除昨日海报');
-                }
+                //转化小驼峰方法名
+                $functionName = Str::camel($item['mark']);
+                //执行定时任务
+                $crontabRunServices->$functionName();
                 //写入本次执行时间和下次执行时间
                 $this->dao->update(['mark' => $item['mark']], ['last_execution_time' => $time, 'next_execution_time' => $this->getTimerCycleTime($item)]);
             }
         }
     }
-
-    /**
-     * 定时任务日志
-     * @param $msg
-     * @author 吴汐
-     * @email 442384644@qq.com
-     * @date 2023/02/21
-     */
-    public function crontabLog($msg)
-    {
-        $timer_log_open = config("log.timer_log", false);
-        if ($timer_log_open) {
-            $date = date('Y-m-d H:i:s', time());
-            Log::write($date . $msg, 'crontab');
-        }
-    }
 }