Files
cunkebao_v3/Server/application/job/WechatFriendJob.php
2025-04-12 18:31:53 +08:00

135 lines
4.9 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
namespace app\job;
use think\queue\Job;
use think\facade\Log;
use think\Queue;
use think\facade\Config;
use think\facade\Cache;
use app\api\controller\WechatFriendController;
class WechatFriendJob
{
/**
* 队列任务处理
* @param Job $job 队列任务
* @param array $data 任务数据
* @return void
*/
public function fire(Job $job, $data)
{
try {
// 如果任务执行成功后删除任务
if ($this->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'] : 1000;
$preFriendId = isset($data['preFriendId']) ? $data['preFriendId'] : '';
Log::info('开始获取微信列表,页码:' . $pageIndex . ',页大小:' . $pageSize . '上一好友ID' . $preFriendId);
// 实例化控制器
$wechatFriendController = new WechatFriendController();
// 构建请求参数
$params = [
'pageIndex' => $pageIndex,
'pageSize' => $pageSize,
'preFriendId' => $preFriendId
];
// 设置请求信息
$request = request();
$request->withGet($params);
// 调用设备列表获取方法
$result = $wechatFriendController->getlist($pageIndex,$pageSize,$preFriendId,true);
$response = json_decode($result,true);
// 判断是否成功
if ($response['code'] == 200) {
$data = $response['data'];
// 判断是否有下一页
if (!empty($data) && count($data) > 0) {
// 获取最后一条记录的ID
$lastFriendId = $data[count($data)-1]['id'];
// 更新缓存中的页码和最后一个好友ID设置10分钟过期
Cache::set('friendsPage', $pageIndex + 1, 86400);
Cache::set('preFriendId', $lastFriendId, 86400);
Log::info('更新缓存,下一页页码:' . ($pageIndex + 1) . '最后好友ID' . $lastFriendId . '缓存时间10分钟');
// 有下一页,将下一页任务添加到队列
$nextPageIndex = $pageIndex + 1;
$this->addNextPageToQueue($nextPageIndex, $pageSize, $lastFriendId);
Log::info('添加下一页任务到队列,页码:' . $nextPageIndex);
} else {
// 没有下一页重置缓存设置10分钟过期
Cache::set('friendsPage', 0, 86400);
Cache::set('preFriendId', '', 86400);
Log::info('获取完成重置缓存缓存时间10分钟');
}
return true;
} else {
$errorMsg = isset($response['msg']) ? $response['msg'] : '未知错误';
Log::error('获取微信列表失败:' . $errorMsg);
return false;
}
}
/**
* 添加下一页任务到队列
* @param int $pageIndex 页码
* @param int $pageSize 每页大小
* @param string $preFriendId 上一个好友ID
*/
protected function addNextPageToQueue($pageIndex, $pageSize,$preFriendId)
{
$data = [
'pageIndex' => $pageIndex,
'pageSize' => $pageSize,
'preFriendId' => $preFriendId
];
// 添加到队列,设置任务名为 wechat_friends
Queue::push(self::class, $data, 'wechat_friends');
}
}