Răsfoiți Sursa

将timer的基类独立到cores中

萤火科技 1 an în urmă
părinte
comite
98be31a4f3

+ 4 - 5
app/timer/command/Timer.php

@@ -32,8 +32,8 @@ class Timer extends Command
     // 定时器句柄/ID
     protected $timer;
 
-    // 时间间隔 (单位: 秒, 默认5秒)
-    protected $interval = 1;
+    // 时间间隔 (单位: 秒, 默认1秒)
+    protected int $interval = 1;
 
     protected function configure()
     {
@@ -50,7 +50,7 @@ class Timer extends Command
         global $argv;
 
         if ($input->hasOption('i'))
-            $this->interval = floatval($input->getOption('i'));
+            $this->interval = (int)$input->getOption('i');
 
         $argv[1] = $input->getArgument('status') ?: 'start';
         if ($input->hasOption('d')) {
@@ -98,9 +98,8 @@ class Timer extends Command
      * 停止/删除定时器
      * @return bool
      */
-    public function stop()
+    public function stop(): bool
     {
         return \Workerman\Lib\Timer::del($this->timer);
     }
-
 }

+ 0 - 64
app/timer/controller/Controller.php

@@ -1,64 +0,0 @@
-<?php
-// +----------------------------------------------------------------------
-// | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
-// +----------------------------------------------------------------------
-// | Copyright (c) 2017~2025 https://www.yiovo.com All rights reserved.
-// +----------------------------------------------------------------------
-// | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
-// +----------------------------------------------------------------------
-// | Author: 萤火科技 <admin@yiovo.com>
-// +----------------------------------------------------------------------
-declare (strict_types=1);
-
-namespace app\timer\controller;
-
-use think\facade\Cache;
-
-/**
- * 定时任务监听器
- * Class Controller
- * @package app\timer\controller
- */
-class Controller
-{
-    /**
-     * 定时执行任务
-     * @param int $storeId
-     * @param string $key
-     * @param int|null $expire
-     * @param callable $callback
-     * @return mixed
-     */
-    protected function setInterval(int $storeId, string $key, int $expire, callable $callback)
-    {
-        if (!$this->hasTaskId($storeId, $key)) {
-            $this->setTaskId($storeId, $key, $expire);
-            return call_user_func($callback);
-        }
-        return null;
-    }
-
-    /**
-     * 获取任务ID
-     * @param int $storeId
-     * @param string $key
-     * @return bool
-     */
-    protected function hasTaskId(int $storeId, string $key): bool
-    {
-        return Cache::has("Listener:$storeId:$key");
-    }
-
-    /**
-     * 设置任务ID
-     * 用于实现定时任务的间隔时间, 如果任务ID存在并未过期, 则不执行任务
-     * @param int $storeId
-     * @param string $key
-     * @param int $expire 任务ID过期时长(单位:秒)
-     * @return bool
-     */
-    protected function setTaskId(int $storeId, string $key, int $expire = 60): bool
-    {
-        return Cache::set("Listener:$storeId:$key", true, $expire);
-    }
-}

+ 4 - 6
app/timer/controller/Order.php

@@ -12,25 +12,23 @@ declare (strict_types=1);
 
 namespace app\timer\controller;
 
-use app\timer\service\Order as OrderService;
+use cores\BaseTimer;
 use app\timer\model\Setting as SettingModel;
+use app\timer\service\Order as OrderService;
 
 /**
  * 定时任务:商城订单
  * Class Order
  * @package app\timer\controller
  */
-class Order extends Controller
+class Order extends BaseTimer
 {
     // 当前任务唯一标识
-    private string $taskKey = 'Order';
+    protected string $taskKey = 'Order';
 
     // 任务执行间隔时长 (单位:秒)
     protected int $taskExpire = 60 * 30;
 
-    // 当前商城ID
-    private int $storeId;
-
     /**
      * 任务处理
      * @param array $param

+ 3 - 2
app/timer/controller/Store.php

@@ -12,16 +12,17 @@ declare (strict_types=1);
 
 namespace app\timer\controller;
 
+use cores\BaseTimer;
 use think\facade\Event;
-use app\common\service\system\Process as SystemProcessService;
 use app\timer\model\Store as StoreModel;
+use app\common\service\system\Process as SystemProcessService;
 
 /**
  * 商城定时任务
  * Class Store
  * @package app\timer\controller
  */
-class Store extends Controller
+class Store extends BaseTimer
 {
     /**
      * 任务处理

+ 3 - 5
app/timer/controller/UserCoupon.php

@@ -12,6 +12,7 @@ declare (strict_types=1);
 
 namespace app\timer\controller;
 
+use cores\BaseTimer;
 use app\timer\service\UserCoupon as UserCouponService;
 
 /**
@@ -19,17 +20,14 @@ use app\timer\service\UserCoupon as UserCouponService;
  * Class UserCoupon
  * @package app\timer\controller
  */
-class UserCoupon extends Controller
+class UserCoupon extends BaseTimer
 {
     // 当前任务唯一标识
-    private string $taskKey = 'UserCoupon';
+    protected string $taskKey = 'UserCoupon';
 
     // 任务执行间隔时长 (单位:秒)
     protected int $taskExpire = 60 * 30;
 
-    // 当前商城ID
-    private int $storeId;
-
     /**
      * 任务处理
      * @param array $param

+ 3 - 5
app/timer/controller/UserGrade.php

@@ -12,6 +12,7 @@ declare (strict_types=1);
 
 namespace app\timer\controller;
 
+use cores\BaseTimer;
 use app\timer\service\UserGrade as UserGradeService;
 
 /**
@@ -19,17 +20,14 @@ use app\timer\service\UserGrade as UserGradeService;
  * Class UserGrade
  * @package app\timer\controller
  */
-class UserGrade extends Controller
+class UserGrade extends BaseTimer
 {
     // 当前任务唯一标识
-    private string $taskKey = 'UserGrade';
+    protected string $taskKey = 'UserGrade';
 
     // 任务执行间隔时长 (单位:秒)
     protected int $taskExpire = 60 * 30;
 
-    // 当前商城ID
-    private int $storeId;
-
     /**
      * 任务处理
      * @param array $param

+ 88 - 0
cores/BaseTimer.php

@@ -0,0 +1,88 @@
+<?php
+// +----------------------------------------------------------------------
+// | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
+// +----------------------------------------------------------------------
+// | Copyright (c) 2017~2025 https://www.yiovo.com All rights reserved.
+// +----------------------------------------------------------------------
+// | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
+// +----------------------------------------------------------------------
+// | Author: 萤火科技 <admin@yiovo.com>
+// +----------------------------------------------------------------------
+declare (strict_types=1);
+
+namespace cores;
+
+use think\facade\Cache;
+
+/**
+ * 定时任务监听器
+ * Class BaseTimer
+ * @package app\timer\controller
+ */
+class BaseTimer
+{
+    // 当前任务唯一标识
+    protected string $taskKey = 'BaseTimer';
+
+    // 任务执行间隔时长 (单位:秒)
+    protected int $taskExpire = 60 * 30;
+
+    // 当前商城ID
+    protected int $storeId;
+
+    /**
+     * 定时执行任务 (支持自定义时间间隔)
+     * @param int $storeId 商城ID
+     * @param string $key 任务标识
+     * @param int|null $expire 定时间隔 (单位秒)
+     * @param callable $callback 回调方法
+     * @return mixed
+     */
+    protected final function setInterval(int $storeId, string $key, int $expire, callable $callback)
+    {
+        if (!$this->hasTaskId($storeId, $key)) {
+            $this->setTaskId($storeId, $key, $expire);
+            return call_user_func($callback);
+        }
+        return null;
+    }
+
+    /**
+     * 获取任务ID
+     * @param int $storeId 商城ID
+     * @param string $key 任务标识
+     * @return bool
+     */
+    protected final function hasTaskId(int $storeId, string $key): bool
+    {
+        return Cache::has("Listener:$storeId:$key");
+    }
+
+    /**
+     * 设置任务ID
+     * 用于实现定时任务的间隔时间, 如果任务ID存在并未过期, 则不执行任务
+     * @param int $storeId 商城ID
+     * @param string $key 任务标识
+     * @param int $expire 定时间隔 (单位:秒)
+     * @return bool
+     */
+    protected final function setTaskId(int $storeId, string $key, int $expire = 60): bool
+    {
+        return Cache::set("Listener:$storeId:$key", true, $expire);
+    }
+
+    /**
+     * 任务处理 (cores)
+     * @param array $param 参数 (storeId)
+     * @param callable $callback 回调方法
+     * @return mixed|null
+     */
+    protected final function handleTask(array $param, callable $callback)
+    {
+        ['storeId' => $this->storeId] = $param;
+        return $this->setInterval($this->storeId, $this->taskKey, $this->taskExpire, function () use ($callback) {
+            echo $this->taskKey . PHP_EOL;
+            return call_user_func($callback);
+        });
+    }
+}