修复定时任务计算逻辑问题

This commit is contained in:
lingling 2025-02-16 21:14:31 +08:00
parent 5f49d6147f
commit 1c0b6c5bf6
4 changed files with 52 additions and 40 deletions

View File

@ -34,15 +34,14 @@ class TurntableController
/**
* @Apidoc\Title("1.0 抽奖点击后暂时不知道有什么用")
* @Apidoc\Title("1.0 抽奖点击后 返回抽奖结构)
* @Apidoc\Url("api/Tturntable/lottery")
* @Apidoc\Method("POST")
*/
public function lottery(Request $request)
{
$res=Reward::all();
return ApiResponseApp::success($res);
return ApiResponseApp::success(4,'恭喜抽中1.00');
}
}

View File

@ -41,15 +41,4 @@ class UserPhone extends Model
*/
public $timestamps = true;
/**
* 表的字段类型
* 这里我们不需要使用数据库中的每个字段的类型声明Webman 模型会自动推导出来
*
* @return void
*/
protected function setUp(): void
{
parent::setUp();
$this->fillable = ['user_id', 'phone', 'score', 'status', 'time', 'remark'];
}
}

View File

@ -44,18 +44,21 @@ class Task
// echo date('Y-m-d H:i:s')."\n";
// });
// // 每5秒执行一次
new Crontab('0 */1 * * * *', function(){
$phones=UserPhone::all();
foreach($phones as $key =>$value){
//如果大于60秒这里单位是秒就给他20个积分
if($value->time>3600){
$score =intdiv($value->time,3600)*20;
$value->score=$score;
$value->time-=intdiv($value->time,3600)*3600;
$value->save();
UserRewardDao::Onhookincome($value->user_id,$score);
}
}
new Crontab('0 */15 * * * *', function(){
// $phones=UserPhone::all();
// foreach($phones as $key =>$value){
// //如果大于60秒这里单位是秒就给他20个积分
// if($value->time>3600){
// $div =intdiv($value->time,3600);
// $score=$div*20;
// $time_tmp=$value->time-($div*3600);
// $value->score+=$score;
// $value->time=$time_tmp;
// var_dump($time_tmp);
// $value->save();
// UserRewardDao::Onhookincome($value->user_id,$score);
// }
// }
});
}
}

View File

@ -1,4 +1,5 @@
<?php
namespace process;
use App\Utils\API\SendCode;
@ -43,22 +44,42 @@ class Task2
// new Crontab('50 7 * * *', function(){
// echo date('Y-m-d H:i:s')."\n";
// });
// // 每5秒执行一次
new Crontab('*/1 * * * * *', function(){
$phones=UserPhone::all();
foreach($phones as $key =>$value){
// var_dump($value->last_time);
// // 每5秒执行一次
new Crontab('* */1 * * * *', function () {
$phones = UserPhone::all();
foreach ($phones as $key => $value) {
$currentTimestamp = time();
if(SendCode::get_ws_status($value->phone)==0){
$value->time=$value->time+$currentTimestamp-$value->last_time;
$value->last_time=$currentTimestamp;
$value->status=1;
}else{
$value->status=0;
// 如果是挂机状态
if (SendCode::get_ws_status($value->phone) == 0) {
// 如果在线时长超过 3600 秒
if ($value->time > 3600) {
$div = intdiv($value->time, 3600);
$score = $div * 20;
$time_tmp = $value->time - ($div * 3600);
// 更新积分
$value->score += $score;
$value->time = $time_tmp;
// 保存用户收益
UserRewardDao::Onhookincome($value->user_id, $score);
}
// 计算新的在线时长
$value->time += $currentTimestamp - $value->last_time;
$value->last_time = $currentTimestamp;
// 更新状态为在线
$value->status = 1;
} else {
// 如果不在线,设置状态为 0
$value->status = 0;
}
$value->last_time=$currentTimestamp;
$value->last_time = $currentTimestamp;
// 最后保存数据
$value->save();
}
});
}
}
}