代码提交
This commit is contained in:
@@ -26,7 +26,8 @@ class MessageController extends BaseController
|
||||
}
|
||||
|
||||
$fromTime = $this->request->param('fromTime', date('Y-m-d 00:00:00', strtotime('-1 days')));
|
||||
$toTime = $this->request->param('toTime', date('Y-m-d 00:00:00'));
|
||||
$toTime = $this->request->param('toTime', date('Y-m-d 23:59:59'));
|
||||
|
||||
|
||||
try {
|
||||
// 构建请求参数
|
||||
@@ -191,8 +192,9 @@ class MessageController extends BaseController
|
||||
}
|
||||
|
||||
$fromTime = $this->request->param('fromTime', date('Y-m-d 00:00:00', strtotime('-1 days')));
|
||||
$toTime = $this->request->param('toTime', date('Y-m-d 00:00:00'));
|
||||
$toTime = $this->request->param('toTime', date('Y-m-d 23:59:59'));
|
||||
|
||||
|
||||
try {
|
||||
// 构建请求参数
|
||||
$params = [
|
||||
|
||||
@@ -25,4 +25,5 @@ return [
|
||||
// 'allotChatroom:run' => 'app\command\AllotChatroomCommand', // 自动分配微信群聊
|
||||
'allotrule:list' => 'app\command\AllotRuleListCommand', // 分配规则列表 √
|
||||
'allotrule:autocreate' => 'app\command\AutoCreateAllotRulesCommand', // 自动创建分配规则 √
|
||||
'content:collect' => 'app\command\ContentCollectCommand', // 内容采集任务 √
|
||||
];
|
||||
|
||||
51
Server/application/command/ContentCollectCommand.php
Normal file
51
Server/application/command/ContentCollectCommand.php
Normal file
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace app\command;
|
||||
|
||||
use think\console\Command;
|
||||
use think\console\Input;
|
||||
use think\console\Output;
|
||||
use think\facade\Log;
|
||||
use think\Queue;
|
||||
use app\job\ContentCollectJob;
|
||||
|
||||
class ContentCollectCommand extends Command
|
||||
{
|
||||
protected function configure()
|
||||
{
|
||||
$this->setName('content:collect')
|
||||
->setDescription('执行内容采集任务');
|
||||
}
|
||||
|
||||
protected function execute(Input $input, Output $output)
|
||||
{
|
||||
$output->writeln('开始处理内容采集任务...');
|
||||
|
||||
try {
|
||||
// 将任务添加到队列
|
||||
$this->addToQueue();
|
||||
|
||||
$output->writeln('内容采集任务已添加到队列');
|
||||
} catch (\Exception $e) {
|
||||
Log::error('内容采集任务添加失败:' . $e->getMessage());
|
||||
$output->writeln('内容采集任务添加失败:' . $e->getMessage());
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加任务到队列
|
||||
*/
|
||||
protected function addToQueue()
|
||||
{
|
||||
$data = [
|
||||
'libraryId' => 0, // 0表示采集所有内容库
|
||||
'timestamp' => time()
|
||||
];
|
||||
|
||||
// 添加到队列,设置任务名为 content_collect
|
||||
Queue::push(ContentCollectJob::class, $data, 'content_collect');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace app\cunkebao\controller;
|
||||
|
||||
use think\Controller;
|
||||
use think\Queue;
|
||||
use app\job\ContentCollectJob;
|
||||
|
||||
class ContentCollectController extends Controller
|
||||
{
|
||||
/**
|
||||
* 添加内容采集任务到队列
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function addCollectTask()
|
||||
{
|
||||
try {
|
||||
$data = [
|
||||
'libraryId' => input('libraryId/d', 0), // 0表示采集所有内容库
|
||||
'timestamp' => time()
|
||||
];
|
||||
|
||||
Queue::push(ContentCollectJob::class, $data, 'content_collect');
|
||||
|
||||
return json(['code' => 200, 'msg' => '采集任务已加入队列']);
|
||||
} catch (\Exception $e) {
|
||||
return json(['code' => 500, 'msg' => '添加采集任务失败:' . $e->getMessage()]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -471,7 +471,6 @@ class ContentLibraryController extends Controller
|
||||
*/
|
||||
public function collectMoments()
|
||||
{
|
||||
|
||||
// 查询条件:未删除且已开启的内容库
|
||||
$where = [
|
||||
['isDel', '=', 0], // 未删除
|
||||
@@ -484,7 +483,6 @@ class ContentLibraryController extends Controller
|
||||
->order('id', 'desc')
|
||||
->select()->toArray();
|
||||
|
||||
|
||||
if (empty($libraries)) {
|
||||
return json(['code' => 200, 'msg' => '没有可用的内容库配置']);
|
||||
}
|
||||
@@ -503,7 +501,6 @@ class ContentLibraryController extends Controller
|
||||
$library['keywordExclude'] = json_decode($library['keywordExclude'] ?: '[]', true);
|
||||
$library['groupMembers'] = json_decode($library['groupMembers'] ?: '[]', true);
|
||||
|
||||
|
||||
// 根据数据来源类型执行不同的采集逻辑
|
||||
$collectResult = [];
|
||||
switch ($library['sourceType']) {
|
||||
|
||||
90
Server/application/job/ContentCollectJob.php
Normal file
90
Server/application/job/ContentCollectJob.php
Normal file
@@ -0,0 +1,90 @@
|
||||
<?php
|
||||
|
||||
namespace app\job;
|
||||
|
||||
use think\queue\Job;
|
||||
use think\facade\Log;
|
||||
use think\Queue;
|
||||
use think\facade\Config;
|
||||
use app\cunkebao\controller\ContentLibraryController;
|
||||
|
||||
class ContentCollectJob
|
||||
{
|
||||
/**
|
||||
* 队列任务处理
|
||||
* @param Job $job 队列任务
|
||||
* @param array $data 任务数据
|
||||
* @return void
|
||||
*/
|
||||
public function fire(Job $job, $data)
|
||||
{
|
||||
try {
|
||||
// 如果任务执行成功后删除任务
|
||||
if ($this->processContentCollect($data, $job->attempts())) {
|
||||
$job->delete();
|
||||
Log::info('内容采集任务执行成功,内容库ID:' . $data['libraryId']);
|
||||
} else {
|
||||
if ($job->attempts() > 3) {
|
||||
// 超过重试次数,删除任务
|
||||
Log::error('内容采集任务执行失败,已超过重试次数,内容库ID:' . $data['libraryId']);
|
||||
$job->delete();
|
||||
} else {
|
||||
// 任务失败,重新放回队列
|
||||
Log::warning('内容采集任务执行失败,重试次数:' . $job->attempts() . ',内容库ID:' . $data['libraryId']);
|
||||
$job->release(Config::get('queue.failed_delay', 10));
|
||||
}
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
// 出现异常,记录日志
|
||||
Log::error('内容采集任务异常:' . $e->getMessage());
|
||||
if ($job->attempts() > 3) {
|
||||
$job->delete();
|
||||
} else {
|
||||
$job->release(Config::get('queue.failed_delay', 10));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理内容采集
|
||||
* @param array $data 任务数据
|
||||
* @param int $attempts 重试次数
|
||||
* @return bool
|
||||
*/
|
||||
protected function processContentCollect($data, $attempts)
|
||||
{
|
||||
try {
|
||||
$controller = new ContentLibraryController();
|
||||
$result = $controller->collectMoments();
|
||||
$response = json_decode($result, true);
|
||||
|
||||
if ($response['code'] == 200) {
|
||||
// 记录详细的采集结果
|
||||
if (!empty($response['data'])) {
|
||||
foreach ($response['data'] as $result) {
|
||||
if ($result['status'] == 'success') {
|
||||
Log::info(sprintf(
|
||||
'内容库[%s]采集成功: %s',
|
||||
$result['library_name'],
|
||||
$result['message']
|
||||
));
|
||||
} else {
|
||||
Log::warning(sprintf(
|
||||
'内容库[%s]采集失败: %s',
|
||||
$result['library_name'],
|
||||
$result['message']
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
} else {
|
||||
Log::error('内容采集失败:' . ($response['msg'] ?? '未知错误'));
|
||||
return false;
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
Log::error('内容采集处理异常:' . $e->getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user