This commit is contained in:
Your Name
2026-11-26 15:46:20 +08:00
parent db6d327e91
commit be79e09c83
170 changed files with 21159 additions and 13217 deletions
@@ -0,0 +1,112 @@
<?php
declare(strict_types=1);
namespace app\adminapi\lists\setting\system;
use app\adminapi\lists\BaseAdminDataLists;
use app\common\lists\ListsExtendInterface;
use app\common\lists\Traits\HasDataScopeFilter;
use app\common\service\DataScope\DataScopeService;
use app\common\service\JsonService;
use DateTimeImmutable;
use DateTimeZone;
use think\db\Query;
use think\facade\Db;
class DesktopSessionLists extends BaseAdminDataLists implements ListsExtendInterface
{
use HasDataScopeFilter;
private Query $sessions;
private DateTimeImmutable $now;
private string $daySecondsSql;
private ?array $summary = null;
public function __construct()
{
parent::__construct();
$this->now = new DateTimeImmutable('now', new DateTimeZone('Asia/Shanghai'));
$date = $this->request->get('date', $this->now->format('Y-m-d'));
$day = is_string($date) && preg_match('/^\d{4}-\d{2}-\d{2}$/D', $date)
? DateTimeImmutable::createFromFormat('!Y-m-d', $date, $this->now->getTimezone())
: false;
if (!$day || $day->format('Y-m-d') !== $date) {
JsonService::throw('请选择有效的查询日期');
}
$keyword = $this->request->get('keyword', '');
if (!is_string($keyword)) {
JsonService::throw('请填写账号或姓名');
}
$start = $day->getTimestamp();
$end = $day->modify('+1 day')->getTimestamp();
$now = $this->now->getTimestamp();
// 所有会话最迟到下一次 05:00。用前一日下界限制登录时间索引的扫描范围。
$this->sessions = Db::name('desktop_login_session')
->where('login_time', '>=', $day->modify('-1 day')->getTimestamp())
->where('login_time', '<', $end)
->where('login_time', '<=', $now)
// 同秒登录退出也应保留在登录当日,午夜退出归入前一天。
->whereRaw("(login_time >= {$start} OR COALESCE(logout_time, {$now}) > {$start})");
$this->daySecondsSql = "LEAST(COALESCE(logout_time, {$now}), {$end}) - GREATEST(login_time, {$start})";
if (trim($keyword) !== '') {
$pattern = '%' . strtr(trim($keyword), ['!' => '!!', '%' => '!%', '_' => '!_']) . '%';
$this->sessions->whereRaw("(account LIKE ? ESCAPE '!' OR name LIKE ? ESCAPE '!')", [$pattern, $pattern]);
}
if ((int) ($this->adminInfo['root'] ?? 0) !== 1) {
$this->adminInfo['role_id'] = DataScopeService::roleIdsOf($this->adminId);
}
$this->applyDataScopeByOwner($this->sessions, 'admin_id');
}
public function lists(): array
{
$rows = (clone $this->sessions)
->field('id,admin_id,account,name,login_time,logout_time,end_reason,duration_seconds')
->fieldRaw("({$this->daySecondsSql}) AS day_seconds")
->order(['login_time' => 'desc', 'id' => 'desc'])
->limit($this->limitOffset, $this->limitLength)->select()->toArray();
foreach ($rows as &$row) {
$login = (int) $row['login_time'];
$logout = $row['logout_time'];
$row['status'] = $logout === null ? 'online' : $row['end_reason'];
$row['status_label'] = match ($row['status']) {
'online' => '在线',
'logout' => '已退出',
'replaced' => '重新登录',
'timeout' => '连接超时',
'daily_cutoff' => '每日下线',
'revoked' => '登录已撤销',
default => '已结束',
};
$row['duration_seconds'] = $logout === null
? $this->now->getTimestamp() - $login : (int) $row['duration_seconds'];
$row['day_seconds'] = (int) $row['day_seconds'];
$row['login_time'] = $this->now->setTimestamp($login)->format('Y-m-d H:i:s');
$row['logout_time'] = $logout === null
? null : $this->now->setTimestamp((int) $logout)->format('Y-m-d H:i:s');
unset($row['end_reason']);
}
return $rows;
}
private function summary(): array
{
return $this->summary ??= (clone $this->sessions)
->fieldRaw("COUNT(*) AS session_count, COALESCE(SUM({$this->daySecondsSql}), 0) AS day_seconds")
->find();
}
public function count(): int
{
return (int) $this->summary()['session_count'];
}
public function extend(): array
{
return ['total_day_seconds' => (int) $this->summary()['day_seconds']];
}
}