Explorar o código

修改系统定时任务类名 crontab,容易识别,不和系统接口重名

sugar1569 %!s(int64=2) %!d(string=hai) anos
pai
achega
cfcead9b4c

+ 1 - 1
crmeb/app/event.php

@@ -38,7 +38,7 @@ return [
         'user.userVisit' => [\app\listener\user\UserVisit::class], //用户访问事件
         'notice.notice' => [\app\listener\notice\Notice::class], //通知->消息事件
         'pay.notify' => [\app\listener\pay\Notify::class],//支付异步回调
-        'SystemTimer' => [\app\listener\timer\SystemTimer::class],//定时任务事件
+        'Crontab' => [\app\listener\crontab\SystemCrontab::class],//定时任务事件
     ],
 ];
 

+ 214 - 0
crmeb/app/listener/crontab/SystemCrontab.php

@@ -0,0 +1,214 @@
+<?php
+
+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\timer\SystemTimerServices;
+use crmeb\interfaces\ListenerInterface;
+use think\facade\Log;
+use Workerman\Crontab\Crontab;
+
+/**
+ * 系统定时任务
+ */
+class SystemCrontab implements ListenerInterface
+{
+    public function handle($event): void
+    {
+        $time = time();
+        $date = date('Y-m-d H:i:s', time());
+        $timer_log_open = config("log.timer_log", false);
+
+        new Crontab('*/6 * * * * *', function () use ($time) {
+            file_put_contents(root_path() . 'runtime/.timer', $time);
+        });
+
+        /** @var SystemTimerServices $systemTimerServices */
+        $systemTimerServices = app()->make(SystemTimerServices::class);
+        $list = $systemTimerServices->selectList(['is_del' => 0, 'is_open' => 1])->toArray();
+        foreach ($list as &$item) {
+            //获取定时任务时间字符串
+            $timeStr = $this->getTimerStr($item);
+            //未支付自动取消订单
+            if ($item['mark'] == 'order_cancel') {
+                new Crontab($timeStr, function () use ($date, $timer_log_open) {
+                    try {
+                        /** @var StoreOrderServices $orderServices */
+                        $orderServices = app()->make(StoreOrderServices::class);
+                        $orderServices->orderUnpaidCancel();
+                        $this->crontabLog(' 执行未支付自动取消订单');
+                    } catch (\Throwable $e) {
+                        Log::error('自动取消订单失败,失败原因:' . $e->getMessage());
+                    }
+                });
+            }
+            //拼团到期订单处理
+            if ($item['mark'] == 'pink_expiration') {
+                new Crontab($timeStr, function () use ($date, $timer_log_open) {
+                    try {
+                        /** @var StorePinkServices $storePinkServices */
+                        $storePinkServices = app()->make(StorePinkServices::class);
+                        $storePinkServices->statusPink();
+                        $this->crontabLog(' 执行拼团到期订单处理');
+                    } catch (\Throwable $e) {
+                        Log::error('拼团到期订单处理失败,失败原因:' . $e->getMessage());
+                    }
+                });
+            }
+            //自动解绑上级绑定
+            if ($item['mark'] == 'agent_unbind') {
+                new Crontab($timeStr, function () use ($date, $timer_log_open) {
+                    try {
+                        /** @var AgentManageServices $agentManage */
+                        $agentManage = app()->make(AgentManageServices::class);
+                        $agentManage->removeSpread();
+                        $this->crontabLog(' 执行自动解绑上级绑定');
+                    } catch (\Throwable $e) {
+                        Log::error('自动解除上级绑定失败,失败原因:' . $e->getMessage());
+                    }
+                });
+            }
+            //更新直播商品状态
+            if ($item['mark'] == 'live_product_status') {
+                new Crontab($timeStr, function () use ($date, $timer_log_open) {
+                    try {
+                        /** @var LiveGoodsServices $liveGoods */
+                        $liveGoods = app()->make(LiveGoodsServices::class);
+                        $liveGoods->syncGoodStatus();
+                        $this->crontabLog(' 执行更新直播商品状态');
+                    } catch (\Throwable $e) {
+                        Log::error('更新直播商品状态失败,失败原因:' . $e->getMessage());
+                    }
+                });
+            }
+            //更新直播间状态
+            if ($item['mark'] == 'live_room_status') {
+                new Crontab($timeStr, function () use ($date, $timer_log_open) {
+                    try {
+                        /** @var LiveRoomServices $liveRoom */
+                        $liveRoom = app()->make(LiveRoomServices::class);
+                        $liveRoom->syncRoomStatus();
+                        $this->crontabLog(' 执行更新直播间状态');
+
+                    } catch (\Throwable $e) {
+                        Log::error('更新直播间状态失败,失败原因:' . $e->getMessage());
+                    }
+                });
+            }
+            //自动收货
+            if ($item['mark'] == 'take_delivery') {
+                new Crontab($timeStr, function () use ($date, $timer_log_open) {
+                    try {
+                        /** @var StoreOrderTakeServices $services */
+                        $services = app()->make(StoreOrderTakeServices::class);
+                        $services->autoTakeOrder();
+                        $this->crontabLog(' 执行自动收货');
+                    } catch (\Throwable $e) {
+                        Log::error('自动收货失败,失败原因:' . $e->getMessage());
+                    }
+                });
+            }
+            //查询预售到期商品自动下架
+            if ($item['mark'] == 'advance_off') {
+                new Crontab($timeStr, function () use ($date, $timer_log_open) {
+                    try {
+                        /** @var StoreProductServices $product */
+                        $product = app()->make(StoreProductServices::class);
+                        $product->downAdvance();
+                        $this->crontabLog(' 执行预售到期商品自动下架');
+                    } catch (\Throwable $e) {
+                        Log::error('预售到期商品自动下架失败,失败原因:' . $e->getMessage());
+                    }
+                });
+            }
+            //自动好评
+            if ($item['mark'] == 'product_replay') {
+                new Crontab($timeStr, function () use ($date, $timer_log_open) {
+                    try {
+                        /** @var StoreOrderServices $orderServices */
+                        $orderServices = app()->make(StoreOrderServices::class);
+                        $orderServices->autoComment();
+                        $this->crontabLog(' 执行自动好评');
+                    } catch (\Throwable $e) {
+                        Log::error('自动好评失败,失败原因:' . $e->getMessage());
+                    }
+                });
+            }
+            //清除昨日海报
+            if ($item['mark'] == 'clear_poster') {
+                new Crontab($timeStr, function () use ($date, $timer_log_open) {
+                    try {
+                        /** @var SystemAttachmentServices $attach */
+                        $attach = app()->make(SystemAttachmentServices::class);
+                        $attach->emptyYesterdayAttachment();
+                        $this->crontabLog(' 执行清除昨日海报');
+                    } catch (\Throwable $e) {
+                        Log::error('清除昨日海报失败,失败原因:' . $e->getMessage());
+                    }
+                });
+            }
+        }
+    }
+
+    /** 定时任务日志
+     * @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::notice($date . ' 执行清除昨日海报');
+        }
+    }
+    /**
+     *  0   1   2   3   4   5
+     * |   |   |   |   |   |
+     * |   |   |   |   |   +------ day of week (0 - 6) (Sunday=0)
+     * |   |   |   |   +------ month (1 - 12)
+     * |   |   |   +-------- day of month (1 - 31)
+     * |   |   +---------- hour (0 - 23)
+     * |   +------------ min (0 - 59)
+     * +-------------- sec (0-59)[可省略,如果没有0位,则最小时间粒度是分钟]
+     */
+    /**
+     * 获取定时任务时间表达式
+     * @param $data
+     * @return string
+     */
+    public function getTimerStr($data): string
+    {
+        $timeStr = '';
+        switch ($data['type']) {
+            case 1:// 每隔几秒
+                $timeStr = '*/' . $data['second'] . ' * * * * *';
+                break;
+            case 2:// 每隔几分
+                $timeStr = '0 */' . $data['minute'] . ' * * * *';
+                break;
+            case 3:// 每隔几时第几分钟执行
+                $timeStr = '0 ' . $data['minute'] . ' */' . $data['hour'] . ' * * *';
+                break;
+            case 4:// 每隔几日第几小时第几分钟执行
+                $timeStr = '0 ' . $data['minute'] . ' ' . $data['hour'] . ' */' . $data['day'] . ' * *';
+                break;
+            case 5:// 每日几时几分几秒
+                $timeStr = $data['second'] . ' ' . $data['minute'] . ' ' . $data['hour'] . ' * * *';
+                break;
+            case 6:// 每周周几几时几分几秒
+                $timeStr = $data['second'] . ' ' . $data['minute'] . ' ' . $data['hour'] . ' * * ' . ($data['week'] == 7 ? 0 : $data['week']);
+                break;
+            case 7:// 每月几日几时几分几秒
+                $timeStr = $data['second'] . ' ' . $data['minute'] . ' ' . $data['hour'] . ' ' . $data['day'] . ' * *';
+                break;
+        }
+        return $timeStr;
+    }
+}

+ 1 - 1
crmeb/crmeb/command/Timer.php

@@ -62,7 +62,7 @@ class Timer extends Command
         date_default_timezone_set('PRC');
         $task->count = 1;
         $task->onWorkerStart = function () {
-            event('SystemTimer');
+            event('Crontab');
         };
         $task->runAll();
     }