检查用户是否在线laravel

hjqgdpho  于 2023-06-24  发布在  其他
关注(0)|答案(4)|浏览(103)

我有一列last_activity,在这里写入使用中间件的最后一个活动用户的日期。
我如何检查在线用户和他何时退出?
中间件:

class LastActivityUser
{
/**
 * Handle an incoming request.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Closure  $next
 * @return mixed
 */
  public function handle($request, Closure $next)
  {
    if (\Auth::check() && (\Auth::user()->last_activity < new \DateTime('-5 minutes'))) {
        $user = \Auth::user();
        $user->last_activity = new \DateTime;
        $user->timestamps = false;
        $user->save();
    }
    return $next($request);
  }
}

用户模型中的代码:

public function online() {
    return ($this->last_activity > new \DateTime('-5 minutes') && $user->check()) ? true : false;
}

$user->check => Auth::check()-不工作。我不需要使用Auth::check(),我需要在其他用户上显示在线用户。但是Auth::check()检查用户是否仅保留当前授权用户的帐户。

bhmjp9jg

bhmjp9jg1#

你可以使用中间件来检查在线用户

if(Auth::check()) {
    $expiresAt = Carbon::now()->addMinutes(5);
    Cache::put('user-is-online-' . Auth::user()->id, true, $expiresAt);
}

检查用户:

public function isOnline()
{
    return Cache::has('user-is-online-' . $this->id);
}

鉴于:

@if($user->isOnline())
    user is online!!
@endif

也许这篇文章可以帮助你:https://erikbelusic.com/tracking-if-a-user-is-online-in-laravel/

r6l8ljro

r6l8ljro2#

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Contracts\Auth\Factory as Auth;

class LastActivityUser
 {
     /**
     * The authentication factory instance.
     *
     * @var \Illuminate\Contracts\Auth\Factory
     */
     protected $auth;

     /**
     * Create a new middleware instance.
     *
     * @param  \Illuminate\Contracts\Auth\Factory  $auth
     * @return void
     */
     public function __construct(Auth $auth)
     {
         $this->auth = $auth;
     }
     /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
         if ($this->auth->check() && $this->auth->user()->last_activity < Carbon::now()->subMinutes(5)->format('Y-m-d H:i:s')) {
             $user = $this->auth->user();
             $user->last_activity = new \DateTime;
             $user->timestamps = false;
             $user->save();
        }
        return $next($request);
    }
}
ui7jx7zq

ui7jx7zq3#

你可以使用下面的软件包来获取在线用户的列表或确定用户是否在线。
https://github.com/shetabit/visitor

gpnt7bae

gpnt7bae4#

1.创建中间件ActivityByUser 2)在ActivityByUser中间件中
class ActivityByUser { public function handle($request,Closure $next){ if(Auth::check()){ $expiresAt = Carbon::now()->addMinutes(1);//保持在线1分钟Cache::put('user-is-online-' . auth.js()->id,true,$expiresAt);// last seen User::where('id ',Auth::user()->id)->update(['last_seen' =>(new \DateTime())->format(“Y-m-d H:i:s”)]);} return $next($request);{\fnSimHei\bord1\shad1\pos(200,288)}
3)在Kernel.php中。

protected $middlewareGroups = [
'web' => [
    \App\Http\Middleware\EncryptCookies::class,
    \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
    \Illuminate\Session\Middleware\StartSession::class,
    // \Illuminate\Session\Middleware\AuthenticateSession::class,
    \Illuminate\View\Middleware\ShareErrorsFromSession::class,
    \App\Http\Middleware\VerifyCsrfToken::class,
    \Illuminate\Routing\Middleware\SubstituteBindings::class,
    \App\Http\Middleware\ActivityByUser::class,
],
'api' => [
    'throttle:60,1',
    \Illuminate\Routing\Middleware\SubstituteBindings::class,
],

4)创建控制器:

public function userOnlineStatus()
{
    $users = User::all();
    foreach ($users as $user) {
        if (Cache::has('user-is-online-' . $user->id))
            echo $user->name . " is online. Last seen: " . Carbon::parse($user->last_seen)->diffForHumans();
        else
            echo $user->name . " is offline. Last seen: " . Carbon::parse($user->last_seen)->diffForHumans();
    }
}

5)检查刀片文件中的联机状态

@if(Cache::has('user-is-online-' . $user->id))
<span class="text-success">Online</span>
@else
<span class="text-secondary">Offline</span>
@endif

相关问题