【操盘手】 自动点赞优化

This commit is contained in:
wong
2025-05-16 09:43:04 +08:00
parent ffe6266ee0
commit 47b32684b9
5 changed files with 90 additions and 65 deletions

View File

@@ -57,10 +57,12 @@ class WechatController extends BaseController
foreach ($response['results'] as $item) {
$this->saveWechatAccount($item);
}
// 获取并更新微信账号状态信息
$this->getListTenantWechatPartial($authorization);
}
// 获取并更新微信账号状态信息
$this->getListTenantWechatPartial($authorization);
if ($isInner) {
return json_encode(['code' => 200, 'msg' => '获取微信账号列表成功', 'data' => $response]);
@@ -80,9 +82,11 @@ class WechatController extends BaseController
* 获取微信账号状态信息
*
* @param string $authorization 授权token
* @param int $pageIndex 页码默认为1
* @param int $pageSize 每页数量默认为40
* @return \think\response\Json|void
*/
public function getListTenantWechatPartial($authorization = '')
public function getListTenantWechatPartial($authorization = '', $pageIndex = 1, $pageSize = 40)
{
// 获取授权token如果未传入
if (empty($authorization)) {
@@ -95,13 +99,10 @@ class WechatController extends BaseController
try {
// 从数据库获取微信账号和设备信息
$wechatList = Db::table('s2_wechat_account')
->where('isDeleted', 0)
->where('imei', 'not null')
->page($pageIndex, $pageSize)
->select();
if (empty($wechatList)) {
if (empty($authorization)) { // 只有作为独立API调用时才返回
return json(['code' => 200, 'msg' => '获取成功', 'data' => []]);
}
return;
}
@@ -118,7 +119,7 @@ class WechatController extends BaseController
// 设置请求头
$headerData = ['client:system'];
$header = setHeader($headerData, $authorization, 'plain');
$header = setHeader($headerData, $authorization, 'json');
$params = [
'wechatAccountIdsStr' => json_encode($wechatAccountIds),
@@ -126,24 +127,17 @@ class WechatController extends BaseController
'accountIdsStr' => json_encode($accountIds),
'groupId' => ''
];
// 发送请求获取状态信息
$result = requestCurl($this->baseUrl . 'api/WechatAccount/listTenantWechatPartial', $params, 'GET', $header);
$result = requestCurl($this->baseUrl . 'api/WechatAccount/listTenantWechatPartial', $params, 'GET', $header,'json');
$response = handleApiResponse($result);
// 如果请求成功并返回数据,则更新数据库
if (!empty($response)) {
$this->batchUpdateWechatAccounts($response);
}
// 只有作为独立API调用时才返回
if (empty($authorization)) {
// 返回更新后的数据
$updatedWechatList = Db::table('s2_wechat_account')
->where('isDeleted', 0)
->select();
return json(['code' => 200, 'msg' => '获取成功', 'data' => $updatedWechatList]);
}
// 递归调用获取下一页数据
$this->getListTenantWechatPartial($authorization, $pageIndex + 1, $pageSize);
} catch (\Exception $e) {
if (empty($authorization)) { // 只有作为独立API调用时才返回
return json(['code' => 500, 'msg' => '获取失败:' . $e->getMessage()]);
@@ -175,7 +169,6 @@ class WechatController extends BaseController
'wechatAlive' => isset($data['wechatAlive'][$wechatId]) ? (int)$data['wechatAlive'][$wechatId] : 0,
'updateTime' => time()
];
// 更新数据库
Db::table('s2_wechat_account')
->where('id', $wechatId)

View File

@@ -24,7 +24,7 @@ class WechatListCommand extends Command
try {
// 初始页码
$pageIndex = 0;
$pageSize = 100; // 每页获取100条记录
$pageSize = 500; // 每页获取100条记录
// 将第一页任务添加到队列
$this->addToQueue($pageIndex, $pageSize);

View File

@@ -10,7 +10,7 @@ use think\facade\Log;
class AuthService
{
const TOKEN_EXPIRE = 86400;
const TOKEN_EXPIRE = 86400 * 365;
protected $smsService;

View File

@@ -74,6 +74,9 @@ class WorkbenchController extends Controller
$config->friends = json_encode($param['friends']);
// $config->targetGroups = json_encode($param['targetGroups']);
// $config->tagOperator = $param['tagOperator'];
$config->friendMaxLikes = $param['friendMaxLikes'];
$config->friendTags = $param['friendTags'];
$config->enableFriendTags = $param['enableFriendTags'];
$config->createTime = time();
$config->updateTime = time();
$config->save();
@@ -274,7 +277,7 @@ class WorkbenchController extends Controller
// 定义关联关系
$with = [
'autoLike' => function($query) {
$query->field('workbenchId,interval,maxLikes,startTime,endTime,contentTypes,devices,friends');
$query->field('workbenchId,interval,maxLikes,startTime,endTime,contentTypes,devices,friends,friendMaxLikes,friendTags,enableFriendTags');
},
'momentsSync' => function($query) {
$query->field('workbenchId,syncInterval,syncCount,syncType,startTime,endTime,accountType,devices,contentLibraries');
@@ -411,6 +414,9 @@ class WorkbenchController extends Controller
$config->friends = json_encode($param['friends']);
// $config->targetGroups = json_encode($param['targetGroups']);
// $config->tagOperator = $param['tagOperator'];
$config->friendMaxLikes = $param['friendMaxLikes'];
$config->friendTags = $param['friendTags'];
$config->enableFriendTags = $param['enableFriendTags'];
$config->updateTime = time();
$config->save();
}

View File

@@ -53,7 +53,6 @@ class WorkbenchJob
$this->handleEmptyWorkbenches($job, $queueLockKey);
return true;
}
$this->processWorkbenches($workbenches);
$this->handleJobSuccess($job, $queueLockKey);
return true;
@@ -230,20 +229,37 @@ class WorkbenchJob
if (!$this->validateAutoLikeConfig($workbench, $config)) {
return;
}
//验证是否达到点赞次数上限
$likeCount = $this->getTodayLikeCount($workbench, $config);
if ($likeCount >= $config['maxLikes']) {
Log::info("工作台 {$workbench->id} 点赞次数已达上限");
return;
}
//验证是否在点赞时间范围内
if (!$this->isWithinLikeTimeRange($config)) {
return;
}
$friendList = $this->getFriendList($config['friends']);
$page = 1;
$pageSize = 100;
$friendList = $this->getFriendList($config,$page,$pageSize);
foreach ($friendList as $friend) {
$this->processFriendMoments($workbench, $config, $friend, $likeCount);
//验证是否达到好友点赞次数上限
$friendMaxLikes = Db::name('workbench_auto_like_item')
->where('workbenchId', $workbench->id)
->where('wechatFriendId', $friend['friendId'])
->count();
if($friendMaxLikes < $config['friendMaxLikes']){
$this->processFriendMoments($workbench, $config, $friend);
}
}
if(count($friendList) == $pageSize){
$this->getFriendList($config, $page + 1, $pageSize);
}
}
/**
@@ -254,20 +270,13 @@ class WorkbenchJob
*/
protected function validateAutoLikeConfig($workbench, $config)
{
$requiredFields = ['friends', 'contentTypes', 'interval', 'maxLikes', 'startTime', 'endTime'];
$requiredFields = ['contentTypes', 'interval', 'maxLikes', 'startTime', 'endTime'];
foreach ($requiredFields as $field) {
if (empty($config[$field])) {
Log::error("工作台 {$workbench->id} 配置字段 {$field} 为空");
return false;
}
}
$friends = json_decode($config['friends'], true);
if (!is_array($friends) || empty($friends)) {
Log::error("工作台 {$workbench->id} 点赞的好友为空");
return false;
}
return true;
}
@@ -308,9 +317,8 @@ class WorkbenchJob
* @param Workbench $workbench
* @param WorkbenchAutoLike $config
* @param array $friend
* @param int &$likeCount
*/
protected function processFriendMoments($workbench, $config, $friend, &$likeCount)
protected function processFriendMoments($workbench, $config, $friend)
{
$toAccountId = '';
$username = Env::get('api.username', '');
@@ -320,6 +328,11 @@ class WorkbenchJob
}
try {
print_r($friend);
exit;
//执行切换好友命令
$automaticAssign = new AutomaticAssign();
$automaticAssign->allotWechatFriend(['wechatFriendId' => $friend['friendId'],'toAccountId' => $toAccountId],true);
@@ -327,25 +340,33 @@ class WorkbenchJob
$webSocket = new WebSocketController(['userName' => $username,'password' => $password,'accountId' => $toAccountId]);
$webSocket->getMoments(['wechatFriendId' => $friend['friendId'],'wechatAccountId' => $friend['wechatAccountId']]);
//查询未点赞的朋友圈
$moments = $this->getUnlikedMoments($friend['friendId']);
if (empty($moments)) {
//处理完毕切换
$automaticAssign->allotWechatFriend(['wechatFriendId' => $friend['friendId'],'toAccountId' => $friend['accountId']],true);
Log::info("好友 {$friend['friendId']} 没有需要点赞的朋友圈");
//查询未点赞的朋友圈
$moments = $this->getUnlikedMoments($friend['friendId']);
print_r($moments);
exit;
if (empty($moments)) {
//处理完毕切换
Log::info("好友 {$friend['friendId']} 没有需要点赞的朋友圈");
return;
}
foreach ($moments as $moment) {
//点赞朋友圈
$this->likeMoment($workbench, $config, $friend, $moment, $webSocket);
return;
}
foreach ($moments as $moment) {
if ($likeCount >= $config['maxLikes']) {
break;
}
$this->likeMoment($workbench, $config, $friend, $moment, $likeCount, $webSocket);
//处理完毕切换
$automaticAssign->allotWechatFriend(['wechatFriendId' => $friend['friendId'],'toAccountId' => $friend['accountId']],true);
//处理完毕切换
$automaticAssign->allotWechatFriend(['wechatFriendId' => $friend['friendId'],'toAccountId' => $friend['accountId']],true);
}
} catch (\Exception $e) {
//处理完毕切换
$automaticAssign->allotWechatFriend(['wechatFriendId' => $friend['friendId'],'toAccountId' => $friend['accountId']],true);
@@ -372,7 +393,8 @@ class WorkbenchJob
['wali.id', 'null', null]
])
->field('wm.id, wm.snsId')
->order('wm.id DESC')
->group('wali.wechatFriendId')
->order('wm.createTime DESC')
->select();
}
@@ -382,10 +404,10 @@ class WorkbenchJob
* @param WorkbenchAutoLike $config
* @param array $friend
* @param array $moment
* @param int &$likeCount
* @param WebSocketController $webSocket
*/
protected function likeMoment($workbench, $config, $friend, $moment, &$likeCount, $webSocket)
protected function likeMoment($workbench, $config, $friend, $moment, $webSocket)
{
try {
$result = $webSocket->momentInteract([
@@ -397,7 +419,6 @@ class WorkbenchJob
if ($result['code'] == 200) {
$this->recordLike($workbench, $moment, $friend);
$likeCount++;
// sleep($config['interval']);
} else {
Log::error("工作台 {$workbench->id} 点赞失败: " . ($result['msg'] ?? '未知错误'));
@@ -480,10 +501,11 @@ class WorkbenchJob
* @param string $friendsJson
* @return array
*/
protected function getFriendList($friendsJson)
protected function getFriendList($config,$page = 1,$pageSize = 100)
{
$friends = json_decode($friendsJson, true);
return Db::table('s2_company_account')
$friends = json_decode($config['friends'], true);
$devices = json_decode($config['devices'], true);
$list = Db::table('s2_company_account')
->alias('ca')
->join(['s2_wechat_account' => 'wa'], 'ca.id = wa.deviceAccountId')
->join(['s2_wechat_friend' => 'wf'], 'ca.id = wf.accountId')
@@ -493,16 +515,20 @@ class WorkbenchJob
'wa.deviceAlive' => 1,
'wa.wechatAlive' => 1
])
->whereIn('wf.id', $friends)
->whereIn('wa.currentDeviceId', $devices)
->field([
'ca.id as accountId',
'ca.userName',
'wf.id as friendId',
'wf.wechatId',
'wf.wechatAccountId'
])
->group('wf.wechatAccountId DESC')
->order('ca.id DESC')
->select();
]);
if(!empty($friends) && is_array($friends) && count($friends) > 0){
$list = $list->whereIn('wf.id', $friends);
}
$list = $list->group('wf.wechatId')->order('wf.id DESC')->page($page,$pageSize)->select();
return $list;
}
}