feat: 实现挂机修仙、排行榜、大厅重构与全站留言板系统
- (Phase 8) 后台各维度管理与配置 - (Phase 9) 全自动静默挂机修仙升级 - (Phase 9) 四大维度风云排行榜页面 - (Phase 10) 全站留言板与悄悄话私信功能 - 运行 Pint 代码格式化
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* 文件功能:用户等级与权限辅助获取服务
|
||||
*
|
||||
* @author ChatRoom Laravel
|
||||
*
|
||||
* @version 1.0.0
|
||||
*/
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
class UserLevelService
|
||||
{
|
||||
/**
|
||||
* 安全地获取用户的当前权限等级。
|
||||
* 结合 Redis 缓存,防止每次判断权限都产生额外的 MySQL 查询。
|
||||
*
|
||||
* @param string $username 目标查询的用户名
|
||||
* @return int 用户等级,如果没有查到默认为 1
|
||||
*/
|
||||
public function getUserLevel(string $username): int
|
||||
{
|
||||
if (empty($username)) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
return (int) Cache::remember("user_level:{$username}", 60, function () use ($username) {
|
||||
$level = User::where('username', $username)->value('user_level');
|
||||
|
||||
return $level !== null ? $level : 1;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 当用户等级被管理员修改时,调用此方法清空其对应缓存。
|
||||
*
|
||||
* @param string $username 用户名
|
||||
*/
|
||||
public function forgetUserLevel(string $username): void
|
||||
{
|
||||
Cache::forget("user_level:{$username}");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user