Skip to content

Commit 519a2c1

Browse files
committed
[update]添加菜单 角色 用户管理等功能
1 parent 1cf8ff4 commit 519a2c1

File tree

8 files changed

+304
-14
lines changed

8 files changed

+304
-14
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
package com.example.springbootshiro.controller;
2+
3+
import com.example.springbootshiro.domain.MenuInfo;
4+
import com.example.springbootshiro.domain.ResponseResult;
5+
import com.example.springbootshiro.domain.Tree;
6+
import com.example.springbootshiro.service.MenuService;
7+
import org.apache.shiro.authz.annotation.RequiresPermissions;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.stereotype.Controller;
10+
import org.springframework.web.bind.annotation.RequestMapping;
11+
import org.springframework.web.bind.annotation.ResponseBody;
12+
13+
import java.util.List;
14+
15+
/**
16+
* Created by Douglee on 2018/4/9.
17+
*/
18+
@Controller
19+
public class MenuController extends BaseController {
20+
@Autowired
21+
private MenuService menuService;
22+
23+
@RequestMapping("menu")
24+
public String index(){
25+
return "menu/menu";
26+
}
27+
28+
@RequestMapping("menu/menu")
29+
@ResponseBody
30+
public ResponseResult getMenu(String userName){
31+
try {
32+
List<MenuInfo> menus = menuService.findUserMenus(userName);
33+
return ResponseResult.ok();
34+
}catch (Exception ex){
35+
ex.printStackTrace();
36+
return ResponseResult.error("获取菜单失败");
37+
}
38+
}
39+
40+
@RequestMapping("menu/tree")
41+
@ResponseBody
42+
public ResponseResult getMenuTree() {
43+
try {
44+
Tree<MenuInfo> tree = this.menuService.getMenuTree();
45+
return ResponseResult.ok(tree);
46+
} catch (Exception e) {
47+
e.printStackTrace();
48+
return ResponseResult.error("获取菜单列表失败!");
49+
}
50+
}
51+
52+
@RequestMapping("menu/menuButtonTree")
53+
@ResponseBody
54+
public ResponseResult getMenuButtonTree() {
55+
try {
56+
Tree<MenuInfo> tree = this.menuService.getMenuButtonTree();
57+
return ResponseResult.ok(tree);
58+
} catch (Exception e) {
59+
e.printStackTrace();
60+
return ResponseResult.error("获取菜单列表失败!");
61+
}
62+
}
63+
64+
@RequestMapping("menu/getMenu")
65+
@ResponseBody
66+
public ResponseResult getMenu(Long menuId){
67+
try {
68+
MenuInfo menuInfo = menuService.findById(menuId);
69+
return ResponseResult.ok(menuInfo);
70+
}catch (Exception ex){
71+
ex.printStackTrace();
72+
return ResponseResult.error("获取信息失败");
73+
}
74+
}
75+
76+
77+
@RequestMapping("menu/getUserMenu")
78+
@ResponseBody
79+
public ResponseResult getUserMenu(String userName){
80+
try {
81+
Tree<MenuInfo> tree = menuService.getUserMenu(userName);
82+
return ResponseResult.ok(tree);
83+
}catch (Exception ex){
84+
ex.printStackTrace();
85+
return ResponseResult.error("获取用户菜单失败");
86+
}
87+
}
88+
89+
@RequestMapping("menu/list")
90+
@ResponseBody
91+
public List<MenuInfo> menuList(MenuInfo menuInfo){
92+
try {
93+
return menuService.findAllMenus(menuInfo);
94+
}catch (Exception ex){
95+
ex.printStackTrace();
96+
return null;
97+
}
98+
}
99+
100+
@RequiresPermissions("menu:add")
101+
@RequestMapping("menu/add")
102+
@ResponseBody
103+
public ResponseResult addMenu(MenuInfo menuInfo){
104+
String name = "";
105+
if (MenuInfo.TYPE_MENU.equals(menuInfo.getType())){
106+
name = "菜单";
107+
}else {
108+
name = "按钮";
109+
}
110+
111+
try {
112+
menuService.addMenu(menuInfo);
113+
return ResponseResult.ok("新增" + name + "成功");
114+
}catch (Exception ex){
115+
ex.printStackTrace();
116+
return ResponseResult.ok("新增" + name + "失败");
117+
}
118+
}
119+
120+
@RequiresPermissions("menu:delete")
121+
@RequestMapping("menu/delete")
122+
@ResponseBody
123+
public ResponseResult deleteMenus(String ids) {
124+
try {
125+
this.menuService.deleteMeuns(ids);
126+
return ResponseResult.ok("删除成功!");
127+
} catch (Exception e) {
128+
e.printStackTrace();
129+
return ResponseResult.error("删除失败,请联系网站管理员!");
130+
}
131+
}
132+
133+
@RequiresPermissions("menu:update")
134+
@RequestMapping("menu/update")
135+
@ResponseBody
136+
public ResponseResult updateMenu(MenuInfo menu) {
137+
String name = "";
138+
if (MenuInfo.TYPE_MENU.equals(menu.getType()))
139+
name = "菜单";
140+
else
141+
name = "按钮";
142+
try {
143+
this.menuService.updateMenu(menu);
144+
return ResponseResult.ok("修改" + name + "成功!");
145+
} catch (Exception e) {
146+
e.printStackTrace();
147+
return ResponseResult.error("修改" + name + "失败,请联系网站管理员!");
148+
}
149+
}
150+
151+
152+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.example.springbootshiro.controller;
2+
3+
import com.example.springbootshiro.domain.QueryRequest;
4+
import com.example.springbootshiro.domain.ResponseResult;
5+
import com.example.springbootshiro.domain.RoleInfo;
6+
import com.example.springbootshiro.service.RoleService;
7+
import com.github.pagehelper.PageHelper;
8+
import com.github.pagehelper.PageInfo;
9+
import org.apache.shiro.authz.annotation.RequiresPermissions;
10+
import org.springframework.beans.factory.annotation.Autowired;
11+
import org.springframework.stereotype.Controller;
12+
import org.springframework.web.bind.annotation.RequestMapping;
13+
import org.springframework.web.bind.annotation.ResponseBody;
14+
15+
import java.util.List;
16+
import java.util.Map;
17+
18+
/**
19+
* Created by Douglee on 2018/4/9.
20+
*/
21+
@Controller
22+
public class RoleController extends BaseController{
23+
24+
@Autowired
25+
private RoleService roleService;
26+
27+
@RequestMapping("role")
28+
public String index(){
29+
return "role/role";
30+
}
31+
32+
@RequestMapping("role/list")
33+
@ResponseBody
34+
public Map<String, Object> roleList(QueryRequest request, RoleInfo roleInfo){
35+
PageHelper.startPage(request.getPageNum(), request.getPageSize());
36+
List<RoleInfo> list = roleService.findAllRole(roleInfo);
37+
38+
PageInfo<RoleInfo> pageInfo = new PageInfo<>(list);
39+
return getDataTable(pageInfo);
40+
}
41+
42+
43+
@RequiresPermissions("role:add")
44+
@RequestMapping("role/add")
45+
@ResponseBody
46+
public ResponseResult addRole(RoleInfo role, Long[] menuId) {
47+
try {
48+
this.roleService.addRole(role, menuId);
49+
return ResponseResult.ok("新增角色成功!");
50+
} catch (Exception e) {
51+
e.printStackTrace();
52+
return ResponseResult.error("新增角色失败,请联系网站管理员!");
53+
}
54+
}
55+
56+
@RequiresPermissions("role:delete")
57+
@RequestMapping("role/delete")
58+
@ResponseBody
59+
public ResponseResult deleteRoles(String ids) {
60+
try {
61+
this.roleService.deleteRoles(ids);
62+
return ResponseResult.ok("删除角色成功!");
63+
} catch (Exception e) {
64+
e.printStackTrace();
65+
return ResponseResult.error("删除角色失败,请联系网站管理员!");
66+
}
67+
}
68+
69+
@RequiresPermissions("role:update")
70+
@RequestMapping("role/update")
71+
@ResponseBody
72+
public ResponseResult updateRole(RoleInfo role, Long[] menuId) {
73+
try {
74+
this.roleService.updateRole(role, menuId);
75+
return ResponseResult.ok("修改角色成功!");
76+
} catch (Exception e) {
77+
e.printStackTrace();
78+
return ResponseResult.error("修改角色失败,请联系网站管理员!");
79+
}
80+
}
81+
82+
83+
}

Diff for: spring-boot-shiro/src/main/java/com/example/springbootshiro/controller/UserController.java

+52-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
package com.example.springbootshiro.controller;
22

33
import com.example.springbootshiro.domain.QueryRequest;
4+
import com.example.springbootshiro.domain.ResponseResult;
45
import com.example.springbootshiro.domain.UserInfo;
56
import com.example.springbootshiro.service.UserService;
67
import com.github.pagehelper.PageHelper;
78
import com.github.pagehelper.PageInfo;
9+
import org.apache.shiro.authz.annotation.RequiresPermissions;
810
import org.springframework.beans.factory.annotation.Autowired;
911
import org.springframework.stereotype.Controller;
1012
import org.springframework.ui.Model;
11-
import org.springframework.web.bind.annotation.GetMapping;
13+
import org.springframework.web.bind.annotation.RequestMapping;
1214
import org.springframework.web.bind.annotation.ResponseBody;
1315

1416
import java.util.List;
@@ -22,14 +24,14 @@ public class UserController extends BaseController {
2224
@Autowired
2325
private UserService userService;
2426

25-
@GetMapping("user")
27+
@RequestMapping("user")
2628
public String index(Model model) {
2729
UserInfo userInfo = super.getCurrentUser();
2830
model.addAttribute("user", userInfo);
2931
return "user/user";
3032
}
3133

32-
@GetMapping("user/list")
34+
@RequestMapping("user/list")
3335
@ResponseBody
3436
public Map<String, Object> userList(QueryRequest request, UserInfo userInfo) {
3537
PageHelper.startPage(request.getPageNum(), request.getPageSize());
@@ -39,4 +41,51 @@ public Map<String, Object> userList(QueryRequest request, UserInfo userInfo) {
3941
return getDataTable(pageInfo);
4042
}
4143

44+
@RequiresPermissions("user:add")
45+
@RequestMapping("user/add")
46+
@ResponseBody
47+
public ResponseResult addUser(UserInfo user, Long[] roles) {
48+
try {
49+
if ("on".equalsIgnoreCase(user.getStatus()))
50+
user.setStatus("1");
51+
else
52+
user.setStatus("0");
53+
this.userService.addUser(user, roles);
54+
return ResponseResult.ok("新增用户成功!");
55+
} catch (Exception e) {
56+
e.printStackTrace();
57+
return ResponseResult.error("新增用户失败,请联系网站管理员!");
58+
}
59+
}
60+
61+
@RequiresPermissions("user:update")
62+
@RequestMapping("user/update")
63+
@ResponseBody
64+
public ResponseResult updateUser(UserInfo user, Long[] rolesSelect) {
65+
try {
66+
if ("on".equalsIgnoreCase(user.getStatus()))
67+
user.setStatus("1");
68+
else
69+
user.setStatus("0");
70+
this.userService.updateUser(user, rolesSelect);
71+
return ResponseResult.ok("修改用户成功!");
72+
} catch (Exception e) {
73+
e.printStackTrace();
74+
return ResponseResult.error("修改用户失败,请联系网站管理员!");
75+
}
76+
}
77+
78+
@RequiresPermissions("user:delete")
79+
@RequestMapping("user/delete")
80+
@ResponseBody
81+
public ResponseResult deleteUsers(String ids) {
82+
try {
83+
this.userService.deleteUsers(ids);
84+
return ResponseResult.ok("删除用户成功!");
85+
} catch (Exception e) {
86+
e.printStackTrace();
87+
return ResponseResult.error("删除用户失败,请联系网站管理员!");
88+
}
89+
}
90+
4291
}

Diff for: spring-boot-shiro/src/main/java/com/example/springbootshiro/domain/MenuInfo.java

+6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@
1414
public class MenuInfo implements Serializable {
1515
private static final long serialVersionUID = 5653758334711519506L;
1616

17+
// Type=0表示菜单
18+
public static final String TYPE_MENU = "0";
19+
20+
//Type=1表示按钮
21+
public static final String TYPE_BUTTON = "1";
22+
1723
@Id
1824
@GeneratedValue(generator = "JDBC")
1925
@Column(name = "MENU_ID")

Diff for: spring-boot-shiro/src/main/java/com/example/springbootshiro/service/impl/MenuServiceImpl.java

+7-10
Original file line numberDiff line numberDiff line change
@@ -73,22 +73,19 @@ public Tree<MenuInfo> getMenuButtonTree() {
7373
return t;
7474
}
7575

76-
@Override
7776
public Tree<MenuInfo> getMenuTree() {
78-
List<Tree<MenuInfo>> trees = new ArrayList<>();
77+
List<Tree<MenuInfo>> trees = new ArrayList<Tree<MenuInfo>>();
7978
Example example = new Example(MenuInfo.class);
80-
example.createCriteria().andCondition("type=", 0);
79+
example.createCriteria().andCondition("type =", 0);
8180
example.setOrderByClause("create_time");
8281
List<MenuInfo> menus = this.menuMapper.selectByExample(example);
83-
for (MenuInfo menuInfo: menus){
84-
Tree<MenuInfo> tree = new Tree<>();
85-
tree.setId(menuInfo.getMenuId().toString());
86-
tree.setParentId(menuInfo.getParentId().toString());
87-
tree.setText(menuInfo.getMenuName());
88-
82+
for (MenuInfo menu : menus) {
83+
Tree<MenuInfo> tree = new Tree<MenuInfo>();
84+
tree.setId(menu.getMenuId().toString());
85+
tree.setParentId(menu.getParentId().toString());
86+
tree.setText(menu.getMenuName());
8987
trees.add(tree);
9088
}
91-
9289
Tree<MenuInfo> t = TreeUtils.build(trees);
9390
return t;
9491
}

Diff for: spring-boot-shiro/src/main/java/com/example/springbootshiro/service/impl/RoleServiceImpl.java

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.springframework.transaction.annotation.Transactional;
1717
import tk.mybatis.mapper.entity.Example;
1818

19+
import java.util.ArrayList;
1920
import java.util.Arrays;
2021
import java.util.Date;
2122
import java.util.List;
@@ -66,6 +67,7 @@ public RoleInfo findByName(String roleName) {
6667
}
6768
}
6869

70+
6971
@Override
7072
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
7173
public void addRole(RoleInfo roleInfo, Long[] menuIds) {

Diff for: spring-boot-shiro/src/main/resources/static/js/app/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@
174174
}
175175
}
176176

177-
$(".user__img").attr("src", avatar);
177+
// $(".user__img").attr("src", avatar);
178178
$("#user__profile").on('click', function() {
179179
$.post(ctx + "user/profile", function(r) {
180180
$breadcrumb.html("").append('<li class="breadcrumb-item">个人信息</li>');

Diff for: spring-boot-shiro/src/main/resources/templates/index.html

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
</body>
3636
<script data-th-inline="javascript">
3737
var ctx = [[@{/}]];
38+
var theme = "green";
3839
var userName = [[${user.username}]];
3940
</script>
4041
<script data-th-src="@{js/app/index.js}"></script>

0 commit comments

Comments
 (0)