server/app/Model/Online.php

96 lines
2.8 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Model;
use App\Utils\Ip;
use Swoole\Http\Request;
/**
* @property int $online_id
* @property string $session_id
* @property int $account_type
* @property int $belong_id
* @property int $account_id
* @property int $fd
* @property int $status
* @property string $ip
* @property string $ua
* @property string $online_time
* @property string $offline_time
*/
class Online extends Model
{
/**
* The table associated with the model.
*/
protected ?string $table = 'online';
/**
* The attributes that are mass assignable.
*/
protected array $fillable = [];
/**
* The attributes that should be cast to native types.
*/
protected array $casts = ['online_id' => 'integer', 'account_type' => 'integer', 'belong_id' => 'integer', 'account_id' => 'integer', 'fd' => 'integer', 'status' => 'integer'];
public static function list(array $param)
{
return self::where("status", 1)
->with(["account" => function ($query) {
$query->select(["account_id", "username", 'dept_id']);
}, "account.dept" => function ($query) {
$query->select(["dept_id", "dept_name"]);
}])
->when(isset($param['username']) && $param['username'] != '', function ($q) use ($param) {
$q->where('username', 'like', "%{$param['username']}%");
})
->orderByDesc("online_id")
->select(['session_id', 'username', 'ip', 'ua', 'online_time', 'account_id'])
->paginate((int)$param['limit']);
}
public static function closeAll(): int
{
return self::where("status", 1)
->where("online_time", ">", date("Y-m-d H:i:s", time() - 24 * 60 * 60))
->update(['status' => 0, 'offline_time' => date("Y-m-d H:i:s")]);
}
public static function leave(string $uuid): int
{
return self::where('session_id', $uuid)
->update(['status' => 0, 'offline_time' => date("Y-m-d H:i:s")]);
}
public static function live(string $uuid, int $fd): int
{
return self::where("session_id", $uuid)
->where("status", 1)
->update(['fd' => $fd, 'update_time' => date("Y-m-d H:i:s")]);
}
public static function reLive(string $uuid): bool
{
$online = self::getByUuid($uuid);
if ($online['status'] == 0) {
return (bool)self::where("session_id", $uuid)
->update(['update_time' => date("Y-m-d H:i:s"), 'status' => 1]);
}
return true;
}
public static function getByUuid(string $uuid)
{
return self::where("session_id", $uuid)->first(['status', 'session_id', 'account_id']);
}
public function account()
{
return $this->hasOne(Account::class, 'account_id', 'account_id');
}
}