diff --git a/Server/application/common/model/AdministratorPermissions.php b/Server/application/common/model/AdministratorPermissions.php new file mode 100644 index 00000000..5c6f177e --- /dev/null +++ b/Server/application/common/model/AdministratorPermissions.php @@ -0,0 +1,49 @@ +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 + ]); + } + } + + +} \ No newline at end of file diff --git a/Server/application/common/model/Menu.php b/Server/application/common/model/Menu.php new file mode 100644 index 00000000..03c52dd3 --- /dev/null +++ b/Server/application/common/model/Menu.php @@ -0,0 +1,126 @@ +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; + } +} \ No newline at end of file diff --git a/Server/application/cunkebao/controller/device/GetDeviceListV1Controller.php b/Server/application/cunkebao/controller/device/GetDeviceListV1Controller.php index 33b14e1e..d344c9bd 100644 --- a/Server/application/cunkebao/controller/device/GetDeviceListV1Controller.php +++ b/Server/application/cunkebao/controller/device/GetDeviceListV1Controller.php @@ -61,6 +61,7 @@ class GetDeviceListV1Controller extends BaseController /** * 获取设备列表 + * * @param array $where 查询条件 * @param int $page 页码 * @param int $limit 每页数量 diff --git a/Server/application/superadmin/config/route.php b/Server/application/superadmin/config/route.php index 57c66a12..5b45bc97 100644 --- a/Server/application/superadmin/config/route.php +++ b/Server/application/superadmin/config/route.php @@ -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'); diff --git a/Server/application/superadmin/controller/AdministratorController.php b/Server/application/superadmin/controller/AdministratorController.php index ff1e17a3..64c32fa2 100644 --- a/Server/application/superadmin/controller/AdministratorController.php +++ b/Server/application/superadmin/controller/AdministratorController.php @@ -1,9 +1,9 @@ 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'); - } -} \ No newline at end of file diff --git a/Server/application/superadmin/controller/administrator/GetAdministratorListController.php b/Server/application/superadmin/controller/administrator/GetAdministratorListController.php new file mode 100644 index 00000000..b593b0c5 --- /dev/null +++ b/Server/application/superadmin/controller/administrator/GetAdministratorListController.php @@ -0,0 +1,178 @@ +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(), + ] + ]); + } + +} \ No newline at end of file