package com.gkhy.exam.admin.controller.system;
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
import com.gkhy.exam.common.api.CommonResult;
|
import com.gkhy.exam.common.constant.UserConstant;
|
import com.gkhy.exam.common.domain.entity.SysMenu;
|
import com.gkhy.exam.common.utils.StringUtils;
|
import com.gkhy.exam.system.service.ISysMenuService;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.security.access.prepost.PreAuthorize;
|
import org.springframework.validation.annotation.Validated;
|
import org.springframework.web.bind.annotation.*;
|
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
import static com.gkhy.exam.common.utils.SecurityUtils.getUserId;
|
import static com.gkhy.exam.common.utils.SecurityUtils.getUsername;
|
|
/**
|
* 菜单信息
|
*
|
* @author expert
|
*/
|
@RestController
|
@RequestMapping("/system/menu")
|
public class SysMenuController
|
{
|
@Autowired
|
private ISysMenuService menuService;
|
|
/**
|
* 获取菜单列表
|
*/
|
//@PreAuthorize("hasAnyAuthority('system:menu:list')")
|
@GetMapping("/list")
|
public CommonResult list(SysMenu menu)
|
{
|
List<SysMenu> menus = menuService.selectMenuList(menu, getUserId());
|
return CommonResult.success(menus);
|
}
|
|
/**
|
* 根据菜单编号获取详细信息
|
*/
|
//@PreAuthorize("hasAnyAuthority('system:menu:query')")
|
@GetMapping(value = "/{menuId}")
|
public CommonResult getInfo(@PathVariable Long menuId)
|
{
|
return CommonResult.success(menuService.selectMenuById(menuId));
|
}
|
|
/**
|
* 获取菜单下拉树列表
|
*/
|
@GetMapping("/treeselect")
|
public CommonResult treeselect(SysMenu menu)
|
{
|
List<SysMenu> menus = menuService.selectMenuList(menu, getUserId());
|
return CommonResult.success(menuService.buildMenuTreeSelect(menus));
|
}
|
|
/**
|
* 加载对应角色菜单列表树
|
*/
|
@GetMapping(value = "/roleMenuTreeselect/{roleId}")
|
public CommonResult roleMenuTreeselect(@PathVariable("roleId") Long roleId)
|
{
|
List<SysMenu> menus = menuService.selectMenuList(getUserId());
|
Map<String, Object> ajax = new HashMap<String, Object>();
|
ajax.put("checkedKeys", menuService.selectMenuListByRoleId(roleId));
|
ajax.put("menus", menuService.buildMenuTreeSelect(menus));
|
return CommonResult.success(ajax);
|
}
|
|
/**
|
* 新增菜单
|
*/
|
//@PreAuthorize("hasAnyAuthority('system:menu:add')")
|
@PostMapping
|
public CommonResult add(@Validated @RequestBody SysMenu menu)
|
{
|
if (!menuService.checkMenuNameUnique(menu))
|
{
|
return CommonResult.failed("新增菜单'" + menu.getMenuName() + "'失败,菜单名称已存在");
|
}
|
else if (UserConstant.YES_FRAME.equals(menu.getIsFrame()) && !StringUtils.ishttp(menu.getPath()))
|
{
|
return CommonResult.failed("新增菜单'" + menu.getMenuName() + "'失败,地址必须以http(s)://开头");
|
}
|
menu.setCreateBy(getUsername());
|
return CommonResult.success(menuService.insertMenu(menu));
|
}
|
|
/**
|
* 修改菜单
|
*/
|
//@PreAuthorize("hasAnyAuthority('system:menu:edit')")
|
|
@PutMapping
|
public CommonResult edit(@Validated @RequestBody SysMenu menu)
|
{
|
if (!menuService.checkMenuNameUnique(menu))
|
{
|
return CommonResult.failed("修改菜单'" + menu.getMenuName() + "'失败,菜单名称已存在");
|
}
|
else if (UserConstant.YES_FRAME.equals(menu.getIsFrame()) && !StringUtils.ishttp(menu.getPath()))
|
{
|
return CommonResult.failed("修改菜单'" + menu.getMenuName() + "'失败,地址必须以http(s)://开头");
|
}
|
else if (menu.getMenuId().equals(menu.getParentId()))
|
{
|
return CommonResult.failed("修改菜单'" + menu.getMenuName() + "'失败,上级菜单不能选择自己");
|
}
|
menu.setUpdateBy(getUsername());
|
return CommonResult.success(menuService.updateMenu(menu));
|
}
|
|
/**
|
* 删除菜单
|
*/
|
//@PreAuthorize("hasAnyAuthority('system:menu:remove')")
|
@DeleteMapping("/{menuId}")
|
public CommonResult remove(@PathVariable("menuId") Long menuId)
|
{
|
if (menuService.hasChildByMenuId(menuId))
|
{
|
return CommonResult.failed("存在子菜单,不允许删除");
|
}
|
if (menuService.checkMenuExistRole(menuId))
|
{
|
return CommonResult.failed("菜单已分配,不允许删除");
|
}
|
return CommonResult.success(menuService.deleteMenuById(menuId));
|
}
|
}
|