代码优化
This commit is contained in:
57
Server/application/command/DepartmentListCommand.php
Normal file
57
Server/application/command/DepartmentListCommand.php
Normal file
@@ -0,0 +1,57 @@
|
||||
<?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\DepartmentListJob;
|
||||
|
||||
class DepartmentListCommand extends Command
|
||||
{
|
||||
protected function configure()
|
||||
{
|
||||
$this->setName('department:list')
|
||||
->setDescription('获取部门列表,并根据分页自动处理下一页');
|
||||
}
|
||||
|
||||
protected function execute(Input $input, Output $output)
|
||||
{
|
||||
$output->writeln('开始处理部门列表任务...');
|
||||
|
||||
try {
|
||||
// 初始页码
|
||||
$pageIndex = 0;
|
||||
$pageSize = 100; // 每页获取100条记录
|
||||
|
||||
// 将第一页任务添加到队列
|
||||
$this->addToQueue($pageIndex, $pageSize);
|
||||
|
||||
$output->writeln('部门列表任务已添加到队列');
|
||||
} catch (\Exception $e) {
|
||||
Log::error('部门列表任务添加失败:' . $e->getMessage());
|
||||
$output->writeln('部门列表任务添加失败:' . $e->getMessage());
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加任务到队列
|
||||
* @param int $pageIndex 页码
|
||||
* @param int $pageSize 每页大小
|
||||
*/
|
||||
protected function addToQueue($pageIndex, $pageSize)
|
||||
{
|
||||
$data = [
|
||||
'pageIndex' => $pageIndex,
|
||||
'pageSize' => $pageSize
|
||||
];
|
||||
|
||||
// 添加到队列,设置任务名为 account_list
|
||||
Queue::push(DepartmentListJob::class, $data, 'department_list');
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,7 @@ use think\console\Output;
|
||||
use think\facade\Log;
|
||||
use think\Queue;
|
||||
use app\job\FriendTaskJob;
|
||||
use think\facade\Cache;
|
||||
|
||||
class FriendTaskCommand extends Command
|
||||
{
|
||||
@@ -22,11 +23,13 @@ class FriendTaskCommand extends Command
|
||||
$output->writeln('开始处理添加好友任务...');
|
||||
|
||||
try {
|
||||
// 初始页码
|
||||
$pageIndex = 0;
|
||||
$pageSize = 100; // 每页获取100条记录
|
||||
// 从缓存获取初始页码,缓存10分钟有效
|
||||
$pageIndex = Cache::get('friendTaskPage', 21);
|
||||
$output->writeln('从缓存获取页码:' . $pageIndex);
|
||||
|
||||
// 将第一页任务添加到队列
|
||||
$pageSize = 1000; // 每页获取1000条记录
|
||||
|
||||
// 将任务添加到队列
|
||||
$this->addToQueue($pageIndex, $pageSize);
|
||||
|
||||
$output->writeln('添加好友任务已添加到队列');
|
||||
|
||||
@@ -8,6 +8,7 @@ use think\console\Output;
|
||||
use think\facade\Log;
|
||||
use think\Queue;
|
||||
use app\job\WechatChatroomJob;
|
||||
use think\facade\Cache;
|
||||
|
||||
class WechatChatroomCommand extends Command
|
||||
{
|
||||
@@ -22,11 +23,13 @@ class WechatChatroomCommand extends Command
|
||||
$output->writeln('开始处理微信聊天室列表任务...');
|
||||
|
||||
try {
|
||||
// 初始页码
|
||||
$pageIndex = 0;
|
||||
// 从缓存获取初始页码,缓存10分钟有效
|
||||
$pageIndex = Cache::get('chatroomPage', 0);
|
||||
$output->writeln('从缓存获取页码:' . $pageIndex);
|
||||
|
||||
$pageSize = 100; // 每页获取100条记录
|
||||
|
||||
// 将第一页任务添加到队列
|
||||
// 将任务添加到队列
|
||||
$this->addToQueue($pageIndex, $pageSize);
|
||||
|
||||
$output->writeln('微信聊天室列表任务已添加到队列');
|
||||
@@ -51,7 +54,7 @@ class WechatChatroomCommand extends Command
|
||||
'pageSize' => $pageSize
|
||||
];
|
||||
|
||||
// 添加到队列,设置任务名为 device_list
|
||||
// 添加到队列,设置任务名为 wechat_chatroom
|
||||
Queue::push(WechatChatroomJob::class, $data, 'wechat_chatroom');
|
||||
}
|
||||
}
|
||||
@@ -24,8 +24,8 @@ class WechatFriendCommand extends Command
|
||||
|
||||
try {
|
||||
// 从缓存获取初始页码和上次处理的好友ID,缓存10分钟有效
|
||||
$pageIndex = Cache::get('friendsPage', 0);
|
||||
$preFriendId = Cache::get('preFriendId', '');
|
||||
$pageIndex = Cache::get('friendsPage', 21);
|
||||
$preFriendId = Cache::get('preFriendId', 19426090);
|
||||
|
||||
$output->writeln('从缓存获取页码:' . $pageIndex . ',上次处理的好友ID:' . ($preFriendId ?: '无'));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user