UserJsonController.java
4.3 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
package com.tianbo.controller.json;
import com.tianbo.common.ModelAndPage;
import com.tianbo.controller.base.BaseController;
import com.tianbo.model.Users;
import com.tianbo.model.UsersExample;
import com.tianbo.service.UserService;
import com.tianbo.util.dao.Page;
import com.tianbo.util.json.ResponseModel;
import com.tianbo.util.tools.MD5Tools;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* Created by mrz on 2017/8/23.
*/
@Controller
@RequestMapping("/user")
public class UserJsonController extends BaseController{
//service类
@Autowired
private UserService userService;
@RequestMapping("userExample.json")
@RequiresRoles("admin")
@ResponseBody
public Users userExample() {
Users users = new Users();
return users;
}
@RequestMapping("userById.json")
@RequiresRoles("admin")
@ResponseBody
public Users userList(@ModelAttribute Users user) throws Exception{
Users users = userService.selectByUserId(user.getUserId());
return users;
}
/**
* 查找所用用户控制器方法
* @return
* @throws Exception
*/
@RequestMapping("list.json")
@RequiresRoles("admin")
@ResponseBody
public ModelAndPage userList(@ModelAttribute Page frontPage,@ModelAttribute Users user) throws Exception{
if(frontPage.getBegin()>=0){ defaultDataStart = frontPage.getBegin();}
if (frontPage.getLength()>0){ defaultPerPage = frontPage.getLength();}
//调用service方法得到用户列表
ModelAndPage users = userService.userList(defaultDataStart,defaultPerPage,user);
return users;
}
@RequestMapping(value = "baseUpdate.json",method = RequestMethod.POST)
@RequiresRoles("admin")
@RequiresPermissions("user:edit")
@ResponseBody
public ResponseModel baseUpdate(@RequestBody Users user){
user.setUpdatetime(new Date());
int n = userService.updateByPrimaryKey(user);
ResponseModel responseModel = new ResponseModel();
if (n>0){
responseModel.setStatus(200);
responseModel.setMsg("用户资料更新成功");
return responseModel;
}else {
responseModel.setStatus(500);
responseModel.setMsg("用户资料更新失败");
return responseModel;
}
}
@RequestMapping(value = "add.json",method = RequestMethod.POST)
@RequiresRoles("admin")
@ResponseBody
public ResponseModel add(@RequestBody Users user){
ResponseModel responseModel = new ResponseModel();
if(checkUser(user)){
String userPW = user.getPassword();
String PWMD5 = MD5Tools.MD5(userPW);
user.setPassword(PWMD5);
user.setCreattime(new Date());
int n = userService.insert(user);
if (n>0){
responseModel.setStatus(200);
responseModel.setMsg("用户添加成功");
}else {
}
}else {
responseModel.setStatus(500);
responseModel.setMsg("用户添加失败,已存在用户");
}
return responseModel;
}
@RequestMapping(value = "del.json",method = RequestMethod.POST)
@RequiresRoles("admin")
@ResponseBody
public ResponseModel del(@ModelAttribute Users user){
ResponseModel responseModel = new ResponseModel();
int n = userService.deleteByPrimaryKey(user.getUserId());
if (n>0){
responseModel.setStatus(200);
responseModel.setMsg("用户删除成功");
}else {
responseModel.setStatus(500);
responseModel.setMsg("用户删除失败");
}
return responseModel;
}
private Boolean checkUser( Users user){
if (userService.checkUser(user)){
return Boolean.FALSE; //存在用户,返回false
}else {
return Boolean.TRUE; //不存在用户,返回true,可以添加用户
}
}
}