82 lines
3.0 KiB
PHP
82 lines
3.0 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;
|
|
|
|
/**
|
|
* 查询用户是否在线类
|
|
*/
|
|
class Task2
|
|
{
|
|
public function onWorkerStart()
|
|
{
|
|
|
|
// // 每5秒执行一次
|
|
new Crontab('0 */1 * * * *', function () {
|
|
$phones = UserPhone::all();
|
|
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);
|
|
}
|
|
|
|
// 计算新的在线时长
|
|
$value->time += $currentTimestamp - $value->last_time;
|
|
$value->last_time = $currentTimestamp;
|
|
|
|
// 更新状态为在线
|
|
$value->status = 1;
|
|
$value->save();
|
|
UserPhoneLogDao::setOnlineTimeByPhone($value->phone, $status, $currentTimestamp);
|
|
|
|
|
|
}
|
|
if ($status == 1) {
|
|
// 如果不在线,设置状态为 0
|
|
$value->status = 0;
|
|
$value->last_time = $currentTimestamp;
|
|
$value->save();
|
|
UserPhoneLogDao::setOnlineTimeByPhone($value->phone, $status, $currentTimestamp);
|
|
}
|
|
if ($status == 3) {
|
|
// 如果账户不存在直接删除
|
|
// $value->delete();
|
|
$value->status = 0;
|
|
$value->last_time = $currentTimestamp;
|
|
$value->save();
|
|
}
|
|
// UserPhoneLogDao::setOnlineTimeByPhone($value->phone, $status, $currentTimestamp);
|
|
// $value->last_time = $currentTimestamp;
|
|
// // 最后保存数据
|
|
// $value->save();
|
|
}
|
|
});
|
|
}
|
|
}
|