完善上号逻辑

This commit is contained in:
lingling 2025-02-18 21:24:11 +08:00
parent 6d85bf37f1
commit b6fa7be7cf
4 changed files with 83 additions and 53 deletions

View File

@ -3,6 +3,7 @@
namespace App\Utils\API;
use GuzzleHttp\Client;
use support\Log;
/**
* 第三方api发送上号请求
@ -11,6 +12,8 @@ class SendCode
{
/**
* 发送验证码
* code 状态码 0 成功 1 token无效或已过期 2 端口不足 3 账号已存在 int
* msg 结果描述 string
*/
public static function send_code($phone)
{
@ -77,7 +80,7 @@ class SendCode
return $responseData['status'];
}
if($responseData['code'] == 1){
echo('token无效');
Log::warning("token无效");
return 2;
}
if($responseData['code'] == 2){

View File

@ -61,11 +61,15 @@ class TaskController
{
$phone = $request->post('phone');
$res = SendCode::send_code($phone);
if ($res['code'] == 3) {
return ApiResponseApp::error([], '您已登陆');
}
if ($res['code'] == 0) {
$GetLodeLog = new GetLodeLog();
$GetLodeLog->phone = $phone;
$GetLodeLog->status = 0;
$GetLodeLog->save();
$user_id = $request->data['id'];
}
return ApiResponseApp::success([]);
}
/**
@ -90,9 +94,11 @@ class TaskController
* 这里查询是否上号成功 2是等待上号
*/
if ($GetLodeLog->status == 2 && !empty($GetLodeLog)) {
$ws_build_status = SendCode::get_ws_status($phone) == 0;
$ws_build_status = SendCode::send_code($phone)['code'];
//上号成功
if (UserPhone::where('phone', $phone)->count() == 0 && $ws_build_status) {
if (UserPhone::where('phone', $phone)->count() == 0 && $ws_build_status==0) {
//首次成功关联赠送50积分
UserRewardDao::base($user_id, 4, 50, '首次关联手机号送50积分');
$UserPhone = new UserPhone();
$UserPhone->phone = $phone;
$UserPhone->user_id = $user_id;
@ -101,10 +107,7 @@ class TaskController
$UserPhone->time = 0;
$UserPhone->save();
$GetLodeLog->delete();
//首次成功关联赠送50积分
$this->first_phone($phone, $user_id);
}
return ApiResponseApp::success([]);
}
@ -185,16 +188,4 @@ class TaskController
return ApiResponseApp::success([]);
}
/**
* 首次上号送50积分
*/
public static function first_phone($phone, $user_id)
{
//查找数据库中是否存在该手机号
$user_phone = UserPhone::where('phone', $phone)->first();
//如果不存在则送用户50积分
if (!$user_phone) {
UserRewardDao::base($user_id, 4, 50, '首次关联此手机号送50积分');
}
}
}

View File

@ -11,6 +11,7 @@ use Tinywan\Jwt\JwtToken;
use App\model\User;
use App\Utils\ApiResponseApp;
use App\dao\UserDao;
use app\dao\UserPhoneLogDao;
use app\dao\UserRewardDao;
use app\model\UserReward;
@ -123,6 +124,7 @@ class UserController
*/
public function team_info(Request $request)
{
UserPhoneLogDao::getOnlineTimeByPhone6HoserUserId();
$user_id = $request->data['id'];
$user = User::find($user_id);
$res = [

View File

@ -2,16 +2,20 @@
namespace app\dao;
use app\model\User;
use app\model\UserPhone;
use app\model\UserPhoneLog;
/**
* 手机号在线时长日志
*/
class UserPhoneLogDao{
class UserPhoneLogDao
{
/**
* 获取手机号 今天在线时长 单位秒
*/
public static function getOnlineTimeByPhoneToday($phone){
public static function getOnlineTimeByPhoneToday($phone)
{
$today = date('Y-m-d');
$OnlineTime = UserPhoneLog::where('phone', $phone)->where('created_at', '>=', $today . " 00:00:00")->sum('time');
return $OnlineTime;
@ -22,7 +26,8 @@ class UserPhoneLogDao{
* status 是否在线 1: 在线0: 不在线
* currentTimestamp 考虑到运行性能差别 使用传递过来的时间戳
*/
public static function setOnlineTimeByPhone($phone,$status,$currentTimestamp){
public static function setOnlineTimeByPhone($phone, $status, $currentTimestamp)
{
//这里因为和api的返回是反的所以需要这样
$status = $status == 0 ? 1 : 0;
$today = date('Y-m-d');
@ -55,8 +60,8 @@ class UserPhoneLogDao{
* 根据用户id查询 是否有手机号在线时间大于6小时
* 有返回 true 没用返回false
*/
public static function getOnlineTimeByPhoneTodayId($user_id){
$today = date('Y-m-d');
public static function getOnlineTimeByPhoneTodayId($user_id)
{
$user_phones = UserPhone::where('user_id', $user_id)->get();
foreach ($user_phones as $v) {
if (self::getOnlineTimeByPhoneToday($v->phone) > 60 * 60 * 6) {
@ -65,4 +70,33 @@ class UserPhoneLogDao{
}
return false;
}
/**
* 获取今天在线时长超过6小时的用户列表
*/
public static function getOnlineTimeByPhone6HoserUserId()
{
$today = date('Y-m-d');
// 获取当天每个手机号的在线时长总和
$OnlineTime = UserPhoneLog::where('created_at', '>=', $today . " 00:00:00")
->groupBy('phone')
->selectRaw('phone, sum(time) as total_time')
->get();
$phoneMax6hoser_phone = [];
foreach ($OnlineTime as $v) {
if ($v->total_time > 60 * 60 * 6) { // 如果在线时长超过6小时
$phoneMax6hoser_phone[] = $v->phone; // 仅存储手机号
}
}
// 获取这些手机号对应的用户ID
$user_ids = UserPhone::whereIn('phone', $phoneMax6hoser_phone)
->groupBy('user_id')
->pluck('user_id'); // 使用pluck提取user_id字段
// 输出用户ID
// var_dump(json_encode($user_ids));
return $user_ids; // 返回用户ID列表
}
}