Compare commits

...

2 Commits

2 changed files with 142 additions and 5 deletions

View File

@ -112,15 +112,15 @@ class PaymentNew
return json_decode($responseBody);
} catch (\GuzzleHttp\Exception\RequestException $e) {
Log::warning("请求支付api失未知失败:" . $e->getMessage());
// Log::warning("请求支付api失未知失败:" . $e->getMessage());
// 如果是 400 错误,可能有额外的请求错误信息
if ($e->hasResponse()) {
$response = $e->getResponse();
$httpCode = $response->getStatusCode();
if ($httpCode == 400) {
$responseBody = $response->getBody()->getContents();
Log::warning("请求支付api失败状态码 400响应体" . $responseBody);
}
$responseBody = $response->getBody()->getContents();
Log::warning("请求支付api失败状态码: $httpCode ,响应体:" . $responseBody);
}else{
Log::warning("请求支付api未知失败并且没有返回头:" . $e->getMessage());
}
return json_decode($responseBody);
}

137
app/dao/PhoneDao.php Normal file
View File

@ -0,0 +1,137 @@
<?php
namespace app\dao;
use app\dao\UserPhoneLogDao;
use app\model\UserPhone;
use app\dao\UserRewardDao;
use app\model\Dictionary;
use support\Log;
use App\Utils\API\Rocketgo;
/**
* 用户手机号在线操作类
* 暂时未启用方便测试重写的类并没有通过测试
*/
class PhoneDao
{
public static function main()
{
$start_time = time();
Rocketgo::test_login();
// 查询系统配置 是否自动删除用户
$autodelete = Dictionary::where('key', 'autodelete')->first();
Log::info("查询任务开始");
// 获取账号列表
$res = Rocketgo::account_list();
$usernames = array_column($res, 'username');
// 批量查询用户信息
$phones = UserPhone::whereIn('phone', $usernames)->get()->keyBy('phone');
$currentTimestamp = time();
$updateData = [];
$updateData2 = [];
// 批量处理用户信息
foreach ([$res, Rocketgo::account_storehouse_list()] as $index => $accountList) {
$phoneMap = ($index === 0) ? $phones : UserPhone::whereIn('phone', array_column($accountList, 'username'))->get()->keyBy('phone');
$allUsernames = array_column($accountList, 'username');
foreach ($accountList as $v) {
$phone = $phoneMap[$v['username']] ?? null;
if (!$phone) {
continue;
}
// 处理在线和离线状态
if ($v['logged'] == 1) {
self::processOnlineStatus($phone, $v, $currentTimestamp, $updateData);
} else {
self::processOfflineStatus($phone, $currentTimestamp, $updateData);
}
// 更新状态
if ($index === 0) {
$updateData[] = $phone;
} else {
$updateData2[] = $phone;
}
}
// 更新离线用户状态
UserPhone::whereNotIn('phone', $allUsernames)
->where('status', '!=', 2)
->update(['status' => 2, 'last_time' => $currentTimestamp]);
UserPhone::where('status', 2)->update(['last_time' => $currentTimestamp, 'time' => 0]);
}
// 批量更新用户状态
self::batchUpdatePhones($updateData);
self::batchUpdatePhones($updateData2);
$end_time = time();
Log::info("Task2: Processed " . count($usernames) . " phones in " . ($end_time - $start_time) . " seconds");
}
/**
* 处理用户在线状态
*/
private static function processOnlineStatus($phone, $v, $currentTimestamp, &$updateData)
{
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, 1, $currentTimestamp);
$phone->status = 1;
$phone->time += $currentTimestamp - $phone->last_time;
$phone->last_time = $currentTimestamp;
$updateData[] = $phone;
}
/**
* 处理用户离线状态
*/
private static function processOfflineStatus($phone, $currentTimestamp, &$updateData)
{
$phone->status = 0;
$phone->last_time = $currentTimestamp;
// 保存在线时长记录
UserPhoneLogDao::setOnlineTimeByPhone($phone->phone, 0, $currentTimestamp);
$updateData[] = $phone;
}
/**
* 批量更新用户状态
*/
private static function batchUpdatePhones($phones)
{
// 如果用户列表不为空,批量更新
if (!empty($phones)) {
UserPhone::whereIn('id', array_column($phones, 'id'))->update([
'status' => 1,
'time' => $phones[0]->time, // 更新时间等信息
'last_time' => $phones[0]->last_time,
]);
}
}
}