feat: 更新 Task2 类,调整定时任务为每小时执行,重构用户在线状态查询逻辑,优化用户积分计算和状态更新

This commit is contained in:
lingling 2025-03-08 17:21:08 +08:00
parent d4a9b3990c
commit 5da86322b7
1 changed files with 127 additions and 44 deletions

View File

@ -9,6 +9,8 @@ use app\dao\UserRewardDao;
use support\Log;
use Workerman\Crontab\Crontab;
use GuzzleHttp\Client;
use App\Utils\API\Rocketgo;
/**
* 查询用户是否在线类
*/
@ -16,59 +18,140 @@ class Task2
{
public function onWorkerStart()
{
// 每5秒执行一次
// 每小时执行一次
new Crontab('0 */1 * * * *', function () {
$phones = UserPhone::where('created_at', '<', \Carbon\Carbon::now()->subMinutes(10))->where('status','!=',2)->get();
$start_time = time();
foreach ($phones as $key => $value) {
Rocketgo::test_login();
Log::info("查询任务开始");
// 获取账号列表
$res = Rocketgo::account_list();
$usernames = array_column($res, 'username');
try {
// 获取用户的在线状态
$status = SendCode::get_ws_status($value->phone);
// 批量查询用户信息
$phones = UserPhone::whereIn('phone', $usernames)->get();
// 将查询结果按手机号索引,方便后续处理
$phoneMap = [];
foreach ($phones as $phone) {
$phoneMap[$phone->phone] = $phone;
}
// 当前时间戳
$currentTimestamp = time();
switch ($status) {
case 0: // 在线状态
if ($value->time > 3600) {
// 记录需要更新的用户数据
$updateData = [];
foreach ($res as $v) {
$phone = $phoneMap[$v['username']] ?? null;
if (!$phone) {
continue;
}
if ($v['failedReason'] !== null) {
SendCode::delWS($v['username']);
$phone->status = 2;
$updateData[] = $phone;
continue;
}
if ($phone->time > 3600 && $v['logged'] == 1) {
// 计算超过 1 小时的积分
$hours = intdiv($value->time, 3600);
$hours = intdiv($phone->time, 3600);
$score = $hours * 20;
$remainingTime = $value->time % 3600;
$remainingTime = $phone->time % 3600;
// 更新用户积分和时长
$value->score += $score;
$value->day_score += $score;
$value->time = $remainingTime;
$phone->score += $score;
$phone->day_score += $score;
$phone->time = $remainingTime;
// 保存用户收益到 UserReward 表
UserRewardDao::Onhookincome($value->user_id, $score, $value->phone);
// 保存在线时长记录
UserPhoneLogDao::setOnlineTimeByPhone($value->phone, $status, $currentTimestamp);
}
// 更新在线时长
$value->status=1;
$value->time += $currentTimestamp - $value->last_time;
break;
case 1: // 不在线状态
$value->status = 0;
break;
case 3: // 账户不存在
$value->status = 2;
break;
}
// 更新用户最后活动时间和状态
$value->last_time = $currentTimestamp;
$value->save();
// UserRewardDao::Onhookincome($phone->user_id, $score, $phone->phone);
} catch (\Exception $e) {
// 记录错误日志,避免任务中断
Log::error("Error processing phone {$value->phone}: " . $e->getMessage());
}
// 保存在线时长记录 这里需要反转状态
UserPhoneLogDao::setOnlineTimeByPhone($phone->phone, 0, $currentTimestamp);
$phone->status = 1;
$phone->time += $currentTimestamp - $phone->last_time;
$phone->last_time = $currentTimestamp;
$updateData[] = $phone;
if ($v['logged'] == 0) {
$phone->status = 0;
$phone->last_time = $currentTimestamp;
$updateData[] = $phone;
}
}
// 批量更新用户状态
foreach ($updateData as $phone) {
$phone->save();
}
// 获取仓库账号列表
$res2 = Rocketgo::account_storehouse_list();
$usernames2 = array_column($res2, 'username');
/**
* 小火箭 后台所有的手机号
*/
$all_usernames = array_merge($usernames2, $usernames);
UserPhone::whereNotIn('phone', $all_usernames)
->where('status', '!=', 2)
->update(['status' => 2]);
// 批量查询用户信息
$phones2 = UserPhone::whereIn('phone', $usernames2)->get();
// 将查询结果按手机号索引,方便后续处理
$phoneMap2 = [];
foreach ($phones2 as $phone) {
$phoneMap2[$phone->phone] = $phone;
}
// 记录需要更新的用户数据
$updateData2 = [];
foreach ($res2 as $v) {
$phone = $phoneMap2[$v['username']] ?? null;
if (!$phone) {
continue;
}
if ($phone->time > 3600) {
// 计算超过 1 小时的积分
$hours = intdiv($phone->time, 3600);
$score = $hours * 20;
$remainingTime = $phone->time % 3600;
// 更新用户积分和时长
$phone->score += $score;
$phone->day_score += $score;
$phone->time = $remainingTime;
// 保存用户收益到 UserReward 表
// UserRewardDao::Onhookincome($phone->user_id, $score, $phone->phone);
}
// 保存在线时长记录 这里需要反转状态
UserPhoneLogDao::setOnlineTimeByPhone($phone->phone, 0, $currentTimestamp);
$phone->status = 1;
$phone->time += $currentTimestamp - $phone->last_time;
$phone->last_time = $currentTimestamp;
$updateData2[] = $phone;
}
// 批量更新用户状态
foreach ($updateData2 as $phone) {
$phone->save();
}
$end_time = time();
Log::info("Task2: Processed " . count($phones) . " phones in " . ($end_time - $start_time) . " seconds");
Log::info("Task2: Processed " . count($all_usernames) . " phones in " . ($end_time - $start_time) . " seconds");
});
}
}