From 2df22cdecd29ccb6a4b22b61bc8a934dd29afdc3 Mon Sep 17 00:00:00 2001 From: Ghost <106998207@qq.com> Date: Mon, 24 Mar 2025 16:42:36 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9A=E6=97=B6=E4=BB=BB=E5=8A=A1=E6=8F=90?= =?UTF-8?q?=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Server/README_wechat_chatroom_sync.md | 105 +++++++++++++++ .../api/controller/AccountController.php | 2 +- .../api/controller/BaseController.php | 2 - .../api/controller/DeviceController.php | 2 +- .../api/controller/FriendTaskController.php | 2 +- .../api/controller/UserController.php | 2 +- .../api/controller/WebSocketController.php | 43 +++--- .../controller/WechatChatroomController.php | 33 ++++- .../api/controller/WechatController.php | 2 +- .../api/controller/WechatFriendController.php | 2 +- .../model/CompanyAccountModel.php | 2 +- .../{common => api}/model/DeviceModel.php | 2 +- .../{common => api}/model/FriendTaskModel.php | 2 +- .../model/WechatAccountModel.php | 2 +- .../model/WechatChatroomMemberModel.php | 2 +- .../model/WechatChatroomModel.php | 2 +- .../api/model/WechatFriendModel.php | 10 ++ Server/application/command.php | 9 +- .../command/AccountListCommand.php | 57 ++++++++ .../command/DeviceListCommand.php | 4 +- .../application/command/FriendTaskCommand.php | 57 ++++++++ .../command/WechatChatroomCommand.php | 57 ++++++++ .../command/WechatFriendCommand.php | 57 ++++++++ .../application/command/WechatListCommand.php | 57 ++++++++ Server/application/common.php | 4 +- .../common/command/BaseCommand.php | 31 ----- .../common/command/TestCommand.php | 20 --- Server/application/job/AccountListJob.php | 123 ++++++++++++++++++ Server/application/job/FriendTaskJob.php | 123 ++++++++++++++++++ Server/application/job/WechatChatroomJob.php | 123 ++++++++++++++++++ Server/application/job/WechatFriendJob.php | 123 ++++++++++++++++++ Server/application/job/WechatListJob.php | 123 ++++++++++++++++++ 32 files changed, 1091 insertions(+), 94 deletions(-) create mode 100644 Server/README_wechat_chatroom_sync.md rename Server/application/{common => api}/model/CompanyAccountModel.php (72%) rename Server/application/{common => api}/model/DeviceModel.php (68%) rename Server/application/{common => api}/model/FriendTaskModel.php (71%) rename Server/application/{common => api}/model/WechatAccountModel.php (71%) rename Server/application/{common => api}/model/WechatChatroomMemberModel.php (73%) rename Server/application/{common => api}/model/WechatChatroomModel.php (71%) create mode 100644 Server/application/api/model/WechatFriendModel.php create mode 100644 Server/application/command/AccountListCommand.php rename Server/application/{common => }/command/DeviceListCommand.php (95%) create mode 100644 Server/application/command/FriendTaskCommand.php create mode 100644 Server/application/command/WechatChatroomCommand.php create mode 100644 Server/application/command/WechatFriendCommand.php create mode 100644 Server/application/command/WechatListCommand.php delete mode 100644 Server/application/common/command/BaseCommand.php delete mode 100644 Server/application/common/command/TestCommand.php create mode 100644 Server/application/job/AccountListJob.php create mode 100644 Server/application/job/FriendTaskJob.php create mode 100644 Server/application/job/WechatChatroomJob.php create mode 100644 Server/application/job/WechatFriendJob.php create mode 100644 Server/application/job/WechatListJob.php diff --git a/Server/README_wechat_chatroom_sync.md b/Server/README_wechat_chatroom_sync.md new file mode 100644 index 00000000..fb2e1868 --- /dev/null +++ b/Server/README_wechat_chatroom_sync.md @@ -0,0 +1,105 @@ +# 微信群聊同步功能 + +本功能用于自动同步微信群聊数据,支持分页获取群聊列表以及群成员信息,并将数据保存到数据库中。 + +## 功能特点 + +1. 支持分页获取微信群聊列表 +2. 自动获取每个群聊的成员信息 +3. 支持通过关键词筛选群聊 +4. 支持按微信账号筛选群聊 +5. 可选择是否包含已删除的群聊 +6. 使用队列处理,支持大量数据的同步 +7. 支持失败重试机制 +8. 提供命令行和HTTP接口两种触发方式 + +## 数据表结构 + +本功能使用以下数据表: + +1. **wechat_chatroom** - 存储微信群聊信息 +2. **wechat_chatroom_member** - 存储微信群聊成员信息 + +## 使用方法 + +### 1. HTTP接口触发 + +``` +GET/POST /api/wechat_chatroom/syncChatrooms +``` + +**参数:** +- `pageIndex`: 起始页码,默认0 +- `pageSize`: 每页大小,默认100 +- `keyword`: 群名关键词,可选 +- `wechatAccountKeyword`: 微信账号关键词,可选 +- `isDeleted`: 是否包含已删除群聊,可选 + +**示例:** +``` +/api/wechat_chatroom/syncChatrooms?pageSize=50 +``` + +### 2. 命令行触发 + +```bash +php think sync:wechat:chatrooms [选项] +``` + +**选项:** +- `-p, --pageIndex`: 起始页码,默认0 +- `-s, --pageSize`: 每页大小,默认100 +- `-k, --keyword`: 群名关键词,可选 +- `-a, --account`: 微信账号关键词,可选 +- `-d, --deleted`: 是否包含已删除群聊,可选 + +**示例:** +```bash +# 基本用法 +php think sync:wechat:chatrooms + +# 指定页大小和关键词 +php think sync:wechat:chatrooms -s 50 -k "测试群" + +# 指定账号关键词 +php think sync:wechat:chatrooms --account "张三" +``` + +### 3. 定时任务配置 + +可以将命令添加到系统的定时任务(crontab)中,实现定期自动同步: + +``` +# 每天凌晨3点执行微信群聊同步 +0 3 * * * cd /path/to/your/project && php think sync:wechat:chatrooms +``` + +## 队列消费者配置 + +为了处理同步任务,需要启动队列消费者: + +```bash +# 启动微信群聊队列消费者 +php think queue:work --queue wechat_chatrooms +``` + +建议在生产环境中使用supervisor等工具来管理队列消费者进程。 + +## 同步过程 + +1. 触发同步任务,将初始页任务加入队列 +2. 队列消费者处理任务,获取当前页的群聊列表 +3. 如果当前页有数据且数量等于页大小,则将下一页任务加入队列 +4. 对每个获取到的群聊,添加获取群成员的任务 +5. 所有数据会自动保存到数据库中 + +## 调试与日志 + +同步过程的日志会记录在应用的日志目录中,可以通过查看日志了解同步状态和错误信息。 + +## 注意事项 + +1. 页大小建议设置为合理值(50-100),过大会导致请求超时 +2. 当数据量较大时,建议增加队列消费者的数量 +3. 确保系统授权信息正确,否则无法获取数据 +4. 数据同步是增量的,会自动更新已存在的记录 \ No newline at end of file diff --git a/Server/application/api/controller/AccountController.php b/Server/application/api/controller/AccountController.php index ddd24509..087694b0 100644 --- a/Server/application/api/controller/AccountController.php +++ b/Server/application/api/controller/AccountController.php @@ -2,7 +2,7 @@ namespace app\api\controller; -use app\common\model\CompanyAccountModel; +use app\api\model\CompanyAccountModel; use think\facade\Request; class AccountController extends BaseController diff --git a/Server/application/api/controller/BaseController.php b/Server/application/api/controller/BaseController.php index f3aac3bd..92383990 100644 --- a/Server/application/api/controller/BaseController.php +++ b/Server/application/api/controller/BaseController.php @@ -2,8 +2,6 @@ namespace app\api\controller; -use app\common\model\UserModel; -use app\common\model\UserTokenModel; use think\Controller; use think\facade\Env; diff --git a/Server/application/api/controller/DeviceController.php b/Server/application/api/controller/DeviceController.php index 9570fcbc..19dbb3b2 100644 --- a/Server/application/api/controller/DeviceController.php +++ b/Server/application/api/controller/DeviceController.php @@ -2,7 +2,7 @@ namespace app\api\controller; -use app\common\model\DeviceModel; +use app\api\model\DeviceModel; use think\facade\Request; use think\facade\Env; use Endroid\QrCode\QrCode; diff --git a/Server/application/api/controller/FriendTaskController.php b/Server/application/api/controller/FriendTaskController.php index e11e2ed3..0ca80ee9 100644 --- a/Server/application/api/controller/FriendTaskController.php +++ b/Server/application/api/controller/FriendTaskController.php @@ -2,7 +2,7 @@ namespace app\api\controller; -use app\common\model\FriendTaskModel; +use app\api\model\FriendTaskModel; use think\facade\Request; class FriendTaskController extends BaseController diff --git a/Server/application/api/controller/UserController.php b/Server/application/api/controller/UserController.php index 2be2fa3e..5fc6b159 100644 --- a/Server/application/api/controller/UserController.php +++ b/Server/application/api/controller/UserController.php @@ -2,7 +2,7 @@ namespace app\api\controller; -use app\common\model\CompanyAccountModel; +use app\api\model\CompanyAccountModel; use think\facade\Env; use think\Response; diff --git a/Server/application/api/controller/WebSocketController.php b/Server/application/api/controller/WebSocketController.php index 12eada22..f04a43b6 100644 --- a/Server/application/api/controller/WebSocketController.php +++ b/Server/application/api/controller/WebSocketController.php @@ -100,12 +100,13 @@ class WebSocketController extends BaseController $result = json_encode($result); $this->client->send($result); $message = $this->client->receive(); + $message = json_decode($message, 1); //关闭WS链接 $this->client->close(); - Log::write('WS个人消息发送'); - $this->success('消息成功发送', json_decode($message, 1), 200); + //Log::write('WS个人消息发送'); + successJson($message, '消息成功发送'); } else { - $this->error('非法请求'); + errorJson('非法请求'); } } @@ -160,17 +161,16 @@ class WebSocketController extends BaseController $message = $this->client->receive(); //关闭WS链接 $this->client->close(); - Log::write('WS群消息发送'); - Log::write($message); + //Log::write('WS群消息发送'); + //Log::write($message); $message = json_decode($message, 1); } catch (\Exception $e) { $msg = $e->getMessage(); } - - $this->success($msg, $message, 200); + successJson($message,$msg); } else { - $this->error('非法请求'); + errorJson('非法请求'); } } @@ -224,15 +224,14 @@ class WebSocketController extends BaseController $message = $this->client->receive(); //关闭WS链接 $this->client->close(); - Log::write('WS群消息发送'); - Log::write($message); + //Log::write('WS群消息发送'); + //Log::write($message); $message = json_decode($message, 1); } catch (\Exception $e) { $msg = $e->getMessage(); } - $this->success($msg, $message, 200); - + successJson($message,$msg); } @@ -274,10 +273,10 @@ class WebSocketController extends BaseController "seq" => time(), ]; $params = json_encode($params); - Log::write('WS获取朋友圈信息参数:' . json_encode($params, 256)); + //Log::write('WS获取朋友圈信息参数:' . json_encode($params, 256)); $this->client->send($params); $message = $this->client->receive(); - Log::write('WS获取朋友圈信息成功,结果:' . $message); + //Log::write('WS获取朋友圈信息成功,结果:' . $message); $message = json_decode($message, 1); // 存储朋友圈数据到数据库 @@ -290,9 +289,10 @@ class WebSocketController extends BaseController } catch (\Exception $e) { $msg = $e->getMessage(); } - $this->success($msg, $message, 200); + + successJson($message,$msg); } else { - $this->error('非法请求'); + errorJson('非法请求'); } } @@ -348,10 +348,10 @@ class WebSocketController extends BaseController } } - Log::write('朋友圈数据已存入数据库,共' . count($momentList) . '条'); + //Log::write('朋友圈数据已存入数据库,共' . count($momentList) . '条'); return true; } catch (\Exception $e) { - Log::write('保存朋友圈数据失败:' . $e->getMessage(), 'error'); + //Log::write('保存朋友圈数据失败:' . $e->getMessage(), 'error'); return false; } } @@ -396,15 +396,16 @@ class WebSocketController extends BaseController $params = json_encode($params); $this->client->send($params); $message = $this->client->receive(); - Log::write('WS获取朋友圈图片/视频链接成功,结果:' . json_encode($message, 256)); + //Log::write('WS获取朋友圈图片/视频链接成功,结果:' . json_encode($message, 256)); //关闭WS链接 $this->client->close(); } catch (\Exception $e) { $msg = $e->getMessage(); } - $this->success($msg, $message, 200); + + successJson($message,$msg); } else { - $this->error('非法请求'); + errorJson('非法请求'); } } } \ No newline at end of file diff --git a/Server/application/api/controller/WechatChatroomController.php b/Server/application/api/controller/WechatChatroomController.php index 5008e8b5..e78569a6 100644 --- a/Server/application/api/controller/WechatChatroomController.php +++ b/Server/application/api/controller/WechatChatroomController.php @@ -2,8 +2,9 @@ namespace app\api\controller; -use app\common\model\WechatChatroomModel; -use app\common\model\WechatChatroomMemberModel; +use app\api\model\WechatChatroomModel; +use app\api\model\WechatChatroomMemberModel; +use app\job\WechatChatroomJob; use think\facade\Request; class WechatChatroomController extends BaseController @@ -180,4 +181,32 @@ class WechatChatroomController extends BaseController WechatChatroomMemberModel::create($data); } } + + /** + * 同步微信群聊数据 + * 此方法用于手动触发微信群聊数据同步任务 + * @return \think\response\Json + */ + public function syncChatrooms() + { + try { + // 获取请求参数 + $pageIndex = $this->request->param('pageIndex', 0); + $pageSize = $this->request->param('pageSize', 100); + $keyword = $this->request->param('keyword', ''); + $wechatAccountKeyword = $this->request->param('wechatAccountKeyword', ''); + $isDeleted = $this->request->param('isDeleted', ''); + + // 添加同步任务到队列 + $result = WechatChatroomJob::addSyncTask($pageIndex, $pageSize, $keyword, $wechatAccountKeyword, $isDeleted); + + if ($result) { + return successJson([], '微信群聊同步任务已添加到队列'); + } else { + return errorJson('添加同步任务失败'); + } + } catch (\Exception $e) { + return errorJson('添加同步任务异常:' . $e->getMessage()); + } + } } \ No newline at end of file diff --git a/Server/application/api/controller/WechatController.php b/Server/application/api/controller/WechatController.php index 469a37ee..2523620d 100644 --- a/Server/application/api/controller/WechatController.php +++ b/Server/application/api/controller/WechatController.php @@ -2,7 +2,7 @@ namespace app\api\controller; -use app\common\model\WechatAccountModel; +use app\api\model\WechatAccountModel; class WechatController extends BaseController { diff --git a/Server/application/api/controller/WechatFriendController.php b/Server/application/api/controller/WechatFriendController.php index 4f1ca0bf..92d00d9c 100644 --- a/Server/application/api/controller/WechatFriendController.php +++ b/Server/application/api/controller/WechatFriendController.php @@ -2,7 +2,7 @@ namespace app\api\controller; -use app\common\model\WechatFriendModel; +use app\api\model\WechatFriendModel; use think\facade\Request; class WechatFriendController extends BaseController diff --git a/Server/application/common/model/CompanyAccountModel.php b/Server/application/api/model/CompanyAccountModel.php similarity index 72% rename from Server/application/common/model/CompanyAccountModel.php rename to Server/application/api/model/CompanyAccountModel.php index 1775b8c7..183e61bd 100644 --- a/Server/application/common/model/CompanyAccountModel.php +++ b/Server/application/api/model/CompanyAccountModel.php @@ -1,6 +1,6 @@ 'app\common\command\TestCommand', - 'device:list' => 'app\common\command\DeviceListCommand', + 'device:list' => 'app\command\DeviceListCommand', // 设备列表 + 'wechatFriends:list' => 'app\command\WechatFriendCommand', // 微信好友列表 + 'wechatChatroom:list' => 'app\command\WechatChatroomCommand', // 微信群列表 + 'friendTask:list' => 'app\command\FriendTaskCommand', // 添加好友任务列表 + 'wechatList:list' => 'app\command\WechatListCommand', // 微信客服列表 + 'account:list' => 'app\command\AccountListCommand', // 公司账号列表 + 'sync:wechat:chatrooms' => 'app\command\SyncWechatChatrooms', // 同步微信群聊数据 ]; diff --git a/Server/application/command/AccountListCommand.php b/Server/application/command/AccountListCommand.php new file mode 100644 index 00000000..7a7fabd4 --- /dev/null +++ b/Server/application/command/AccountListCommand.php @@ -0,0 +1,57 @@ +setName('account: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(AccountListJob::class, $data, 'account_list'); + } +} \ No newline at end of file diff --git a/Server/application/common/command/DeviceListCommand.php b/Server/application/command/DeviceListCommand.php similarity index 95% rename from Server/application/common/command/DeviceListCommand.php rename to Server/application/command/DeviceListCommand.php index 76b12029..4def0764 100644 --- a/Server/application/common/command/DeviceListCommand.php +++ b/Server/application/command/DeviceListCommand.php @@ -1,6 +1,6 @@ setName('friend:task') + ->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 + ]; + + // 添加到队列,设置任务名为 friend_task + Queue::push(FriendTaskJob::class, $data, 'friend_task'); + } +} \ No newline at end of file diff --git a/Server/application/command/WechatChatroomCommand.php b/Server/application/command/WechatChatroomCommand.php new file mode 100644 index 00000000..66fd37c1 --- /dev/null +++ b/Server/application/command/WechatChatroomCommand.php @@ -0,0 +1,57 @@ +setName('wechatChatroom: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 + ]; + + // 添加到队列,设置任务名为 device_list + Queue::push(WechatChatroomJob::class, $data, 'wechat_chatroom'); + } +} \ No newline at end of file diff --git a/Server/application/command/WechatFriendCommand.php b/Server/application/command/WechatFriendCommand.php new file mode 100644 index 00000000..f3381056 --- /dev/null +++ b/Server/application/command/WechatFriendCommand.php @@ -0,0 +1,57 @@ +setName('wechatFriends: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 + ]; + + // 添加到队列,设置任务名为 wechat_friends + Queue::push(WechatFriendJob::class, $data, 'wechat_friends'); + } +} \ No newline at end of file diff --git a/Server/application/command/WechatListCommand.php b/Server/application/command/WechatListCommand.php new file mode 100644 index 00000000..3b03fcea --- /dev/null +++ b/Server/application/command/WechatListCommand.php @@ -0,0 +1,57 @@ +setName('wechat: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 + ]; + + // 添加到队列,设置任务名为 wechat_list + Queue::push(WechatListJob::class, $data, 'wechat_list'); + } +} \ No newline at end of file diff --git a/Server/application/common.php b/Server/application/common.php index 29010f44..4fd7c7c9 100644 --- a/Server/application/common.php +++ b/Server/application/common.php @@ -104,7 +104,7 @@ if (!function_exists('setHeader')) { if (!function_exists('errorJson')) { function errorJson($error = '', $code = 500) { - return json([ + return json_encode([ 'code' => $code, 'msg' => $error, ]); @@ -115,7 +115,7 @@ if (!function_exists('errorJson')) { if (!function_exists('successJson')) { function successJson($data = [] ,$msg = '操作成功', $code = 200) { - return json([ + return json_encode([ 'data' => $data, 'code' => $code, 'msg' => $msg, diff --git a/Server/application/common/command/BaseCommand.php b/Server/application/common/command/BaseCommand.php deleted file mode 100644 index 62b5693d..00000000 --- a/Server/application/common/command/BaseCommand.php +++ /dev/null @@ -1,31 +0,0 @@ -logSave === 1) { - $name = get_class($this); - $name = substr($name, strrpos($name, '\\') + 1); - $file = ROOT_PATH . DS . 'runtime' . DS . $name . '_' . date('Ymd') . '.txt'; - - file_put_contents($file, $data, FILE_APPEND); - } else { - echo $data; - } - } -} \ No newline at end of file diff --git a/Server/application/common/command/TestCommand.php b/Server/application/common/command/TestCommand.php deleted file mode 100644 index c63b5d08..00000000 --- a/Server/application/common/command/TestCommand.php +++ /dev/null @@ -1,20 +0,0 @@ -setName('Test') - ->setDescription('command test.'); - } - - protected function execute(Input $input, Output $output) { - print_r('hello test.'); - } -} \ No newline at end of file diff --git a/Server/application/job/AccountListJob.php b/Server/application/job/AccountListJob.php new file mode 100644 index 00000000..6218418d --- /dev/null +++ b/Server/application/job/AccountListJob.php @@ -0,0 +1,123 @@ +processAccountList($data, $job->attempts())) { + $job->delete(); + Log::info('公司账号列表任务执行成功,页码:' . $data['pageIndex']); + } else { + if ($job->attempts() > 3) { + // 超过重试次数,删除任务 + Log::error('公司账号列表任务执行失败,已超过重试次数,页码:' . $data['pageIndex']); + $job->delete(); + } else { + // 任务失败,重新放回队列 + Log::warning('公司账号列表任务执行失败,重试次数:' . $job->attempts() . ',页码:' . $data['pageIndex']); + $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 processAccountList($data, $attempts) + { + // 获取参数 + $pageIndex = isset($data['pageIndex']) ? $data['pageIndex'] : 0; + $pageSize = isset($data['pageSize']) ? $data['pageSize'] : 100; + + Log::info('开始获取公司账号列表,页码:' . $pageIndex . ',页大小:' . $pageSize); + + // 实例化控制器 + $accountController = new AccountController(); + + // 构建请求参数 + $params = [ + 'pageIndex' => $pageIndex, + 'pageSize' => $pageSize + ]; + + // 设置请求信息 + $request = request(); + $request->withGet($params); + + // 获取系统授权信息 + $authorization = AuthService::getSystemAuthorization(); + if (empty($authorization)) { + Log::error('获取系统授权信息失败'); + return false; + } + + // 调用公司账号列表获取方法 + $result = $accountController->getlist($pageIndex,$pageSize,$authorization); + $response = json_decode($result,true); + + + // 判断是否成功 + if ($response['code'] == 200) { + $data = $response['data']; + + // 判断是否有下一页 + if (!empty($data) && count($data['results']) > 0) { + // 有下一页,将下一页任务添加到队列 + $nextPageIndex = $pageIndex + 1; + $this->addNextPageToQueue($nextPageIndex, $pageSize); + Log::info('添加下一页任务到队列,页码:' . $nextPageIndex); + } + + return true; + } else { + $errorMsg = isset($response['msg']) ? $response['msg'] : '未知错误'; + Log::error('获取公司账号列表失败:' . $errorMsg); + return false; + } + } + + /** + * 添加下一页任务到队列 + * @param int $pageIndex 页码 + * @param int $pageSize 每页大小 + */ + protected function addNextPageToQueue($pageIndex, $pageSize) + { + $data = [ + 'pageIndex' => $pageIndex, + 'pageSize' => $pageSize + ]; + + // 添加到队列,设置任务名为 account_list + Queue::push(self::class, $data, 'account_list'); + } +} \ No newline at end of file diff --git a/Server/application/job/FriendTaskJob.php b/Server/application/job/FriendTaskJob.php new file mode 100644 index 00000000..f1f43b1a --- /dev/null +++ b/Server/application/job/FriendTaskJob.php @@ -0,0 +1,123 @@ +processFriendTask($data, $job->attempts())) { + $job->delete(); + Log::info('添加好友任务执行成功,页码:' . $data['pageIndex']); + } else { + if ($job->attempts() > 3) { + // 超过重试次数,删除任务 + Log::error('添加好友任务执行失败,已超过重试次数,页码:' . $data['pageIndex']); + $job->delete(); + } else { + // 任务失败,重新放回队列 + Log::warning('添加好友任务执行失败,重试次数:' . $job->attempts() . ',页码:' . $data['pageIndex']); + $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 processFriendTask($data, $attempts) + { + // 获取参数 + $pageIndex = isset($data['pageIndex']) ? $data['pageIndex'] : 0; + $pageSize = isset($data['pageSize']) ? $data['pageSize'] : 100; + + Log::info('开始获取添加好友任务,页码:' . $pageIndex . ',页大小:' . $pageSize); + + // 实例化控制器 + $friendTaskController = new FriendTaskController(); + + // 构建请求参数 + $params = [ + 'pageIndex' => $pageIndex, + 'pageSize' => $pageSize + ]; + + // 设置请求信息 + $request = request(); + $request->withGet($params); + + // 获取系统授权信息 + $authorization = AuthService::getSystemAuthorization(); + if (empty($authorization)) { + Log::error('获取系统授权信息失败'); + return false; + } + + // 调用添加好友任务获取方法 + $result = $friendTaskController->getlist($pageIndex,$pageSize,$authorization); + $response = json_decode($result,true); + + + // 判断是否成功 + if ($response['code'] == 200) { + $data = $response['data']; + + // 判断是否有下一页 + if (!empty($data) && count($data['results']) > 0) { + // 有下一页,将下一页任务添加到队列 + $nextPageIndex = $pageIndex + 1; + $this->addNextPageToQueue($nextPageIndex, $pageSize); + Log::info('添加下一页任务到队列,页码:' . $nextPageIndex); + } + + return true; + } else { + $errorMsg = isset($response['msg']) ? $response['msg'] : '未知错误'; + Log::error('获取添加好友任务失败:' . $errorMsg); + return false; + } + } + + /** + * 添加下一页任务到队列 + * @param int $pageIndex 页码 + * @param int $pageSize 每页大小 + */ + protected function addNextPageToQueue($pageIndex, $pageSize) + { + $data = [ + 'pageIndex' => $pageIndex, + 'pageSize' => $pageSize + ]; + + // 添加到队列,设置任务名为 friend_task + Queue::push(self::class, $data, 'friend_task'); + } +} \ No newline at end of file diff --git a/Server/application/job/WechatChatroomJob.php b/Server/application/job/WechatChatroomJob.php new file mode 100644 index 00000000..99563f05 --- /dev/null +++ b/Server/application/job/WechatChatroomJob.php @@ -0,0 +1,123 @@ +processWechatChatroomList($data, $job->attempts())) { + $job->delete(); + Log::info('微信群列表任务执行成功,页码:' . $data['pageIndex']); + } else { + if ($job->attempts() > 3) { + // 超过重试次数,删除任务 + Log::error('微信群列表任务执行失败,已超过重试次数,页码:' . $data['pageIndex']); + $job->delete(); + } else { + // 任务失败,重新放回队列 + Log::warning('微信群列表任务执行失败,重试次数:' . $job->attempts() . ',页码:' . $data['pageIndex']); + $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 processWechatChatroomList($data, $attempts) + { + // 获取参数 + $pageIndex = isset($data['pageIndex']) ? $data['pageIndex'] : 0; + $pageSize = isset($data['pageSize']) ? $data['pageSize'] : 100; + + Log::info('开始获取微信群列表,页码:' . $pageIndex . ',页大小:' . $pageSize); + + // 实例化控制器 + $wechatChatroomController = new WechatChatroomController(); + + // 构建请求参数 + $params = [ + 'pageIndex' => $pageIndex, + 'pageSize' => $pageSize + ]; + + // 设置请求信息 + $request = request(); + $request->withGet($params); + + // 获取系统授权信息 + $authorization = AuthService::getSystemAuthorization(); + if (empty($authorization)) { + Log::error('获取系统授权信息失败'); + return false; + } + + // 调用设备列表获取方法 + $result = $wechatChatroomController->getlist($pageIndex,$pageSize,$authorization); + $response = json_decode($result,true); + + + // 判断是否成功 + if ($response['code'] == 200) { + $data = $response['data']; + + // 判断是否有下一页 + if (!empty($data) && count($data['results']) > 0) { + // 有下一页,将下一页任务添加到队列 + $nextPageIndex = $pageIndex + 1; + $this->addNextPageToQueue($nextPageIndex, $pageSize); + Log::info('添加下一页任务到队列,页码:' . $nextPageIndex); + } + + return true; + } else { + $errorMsg = isset($response['msg']) ? $response['msg'] : '未知错误'; + Log::error('获取微信群列表失败:' . $errorMsg); + return false; + } + } + + /** + * 添加下一页任务到队列 + * @param int $pageIndex 页码 + * @param int $pageSize 每页大小 + */ + protected function addNextPageToQueue($pageIndex, $pageSize) + { + $data = [ + 'pageIndex' => $pageIndex, + 'pageSize' => $pageSize + ]; + + // 添加到队列,设置任务名为 wechat_chatroom + Queue::push(self::class, $data, 'wechat_chatroom'); + } +} \ No newline at end of file diff --git a/Server/application/job/WechatFriendJob.php b/Server/application/job/WechatFriendJob.php new file mode 100644 index 00000000..769c7a06 --- /dev/null +++ b/Server/application/job/WechatFriendJob.php @@ -0,0 +1,123 @@ +processWechatFriendList($data, $job->attempts())) { + $job->delete(); + Log::info('微信列表任务执行成功,页码:' . $data['pageIndex']); + } else { + if ($job->attempts() > 3) { + // 超过重试次数,删除任务 + Log::error('微信列表任务执行失败,已超过重试次数,页码:' . $data['pageIndex']); + $job->delete(); + } else { + // 任务失败,重新放回队列 + Log::warning('微信列表任务执行失败,重试次数:' . $job->attempts() . ',页码:' . $data['pageIndex']); + $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 processWechatFriendList($data, $attempts) + { + // 获取参数 + $pageIndex = isset($data['pageIndex']) ? $data['pageIndex'] : 0; + $pageSize = isset($data['pageSize']) ? $data['pageSize'] : 100; + + Log::info('开始获取微信列表,页码:' . $pageIndex . ',页大小:' . $pageSize); + + // 实例化控制器 + $wechatFriendController = new WechatFriendController(); + + // 构建请求参数 + $params = [ + 'pageIndex' => $pageIndex, + 'pageSize' => $pageSize + ]; + + // 设置请求信息 + $request = request(); + $request->withGet($params); + + // 获取系统授权信息 + $authorization = AuthService::getSystemAuthorization(); + if (empty($authorization)) { + Log::error('获取系统授权信息失败'); + return false; + } + + // 调用设备列表获取方法 + $result = $wechatFriendController->getlist($pageIndex,$pageSize,$authorization); + $response = json_decode($result,true); + + + // 判断是否成功 + if ($response['code'] == 200) { + $data = $response['data']; + + // 判断是否有下一页 + if (!empty($data) && count($data['results']) > 0) { + // 有下一页,将下一页任务添加到队列 + $nextPageIndex = $pageIndex + 1; + $this->addNextPageToQueue($nextPageIndex, $pageSize); + Log::info('添加下一页任务到队列,页码:' . $nextPageIndex); + } + + return true; + } else { + $errorMsg = isset($response['msg']) ? $response['msg'] : '未知错误'; + Log::error('获取微信列表失败:' . $errorMsg); + return false; + } + } + + /** + * 添加下一页任务到队列 + * @param int $pageIndex 页码 + * @param int $pageSize 每页大小 + */ + protected function addNextPageToQueue($pageIndex, $pageSize) + { + $data = [ + 'pageIndex' => $pageIndex, + 'pageSize' => $pageSize + ]; + + // 添加到队列,设置任务名为 wechat_friends + Queue::push(self::class, $data, 'wechat_friends'); + } +} \ No newline at end of file diff --git a/Server/application/job/WechatListJob.php b/Server/application/job/WechatListJob.php new file mode 100644 index 00000000..3cfccfb3 --- /dev/null +++ b/Server/application/job/WechatListJob.php @@ -0,0 +1,123 @@ +processWechatList($data, $job->attempts())) { + $job->delete(); + Log::info('微信客服列表任务执行成功,页码:' . $data['pageIndex']); + } else { + if ($job->attempts() > 3) { + // 超过重试次数,删除任务 + Log::error('微信客服列表任务执行失败,已超过重试次数,页码:' . $data['pageIndex']); + $job->delete(); + } else { + // 任务失败,重新放回队列 + Log::warning('微信客服列表任务执行失败,重试次数:' . $job->attempts() . ',页码:' . $data['pageIndex']); + $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 processWechatList($data, $attempts) + { + // 获取参数 + $pageIndex = isset($data['pageIndex']) ? $data['pageIndex'] : 0; + $pageSize = isset($data['pageSize']) ? $data['pageSize'] : 100; + + Log::info('开始获取微信客服列表,页码:' . $pageIndex . ',页大小:' . $pageSize); + + // 实例化控制器 + $wechatController = new WechatController(); + + // 构建请求参数 + $params = [ + 'pageIndex' => $pageIndex, + 'pageSize' => $pageSize + ]; + + // 设置请求信息 + $request = request(); + $request->withGet($params); + + // 获取系统授权信息 + $authorization = AuthService::getSystemAuthorization(); + if (empty($authorization)) { + Log::error('获取系统授权信息失败'); + return false; + } + + // 调用设备列表获取方法 + $result = $wechatController->getlist($pageIndex,$pageSize,$authorization); + $response = json_decode($result,true); + + + // 判断是否成功 + if ($response['code'] == 200) { + $data = $response['data']; + + // 判断是否有下一页 + if (!empty($data) && count($data['results']) > 0) { + // 有下一页,将下一页任务添加到队列 + $nextPageIndex = $pageIndex + 1; + $this->addNextPageToQueue($nextPageIndex, $pageSize); + Log::info('添加下一页任务到队列,页码:' . $nextPageIndex); + } + + return true; + } else { + $errorMsg = isset($response['msg']) ? $response['msg'] : '未知错误'; + Log::error('获取微信客服列表失败:' . $errorMsg); + return false; + } + } + + /** + * 添加下一页任务到队列 + * @param int $pageIndex 页码 + * @param int $pageSize 每页大小 + */ + protected function addNextPageToQueue($pageIndex, $pageSize) + { + $data = [ + 'pageIndex' => $pageIndex, + 'pageSize' => $pageSize + ]; + + // 添加到队列,设置任务名为 wechat_list + Queue::push(self::class, $data, 'wechat_list'); + } +} \ No newline at end of file