shikeyin
2024-01-11 65da8373531677b1c37a98f53eaa30c892f35e5a
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package com.iplatform.base.controller;
 
import com.iplatform.base.SystemController;
import com.iplatform.base.cache.MenuCacheProvider;
import com.iplatform.base.service.MenuServiceImpl;
import com.iplatform.base.service.RoleServiceImpl;
import com.iplatform.base.util.MenuUtils;
import com.iplatform.base.util.menu.SystemMenu;
import com.iplatform.model.po.S_menu;
import com.iplatform.model.po.S_user_core;
import com.walker.infrastructure.utils.DateUtils;
import com.walker.infrastructure.utils.StringUtils;
import com.walker.web.DataStatus;
import com.walker.web.ResponseValue;
import com.walker.web.UserPrincipal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
@RestController
@RequestMapping("/system/menu")
public class MenuController extends SystemController {
 
    private MenuCacheProvider menuCacheProvider;
    private RoleServiceImpl roleService;
    private MenuServiceImpl menuService;
 
    @Autowired
    public MenuController(MenuCacheProvider menuCacheProvider
            , RoleServiceImpl roleService, MenuServiceImpl menuService){
        this.menuCacheProvider = menuCacheProvider;
        this.roleService = roleService;
        this.menuService = menuService;
    }
 
    @RequestMapping("/remove/{menuId}")
    public ResponseValue remove(@PathVariable String menuId){
        boolean existChildren = this.menuCacheProvider.isHasChildren(menuId);
        if(existChildren){
            return ResponseValue.error("该菜单存在子菜单,无法删除");
        }
        int roleMenuSize = this.menuService.queryRoleMenuSize(menuId);
        if(roleMenuSize > 0){
            return ResponseValue.error("菜单已分配,无法删除");
        }
        this.menuService.delete(new S_menu(menuId));
        this.menuCacheProvider.removeCacheData(menuId);
        return ResponseValue.success();
    }
 
    @RequestMapping("/view/{menuId}")
    public ResponseValue view(@PathVariable String menuId){
        S_menu s_menu = this.menuService.get(new S_menu(menuId));
        if(s_menu == null){
            return ResponseValue.error("菜单不存在:" + menuId);
        }
        return ResponseValue.success(new SystemMenu(s_menu));
    }
 
    @RequestMapping("/edit")
    public ResponseValue saveEdit(@RequestBody S_menu s_menu){
        if(s_menu == null || StringUtils.isEmpty(s_menu.getParent_id()) || StringUtils.isEmpty(s_menu.getMenu_name())){
            return ResponseValue.error("参数错误");
        }
        S_menu exist = this.menuService.queryExistMenuInParent(s_menu.getParent_id(), s_menu.getMenu_name());
        if(exist != null && !exist.getMenu_id().equals(s_menu.getMenu_id())){
            return ResponseValue.error("父菜单下已有相同名称菜单存在:" + s_menu.getMenu_name());
        }
        if(s_menu.getIs_frame().intValue() == MenuUtils.MENU_FRAME_YES && !StringUtils.isHttpLink(s_menu.getPath())){
            return ResponseValue.error("地址必须以http(s)://开头");
        }
        if(s_menu.getMenu_type().equals(MenuUtils.MENU_TYPE_ITEM) && StringUtils.isEmpty(s_menu.getComponent())){
            return ResponseValue.error("菜单项必须填写:组件路径(component)");
        }
        this.menuService.save(s_menu);
        this.menuCacheProvider.updateCacheData(s_menu.getMenu_id(), s_menu);
        return ResponseValue.success();
    }
 
    @RequestMapping("/add")
    public ResponseValue saveAddMenu(@RequestBody S_menu s_menu){
        if(s_menu == null || StringUtils.isEmpty(s_menu.getParent_id()) || StringUtils.isEmpty(s_menu.getMenu_name())){
            return ResponseValue.error("参数错误");
        }
        S_menu exist = this.menuService.queryExistMenuInParent(s_menu.getParent_id(), s_menu.getMenu_name());
        if(exist != null){
            return ResponseValue.error("父菜单下已有相同名称菜单存在:" + s_menu.getMenu_name());
        }
        if(s_menu.getIs_frame().intValue() == MenuUtils.MENU_FRAME_YES && !StringUtils.isHttpLink(s_menu.getPath())){
            return ResponseValue.error("地址必须以http(s)://开头");
        }
        if(s_menu.getMenu_type().equals(MenuUtils.MENU_TYPE_ITEM) && StringUtils.isEmpty(s_menu.getComponent())){
            return ResponseValue.error("菜单项必须填写:组件路径(component)");
        }
        s_menu.setMenu_id(DateUtils.getDateTimeSecondForShow());
        this.menuService.insert(s_menu);
        S_menu saved = this.menuService.get(new S_menu(s_menu.getMenu_id()));
        this.menuCacheProvider.putCacheData(s_menu.getMenu_id(), saved);
        return ResponseValue.success();
    }
 
    /**
     * 角色编辑时,显示已经设置的功能树结构目录。
     * @param roleId
     * @return
     * @date 2022-12-19
     */
    @RequestMapping("/select/roleMenuTree/{roleId}")
    public ResponseValue selectRoleMenuTree(@PathVariable Long roleId){
        if(roleId == null || roleId.longValue() <= 0){
            throw new IllegalArgumentException("角色参数不存在");
        }
        Map<String, Object> data = new HashMap<>(4);
        List<String> roleMenuIds = this.roleService.queryRoleMenuIdList(roleId);
        int menuScope = this.getCurrentOrgMenuScope();
        List<SystemMenu> menuTreeList = this.menuCacheProvider.getMenuTreeAll(null, true, menuScope);
        data.put("checkedKeys", roleMenuIds);
        data.put("menus", menuTreeList);
        return ResponseValue.success(data);
    }
 
    /**
     * 返回整个系统菜单树结构数据,目前前端角色设置中使用选择关联功能权限。
     * @return
     * @date 2022-12-18
     */
    @RequestMapping("/select/tree")
    public ResponseValue selectMenuTree(){
        int menuScope = this.getCurrentOrgMenuScope();
        List<SystemMenu> menuTreeList = this.menuCacheProvider.getMenuTreeAll(null, true, menuScope);
        return ResponseValue.success(menuTreeList);
    }
 
    @GetMapping("/list")
    public ResponseValue listMenuWithoutTree(){
        List<SystemMenu> menuList = null;
        UserPrincipal<S_user_core> userPrincipal = this.getCurrentUserPrincipal();
        if(this.isSupervisor()){
            menuList = this.menuCacheProvider.getMenuList(null, MenuUtils.MENU_SCOPE_PLATFORM);
        } else {
            int menuScope = this.getCurrentOrgMenuScope();
            menuList = this.menuCacheProvider.getMenuList(userPrincipal.getRoleIdList(), menuScope);
        }
        return ResponseValue.success(menuList);
    }
 
}