Kirin 3 주 전
부모
커밋
7649683078
3개의 변경된 파일118개의 추가작업 그리고 4개의 파일을 삭제
  1. 6 4
      app/event.php
  2. 78 0
      app/listener/system/timer/SystemTimer.php
  3. 34 0
      qiniu/listeners/SwooleCronListen.php

+ 6 - 4
app/event.php

@@ -15,9 +15,9 @@ return [
             \qiniu\listeners\InitSwooleLockListen::class, //初始化
         ],
         //swoole 启动事件
-//        'swoole.workerStart' => [
-//            \crmeb\listeners\SwooleCronListen::class, //定时任务
-//        ],
+        'swoole.workerStart' => [
+            \qiniu\listeners\SwooleCronListen::class, //定时任务
+        ],
         'swoole.workerExit' => [],
         'swoole.workerError' => [],
         'swoole.workerStop' => [],
@@ -25,11 +25,13 @@ return [
         'swoole.websocket.user' => [\app\webscoket\handler\UserHandler::class],//socket 用户调用事件
 //        'swoole.websocket.kefu' => [\app\webscoket\handler\KefuHandler::class],//socket 客服事件
 //        'swoole.websocket.admin' => [\app\webscoket\handler\AdminHandler::class],//socket 后台事件
+
+        'crontab' => [\app\listener\system\timer\SystemTimer::class],
+
         'config.create' => [\app\listener\system\config\CreateSuccess::class],//创建配置事件
         'config.delete' => [\app\listener\system\config\DeleteSuccess::class],//删除配置事件
         'config.update' => [\app\listener\system\config\StatusSuccess::class],//删除配置事件
         'user.cancelUser' => [\app\listener\user\CancelUser::class],
-        'decoration.cases_audit' => [\app\listener\decoration\CasesAudit::class],
 
     ],
 

+ 78 - 0
app/listener/system/timer/SystemTimer.php

@@ -0,0 +1,78 @@
+<?php
+// +----------------------------------------------------------------------
+// | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
+// +----------------------------------------------------------------------
+// | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
+// +----------------------------------------------------------------------
+// | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
+// +----------------------------------------------------------------------
+// | Author: CRMEB Team <admin@crmeb.com>
+// +----------------------------------------------------------------------
+namespace app\listener\system\timer;
+
+use qiniu\services\blockchain\TransactionService;
+use qiniu\services\CacheService;
+use qiniu\utils\Cron;
+use qiniu\interfaces\ListenerInterface;
+use think\facade\Log;
+
+/**
+ * 定时任务
+ * Class Create
+ * @package app\listener\system\timer
+ */
+class SystemTimer extends Cron implements ListenerInterface
+{
+    public $tasks = [
+        'auto' => [
+            'is_open' => 1,
+            'time' => 30,
+        ]
+    ];
+
+    /**
+     * @param $event
+     */
+    public function handle($event): void
+    {
+        $this->tick(1000, function () {
+            $time = time();
+            $list = $this->tasks;
+            /** @var CacheService $cacheService */
+            $cacheService = app()->make(CacheService::class);
+            foreach ($list as $index => $item) {
+                if ($item['is_open'] == 1) {
+                    $data = $cacheService->get($index);
+                    if ($time >= $data + $item['time']) {
+                        $this->after(1000, function () use ($index, $time, $cacheService) {
+                            $cacheService->set($index, $time);//上次执行时间保存
+                            $this->implement_timer($index);
+                        });
+                    }
+                }
+            }
+        });
+    }
+
+    /**
+     * 执行定时任务
+     * @param string $mark
+     * @return bool|void
+     */
+    public function implement_timer(string $mark)
+    {
+        switch ($mark) {
+            case 'auto': //自动取消订单
+                try {
+                    Log::debug('auto');
+                } catch (\Throwable $e) {
+                    response_log_write([
+                        'message' => '失败原因:[' . class_basename($this) . ']' . $e->getMessage(),
+                        'file' => $e->getFile(),
+                        'line' => $e->getLine()
+                    ]);
+                }
+                break;
+        }
+    }
+}

+ 34 - 0
qiniu/listeners/SwooleCronListen.php

@@ -0,0 +1,34 @@
+<?php
+
+// +----------------------------------------------------------------------
+// | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
+// +----------------------------------------------------------------------
+// | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
+// +----------------------------------------------------------------------
+// | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
+// +----------------------------------------------------------------------
+// | Author: CRMEB Team <admin@crmeb.com>
+// +----------------------------------------------------------------------
+
+
+namespace qiniu\listeners;
+
+use qiniu\interfaces\ListenerInterface;
+use think\facade\Log;
+
+/**
+ * swoole 定时任务
+ */
+class SwooleCronListen implements ListenerInterface
+{
+
+    public function handle($event): void
+    {
+        try {
+            event('crontab');//app/event.php 里面配置事件
+        } catch (\Throwable $e) {
+            Log::error('监听定时器报错: ' . $e->getMessage());
+        }
+
+    }
+}