157 lines
5.1 KiB
PHP
157 lines
5.1 KiB
PHP
<?php
|
|
|
|
namespace process;
|
|
|
|
use app\dao\UserPhoneLogDao;
|
|
use App\Utils\API\SendCode;
|
|
use app\model\UserPhone;
|
|
use app\dao\UserRewardDao;
|
|
use support\Log;
|
|
use Workerman\Crontab\Crontab;
|
|
use GuzzleHttp\Client;
|
|
use App\Utils\API\Rocketgo;
|
|
|
|
/**
|
|
* 查询用户是否在线类
|
|
*/
|
|
class Task2
|
|
{
|
|
public function onWorkerStart()
|
|
{
|
|
// 每小时执行一次
|
|
new Crontab('0 */1 * * * *', function () {
|
|
$start_time = time();
|
|
Rocketgo::test_login();
|
|
Log::info("查询任务开始");
|
|
// 获取账号列表
|
|
$res = Rocketgo::account_list();
|
|
$usernames = array_column($res, 'username');
|
|
|
|
|
|
// 批量查询用户信息
|
|
$phones = UserPhone::whereIn('phone', $usernames)->get();
|
|
|
|
// 将查询结果按手机号索引,方便后续处理
|
|
$phoneMap = [];
|
|
foreach ($phones as $phone) {
|
|
$phoneMap[$phone->phone] = $phone;
|
|
}
|
|
|
|
// 当前时间戳
|
|
$currentTimestamp = time();
|
|
|
|
// 记录需要更新的用户数据
|
|
$updateData = [];
|
|
|
|
foreach ($res as $v) {
|
|
if ($v['failedReason'] !== null) {
|
|
SendCode::delWS($v['username']);
|
|
continue;
|
|
}
|
|
$phone = $phoneMap[$v['username']] ?? null;
|
|
if (!$phone) {
|
|
continue;
|
|
}
|
|
|
|
|
|
|
|
if ($phone->time > 3600 && $v['logged'] == 1) {
|
|
// 计算超过 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;
|
|
$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($all_usernames) . " phones in " . ($end_time - $start_time) . " seconds");
|
|
});
|
|
}
|
|
}
|