MenuController.java
5.81 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/**
* Copyright © 2015-2018 ODM All rights reserved.
*/
package com.thinkgem.jeesite.modules.sys.web;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.Menu;
import com.thinkgem.jeesite.modules.sys.service.SystemService;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
/**
* 菜单Controller
* @author ThinkGem
* @version 2013-3-23
*/
@Controller
@RequestMapping(value = "${adminPath}/sys/menu")
public class MenuController extends BaseController {
@Autowired
private SystemService systemService;
@ModelAttribute("menu")
public Menu get(@RequestParam(required=false) String id) {
if (StringUtils.isNotBlank(id)){
return systemService.getMenu(id);
}else{
return new Menu();
}
}
@RequiresPermissions("sys:menu:view")
@RequestMapping(value = {"list", ""})
public String list(Model model) {
List<Menu> list = Lists.newArrayList();
List<Menu> sourcelist = systemService.findAllMenu();
Menu.sortList(list, sourcelist, Menu.getRootId(), true);
model.addAttribute("list", list);
return "modules/sys/menuList";
}
@RequiresPermissions("sys:menu:view")
@RequestMapping(value = "form")
public String form(Menu menu, Model model) {
if (menu.getParent()==null||menu.getParent().getId()==null){
menu.setParent(new Menu(Menu.getRootId()));
}
menu.setParent(systemService.getMenu(menu.getParent().getId()));
// 获取排序号,最末节点排序号+30
if (StringUtils.isBlank(menu.getId())){
List<Menu> list = Lists.newArrayList();
List<Menu> sourcelist = systemService.findAllMenu();
Menu.sortList(list, sourcelist, menu.getParentId(), false);
if (list.size() > 0){
menu.setSort(list.get(list.size()-1).getSort() + 30);
}
}
model.addAttribute("menu", menu);
return "modules/sys/menuForm";
}
@RequiresPermissions("sys:menu:edit")
@RequestMapping(value = "save")
public String save(Menu menu, Model model, RedirectAttributes redirectAttributes) {
if(!UserUtils.getUser().isAdmin()){
addMessage(redirectAttributes, "越权操作,只有超级管理员才能添加或修改数据!");
return "redirect:" + adminPath + "/sys/role/?repage";
}
if(Global.isDemoMode()){
addMessage(redirectAttributes, "演示模式,不允许操作!");
return "redirect:" + adminPath + "/sys/menu/";
}
if (!beanValidator(model, menu)){
return form(menu, model);
}
systemService.saveMenu(menu);
addMessage(redirectAttributes, "保存菜单'" + menu.getName() + "'成功");
return "redirect:" + adminPath + "/sys/menu/";
}
@RequiresPermissions("sys:menu:edit")
@RequestMapping(value = "delete")
public String delete(Menu menu, RedirectAttributes redirectAttributes) {
if(Global.isDemoMode()){
addMessage(redirectAttributes, "演示模式,不允许操作!");
return "redirect:" + adminPath + "/sys/menu/";
}
// if (Menu.isRoot(id)){
// addMessage(redirectAttributes, "删除菜单失败, 不允许删除顶级菜单或编号为空");
// }else{
systemService.deleteMenu(menu);
addMessage(redirectAttributes, "删除菜单成功");
// }
return "redirect:" + adminPath + "/sys/menu/";
}
@RequiresPermissions("user")
@RequestMapping(value = "tree")
public String tree() {
return "modules/sys/menuTree";
}
@RequiresPermissions("user")
@RequestMapping(value = "treeselect")
public String treeselect(String parentId, Model model) {
model.addAttribute("parentId", parentId);
return "modules/sys/menuTreeselect";
}
/**
* 批量修改菜单排序
*/
@RequiresPermissions("sys:menu:edit")
@RequestMapping(value = "updateSort")
public String updateSort(String[] ids, Integer[] sorts, RedirectAttributes redirectAttributes) {
if(Global.isDemoMode()){
addMessage(redirectAttributes, "演示模式,不允许操作!");
return "redirect:" + adminPath + "/sys/menu/";
}
for (int i = 0; i < ids.length; i++) {
Menu menu = new Menu(ids[i]);
menu.setSort(sorts[i]);
systemService.updateMenuSort(menu);
}
addMessage(redirectAttributes, "保存菜单排序成功!");
return "redirect:" + adminPath + "/sys/menu/";
}
/**
* isShowHide是否显示隐藏菜单
* @param extId
* @param isShowHidden
* @param response
* @return
*/
@RequiresPermissions("user")
@ResponseBody
@RequestMapping(value = "treeData")
public List<Map<String, Object>> treeData(@RequestParam(required=false) String extId,@RequestParam(required=false) String isShowHide, HttpServletResponse response) {
List<Map<String, Object>> mapList = Lists.newArrayList();
List<Menu> list = systemService.findAllMenu();
for (int i=0; i<list.size(); i++){
Menu e = list.get(i);
if (StringUtils.isBlank(extId) || (extId!=null && !extId.equals(e.getId()) && e.getParentIds().indexOf(","+extId+",")==-1)){
if(isShowHide != null && isShowHide.equals("0") && e.getIsShow().equals("0")){
continue;
}
Map<String, Object> map = Maps.newHashMap();
map.put("id", e.getId());
map.put("pId", e.getParentId());
map.put("name", e.getName());
mapList.add(map);
}
}
return mapList;
}
}