From 73d6a5f190cdfdf27875320811f35922c8633401 Mon Sep 17 00:00:00 2001 From: lingling <1077478963@qq.com> Date: Fri, 7 Mar 2025 13:32:01 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=20Task2=20=E7=B1=BB?= =?UTF-8?q?=E7=9A=84=E7=94=A8=E6=88=B7=E5=9C=A8=E7=BA=BF=E7=8A=B6=E6=80=81?= =?UTF-8?q?=E5=A4=84=E7=90=86=EF=BC=8C=E5=A2=9E=E5=8A=A0=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=E6=8D=95=E8=8E=B7=E5=92=8C=E6=97=A5=E5=BF=97=E8=AE=B0=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- process/Task2.php | 99 ++++++++++++++++++++++------------------------- 1 file changed, 46 insertions(+), 53 deletions(-) diff --git a/process/Task2.php b/process/Task2.php index bbee9d2..dd08d40 100644 --- a/process/Task2.php +++ b/process/Task2.php @@ -16,67 +16,60 @@ class Task2 { public function onWorkerStart() { - - // // 每5秒执行一次 + // 每5秒执行一次 new Crontab('0 */1 * * * *', function () { - $phones = UserPhone::all(); + $phones = UserPhone::where('status' ,'!=', 2)->get(); + $start_time = time(); + foreach ($phones as $key => $value) { - $currentTimestamp = time(); - $status = SendCode::get_ws_status($value->phone); - // 如果是挂机状态 - if ($status == 0) { - // 如果在线时长超过 3600 秒 - if ($value->time > 3600) { - // 计算超过 1 小时的完整小时数 - $hours = intdiv($value->time, 3600); - - // 根据每小时 20 分积分计算总积分 - $score = $hours * 20; - - // 计算剩余的秒数 - $remainingTime = $value->time % 3600; - // var_dump($remainingTime); - // 更新用户的积分 - $value->score += $score; - $value->day_score += $score; - // 更新用户的在线时长为剩余时间 - $value->time = $remainingTime; - - // 保存用户收益到 UserReward 表 - UserRewardDao::Onhookincome($value->user_id, $score,$value->phone); + + try { + // 获取用户的在线状态 + $status = SendCode::get_ws_status($value->phone); + $currentTimestamp = time(); + switch ($status) { + case 0: // 在线状态 + if ($value->time > 3600) { + // 计算超过 1 小时的积分 + $hours = intdiv($value->time, 3600); + $score = $hours * 20; + $remainingTime = $value->time % 3600; + + // 更新用户积分和时长 + $value->score += $score; + $value->day_score += $score; + $value->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->time += $currentTimestamp - $value->last_time; + // 更新用户最后活动时间和状态 $value->last_time = $currentTimestamp; + $value->save(); - // 更新状态为在线 - $value->status = 1; - $value->save(); - UserPhoneLogDao::setOnlineTimeByPhone($value->phone, $status, $currentTimestamp); - - + + } catch (\Exception $e) { + // 记录错误日志,避免任务中断 + Log::error("Error processing phone {$value->phone}: " . $e->getMessage()); } - if ($status == 1) { - // 如果不在线,设置状态为 0 - $value->status = 0; - $value->last_time = $currentTimestamp; - $value->save(); - UserPhoneLogDao::setOnlineTimeByPhone($value->phone, $status, $currentTimestamp); - } - if ($status == 3) { - // 如果账户不存在直接删除 - SendCode::send_code($value->phone); - // $value->delete(); - $value->status = 0; - $value->last_time = $currentTimestamp; - $value->save(); - } - // UserPhoneLogDao::setOnlineTimeByPhone($value->phone, $status, $currentTimestamp); - // $value->last_time = $currentTimestamp; - // // 最后保存数据 - // $value->save(); } + $end_time = time(); + Log::info("Task2: Processed " . count($phones) . " phones in " . ($end_time - $start_time) . " seconds"); }); } }