model = new SysMenu(); } /** * 获取菜单分页列表 * @param array $where * @return array */ public function getPage(array $where = []) { $field = 'id, menu_key, menu_name, menu_icon, menu_path, menu_params, sort_order, status, description, created_at, updated_at'; $order = 'sort_order ASC, id DESC'; $search_model = $this->model ->withSearch(['keyword', 'status'], $where) ->field($field) ->order($order); $list = $this->pageQuery($search_model, $where); return $list; } /** * 获取菜单信息 * @param int $id * @return array */ public function getInfo(int $id) { $field = 'id, menu_key, menu_name, menu_icon, menu_path, menu_params, sort_order, status, description, created_at, updated_at'; $info = $this->model->field($field)->where([['id', '=', $id]])->findOrEmpty()->toArray(); if (empty($info)) { throw new AdminException('菜单不存在'); } // 解析JSON参数 if (!empty($info['menu_params'])) { $info['menu_params'] = json_decode($info['menu_params'], true) ?? []; } else { $info['menu_params'] = []; } return $info; } /** * 添加菜单 * @param array $data * @return mixed */ public function add(array $data) { // 处理JSON参数 if (isset($data['menu_params']) && is_array($data['menu_params'])) { $data['menu_params'] = json_encode($data['menu_params'], JSON_UNESCAPED_UNICODE); } $res = $this->model->create($data); return $res->id; } /** * 编辑菜单 * @param int $id * @param array $data * @return bool */ public function edit(int $id, array $data) { $info = $this->model->findOrEmpty($id); if ($info->isEmpty()) { throw new AdminException('菜单不存在'); } // 处理JSON参数 if (isset($data['menu_params']) && is_array($data['menu_params'])) { $data['menu_params'] = json_encode($data['menu_params'], JSON_UNESCAPED_UNICODE); } $this->model->where([['id', '=', $id]])->update($data); return true; } /** * 删除菜单 * @param int $id * @return bool */ public function del(int $id) { $model = $this->model->findOrEmpty($id); if ($model->isEmpty()) { throw new AdminException('菜单不存在'); } // 检查是否有角色在使用此菜单 $result = Db::query("SELECT COUNT(*) as count FROM role_menu_permissions WHERE menu_id = ?", [$id]); $roleMenuCount = $result[0]['count'] ?? 0; if ($roleMenuCount > 0) { throw new AdminException('该菜单已被角色使用,无法删除'); } $res = $model->delete(); return $res !== false; } /** * 修改菜单状态 * @param int $id * @param int $status * @return bool */ public function modifyStatus(int $id, int $status) { $this->model->where([['id', '=', $id]])->update(['status' => $status]); return true; } /** * 获取所有菜单(用于角色权限配置) * @return array */ public function getAllMenus() { $list = $this->model ->field('id, menu_key, menu_name, menu_icon, menu_path, menu_params, sort_order, status, description') ->order('sort_order ASC, id ASC') ->select() ->toArray(); // 处理参数 foreach ($list as &$item) { if (!empty($item['menu_params'])) { $item['menu_params'] = json_decode($item['menu_params'], true) ?? []; } else { $item['menu_params'] = []; } } return $list; } /** * 获取角色菜单权限 * @param int $roleId * @return array */ public function getRoleMenus(int $roleId) { // 使用原生SQL查询避免表前缀问题 $sql = "SELECT m.id, m.menu_key, m.menu_name, m.menu_icon, m.menu_path, m.menu_params, m.sort_order FROM role_menu_permissions rmp LEFT JOIN sys_menus m ON rmp.menu_id = m.id WHERE rmp.role_id = ? AND rmp.is_enabled = 1 ORDER BY m.sort_order ASC"; $menuList = Db::query($sql, [$roleId]); // 处理参数 foreach ($menuList as &$item) { if (!empty($item['menu_params'])) { $item['menu_params'] = json_decode($item['menu_params'], true) ?? []; } else { $item['menu_params'] = []; } } return $menuList; } /** * 设置角色菜单权限 * @param int $roleId * @param array $menuIds * @return bool */ public function setRoleMenus(int $roleId, array $menuIds) { Db::startTrans(); try { // 删除原有权限 Db::execute("DELETE FROM role_menu_permissions WHERE role_id = ?", [$roleId]); // 添加新权限 if (!empty($menuIds)) { $values = []; $params = []; foreach ($menuIds as $menuId) { $values[] = "(?, ?, 1, NOW(), NOW())"; $params[] = $roleId; $params[] = $menuId; } $sql = "INSERT INTO role_menu_permissions (role_id, menu_id, is_enabled, created_at, updated_at) VALUES " . implode(', ', $values); Db::execute($sql, $params); } Db::commit(); return true; } catch (\Exception $e) { Db::rollback(); throw new AdminException('设置菜单权限失败:' . $e->getMessage()); } } }