Files
wh-api/app/Service/QueueService.php
ykxiao 8a3ab17b25
Some checks failed
Build Docker / build (push) Has been cancelled
增加基础配置
2025-07-12 11:59:33 +08:00

97 lines
2.2 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* Author: ykxiao
* Date: 2025/6/3
* Time: 下午10:53
* Description:
*
* (c) ykxiao <yk_9001@hotmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
declare(strict_types=1);
namespace App\Service;
use App\Context\ApiUrlContext;
use App\Context\UserContext;
use App\Job\ColumnConfigJob;
use App\Job\OpLogsJob;
use App\Job\RequestWriteLogsJob;
use Hyperf\Amqp\Producer;
use Hyperf\AsyncQueue\Driver\DriverFactory;
use Hyperf\AsyncQueue\Driver\DriverInterface;
use Hyperf\Di\Annotation\Inject;
class QueueService
{
// 存储配置参数
protected array $params = [];
// 当前操作的函数名
protected string $function = '';
// 队列驱动实例
protected DriverInterface $driver;
// 生产者实例,用于发送消息
#[Inject]
protected Producer $producer;
/**
* 构造函数,初始化队列服务。
*
* @param DriverFactory $driverFactory 驱动工厂,用于获取具体的队列驱动实例
*/
public function __construct(DriverFactory $driverFactory)
{
$this->driver = $driverFactory->get('whf');
}
/**
* 创建队列任务并执行。
*
* @param array $params
* @return QueueService
*/
public function make(array $params): static
{
// 将当前用户信息添加到参数中
$params['user'] = UserContext::getCurrentUser();
if (ApiUrlContext::hasApiUrl()) {
$params['api_url'] = ApiUrlContext::getApiUrl();
}
$this->params = $params;
return $this;
}
/**
* 将请求日志写入文件。
*
*/
public function writeRequestLogs(): void
{
$this->driver->push(new RequestWriteLogsJob($this->params));
}
/**
* 列配置保存.
*/
public function saveColumnConfig(): void
{
$this->driver->push(new ColumnConfigJob($this->params));
}
/**
* 将操作日志推送到队列中.
*/
public function opLogs(): void
{
// 将操作日志封装为OpLogsJob任务并推送到驱动器对应的队列中
$this->driver->push(new OpLogsJob($this->params));
}
}