diff --git a/Server/application/api/controller/MessageController.php b/Server/application/api/controller/MessageController.php index 1edfeb92..b19930ff 100644 --- a/Server/application/api/controller/MessageController.php +++ b/Server/application/api/controller/MessageController.php @@ -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 = [ diff --git a/Server/application/command.php b/Server/application/command.php index 171211bc..bd78dff6 100644 --- a/Server/application/command.php +++ b/Server/application/command.php @@ -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', // 内容采集任务 √ ]; diff --git a/Server/application/command/ContentCollectCommand.php b/Server/application/command/ContentCollectCommand.php new file mode 100644 index 00000000..f7c79f85 --- /dev/null +++ b/Server/application/command/ContentCollectCommand.php @@ -0,0 +1,51 @@ +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'); + } +} \ No newline at end of file diff --git a/Server/application/cunkebao/controller/ContentCollectController.php b/Server/application/cunkebao/controller/ContentCollectController.php new file mode 100644 index 00000000..171c57e1 --- /dev/null +++ b/Server/application/cunkebao/controller/ContentCollectController.php @@ -0,0 +1,30 @@ + 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()]); + } + } +} \ No newline at end of file diff --git a/Server/application/cunkebao/controller/ContentLibraryController.php b/Server/application/cunkebao/controller/ContentLibraryController.php index 449909c8..2dcc291a 100644 --- a/Server/application/cunkebao/controller/ContentLibraryController.php +++ b/Server/application/cunkebao/controller/ContentLibraryController.php @@ -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']) { diff --git a/Server/application/job/ContentCollectJob.php b/Server/application/job/ContentCollectJob.php new file mode 100644 index 00000000..4c4c1ea4 --- /dev/null +++ b/Server/application/job/ContentCollectJob.php @@ -0,0 +1,90 @@ +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; + } + } +} \ No newline at end of file