朋友圈采集自己的任务完成
This commit is contained in:
@@ -448,6 +448,43 @@ class WebSocketController extends BaseController
|
||||
return json_encode(['code' => 400, 'msg' => '资源信息格式错误,应为数组']);
|
||||
}
|
||||
|
||||
// 判断snsUrls是否已经是远程图片(http/https)
|
||||
$allRemoteUrls = true;
|
||||
foreach ($data['snsUrls'] as $url) {
|
||||
if (empty($url)) {
|
||||
continue;
|
||||
}
|
||||
// 检查URL是否以http://或https://开头
|
||||
if (!preg_match('/^https?:\/\//i', $url)) {
|
||||
$allRemoteUrls = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// 如果全部都是远程URL,直接使用,不需要通过WebSocket下载
|
||||
if ($allRemoteUrls && !empty($data['snsUrls'])) {
|
||||
$urls = json_encode($data['snsUrls'], 256);
|
||||
|
||||
// 上传图片到OSS
|
||||
$ossUrls = $this->uploadMomentImagesToOss($data['snsUrls'], $data['snsId']);
|
||||
|
||||
// 更新数据库:保存原始URL和OSS URL,并标记已上传
|
||||
$updateData = [
|
||||
'resUrls' => $urls,
|
||||
'isOssUploaded' => 1, // 标识已上传到OSS
|
||||
'update_time' => time()
|
||||
];
|
||||
|
||||
// 如果有OSS URL,保存到ossUrls字段
|
||||
if (!empty($ossUrls)) {
|
||||
$updateData['ossUrls'] = json_encode($ossUrls, 256);
|
||||
}
|
||||
Db::table('s2_wechat_moments')->where('snsId', $data['snsId'])->update($updateData);
|
||||
|
||||
return json_encode(['code' => 200, 'msg' => '获取朋友圈资源链接成功(已为远程URL)', 'data' => ['urls' => $data['snsUrls']]]);
|
||||
}
|
||||
|
||||
// 如果不是远程URL,需要通过WebSocket下载
|
||||
// 检查连接状态
|
||||
if (!$this->isConnected) {
|
||||
$this->connect();
|
||||
@@ -659,6 +696,7 @@ class WebSocketController extends BaseController
|
||||
$dataToSave['wechatAccountId'] = $wechatAccountId;
|
||||
$dataToSave['wechatFriendId'] = $wechatFriendId ?? 0;
|
||||
$dataToSave['create_time'] = time();
|
||||
$dataToSave['ossUrls'] = json_encode([],256);
|
||||
$dataToSave['isOssUploaded'] = 0; // 新记录默认为未上传
|
||||
$res = WechatMoments::create($dataToSave);
|
||||
}
|
||||
@@ -1114,4 +1152,25 @@ class WebSocketController extends BaseController
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public function ttttt($data = [])
|
||||
{
|
||||
try {
|
||||
$params = [
|
||||
"chatroomOperateType" => $data['chatroomOperateType'],
|
||||
"cmdType" => "CmdChatroomOperate",
|
||||
"seq" => time(),
|
||||
"wechatAccountId" => $data['wechatAccountId'],
|
||||
"wechatChatroomId" => $data['wechatChatroomId']
|
||||
];
|
||||
$message = $this->sendMessage($params,false);
|
||||
return json_encode(['code' => 200, 'msg' => '234', 'data' => $message]);
|
||||
} catch (\Exception $e) {
|
||||
Log::error('修改群信息异常: ' . $e->getMessage());
|
||||
return json_encode(['code' => 500, 'msg' => '567567: ' . $e->getMessage()]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -27,6 +27,7 @@ return [
|
||||
'allotrule:autocreate' => 'app\command\AutoCreateAllotRulesCommand', // 自动创建分配规则 √
|
||||
'content:collect' => 'app\command\ContentCollectCommand', // 内容采集任务 √
|
||||
'moments:collect' => 'app\command\WechatMomentsCommand', // 朋友圈采集任务
|
||||
'own:moments:collect' => 'app\command\OwnMomentsCollectCommand', // 采集在线微信账号自己的朋友圈
|
||||
'switch:friends' => 'app\command\SwitchFriendsCommand',
|
||||
'call-recording:list' => 'app\command\CallRecordingListCommand', // 通话记录列表 √
|
||||
'sync:wechatData' => 'app\command\SyncWechatDataToCkbTask', // 同步微信数据到存客宝
|
||||
|
||||
51
Server/application/command/OwnMomentsCollectCommand.php
Normal file
51
Server/application/command/OwnMomentsCollectCommand.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\OwnMomentsCollectJob;
|
||||
|
||||
class OwnMomentsCollectCommand extends Command
|
||||
{
|
||||
protected function configure()
|
||||
{
|
||||
$this->setName('own:moments: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 = [
|
||||
'timestamp' => time()
|
||||
];
|
||||
|
||||
// 添加到队列,设置任务名为 own_moments_collect
|
||||
Queue::push(OwnMomentsCollectJob::class, $data, 'own_moments_collect');
|
||||
}
|
||||
}
|
||||
|
||||
159
Server/application/job/OwnMomentsCollectJob.php
Normal file
159
Server/application/job/OwnMomentsCollectJob.php
Normal file
@@ -0,0 +1,159 @@
|
||||
<?php
|
||||
|
||||
namespace app\job;
|
||||
|
||||
use think\queue\Job;
|
||||
use think\facade\Log;
|
||||
use think\Queue;
|
||||
use think\facade\Config;
|
||||
use think\Db;
|
||||
use think\facade\Env;
|
||||
use app\api\controller\WebSocketController;
|
||||
|
||||
class OwnMomentsCollectJob
|
||||
{
|
||||
/**
|
||||
* 队列任务处理
|
||||
* @param Job $job 队列任务
|
||||
* @param array $data 任务数据
|
||||
* @return void
|
||||
*/
|
||||
public function fire(Job $job, $data)
|
||||
{
|
||||
try {
|
||||
// 如果任务执行成功后删除任务
|
||||
if ($this->processOwnMomentsCollect($data, $job->attempts())) {
|
||||
$job->delete();
|
||||
Log::info('自己朋友圈采集任务执行成功');
|
||||
} else {
|
||||
if ($job->attempts() > 3) {
|
||||
// 超过重试次数,删除任务
|
||||
Log::error('自己朋友圈采集任务执行失败,已超过重试次数');
|
||||
$job->delete();
|
||||
} else {
|
||||
// 任务失败,重新放回队列
|
||||
Log::warning('自己朋友圈采集任务执行失败,重试次数:' . $job->attempts());
|
||||
$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 processOwnMomentsCollect($data, $attempts)
|
||||
{
|
||||
try {
|
||||
// 获取在线微信账号列表(只获取在线微信)
|
||||
$onlineWechatAccounts = $this->getOnlineWechatAccounts();
|
||||
|
||||
if (empty($onlineWechatAccounts)) {
|
||||
Log::info('没有在线微信账号,跳过朋友圈采集');
|
||||
return true;
|
||||
}
|
||||
|
||||
Log::info('找到 ' . count($onlineWechatAccounts) . ' 个在线微信账号,开始采集朋友圈');
|
||||
|
||||
// 获取API账号配置
|
||||
$username = Env::get('api.username2', '');
|
||||
$password = Env::get('api.password2', '');
|
||||
|
||||
if (empty($username) || empty($password)) {
|
||||
Log::error('API账号配置缺失,无法执行朋友圈采集');
|
||||
return false;
|
||||
}
|
||||
|
||||
// 获取账号ID
|
||||
$toAccountId = Db::name('users')->where('account', $username)->value('s2_accountId');
|
||||
if (empty($toAccountId)) {
|
||||
Log::error('未找到API账号对应的账号ID');
|
||||
return false;
|
||||
}
|
||||
|
||||
$successCount = 0;
|
||||
$failCount = 0;
|
||||
|
||||
// 遍历每个在线微信账号,采集自己的朋友圈
|
||||
foreach ($onlineWechatAccounts as $account) {
|
||||
try {
|
||||
$wechatAccountId = $account['id'];
|
||||
$wechatId = $account['wechatId'];
|
||||
|
||||
Log::info("开始采集微信账号 {$wechatId} (ID: {$wechatAccountId}) 的朋友圈");
|
||||
|
||||
// 创建WebSocket控制器实例
|
||||
$webSocket = new WebSocketController([
|
||||
'userName' => $username,
|
||||
'password' => $password,
|
||||
'accountId' => $toAccountId
|
||||
]);
|
||||
|
||||
// 采集自己的朋友圈(wechatFriendId传0或空,表示采集自己的朋友圈)
|
||||
$result = $webSocket->getMoments([
|
||||
'wechatAccountId' => $wechatAccountId,
|
||||
'wechatFriendId' => 0, // 0表示采集自己的朋友圈
|
||||
'count' => 10 // 每次采集10条
|
||||
]);
|
||||
|
||||
$resultData = json_decode($result, true);
|
||||
if (!empty($resultData) && $resultData['code'] == 200) {
|
||||
$successCount++;
|
||||
Log::info("微信账号 {$wechatId} 朋友圈采集成功");
|
||||
} else {
|
||||
$failCount++;
|
||||
Log::warning("微信账号 {$wechatId} 朋友圈采集失败:" . ($resultData['msg'] ?? '未知错误'));
|
||||
}
|
||||
|
||||
// 避免请求过于频繁,每个账号之间稍作延迟
|
||||
usleep(500000); // 延迟0.5秒
|
||||
|
||||
} catch (\Exception $e) {
|
||||
$failCount++;
|
||||
Log::error("采集微信账号 {$account['wechatId']} 朋友圈异常:" . $e->getMessage());
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
Log::info("自己朋友圈采集任务完成,成功:{$successCount},失败:{$failCount}");
|
||||
return true;
|
||||
|
||||
} catch (\Exception $e) {
|
||||
Log::error('自己朋友圈采集处理异常:' . $e->getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取在线微信账号列表
|
||||
* @return array
|
||||
*/
|
||||
protected function getOnlineWechatAccounts()
|
||||
{
|
||||
try {
|
||||
// 查询在线微信账号(deviceAlive=1 且 wechatAlive=1)
|
||||
$accounts = Db::table('s2_wechat_account')
|
||||
->where('deviceAlive', 1)
|
||||
->where('wechatAlive', 1)
|
||||
->field('id, wechatId, nickname, alias')
|
||||
->select();
|
||||
|
||||
return $accounts ?: [];
|
||||
} catch (\Exception $e) {
|
||||
Log::error('获取在线微信账号列表失败:' . $e->getMessage());
|
||||
return [];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user