超级总管 - 管理员列表返工

This commit is contained in:
柳清爽
2025-04-17 15:33:49 +08:00
parent d7d9f6e1bb
commit 61d2dbfeee
7 changed files with 356 additions and 65 deletions

View File

@@ -0,0 +1,49 @@
<?php
namespace app\common\model;
use think\Model;
/**
* 超级管理员权限配置模型类
*/
class AdministratorPermissions extends Model
{
// 设置数据表名
protected $name = 'administrator_permissions';
/**
* 保存管理员权限
* @param int $adminId 管理员ID
* @param array $permissionIds 权限ID数组
* @return bool
*/
public static function savePermissions($adminId, $permissionIds)
{
// 检查是否已有记录
$record = self::where('adminId', $adminId)->find();
// 准备权限数据
$permissionData = [
'ids' => is_array($permissionIds) ? implode(',', $permissionIds) : $permissionIds
];
if ($record) {
// 更新已有记录
return $record->save([
'permissions' => json_encode($permissionData),
'updateTime' => time()
]);
} else {
// 创建新记录
return self::create([
'adminId' => $adminId,
'permissions' => json_encode($permissionData),
'createTime' => time(),
'updateTime' => time(),
'deleteTime' => 0
]);
}
}
}

View File

@@ -0,0 +1,126 @@
<?php
namespace app\common\model;
use think\Model;
use think\facade\Cache;
/**
* 菜单模型类
*/
class Menu extends Model
{
// 设置数据表名
protected $name = 'menus';
/**
* 获取所有菜单,并组织成树状结构
* @param bool $onlyEnabled 是否只获取启用的菜单
* @param bool $useCache 是否使用缓存
* @return array
*/
public static function getMenuTree($onlyEnabled = true, $useCache = true)
{
$cacheKey = 'superadmin_menu_tree' . ($onlyEnabled ? '_enabled' : '_all');
// 查询条件
$where = [];
if ($onlyEnabled) {
$where[] = ['status', '=', 1];
}
// 获取所有菜单
$allMenus = self::where($where)
->order('sort', 'asc')
->select()
->toArray();
// 组织成树状结构
$menuTree = self::buildMenuTree($allMenus);
// 缓存结果
if ($useCache) {
Cache::set($cacheKey, $menuTree, 3600); // 缓存1小时
}
return $menuTree;
}
/**
* 构建菜单树
* @param array $menus 所有菜单
* @param int $parentId 父菜单ID
* @return array
*/
private static function buildMenuTree($menus, $parentId = 0)
{
$tree = [];
foreach ($menus as $menu) {
if ($menu['parent_id'] == $parentId) {
$children = self::buildMenuTree($menus, $menu['id']);
if (!empty($children)) {
$menu['children'] = $children;
}
$tree[] = $menu;
}
}
return $tree;
}
/**
* 根据权限ID获取相应的菜单树
* @param array $permissionIds 权限ID数组
* @param bool $onlyEnabled 是否只获取启用的菜单
* @return array
*/
public static function getMenuTreeByPermissions($permissionIds, $onlyEnabled = true)
{
// 如果没有权限,返回空数组
if (empty($permissionIds)) {
return [];
}
// 查询条件
$where = [];
if ($onlyEnabled) {
$where[] = ['status', '=', 1];
}
// 获取所有一级菜单(用户拥有权限的)
$topMenus = self::where($where)
->where('parent_id', 0)
->whereIn('id', $permissionIds)
->order('sort', 'asc')
->select()
->toArray();
// 菜单ID集合用于获取子菜单
$menuIds = array_column($topMenus, 'id');
// 获取所有子菜单
$childMenus = self::where($where)
->where('parent_id', 'in', $menuIds)
->order('sort', 'asc')
->select()
->toArray();
// 将子菜单按照父ID进行分组
$childMenusGroup = [];
foreach ($childMenus as $menu) {
$childMenusGroup[$menu['parent_id']][] = $menu;
}
// 构建菜单树
$menuTree = [];
foreach ($topMenus as $topMenu) {
// 添加子菜单
if (isset($childMenusGroup[$topMenu['id']])) {
$topMenu['children'] = $childMenusGroup[$topMenu['id']];
}
$menuTree[] = $topMenu;
}
return $menuTree;
}
}

View File

@@ -61,6 +61,7 @@ class GetDeviceListV1Controller extends BaseController
/**
* 获取设备列表
*
* @param array $where 查询条件
* @param int $page 页码
* @param int $limit 每页数量

View File

@@ -18,7 +18,7 @@ Route::group('', function () {
// 管理员相关路由
Route::group('administrator', function () {
Route::get('list', 'app\\superadmin\\controller\\AdministratorController@getList');
Route::get('list', 'app\\superadmin\\controller\\administrator\\GetAdministratorListController@index');
Route::get('detail/:id', 'app\\superadmin\\controller\\AdministratorController@getDetail');
Route::post('update', 'app\\superadmin\\controller\\AdministratorController@updateAdmin');
Route::post('add', 'app\\superadmin\\controller\\AdministratorController@addAdmin');

View File

@@ -1,9 +1,9 @@
<?php
namespace app\superadmin\controller;
use app\superadmin\model\Administrator as AdminModel;
use app\superadmin\model\AdministratorPermissions;
use think\Controller;
use app\superadmin\model\Administrator as AdminModel;
/**
* 管理员控制器

View File

@@ -1,63 +0,0 @@
<?php
namespace app\superadmin\controller;
use think\Controller;
use app\superadmin\model\Administrator;
class AuthController extends Controller
{
/**
* 管理员登录
* @return \think\response\Json
*/
public function login()
{
if (!$this->request->isPost()) {
return json(['code' => 405, 'msg' => '请求方法不允许']);
}
$account = $this->request->post('account');
$password = $this->request->post('password');
if (empty($account) || empty($password)) {
return json(['code' => 400, 'msg' => '账号和密码不能为空']);
}
$admin = Administrator::login($account, $password);
if (!$admin) {
return json(['code' => 401, 'msg' => '账号或密码错误']);
}
// 更新登录信息
$admin->lastLoginTime = time();
$admin->lastLoginIp = $this->request->ip();
$admin->save();
// 设置登录Cookie有效期24小时
cookie('admin_id', $admin->id, 86400);
cookie('admin_token', $this->createToken($admin), 86400);
return json([
'code' => 200,
'msg' => '登录成功',
'data' => [
'id' => $admin->id,
'name' => $admin->name,
'account' => $admin->account,
'token' => cookie('admin_token')
]
]);
}
/**
* 创建登录令牌
* @param AdministratorController $admin
* @return string
*/
private function createToken($admin)
{
$data = $admin->id . '|' . $admin->account;
return md5($data . 'cunkebao_admin_secret');
}
}

View File

@@ -0,0 +1,178 @@
<?php
namespace app\superadmin\controller\administrator;
use app\common\model\Administrator as AdministratorModel;
use app\common\model\AdministratorPermissions as AdministratorPermissionsModel;
use app\common\model\Menu as MenuModel;
use think\Controller;
/**
* 管理员控制器
*/
class GetAdministratorListController extends Controller
{
/**
* 构建查询条件
*
* @param array $params
* @return array
*/
protected function makeWhere(array $params = []): array
{
$where = [['deleteTime', '=', 0]];
// 如果有搜索关键词
if (!empty($keyword = $this->request->param('keyword/s', ''))) {
$where[] = ['account|name', 'like', "%{$keyword}%"];
}
return array_merge($params, $where);
}
/**
* 获取管理员列表
*
* @param array $where 查询条件
* @param int $page 页码
* @param int $limit 每页数量
* @return \think\Paginator 分页对象
*/
protected function getAdministratorList(array $where): \think\Paginator
{
$query = AdministratorModel::alias('a')
->field(
'id, account, name, status, authId, createTime createdAt, lastLoginTime, lastLoginIp'
);
foreach ($where as $key => $value) {
if (is_numeric($key) && is_array($value) && isset($value[0]) && $value[0] === 'exp') {
$query->whereExp('', $value[1]);
continue;
}
$query->where($key, $value);
}
return $query->paginate($this->request->param('limit/d', 10), false, ['page' => $this->request->param('page/d', 1)]);
}
/**
* 根据权限ID获取角色名称
*
* @param int $authId 权限ID
* @return string
*/
protected function getRoleName($authId): string
{
switch ($authId) {
case 1:
return '超级管理员';
case 2:
return '项目管理员';
case 3:
return '客户管理员';
default:
return '普通管理员';
}
}
/**
* 获取管理员权限
*
* @param int $adminId
* @return array
*/
protected function _getPermissions(int $adminId): array
{
$record = AdministratorPermissionsModel::where('adminId', $adminId)->find();
if (!$record || empty($record->permissions)) {
return [];
}
$permissions = $record->permissions ? json_decode($record->permissions, true) : [];
if (isset($permissions['ids']) && !empty($permissions['ids'])) {
return is_string($permissions['ids']) ? explode(',', $permissions['ids']) : $permissions['ids'];
}
return [];
}
/**
* 通过菜单的id获取菜单的名字
*
* @param array $ids
* @return array
*/
protected function getMenusNameByIds(array $ids): array
{
return MenuModel::whereIn('id', $ids)->column('title');
}
/**
* 根据权限ID获取权限列表
*
* @param int $authId 权限ID
* @return array
*/
protected function getPermissions($authId): array
{
$ids = $this->_getPermissions($authId);
if ($ids) {
return $this->getMenusNameByIds($ids);
}
return [];
}
/**
* 构建返回数据
*
* @param \think\Paginator $list
* @return array
*/
protected function makeReturnedResult(\think\Paginator $list): array
{
$result = [];
foreach ($list->items() as $item) {
$section = [
'id' => $item->id,
'username' => $item->account,
'name' => $item->name,
'role' => $this->getRoleName($item->authId),
'status' => $item->status,
'lastLogin' => !empty($item->lastLoginTime) ? date('Y-m-d H:i:s', $item->lastLoginTime) : '从未登录',
'permissions' => $this->getPermissions($item->id)
];
array_push($result, $section);
}
return $result;
}
/**
* 获取管理员列表
*
* @return \think\response\Json
*/
public function index()
{
$where = $this->makeWhere();
$result = $this->getAdministratorList($where);
return json([
'code' => 200,
'msg' => '获取成功',
'data' => [
'list' => $this->makeReturnedResult($result),
'total' => $result->total(),
]
]);
}
}