where('created_at', '>=', $today." 00:00:00")->sum('time'); return $OnlineTime; } /** * 自动处理time * phone 手机号 * status — 是否在线 1: 在线,0: 不在线 * currentTimestamp 考虑到运行性能差别 使用传递过来的时间戳 */ public static function setOnlineTimeByPhone($phone,$status,$currentTimestamp){ //这里因为和api的返回是反的所以需要这样 $status=$status==0?1:0; $today = date('Y-m-d'); // $UserPhoneLog=UserPhoneLog::where('phone',$phone)->where('created_at', '>=', $today." 00:00:00")->limit(2)->orderByDesc('id')->get(); $UserPhoneLog=UserPhoneLog::where('phone',$phone)->where('created_at', '>=', $today." 00:00:00")->orderBy('id', 'desc')->first(); if(empty($UserPhoneLog)){ $new_UserPhoneLog=new UserPhoneLog(); $new_UserPhoneLog->status=$status; $new_UserPhoneLog->phone=$phone; $new_UserPhoneLog->time=0; $new_UserPhoneLog->save(); return; } if($UserPhoneLog->status==1&&$status==1){ $new_UserPhoneLog=new UserPhoneLog(); $new_UserPhoneLog->status=$status; $new_UserPhoneLog->phone=$phone; $new_UserPhoneLog->time=$currentTimestamp-strtotime($UserPhoneLog->created_at); $new_UserPhoneLog->save(); echo($currentTimestamp-strtotime($UserPhoneLog->created_at)); }else{ $new_UserPhoneLog=new UserPhoneLog(); $new_UserPhoneLog->status=$status; $new_UserPhoneLog->phone=$phone; $new_UserPhoneLog->time=0; $new_UserPhoneLog->save(); } } /** * 根据用户id查询 是否有手机号在线时间大于6小时 * 有返回 true 没用返回false */ public static function getOnlineTimeByPhoneTodayId($user_id){ $today = date('Y-m-d'); $user_phones=UserPhone::where('user_id',$user_id)->get(); foreach($user_phones as $v){ if(self::getOnlineTimeByPhoneToday($v->phone)>60*60*6){ return true; } } return false; } }