“djh”
4 天以前 f99d902db3c31e57229439962abd2746bb06868d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
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));
    }
}