|
@@ -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;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|