智慧教务系统
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

261 lines
7.5 KiB

<?php
// +----------------------------------------------------------------------
// | Niucloud-admin 企业快速开发的多应用管理平台
// +----------------------------------------------------------------------
// | 官方网址:https://www.niucloud.com
// +----------------------------------------------------------------------
// | niucloud团队 版权所有 开源版本可自由商用
// +----------------------------------------------------------------------
// | Author: Niucloud Team
// +----------------------------------------------------------------------
namespace app\service\admin\sys;
use app\model\sys\SysMenu;
use core\base\BaseAdminService;
use core\exception\AdminException;
use think\facade\Db;
/**
* 系统菜单服务类
*/
class SysMenuService extends BaseAdminService
{
public function __construct()
{
parent::__construct();
$this->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('菜单不存在');
}
Db::startTrans();
try {
// 递归删除所有子菜单
$this->deleteMenuRecursive($model->menu_key);
Db::commit();
return true;
} catch (\Exception $e) {
Db::rollback();
throw new AdminException('删除菜单失败:' . $e->getMessage());
}
}
/**
* 递归删除菜单及其子菜单
* @param string $menuKey
* @return void
*/
private function deleteMenuRecursive(string $menuKey)
{
// 获取当前菜单信息
$currentMenu = $this->model->where('menu_key', $menuKey)->find();
if (!$currentMenu) {
return;
}
// 查找所有子菜单
$childMenus = $this->model->where('parent_key', $menuKey)->select();
// 递归删除所有子菜单
foreach ($childMenus as $childMenu) {
$this->deleteMenuRecursive($childMenu->menu_key);
}
// 删除角色菜单权限关联
Db::execute("DELETE FROM role_menu_permissions WHERE menu_id = ?", [$currentMenu->id]);
// 删除当前菜单
$currentMenu->delete();
}
/**
* 修改菜单状态
* @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());
}
}
}