webman/process/Task2.php

111 lines
3.9 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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()
{
// // 每秒钟执行一次
// new Crontab('*/1 * * * * *', function(){
// echo date('Y-m-d H:i:s')."\n";
// });
// // 每5秒执行一次
// new Crontab('*/5 * * * * *', function(){
// echo date('Y-m-d H:i:s')."\n";
// });
// // 每分钟执行一次
// new Crontab('0 */1 * * * *', function(){
// echo date('Y-m-d H:i:s')."\n";
// });
// // 每5分钟执行一次
// new Crontab('0 */5 * * * *', function(){
// echo date('Y-m-d H:i:s')."\n";
// });
// // 每分钟的第一秒执行
// new Crontab('1 * * * * *', function(){
// echo date('Y-m-d H:i:s')."\n";
// });
// // 每天的7点50执行注意这里省略了秒位
// new Crontab('50 7 * * *', function(){
// echo date('Y-m-d H:i:s')."\n";
// });
// // 每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->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();
}
});
}
}