Commit 96bc081ea94bdb76af43439a43ee12c66a8bb9c7

Authored by 648540858
1 parent d2367352

添加用户操作接口

sql/mysql.sql
... ... @@ -219,5 +219,8 @@ create table user
219 219 update_time varchar(50) not null
220 220 );
221 221  
  222 +create unique index user_username_uindex
  223 + on user (username);
  224 +
222 225 insert into user (username, password, roleId, create_time, update_time) values ('admin', '21232f297a57a5a743894a0e4a801fc3', '0', '2021-04-13 14:14:57', '2021-04-13 14:14:57');
223 226  
... ...
src/main/java/com/genersoft/iot/vmp/conf/MediaConfig.java
... ... @@ -188,8 +188,8 @@ public class MediaConfig{
188 188 mediaServerItem.setRecordAssistPort(recordAssistPort);
189 189  
190 190 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
191   - mediaServerItem.setCreateTime(format.format(new Date(System.currentTimeMillis())));
192   - mediaServerItem.setUpdateTime(format.format(new Date(System.currentTimeMillis())));
  191 + mediaServerItem.setCreateTime(format.format(System.currentTimeMillis()));
  192 + mediaServerItem.setUpdateTime(format.format(System.currentTimeMillis()));
193 193  
194 194 return mediaServerItem;
195 195 }
... ...
src/main/java/com/genersoft/iot/vmp/conf/security/dto/LoginUser.java
... ... @@ -92,4 +92,10 @@ public class LoginUser implements UserDetails, CredentialsContainer {
92 92 public int getId() {
93 93 return user.getId();
94 94 }
  95 +
  96 + public int getRoleId() {
  97 + return user.getRoleId();
  98 + }
  99 +
  100 +
95 101 }
... ...
src/main/java/com/genersoft/iot/vmp/gb28181/event/online/OnlineEventListener.java
... ... @@ -52,7 +52,7 @@ public class OnlineEventListener implements ApplicationListener<OnlineEvent> {
52 52 case VideoManagerConstants.EVENT_ONLINE_REGISTER:
53 53 // 超时时间
54 54 redis.set(key, event.getDevice().getDeviceId(), sipConfig.getKeepaliveTimeOut());
55   - device.setRegisterTime(format.format(new Date(System.currentTimeMillis())));
  55 + device.setRegisterTime(format.format(System.currentTimeMillis()));
56 56 break;
57 57 // 设备主动发送心跳触发的在线事件
58 58 case VideoManagerConstants.EVENT_ONLINE_KEEPLIVE:
... ... @@ -63,7 +63,7 @@ public class OnlineEventListener implements ApplicationListener<OnlineEvent> {
63 63 } else {
64 64 redis.expire(key, sipConfig.getKeepaliveTimeOut());
65 65 }
66   - device.setKeepaliveTime(format.format(new Date(System.currentTimeMillis())));
  66 + device.setKeepaliveTime(format.format(System.currentTimeMillis()));
67 67 break;
68 68 // 设备主动发送消息触发的在线事件
69 69 case VideoManagerConstants.EVENT_ONLINE_MESSAGE:
... ...
src/main/java/com/genersoft/iot/vmp/service/IUserService.java
... ... @@ -12,11 +12,11 @@ public interface IUserService {
12 12  
13 13 User getUserByUsername(String username);
14 14  
15   - void addUser(User user);
  15 + int addUser(User user);
16 16  
17   - void deleteUser(int id);
  17 + int deleteUser(int id);
18 18  
19 19 List<User> getAllUsers();
20 20  
21   - void updateUsers(User user);
  21 + int updateUsers(User user);
22 22 }
... ...
src/main/java/com/genersoft/iot/vmp/service/impl/MediaServerServiceImpl.java
... ... @@ -276,7 +276,7 @@ public class MediaServerServiceImpl implements IMediaServerService, CommandLineR
276 276 resetOnlineServerItem(serverItemFromConfig);
277 277 setZLMConfig(serverItemFromConfig);
278 278 }else {
279   - String now = this.format.format(new Date(System.currentTimeMillis()));
  279 + String now = this.format.format(System.currentTimeMillis());
280 280 if (serverItem == null){
281 281 // 一个新的zlm接入wvp
282 282 serverItem = new MediaServerItem(zlmServerConfig, sipConfig.getSipIp());
... ...
src/main/java/com/genersoft/iot/vmp/service/impl/UserServiceImpl.java
... ... @@ -13,8 +13,7 @@ public class UserServiceImpl implements IUserService {
13 13  
14 14 @Autowired
15 15 private UserMapper userMapper;
16   -
17   -
  16 +
18 17 @Override
19 18 public User getUser(String username, String password) {
20 19 return userMapper.select(username, password);
... ... @@ -33,12 +32,14 @@ public class UserServiceImpl implements IUserService {
33 32 }
34 33  
35 34 @Override
36   - public void addUser(User user) {
37   - userMapper.add(user);
  35 + public int addUser(User user) {
  36 + User userByUsername = userMapper.getUserByUsername(user.getUsername());
  37 + if (userByUsername != null) return 0;
  38 + return userMapper.add(user);
38 39 }
39 40 @Override
40   - public void deleteUser(int id) {
41   - userMapper.delete(id);
  41 + public int deleteUser(int id) {
  42 + return userMapper.delete(id);
42 43 }
43 44  
44 45 @Override
... ... @@ -47,8 +48,8 @@ public class UserServiceImpl implements IUserService {
47 48 }
48 49  
49 50 @Override
50   - public void updateUsers(User user) {
51   - userMapper.update(user);
  51 + public int updateUsers(User user) {
  52 + return userMapper.update(user);
52 53 }
53 54  
54 55  
... ...
src/main/java/com/genersoft/iot/vmp/storager/impl/VideoManagerStoragerImpl.java
... ... @@ -109,7 +109,7 @@ public class VideoManagerStoragerImpl implements IVideoManagerStorager {
109 109 */
110 110 @Override
111 111 public synchronized boolean updateDevice(Device device) {
112   - String now = this.format.format(new Date(System.currentTimeMillis()));
  112 + String now = this.format.format(System.currentTimeMillis());
113 113 device.setUpdateTime(now);
114 114 Device deviceByDeviceId = deviceMapper.getDeviceByDeviceId(device.getDeviceId());
115 115 if (deviceByDeviceId == null) {
... ... @@ -126,7 +126,7 @@ public class VideoManagerStoragerImpl implements IVideoManagerStorager {
126 126 String channelId = channel.getChannelId();
127 127 channel.setDeviceId(deviceId);
128 128 channel.setStreamId(streamSession.getStreamId(deviceId, channel.getChannelId()));
129   - String now = this.format.format(new Date(System.currentTimeMillis()));
  129 + String now = this.format.format(System.currentTimeMillis());
130 130 channel.setUpdateTime(now);
131 131 DeviceChannel deviceChannel = deviceChannelMapper.queryChannel(deviceId, channelId);
132 132 if (deviceChannel == null) {
... ... @@ -463,7 +463,7 @@ public class VideoManagerStoragerImpl implements IVideoManagerStorager {
463 463 boolean result = false;
464 464 streamProxyItem.setStreamType("proxy");
465 465 streamProxyItem.setStatus(true);
466   - String now = this.format.format(new Date(System.currentTimeMillis()));
  466 + String now = this.format.format(System.currentTimeMillis());
467 467 streamProxyItem.setCreateTime(now);
468 468 try {
469 469 if (gbStreamMapper.add(streamProxyItem)<0 || streamProxyMapper.add(streamProxyItem) < 0) {
... ... @@ -609,7 +609,7 @@ public class VideoManagerStoragerImpl implements IVideoManagerStorager {
609 609  
610 610 @Override
611 611 public void updateMediaServer(MediaServerItem mediaServerItem) {
612   - String now = this.format.format(new Date(System.currentTimeMillis()));
  612 + String now = this.format.format(System.currentTimeMillis());
613 613 mediaServerItem.setUpdateTime(now);
614 614 if (mediaServerMapper.queryOne(mediaServerItem.getId()) != null) {
615 615 mediaServerMapper.update(mediaServerItem);
... ...
src/main/java/com/genersoft/iot/vmp/vmanager/user/UserController.java
... ... @@ -3,16 +3,24 @@ package com.genersoft.iot.vmp.vmanager.user;
3 3 import com.genersoft.iot.vmp.conf.security.SecurityUtils;
4 4 import com.genersoft.iot.vmp.conf.security.dto.LoginUser;
5 5 import com.genersoft.iot.vmp.service.IUserService;
  6 +import com.genersoft.iot.vmp.storager.dao.dto.User;
  7 +import com.genersoft.iot.vmp.vmanager.bean.WVPResult;
6 8 import io.swagger.annotations.Api;
7 9 import io.swagger.annotations.ApiImplicitParam;
8 10 import io.swagger.annotations.ApiImplicitParams;
9 11 import io.swagger.annotations.ApiOperation;
10 12 import org.springframework.beans.factory.annotation.Autowired;
  13 +import org.springframework.http.HttpStatus;
  14 +import org.springframework.http.ResponseEntity;
11 15 import org.springframework.security.authentication.AuthenticationManager;
12 16 import org.springframework.util.DigestUtils;
13 17 import org.springframework.web.bind.annotation.*;
14 18  
15 19 import javax.security.sasl.AuthenticationException;
  20 +import javax.xml.crypto.Data;
  21 +import java.text.SimpleDateFormat;
  22 +import java.util.Date;
  23 +import java.util.List;
16 24  
17 25 @Api(tags = "用户管理")
18 26 @CrossOrigin
... ... @@ -26,13 +34,15 @@ public class UserController {
26 34 @Autowired
27 35 private IUserService userService;
28 36  
  37 + private final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  38 +
29 39 @ApiOperation("登录")
30 40 @ApiImplicitParams({
31   - @ApiImplicitParam(name = "username", value = "用户名", dataTypeClass = String.class),
32   - @ApiImplicitParam(name = "password", value = "密码(32位md5加密)", dataTypeClass = String.class),
  41 + @ApiImplicitParam(name = "username", required = true, value = "用户名", dataTypeClass = String.class),
  42 + @ApiImplicitParam(name = "password", required = true, value = "密码(32位md5加密)", dataTypeClass = String.class),
33 43 })
34 44 @GetMapping("/login")
35   - public String login(String username, String password){
  45 + public String login(@RequestParam String username, @RequestParam String password){
36 46 LoginUser user;
37 47 try {
38 48 user = SecurityUtils.login(username, password, authenticationManager);
... ... @@ -49,17 +59,17 @@ public class UserController {
49 59  
50 60 @ApiOperation("修改密码")
51 61 @ApiImplicitParams({
52   - @ApiImplicitParam(name = "username", value = "用户名", dataTypeClass = String.class),
53   - @ApiImplicitParam(name = "oldpassword", value = "旧密码(已md5加密的密码)", dataTypeClass = String.class),
54   - @ApiImplicitParam(name = "password", value = "新密码(未md5加密的密码)", dataTypeClass = String.class),
  62 + @ApiImplicitParam(name = "username", required = true, value = "用户名", dataTypeClass = String.class),
  63 + @ApiImplicitParam(name = "oldpassword", required = true, value = "旧密码(已md5加密的密码)", dataTypeClass = String.class),
  64 + @ApiImplicitParam(name = "password", required = true, value = "新密码(未md5加密的密码)", dataTypeClass = String.class),
55 65 })
56 66 @PostMapping("/changePassword")
57   - public String changePassword(String oldpassword, String password){
  67 + public String changePassword(@RequestParam String oldPassword, @RequestParam String password){
58 68 // 获取当前登录用户id
59 69 String username = SecurityUtils.getUserInfo().getUsername();
60 70 LoginUser user = null;
61 71 try {
62   - user = SecurityUtils.login(username, oldpassword, authenticationManager);
  72 + user = SecurityUtils.login(username, oldPassword, authenticationManager);
63 73 if (user != null) {
64 74 int userId = SecurityUtils.getUserId();
65 75 boolean result = userService.changePassword(userId, DigestUtils.md5DigestAsHex(password.getBytes()));
... ... @@ -72,4 +82,67 @@ public class UserController {
72 82 }
73 83 return "fail";
74 84 }
  85 +
  86 +
  87 + @ApiOperation("添加用户")
  88 + @ApiImplicitParams({
  89 + @ApiImplicitParam(name = "username", required = true, value = "用户名", dataTypeClass = String.class),
  90 + @ApiImplicitParam(name = "password", required = true, value = "密码(未md5加密的密码)", dataTypeClass = String.class),
  91 + @ApiImplicitParam(name = "roleId", required = true, value = "角色ID", dataTypeClass = String.class),
  92 + })
  93 + @PostMapping("/add")
  94 + public ResponseEntity<WVPResult<Integer>> add(@RequestParam String username,
  95 + @RequestParam String password,
  96 + @RequestParam int roleId){
  97 + // 获取当前登录用户id
  98 + int currenRoleId = SecurityUtils.getUserInfo().getRoleId();
  99 + if (currenRoleId != 0) {
  100 + // 只用角色id为0才可以删除和添加用户
  101 + return new ResponseEntity<>(null, HttpStatus.FORBIDDEN);
  102 + }
  103 + User user = new User();
  104 + user.setUsername(username);
  105 + user.setPassword(DigestUtils.md5DigestAsHex(password.getBytes()));
  106 + user.setRoleId(roleId);
  107 + user.setCreateTime(format.format(System.currentTimeMillis()));
  108 + user.setUpdateTime(format.format(System.currentTimeMillis()));
  109 + int addResult = userService.addUser(user);
  110 + WVPResult<Integer> result = new WVPResult<>();
  111 + result.setCode(addResult > 0 ? 0 : -1);
  112 + result.setMsg(addResult > 0 ? "success" : "fail");
  113 + result.setData(addResult);
  114 + return new ResponseEntity<>(result, HttpStatus.OK);
  115 + }
  116 +
  117 + @ApiOperation("删除用户")
  118 + @ApiImplicitParams({
  119 + @ApiImplicitParam(name = "id", required = true, value = "用户Id", dataTypeClass = Integer.class),
  120 + })
  121 + @DeleteMapping("/delete")
  122 + public ResponseEntity<WVPResult<String>> delete(@RequestParam Integer id){
  123 + // 获取当前登录用户id
  124 + int currenRoleId = SecurityUtils.getUserInfo().getRoleId();
  125 + if (currenRoleId != 0) {
  126 + // 只用角色id为0才可以删除和添加用户
  127 + return new ResponseEntity<>(null, HttpStatus.FORBIDDEN);
  128 + }
  129 + int deleteResult = userService.deleteUser(id);
  130 + WVPResult<String> result = new WVPResult<>();
  131 + result.setCode(deleteResult>0? 0 : -1);
  132 + result.setMsg(deleteResult>0? "success" : "fail");
  133 + return new ResponseEntity<>(result, HttpStatus.OK);
  134 + }
  135 +
  136 + @ApiOperation("查询用户")
  137 + @ApiImplicitParams({})
  138 + @GetMapping("/all")
  139 + public ResponseEntity<WVPResult<List<User>>> all(){
  140 + // 获取当前登录用户id
  141 + List<User> allUsers = userService.getAllUsers();
  142 + WVPResult<List<User>> result = new WVPResult<>();
  143 + result.setCode(0);
  144 + result.setMsg("success");
  145 + result.setData(allUsers);
  146 + return new ResponseEntity<>(result, HttpStatus.OK);
  147 + }
75 148 }
... ...
src/main/resources/wvp.sqlite
No preview for this file type