Browse Source

完成用户管理后端接口,以及分配角色,读取OPC的标签结构,并递归成树形,返回给前端

gt 2 years ago
parent
commit
a956128865
17 changed files with 716 additions and 176 deletions
  1. 93 67
      chuanyi_server/src/main/java/com/judong/chuanyiserver/controller/ConnectController.java
  2. 51 8
      chuanyi_server/src/main/java/com/judong/chuanyiserver/controller/UserController.java
  3. 12 0
      chuanyi_server/src/main/java/com/judong/chuanyiserver/dao/ConnectDao.java
  4. 15 5
      chuanyi_server/src/main/java/com/judong/chuanyiserver/dao/UserDao.java
  5. 6 0
      chuanyi_server/src/main/java/com/judong/chuanyiserver/entity/ServerInformation.java
  6. 3 0
      chuanyi_server/src/main/java/com/judong/chuanyiserver/entity/User.java
  7. 18 0
      chuanyi_server/src/main/java/com/judong/chuanyiserver/service/ConnectService.java
  8. 9 1
      chuanyi_server/src/main/java/com/judong/chuanyiserver/service/UserService.java
  9. 193 12
      chuanyi_server/src/main/java/com/judong/chuanyiserver/service/impl/ConnectServiceImpl.java
  10. 7 1
      chuanyi_server/src/main/java/com/judong/chuanyiserver/service/impl/ServerInServiceImpl.java
  11. 111 34
      chuanyi_server/src/main/java/com/judong/chuanyiserver/service/impl/UserServiceImpl.java
  12. 14 6
      chuanyi_server/src/main/java/com/judong/chuanyiserver/util/ConstantStr.java
  13. 65 2
      chuanyi_server/src/main/java/com/judong/chuanyiserver/util/OpcDataUtil.java
  14. 17 17
      chuanyi_server/src/main/java/com/judong/chuanyiserver/util/TokenUtil.java
  15. 19 3
      chuanyi_server/src/main/java/com/judong/chuanyiserver/util/UserUtil.java
  16. 36 4
      chuanyi_server/src/main/resources/mapper/ConnectDao.xml
  17. 47 16
      chuanyi_server/src/main/resources/mapper/UserDao.xml

+ 93 - 67
chuanyi_server/src/main/java/com/judong/chuanyiserver/controller/ConnectController.java

@@ -4,15 +4,13 @@ import com.judong.chuanyiserver.entity.ServerInformation;
 import com.judong.chuanyiserver.enums.ResultEnum;
 import com.judong.chuanyiserver.service.ConnectService;
 import com.judong.chuanyiserver.util.Blank;
-import com.judong.chuanyiserver.util.OpcDataUtil;
 import com.judong.chuanyiserver.util.Result;
 import lombok.extern.slf4j.Slf4j;
 import org.jinterop.dcom.common.JIException;
-import org.openscada.opc.dcom.da.OPCSERVERSTATE;
 import org.openscada.opc.lib.common.AlreadyConnectedException;
-import org.openscada.opc.lib.common.ConnectionInformation;
-import org.openscada.opc.lib.da.Item;
-import org.openscada.opc.lib.da.Server;
+import org.openscada.opc.lib.common.NotConnectedException;
+import org.openscada.opc.lib.da.AddFailedException;
+import org.openscada.opc.lib.da.DuplicateGroupException;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.PostMapping;
@@ -21,8 +19,6 @@ import org.springframework.web.bind.annotation.RestController;
 
 import javax.servlet.http.HttpServletRequest;
 import java.net.UnknownHostException;
-import java.util.Collection;
-import java.util.concurrent.Executors;
 
 @RestController
 @RequestMapping("connect")
@@ -32,86 +28,116 @@ public class ConnectController {
     @Autowired
     private ConnectService connectService;
 
+    /**
+     * 测试连接是否可用
+     *
+     * @param serverInformation
+     * @return
+     * @throws AlreadyConnectedException
+     * @throws JIException
+     * @throws UnknownHostException
+     */
     @GetMapping("/testConnect")
     public Result testConnect(ServerInformation serverInformation) throws AlreadyConnectedException, JIException, UnknownHostException {
         if (Blank.isEmpty(serverInformation.getIpAddress()) || Blank.isEmpty(serverInformation.getIpUserName()) || Blank.isEmpty(serverInformation.getIpPassword()) || Blank.isEmpty(serverInformation.getAgreementType())) {
             return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), ResultEnum.REQUEST_WRONGPARAMS.getRespMsg());
         }
-        // 连接信息
-        final ConnectionInformation ci = new ConnectionInformation();
-        //服务
-        final Server server = new Server(ci, Executors.newSingleThreadScheduledExecutor());
-        try {
-            ci.setHost(serverInformation.getIpAddress()); // 安装opc电脑IP
-            ci.setDomain(""); // 域,为空就行
-            ci.setUser(serverInformation.getIpUserName()); // 电脑上自己建好的用户名
-            ci.setPassword(serverInformation.getIpPassword()); // 用户名的密码
-            ci.setClsid("7BC0CC8E-482C-47CA-ABDC-0FE7F9C6E729"); // KEPServer的注册表ID,可以在“组件服务”里看到
-            // ci.setProgId("");
-
-            // 连接到服务
-            long start = System.currentTimeMillis();
-            server.connect();
-            long finish = System.currentTimeMillis();
-            log.info("连接耗费时间为:" + (finish - start) + "毫秒");
-            if (null == server.getServerState()) {
-                return Result.no(ResultEnum.NOT_FOUND.getRespCode(), "连接失败");
-            }
-            if (OPCSERVERSTATE.OPC_STATUS_RUNNING == server.getServerState().getServerState()) {
-                return Result.ok(true);
-            }
-        } finally {
-            //关闭连接使用
-            server.dispose();
-        }
-        return Result.no(ResultEnum.NOT_FOUND.getRespCode(), "连接失败");
+        return connectService.testConnect(serverInformation);
     }
 
+    /**
+     * 保存用户提交的连接信息
+     *
+     * @param request
+     * @param serverInformation
+     * @return
+     */
     @PostMapping("/saveConnect")
-    public Result saveConnect(HttpServletRequest request,ServerInformation serverInformation ) {
+    public Result saveConnect(HttpServletRequest request, ServerInformation serverInformation) {
         if (Blank.isEmpty(serverInformation.getIpAddress()) || Blank.isEmpty(serverInformation.getIpUserName()) || Blank.isEmpty(serverInformation.getIpPassword()) || Blank.isEmpty(serverInformation.getAgreementType())) {
             return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), ResultEnum.REQUEST_WRONGPARAMS.getRespMsg());
         }
         return connectService.saveConnect(request, serverInformation);
     }
 
+    /**
+     * 查询用户保存的连接信息
+     *
+     * @param request
+     * @return
+     */
+    @GetMapping("/selectAllConnect")
+    public Result selectAllConnect(HttpServletRequest request) {
+        return connectService.selectAllConnect(request);
+    }
+
+    /**
+     * 通过服务器ip打开连接
+     *
+     * @param serverInformationId
+     * @return
+     * @throws AlreadyConnectedException
+     * @throws JIException
+     * @throws UnknownHostException
+     * @throws NotConnectedException
+     * @throws DuplicateGroupException
+     * @throws AddFailedException
+     */
     @GetMapping("/openConnect")
-    public Result openConnect(ServerInformation serverInformation) throws AlreadyConnectedException, JIException, UnknownHostException {
-        if (Blank.isEmpty(serverInformation.getIpAddress()) || Blank.isEmpty(serverInformation.getIpUserName()) || Blank.isEmpty(serverInformation.getIpPassword()) || Blank.isEmpty(serverInformation.getAgreementType())) {
+    public Result openConnect(int serverInformationId) throws AlreadyConnectedException, JIException, UnknownHostException, NotConnectedException, DuplicateGroupException, AddFailedException {
+        if (Blank.isEmpty(serverInformationId)) {
+            return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), ResultEnum.REQUEST_WRONGPARAMS.getRespMsg());
+        }
+        return connectService.openConnect(serverInformationId);
+    }
+
+    /**
+     * 通过传入服务器id和项名称,读取响应的数据
+     *
+     * @param id
+     * @param itemName
+     * @return
+     * @throws JIException
+     * @throws AddFailedException
+     * @throws NotConnectedException
+     * @throws AlreadyConnectedException
+     * @throws UnknownHostException
+     * @throws DuplicateGroupException
+     */
+    @GetMapping("/readItemValue")
+    public Result readItemValue(int id, String itemName) throws JIException, AddFailedException, NotConnectedException, AlreadyConnectedException, UnknownHostException, DuplicateGroupException {
+        if (Blank.isEmpty(id) || Blank.isEmpty(itemName)) {
             return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), ResultEnum.REQUEST_WRONGPARAMS.getRespMsg());
         }
-        // 连接信息
-        final ConnectionInformation ci = new ConnectionInformation();
-        //服务
-        final Server server = new Server(ci, Executors.newSingleThreadScheduledExecutor());
-        try {
-            ci.setHost(serverInformation.getIpAddress()); // 安装opc电脑IP
-            ci.setDomain(""); // 域,为空就行
-            ci.setUser(serverInformation.getIpUserName()); // 电脑上自己建好的用户名
-            ci.setPassword(serverInformation.getIpPassword()); // 用户名的密码
-            ci.setClsid("7BC0CC8E-482C-47CA-ABDC-0FE7F9C6E729"); // KEPServer的注册表ID,可以在“组件服务”里看到
-            // ci.setProgId("");
+        return connectService.readItemValue(id, itemName);
+    }
 
-            // 连接到服务
-            server.connect();
-            if (null == server.getServerState()) {
-                return Result.no(ResultEnum.NOT_FOUND.getRespCode(), "连接失败");
-            }
-            if (OPCSERVERSTATE.OPC_STATUS_RUNNING == server.getServerState().getServerState()) {
-                //获取服务器下所有ITEM列表,平面展示
-                Collection<String> items = server.getFlatBrowser().browse();
-                //读取所有标签信息,且成树形展示
-                return Result.ok(true);
-            }
-        } finally {
-            //关闭连接使用
-            server.dispose();
+    /**
+     * 编辑连接
+     *
+     * @param request
+     * @param serverInformation
+     * @return
+     */
+    @PostMapping("/editConnect")
+    public Result editConnect(HttpServletRequest request, ServerInformation serverInformation) {
+        if (Blank.isEmpty(serverInformation.getIpAddress()) || Blank.isEmpty(serverInformation.getIpUserName()) || Blank.isEmpty(serverInformation.getIpPassword()) || Blank.isEmpty(serverInformation.getAgreementType())) {
+            return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), ResultEnum.REQUEST_WRONGPARAMS.getRespMsg());
         }
-        return Result.no(ResultEnum.NOT_FOUND.getRespCode(), "连接失败");
+        return connectService.editConnect(request, serverInformation);
     }
 
-    @GetMapping("/readItemValue")
-    public Result readItemValue(Item item) throws JIException {
-        return Result.ok(OpcDataUtil.getVal(item.read(true).getValue()));
+    /**
+     * 通过id删除保存的连接信息
+     * @param request
+     * @param id
+     * @return
+     */
+    @PostMapping("/deleteConnect")
+    public Result deleteConnect(HttpServletRequest request, int id) {
+        if (Blank.isEmpty(id)) {
+            return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), ResultEnum.REQUEST_WRONGPARAMS.getRespMsg());
+        }
+        return connectService.deleteConnect(request, id);
     }
 }

+ 51 - 8
chuanyi_server/src/main/java/com/judong/chuanyiserver/controller/UserController.java

@@ -1,14 +1,15 @@
 package com.judong.chuanyiserver.controller;
 
+import com.judong.chuanyiserver.entity.User;
 import com.judong.chuanyiserver.enums.ResultEnum;
 import com.judong.chuanyiserver.service.UserService;
+import com.judong.chuanyiserver.util.Blank;
 import com.judong.chuanyiserver.util.Result;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.util.StringUtils;
-import org.springframework.web.bind.annotation.PostMapping;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.*;
 
+import javax.servlet.http.HttpServletRequest;
 import java.util.List;
 
 @RestController
@@ -50,6 +51,17 @@ public class UserController {
     }
 
     /**
+     * 用户退出登录
+     *
+     * @param request
+     * @return
+     */
+    @PostMapping("/userLoginOut")
+    public Result userLoginOut(HttpServletRequest request) {
+        return userService.userLoginOut(request);
+    }
+
+    /**
      * 添加用户
      *
      * @param userName
@@ -65,6 +77,37 @@ public class UserController {
     }
 
     /**
+     * 管理员分页查询所有用户信息
+     *
+     * @param request
+     * @param page
+     * @param limit
+     * @return
+     */
+    @GetMapping("/getUserPage")
+    public Result getUserPage(HttpServletRequest request, int page, int limit) {
+        if (Blank.isEmpty(page) || Blank.isEmpty(limit) || page < 1 || limit < 1) {
+            return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), ResultEnum.REQUEST_WRONGPARAMS.getRespMsg());
+        }
+        return userService.getUserPage(request, page, limit);
+    }
+
+    /**
+     * 管理员可以查看任何用户的信息,不是管理员只能查看自己的信息
+     *
+     * @param request
+     * @param userId
+     * @return
+     */
+    @GetMapping("/getUserById")
+    public Result getUserById(HttpServletRequest request, String userId) {
+        if (Blank.isEmpty(userId)) {
+            return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), ResultEnum.REQUEST_WRONGPARAMS.getRespMsg());
+        }
+        return userService.getUserById(request, userId);
+    }
+
+    /**
      * 新增角色
      *
      * @param roleName
@@ -81,15 +124,15 @@ public class UserController {
     /**
      * 分配角色
      *
-     * @param userId
-     * @param roleList
+     * @param request
+     * @param user
      * @return
      */
     @PostMapping("/assignRole")
-    public Result assignRole(String userId, List<Integer> roleList) {
-        if (StringUtils.isEmpty(userId) || StringUtils.isEmpty(roleList)) {
+    public Result assignRole(HttpServletRequest request, @RequestBody User user) {
+        if (StringUtils.isEmpty(user) || StringUtils.isEmpty(user.getUserId()) || StringUtils.isEmpty(user.getRoleList())) {
             return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), ResultEnum.REQUEST_WRONGPARAMS.getRespMsg());
         }
-        return userService.assignRole(userId, roleList);
+        return userService.assignRole(request, user.getUserId(), user.getRoleList());
     }
 }

+ 12 - 0
chuanyi_server/src/main/java/com/judong/chuanyiserver/dao/ConnectDao.java

@@ -3,10 +3,22 @@ package com.judong.chuanyiserver.dao;
 import com.judong.chuanyiserver.entity.ServerInformation;
 import org.springframework.stereotype.Repository;
 
+import java.util.List;
+
 @Repository
 public interface ConnectDao {
 
     Integer saveConnect(ServerInformation serverInformation);
 
     ServerInformation getServerInformation(String userId, String ipAddress);
+
+    ServerInformation getServerInformationById(int id);
+
+    List<ServerInformation> selectAllConnect(String userId);
+
+    ServerInformation getServerInformationEdit(int id, String userId, String ipAddress);
+
+    Integer editConnect(ServerInformation serverInformation);
+
+    Integer deleteConnect(int id, String userId);
 }

+ 15 - 5
chuanyi_server/src/main/java/com/judong/chuanyiserver/dao/UserDao.java

@@ -16,7 +16,7 @@ public interface UserDao {
 
     User getUserByName(String userName);
 
-    Integer addUser(String userId, String userName,String password);
+    Integer addUser(String userId, String userName, String password);
 
     Role getRoleByName(String roleName);
 
@@ -24,13 +24,23 @@ public interface UserDao {
 
     Integer addUserRole(String userId, int roleId);
 
-    List<Integer> getRoleListByUserId(String userId);
+    List<Integer> getRoleIdListByUserId(String userId);
 
-    List<Integer> getShareRoleList(String userId, List<Integer> roleList);
+    List<Role> getShareRoleList(String userId, List<Role> roleList);
 
-    Integer deleteNoShareRole(String userId, List<Integer> roleList);
+    List<Integer> getNoShareOldRoleList(String userId, List<Role> roleList);
 
-    Integer addRoleList(String userId, List<Integer> roleList);
+    Integer deleteUserRole(String userId, List<Integer> roleList);
+
+    Integer addRoleList(String userId, List<Role> roleList);
 
     List<Resource> getResourceByRoleList(List<Integer> roleList);
+
+    List<Role> getRoleListByUserId(String userId);
+
+    Long getUserCount();
+
+    List<User> getUserPage(Long startNum, Long limitNum);
+
+    Integer deleteUserRoleByUserId(String userId);
 }

+ 6 - 0
chuanyi_server/src/main/java/com/judong/chuanyiserver/entity/ServerInformation.java

@@ -3,6 +3,8 @@ package com.judong.chuanyiserver.entity;
 import lombok.Data;
 
 import java.io.Serializable;
+import java.util.Date;
+
 @Data
 public class ServerInformation implements Serializable {
 
@@ -35,4 +37,8 @@ public class ServerInformation implements Serializable {
      * ip协议类型
      */
     private Integer agreementType;
+    /**
+     * 创建时间
+     */
+    private Date createTime;
 }

+ 3 - 0
chuanyi_server/src/main/java/com/judong/chuanyiserver/entity/User.java

@@ -4,6 +4,7 @@ import lombok.Data;
 
 import java.io.Serializable;
 import java.util.Date;
+import java.util.List;
 
 @Data
 public class User implements Serializable {
@@ -35,4 +36,6 @@ public class User implements Serializable {
      */
     private Date lastLoginTime;
 
+    private List<Role> roleList;
+
 }

+ 18 - 0
chuanyi_server/src/main/java/com/judong/chuanyiserver/service/ConnectService.java

@@ -3,10 +3,28 @@ package com.judong.chuanyiserver.service;
 import cn.hutool.http.HttpRequest;
 import com.judong.chuanyiserver.entity.ServerInformation;
 import com.judong.chuanyiserver.util.Result;
+import org.jinterop.dcom.common.JIException;
+import org.openscada.opc.lib.common.AlreadyConnectedException;
+import org.openscada.opc.lib.common.NotConnectedException;
+import org.openscada.opc.lib.da.AddFailedException;
+import org.openscada.opc.lib.da.DuplicateGroupException;
 
 import javax.servlet.http.HttpServletRequest;
+import java.net.UnknownHostException;
 
 public interface ConnectService {
 
     Result saveConnect(HttpServletRequest request, ServerInformation serverInformation);
+
+    Result openConnect(int serverInformationId) throws AlreadyConnectedException, JIException, UnknownHostException, NotConnectedException, DuplicateGroupException, AddFailedException;
+
+    Result readItemValue(int id, String itemName) throws AlreadyConnectedException, JIException, UnknownHostException, NotConnectedException, DuplicateGroupException, AddFailedException;
+
+    Result testConnect(ServerInformation serverInformation) throws AlreadyConnectedException, JIException, UnknownHostException;
+
+    Result selectAllConnect(HttpServletRequest request);
+
+    Result editConnect(HttpServletRequest request, ServerInformation serverInformation);
+
+    Result deleteConnect(HttpServletRequest request, int id);
 }

+ 9 - 1
chuanyi_server/src/main/java/com/judong/chuanyiserver/service/UserService.java

@@ -1,7 +1,9 @@
 package com.judong.chuanyiserver.service;
 
+import com.judong.chuanyiserver.entity.Role;
 import com.judong.chuanyiserver.util.Result;
 
+import javax.servlet.http.HttpServletRequest;
 import java.util.List;
 
 public interface UserService {
@@ -40,5 +42,11 @@ public interface UserService {
     Result addRole(String roleName);
 
 
-    Result assignRole(String userId, List<Integer> roleList);
+    Result assignRole(HttpServletRequest request,String userId, List<Role> roleList);
+
+    Result userLoginOut(HttpServletRequest request);
+
+    Result getUserPage(HttpServletRequest request, int page, int limit);
+
+    Result getUserById(HttpServletRequest request, String userId);
 }

+ 193 - 12
chuanyi_server/src/main/java/com/judong/chuanyiserver/service/impl/ConnectServiceImpl.java

@@ -1,42 +1,223 @@
 package com.judong.chuanyiserver.service.impl;
 
 import cn.hutool.http.HttpRequest;
+import com.alibaba.fastjson.JSONObject;
 import com.judong.chuanyiserver.dao.ConnectDao;
 import com.judong.chuanyiserver.entity.ServerInformation;
 import com.judong.chuanyiserver.enums.ResultEnum;
 import com.judong.chuanyiserver.service.ConnectService;
-import com.judong.chuanyiserver.util.Blank;
-import com.judong.chuanyiserver.util.RedisUtil;
-import com.judong.chuanyiserver.util.Result;
-import com.judong.chuanyiserver.util.UserUtil;
+import com.judong.chuanyiserver.util.*;
+import lombok.extern.slf4j.Slf4j;
+import org.jinterop.dcom.common.JIException;
+import org.openscada.opc.dcom.da.OPCSERVERSTATE;
+import org.openscada.opc.lib.common.AlreadyConnectedException;
+import org.openscada.opc.lib.common.ConnectionInformation;
+import org.openscada.opc.lib.common.NotConnectedException;
+import org.openscada.opc.lib.da.*;
+import org.openscada.opc.lib.da.browser.Branch;
+import org.openscada.opc.lib.da.browser.Leaf;
+import org.openscada.opc.lib.da.browser.TreeBrowser;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
 import javax.servlet.http.HttpServletRequest;
+import java.net.UnknownHostException;
+import java.util.*;
+import java.util.concurrent.Executors;
 
 @Service
 @Transactional
+@Slf4j
 public class ConnectServiceImpl implements ConnectService {
 
     @Autowired
     private ConnectDao connectDao;
     @Autowired
-    private RedisUtil redisUtil;
+    private UserUtil userUtil;
 
     @Override
-    public Result saveConnect(HttpServletRequest request, ServerInformation serverInformation) {
+    public Result testConnect(ServerInformation serverInformation) throws AlreadyConnectedException, JIException, UnknownHostException {
+        // 连接信息
+        ConnectionInformation ci = new ConnectionInformation();
+        //服务
+        Server server = new Server(ci, Executors.newSingleThreadScheduledExecutor());
+        try {
+            ci.setHost(serverInformation.getIpAddress()); // 安装opc电脑IP
+            ci.setDomain(""); // 域,为空就行
+            ci.setUser(serverInformation.getIpUserName()); // 电脑上自己建好的用户名
+            ci.setPassword(serverInformation.getIpPassword()); // 用户名的密码
+            ci.setClsid("7BC0CC8E-482C-47CA-ABDC-0FE7F9C6E729"); // KEPServer的注册表ID,可以在“组件服务”里看到
+            // ci.setProgId("");
+
+            // 连接到服务
+            long start = System.currentTimeMillis();
+            server.connect();
+            long finish = System.currentTimeMillis();
+            log.info("连接耗费时间为:" + (finish - start) + "毫秒");
+            if (null == server.getServerState()) {
+                return Result.no(ResultEnum.NOT_FOUND.getRespCode(), "连接失败");
+            }
+            if (OPCSERVERSTATE.OPC_STATUS_RUNNING == server.getServerState().getServerState()) {
+                return Result.ok(true);
+            }
+        } finally {
+            //关闭连接使用
+            server.dispose();
+        }
+        return Result.no(ResultEnum.SERVER_ERROR.getRespCode(), ResultEnum.SERVER_ERROR.getRespMsg());
+    }
+
+    @Override
+    public synchronized Result saveConnect(HttpServletRequest request, ServerInformation serverInformation) {
         String token = request.getHeader("token");
         if (Blank.isEmpty(token)) {
             return Result.no(ResultEnum.UNAUTHORIZED.getRespCode(), ResultEnum.UNAUTHORIZED.getRespMsg());
         }
-//        System.out.println("值为:"+redisUtil.get(token));
-//        return Result.no(200,"1");
-        serverInformation.setUserId(UserUtil.getCurrentUser(token).getUserId());
-        ServerInformation si=connectDao.getServerInformation(UserUtil.getCurrentUser(token).getUserId(),serverInformation.getIpAddress());
-        if(Blank.isNotEmpty(si)){
+        String userId = userUtil.getCurrentUserId(token);
+        if (Blank.isEmpty(userId)){
+            return Result.no(ResultEnum.UNAUTHORIZED.getRespCode(), ResultEnum.UNAUTHORIZED.getRespMsg());
+        }
+        serverInformation.setUserId(userId);
+        ServerInformation si = connectDao.getServerInformation(userId, serverInformation.getIpAddress());
+        if (Blank.isNotEmpty(si)) {
+            return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), "该用户下此ip已被使保存,请更换其他ip");
+        }
+        if (connectDao.saveConnect(serverInformation) <= 0) {
+            return Result.no(ResultEnum.SERVER_ERROR.getRespCode(), "保存连接失败");
+        }
+        return Result.ok("保存连接成功");
+    }
+
+    @Override
+    public Result selectAllConnect(HttpServletRequest request) {
+        String token = request.getHeader("token");
+        if (Blank.isEmpty(token)) {
+            return Result.no(ResultEnum.UNAUTHORIZED.getRespCode(), ResultEnum.UNAUTHORIZED.getRespMsg());
+        }
+        String userId=userUtil.getCurrentUserId(token);
+        if(Blank.isEmpty(userId)){
+            return Result.no(ResultEnum.UNAUTHORIZED.getRespCode(), ResultEnum.UNAUTHORIZED.getRespMsg());
+        }
+        return Result.ok(connectDao.selectAllConnect(userId));
+    }
+
+    @Override
+    public synchronized Result editConnect(HttpServletRequest request, ServerInformation serverInformation) {
+        String token = request.getHeader("token");
+        if (Blank.isEmpty(token)) {
+            return Result.no(ResultEnum.UNAUTHORIZED.getRespCode(), ResultEnum.UNAUTHORIZED.getRespMsg());
+        }
+        String userId = userUtil.getCurrentUserId(token);
+        if (Blank.isEmpty(userId)){
+            return Result.no(ResultEnum.UNAUTHORIZED.getRespCode(), ResultEnum.UNAUTHORIZED.getRespMsg());
+        }
+        ServerInformation si = connectDao.getServerInformationEdit(serverInformation.getId(), userId, serverInformation.getIpAddress());
+        if (Blank.isNotEmpty(si)) {
             return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), "该用户下此ip已被使保存,请更换其他ip");
         }
-        return Result.ok(connectDao.saveConnect(serverInformation));
+        if (connectDao.editConnect(serverInformation) <= 0) {
+            return Result.no(ResultEnum.SERVER_ERROR.getRespCode(), "编辑连接失败");
+        }
+        return Result.ok("编辑连接成功");
+    }
+
+    @Override
+    public Result deleteConnect(HttpServletRequest request, int id) {
+        String token = request.getHeader("token");
+        if (Blank.isEmpty(token)) {
+            return Result.no(ResultEnum.UNAUTHORIZED.getRespCode(), ResultEnum.UNAUTHORIZED.getRespMsg());
+        }
+        String userId=userUtil.getCurrentUserId(token);
+        if (Blank.isEmpty(userId)){
+            return Result.no(ResultEnum.UNAUTHORIZED.getRespCode(), ResultEnum.UNAUTHORIZED.getRespMsg());
+        }
+        if (connectDao.deleteConnect(id, userId) <= 0) {
+            return Result.no(ResultEnum.SERVER_ERROR.getRespCode(), "删除连接失败");
+        }
+        return Result.ok("删除连接成功");
     }
+
+    @Override
+    public Result openConnect(int serverInformationId) throws AlreadyConnectedException, JIException, UnknownHostException, NotConnectedException, DuplicateGroupException, AddFailedException {
+        ServerInformation serverInformation = connectDao.getServerInformationById(serverInformationId);
+        //如果协议类型为OPCDA
+        if (serverInformation.getAgreementType() == ConstantStr.OPC_DA) {
+            ConnectionInformation ci = new ConnectionInformation();
+
+            ci.setHost(serverInformation.getIpAddress()); // 安装opc电脑IP
+            ci.setDomain(""); // 域,为空就行
+            ci.setUser(serverInformation.getIpUserName()); // 电脑上自己建好的用户名
+            ci.setPassword(serverInformation.getIpPassword()); // 用户名的密码
+            ci.setClsid("7BC0CC8E-482C-47CA-ABDC-0FE7F9C6E729"); // KEPServer的注册表ID,可以在“组件服务”里看到
+            // ci.setProgId("");
+
+            Server server = new Server(ci, Executors.newSingleThreadScheduledExecutor());
+            server.connect();
+            Item item = null;
+            if (null == server.getServerState()) {
+                return Result.no(ResultEnum.NOT_FOUND.getRespCode(), "连接失败");
+            }
+            if (OPCSERVERSTATE.OPC_STATUS_RUNNING == server.getServerState().getServerState()) {
+                JSONObject jsonObject = new JSONObject();
+                //获取服务器下所有ITEM列表,平面展示
+//                Collection<String> items = server.getFlatBrowser().browse();
+
+                //获取服务器下所有ITEM列表,树形展示
+//                TreeBrowser treeBrowser = server.getTreeBrowser();
+//                Branch browse = treeBrowser.browse();
+//                Collection<Branch> branches = browse.getBranches();
+//                for (Branch branch : branches) {
+//                    Collection<Leaf> leaves = branch.getLeaves();
+//                }
+                List<JSONObject> jsonObjectList = OpcDataUtil.generOpcDaTree(server);
+
+//                Group group = server.addGroup();
+//                Iterator<String> iterator = items.iterator();
+//                while (iterator.hasNext()) {
+//                    item = group.addItem(iterator.next());
+//                    log.info("读取出来的值为:" + OpcDataUtil.getVal(item.read(true).getValue()));
+//                }
+                jsonObject.put("tree", OpcDataUtil.generOpcDaTree(server));
+                return Result.ok(jsonObject);
+            }
+        }
+        return Result.no(ResultEnum.SERVER_ERROR.getRespCode(), ResultEnum.SERVER_ERROR.getRespMsg());
+    }
+
+    @Override
+    public Result readItemValue(int id, String itemName) throws AlreadyConnectedException, JIException, UnknownHostException, NotConnectedException, DuplicateGroupException, AddFailedException {
+        ServerInformation serverInformation = connectDao.getServerInformationById(id);
+        //如果协议类型为OPCDA
+        if (serverInformation.getAgreementType() == ConstantStr.OPC_DA) {
+            ConnectionInformation ci = new ConnectionInformation();
+
+            ci.setHost(serverInformation.getIpAddress()); // 安装opc电脑IP
+            ci.setDomain(""); // 域,为空就行
+            ci.setUser(serverInformation.getIpUserName()); // 电脑上自己建好的用户名
+            ci.setPassword(serverInformation.getIpPassword()); // 用户名的密码
+            ci.setClsid("7BC0CC8E-482C-47CA-ABDC-0FE7F9C6E729"); // KEPServer的注册表ID,可以在“组件服务”里看到
+            // ci.setProgId("");
+
+            Server server = new Server(ci, Executors.newSingleThreadScheduledExecutor());
+            server.connect();
+            if (null == server.getServerState()) {
+                return Result.no(ResultEnum.NOT_FOUND.getRespCode(), "连接失败");
+            }
+            if (OPCSERVERSTATE.OPC_STATUS_RUNNING == server.getServerState().getServerState()) {
+                Map<String, Object> map = new HashMap<>();
+                //获取服务器下所有ITEM列表,平面展示
+                Group group = server.addGroup();
+                Item item = group.addItem(itemName);
+                Map<String, Object> val = OpcDataUtil.getVal(item.read(true).getValue());
+                log.info("读取出来的Java类型为:" + val.get("javaType"));
+                log.info("读取出来的值为:" + val);
+                map.put("javaType", val.get("javaType"));
+                map.put("value", val);
+                return Result.ok(map);
+            }
+        }
+        return Result.no(ResultEnum.NOT_FOUND.getRespCode(), ResultEnum.NOT_FOUND.getRespMsg());
+    }
+
 }

+ 7 - 1
chuanyi_server/src/main/java/com/judong/chuanyiserver/service/impl/ServerInServiceImpl.java

@@ -19,12 +19,18 @@ public class ServerInServiceImpl implements ServerInService {
     @Autowired
     private ServerInDao serverInDao;
 
+    @Autowired
+    private UserUtil userUtil;
+
     @Override
     public Result selectServerByUser(HttpServletRequest request) {
         String token = request.getHeader("token");
         if (Blank.isEmpty(token)) {
             return Result.no(ResultEnum.UNAUTHORIZED.getRespCode(), ResultEnum.UNAUTHORIZED.getRespMsg());
         }
-        return Result.ok(serverInDao.selectServerByUser(UserUtil.getCurrentUser(token).getUserId()));
+        if (Blank.isEmpty(userUtil.getCurrentUserId(token))) {
+            return Result.no(ResultEnum.UNAUTHORIZED.getRespCode(), ResultEnum.UNAUTHORIZED.getRespMsg());
+        }
+        return Result.ok(serverInDao.selectServerByUser(userUtil.getCurrentUserId(token)));
     }
 }

+ 111 - 34
chuanyi_server/src/main/java/com/judong/chuanyiserver/service/impl/UserServiceImpl.java

@@ -13,6 +13,7 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
+import javax.servlet.http.HttpServletRequest;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.UUID;
@@ -27,6 +28,9 @@ public class UserServiceImpl implements UserService {
     @Autowired
     private RedisUtil redisUtil;
 
+    @Autowired
+    private UserUtil userUtil;
+
     /**
      * 前端用户登录
      *
@@ -41,14 +45,14 @@ public class UserServiceImpl implements UserService {
             return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), "用户名或密码错误");
         }
         JSONObject json = new JSONObject();
-        List<Integer> roleList = userDao.getRoleListByUserId(user.getUserId());
-        json.put("roleList", roleList);
-        List<Resource> resourceList = userDao.getResourceByRoleList(roleList);
+        List<Integer> roleIdList = userDao.getRoleIdListByUserId(user.getUserId());
+        json.put("roleIdList", roleIdList);
+        List<Resource> resourceList = userDao.getResourceByRoleList(roleIdList);
         json.put("resourceList", resourceList);
         //生成token
-        String token = TokenUtil.token(userName, 30);
+        String token = TokenUtil.token(userName, ConstantStr.HALF_HOUR);
         json.put("token", token);
-        redisUtil.set(token, user.getUserId(),ConstantStr.HALF_HOUR);
+        redisUtil.set(token, user.getUserId(), ConstantStr.HALF_HOUR);
         return Result.ok(json);
     }
 
@@ -65,20 +69,20 @@ public class UserServiceImpl implements UserService {
         if (Blank.isEmpty(user)) {
             return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), "用户名或密码错误");
         }
-        List<Integer> roleList = userDao.getRoleListByUserId(user.getUserId());
-        if (Blank.isEmpty(roleList)) {
+        List<Integer> roleIdList = userDao.getRoleIdListByUserId(user.getUserId());
+        if (Blank.isEmpty(roleIdList)) {
             return Result.no(ResultEnum.NO_OPERATION_AUTHORITY.getRespCode(), ResultEnum.NO_OPERATION_AUTHORITY.getRespMsg());
         }
-        for (Integer role : roleList) {
-            if (role == ConstantStr.ROLE_ADMIN) {
+        for (Integer roleId : roleIdList) {
+            if (roleId == ConstantStr.ROLE_ADMIN) {
                 JSONObject json = new JSONObject();
-                json.put("roleList", roleList);
-                List<Resource> resourceList = userDao.getResourceByRoleList(roleList);
+                json.put("roleIdList", roleIdList);
+                List<Resource> resourceList = userDao.getResourceByRoleList(roleIdList);
                 json.put("resourceList", resourceList);
                 //生成token
-                String token = TokenUtil.token(userName, 30);
+                String token = TokenUtil.token(userName, ConstantStr.HALF_HOUR);
                 json.put("token", token);
-                redisUtil.set(token, user.getUserId());
+                redisUtil.set(token, user.getUserId(), ConstantStr.HALF_HOUR);
                 return Result.ok(json);
             }
         }
@@ -128,30 +132,103 @@ public class UserServiceImpl implements UserService {
     }
 
     @Override
-    public synchronized Result assignRole(String userId, List<Integer> roleList) {
-        //查询出拥有新旧角色集合拥有的公共角色
-        List<Integer> shareRoleList = userDao.getShareRoleList(userId, roleList);
-        //增加新增的角色信息
-        List<Integer> newRoleList = new ArrayList<>();
-        if (Blank.isEmpty(shareRoleList)) {
-            newRoleList = roleList;
-        } else {
-            for (int i = 0; i < roleList.size(); i++) {
-                Boolean flage = false;
-                for (int j = 0; j < shareRoleList.size(); j++) {
-                    if (roleList.get(i) == shareRoleList.get(j)) {
-                        flage = true;
-                        break;
+    public synchronized Result assignRole(HttpServletRequest request,String userId, List<Role> roleList) {
+        String token = request.getHeader("token");
+        if (Blank.isEmpty(token)) {
+            return Result.no(ResultEnum.UNAUTHORIZED.getRespCode(), ResultEnum.UNAUTHORIZED.getRespMsg());
+        }
+        String tokenUserId = userUtil.getCurrentUserId(token);
+        if (Blank.isEmpty(tokenUserId)) {
+            return Result.no(ResultEnum.UNAUTHORIZED.getRespCode(), ResultEnum.UNAUTHORIZED.getRespMsg());
+        }
+        List<Integer> roleIdList = userDao.getRoleIdListByUserId(tokenUserId);
+        for (Integer roleId : roleIdList) {
+            if (roleId == ConstantStr.ROLE_ADMIN) {
+                //查询出拥有新旧角色集合拥有的公共角色
+                List<Role> shareRoleList = userDao.getShareRoleList(userId, roleList);
+                //增加新增的角色信息
+                List<Role> newRoleList = new ArrayList<>();
+                if (Blank.isEmpty(shareRoleList)) {
+                    newRoleList = roleList;
+                    //删除原来的久角色
+                    userDao.deleteUserRoleByUserId(userId);
+                } else {
+                    for (int i = 0; i < roleList.size(); i++) {
+                        Boolean flage = false;
+                        for (int j = 0; j < shareRoleList.size(); j++) {
+                            if (roleList.get(i).getId() == shareRoleList.get(j).getId()) {
+                                flage = true;
+                                break;
+                            }
+                        }
+                        if (!flage) {
+                            newRoleList.add(roleList.get(i));
+                        }
+                    }
+                    //删除不在公共角色的旧角色Id
+                    List<Integer> oldRoleList = userDao.getNoShareOldRoleList(userId, shareRoleList);
+                    if(Blank.isNotEmpty(oldRoleList)){
+                        userDao.deleteUserRole(userId, oldRoleList);
                     }
                 }
-                if (!flage) {
-                    newRoleList.add(roleList.get(i));
-                }
+                userDao.addRoleList(userId, newRoleList);
+                return Result.ok("分配角色成功");
             }
         }
-        //删除不在公共角色中的旧角色信息
-        userDao.deleteNoShareRole(userId, roleList);
-        userDao.addRoleList(userId, newRoleList);
-        return Result.ok("分配角色成功");
+        return Result.no(ResultEnum.NO_OPERATION_AUTHORITY.getRespCode(), ResultEnum.NO_OPERATION_AUTHORITY.getRespMsg());
+    }
+
+    @Override
+    public Result userLoginOut(HttpServletRequest request) {
+        String token = request.getHeader("token");
+        redisUtil.del(token);
+        return Result.ok(true);
+    }
+
+    @Override
+    public Result getUserPage(HttpServletRequest request, int page, int limit) {
+        String token = request.getHeader("token");
+        if (Blank.isEmpty(token)) {
+            return Result.no(ResultEnum.UNAUTHORIZED.getRespCode(), ResultEnum.UNAUTHORIZED.getRespMsg());
+        }
+        String userId = userUtil.getCurrentUserId(token);
+        if (Blank.isEmpty(userId)) {
+            return Result.no(ResultEnum.UNAUTHORIZED.getRespCode(), ResultEnum.UNAUTHORIZED.getRespMsg());
+        }
+        List<Integer> roleIdList = userDao.getRoleIdListByUserId(userId);
+        for (Integer roleId : roleIdList) {
+            if (roleId == ConstantStr.ROLE_ADMIN) {
+                JSONObject jsonObject = new JSONObject();
+                Long count = userDao.getUserCount();
+                Long startNum = Long.valueOf((page - 1) * limit);
+                List<User> userList = userDao.getUserPage(startNum, Long.valueOf(limit));
+                jsonObject.put("count", count);
+                jsonObject.put("userList", userList);
+                return Result.ok(jsonObject);
+            }
+        }
+        return Result.no(ResultEnum.NO_OPERATION_AUTHORITY.getRespCode(), ResultEnum.NO_OPERATION_AUTHORITY.getRespMsg());
+    }
+
+    @Override
+    public Result getUserById(HttpServletRequest request, String userId) {
+        String token = request.getHeader("token");
+        if (Blank.isEmpty(token)) {
+            return Result.no(ResultEnum.UNAUTHORIZED.getRespCode(), ResultEnum.UNAUTHORIZED.getRespMsg());
+        }
+        String tokenUserId = userUtil.getCurrentUserId(token);
+        if (Blank.isEmpty(tokenUserId)) {
+            return Result.no(ResultEnum.UNAUTHORIZED.getRespCode(), ResultEnum.UNAUTHORIZED.getRespMsg());
+        }
+        List<Integer> roleIdList = userDao.getRoleIdListByUserId(tokenUserId);
+        for (Integer roleId : roleIdList) {
+            if (roleId == ConstantStr.ROLE_ADMIN) {
+                return Result.ok(userDao.getUserById(userId));
+            }
+        }
+        if (userId.equals(tokenUserId)) {
+            return Result.ok(userDao.getUserById(userId));
+        }
+        return Result.no(ResultEnum.NO_OPERATION_AUTHORITY.getRespCode(), ResultEnum.NO_OPERATION_AUTHORITY.getRespMsg());
     }
 }

+ 14 - 6
chuanyi_server/src/main/java/com/judong/chuanyiserver/util/ConstantStr.java

@@ -12,11 +12,19 @@ public class ConstantStr {
 
 
     //redis过期时间设置
-    public static final long HALF_MINUTE = 1000 * 30;
-    public static final long ONE_MINUTE = 1000 * 60;
-    public static final long FIVE_MINUTE = 1000 * 60 * 5;
-    public static final long TEN_MINUTE = 1000 * 60 * 10;
-    public static final long HALF_HOUR = 1000 * 60 * 30;
-    public static final long ONE_HOUR = 1000 * 60 * 60;
+    public static final long HALF_MINUTE = 30;
+    public static final long ONE_MINUTE = 60;
+    public static final long FIVE_MINUTE = 60 * 5;
+    public static final long TEN_MINUTE = 60 * 10;
+    public static final long HALF_HOUR = 60 * 30;
+    public static final long ONE_HOUR = 60 * 60;
+
+    //用户状态
+    public static final int NORMAL_USE = 0;
+    public static final int LOCK_USER = 1;
+
+    //OPC协议类型
+    public static final int OPC_DA = 0;
+    public static final int OPC_UA = 1;
 
 }

+ 65 - 2
chuanyi_server/src/main/java/com/judong/chuanyiserver/util/OpcDataUtil.java

@@ -1,13 +1,76 @@
 package com.judong.chuanyiserver.util;
 
+import com.alibaba.fastjson.JSONObject;
 import org.jinterop.dcom.common.JIException;
 import org.jinterop.dcom.core.JIVariant;
+import org.openscada.opc.lib.da.Server;
+import org.openscada.opc.lib.da.browser.Branch;
+import org.openscada.opc.lib.da.browser.Leaf;
+import org.openscada.opc.lib.da.browser.TreeBrowser;
 
-import java.util.HashMap;
-import java.util.Map;
+import java.net.UnknownHostException;
+import java.util.*;
 
 public class OpcDataUtil {
 
+    /**
+     * 获取OPCDA服务器的tree
+     *
+     * @return
+     */
+    public static List<JSONObject> generOpcDaTree(Server server) throws JIException, UnknownHostException {
+        List<JSONObject> jsonList = new ArrayList<>();
+        //获取服务器下所有ITEM列表,树形展示
+        TreeBrowser treeBrowser = server.getTreeBrowser();
+        Branch browse = treeBrowser.browse();
+        Collection<Branch> branches = browse.getBranches();
+        for (Branch branch : branches) {
+//            Collection<Leaf> leaves = branch.getLeaves();
+            if (Blank.isNotEmpty(branch.getBranches())) {
+                JSONObject jsonObject = new JSONObject();
+                jsonObject.put("label", branch.getName());
+                jsonObject.put("children", recursionChildren(branch.getBranches()));
+                jsonList.add(jsonObject);
+            }
+        }
+        return jsonList;
+    }
+
+    public static List<JSONObject> recursionChildren(Collection<Branch> branchCollection) {
+        List<JSONObject> jsonList = new ArrayList<>();
+        for (Branch branch : branchCollection) {
+            if (Blank.isNotEmpty(branch.getBranches())) {
+                JSONObject jsonObject = new JSONObject();
+                jsonObject.put("label", branch.getName());
+                jsonObject.put("children", recursionChildren(branch.getBranches()));
+                jsonList.add(jsonObject);
+            } else {
+                JSONObject jsonObject = new JSONObject();
+                jsonObject.put("label", branch.getName());
+                jsonObject.put("children", generLeaf(branch.getLeaves()));
+                jsonList.add(jsonObject);
+            }
+        }
+        return jsonList;
+    }
+
+    public static List<JSONObject> generLeaf(Collection<Leaf> leaves) {
+        List<JSONObject> jsonObjectList = new ArrayList<>();
+        for (Leaf leaf : leaves) {
+            JSONObject jsonObject = new JSONObject();
+            jsonObject.put("label", leaf.getName());
+            jsonObjectList.add(jsonObject);
+        }
+        return jsonObjectList;
+    }
+
+    /**
+     * 通过itemid获取响应的值
+     *
+     * @param var
+     * @return
+     * @throws JIException
+     */
     public static Map<String, Object> getVal(JIVariant var) throws JIException {
         Map<String, Object> map = new HashMap<>();
         Object value;

+ 17 - 17
chuanyi_server/src/main/java/com/judong/chuanyiserver/util/TokenUtil.java

@@ -14,54 +14,54 @@ public class TokenUtil {
     //设置过期时间
     // private static final long EXPIRE_DATE=30*60*100000;
     //token秘钥
-     private static final String TOKEN_SECRET = "ZCfasfhuaUUHufguGuwu2020BQWE";
+    private static final String TOKEN_SECRET = "ZCfasfhuaUUHufguGuwu2020BQWE";
 
-    public static String token (String username, int expire){
+    public static String token(String username, Long expire) {
 
         String token = "";
         try {
             //过期时间
-            Date date = new Date(System.currentTimeMillis() + expire  * 60000L);
+            Date date = new Date(System.currentTimeMillis() + expire * 1000);
             //秘钥及加密算法
             Algorithm algorithm = Algorithm.HMAC256(TOKEN_SECRET);
             //设置头部信息
-            Map<String,Object> header = new HashMap<>();
-            header.put("typ","JWT");
-            header.put("alg","HS256");
+            Map<String, Object> header = new HashMap<>();
+            header.put("typ", "JWT");
+            header.put("alg", "HS256");
             //携带username信息,生成签名
             token = JWT.create()
                     .withHeader(header)
-                    .withClaim("username",username)
+                    .withClaim("username", username)
                     .withExpiresAt(date)
                     .sign(algorithm);
-        }catch (Exception e){
+        } catch (Exception e) {
             e.printStackTrace();
-            return  null;
+            return null;
         }
         return token;
     }
 
     /**
-     * @desc   验证token,通过返回true
+     * @desc 验证token,通过返回true
      * @params [token]需要校验的串
      **/
-    public static boolean verify(String token){
+    public static boolean verify(String token) {
         try {
             Algorithm algorithm = Algorithm.HMAC256(TOKEN_SECRET);
             JWTVerifier verifier = JWT.require(algorithm).build();
             DecodedJWT jwt = verifier.verify(token);
             return true;
-        }catch (Exception e){
+        } catch (Exception e) {
             e.printStackTrace();
-            return  false;
+            return false;
         }
     }
-    
+
     public static Claim parseJWT(String jwt) throws Exception {
-    	 Claim claims = JWT.decode(jwt).getClaims().get("username");
-    		    return claims;
+        Claim claims = JWT.decode(jwt).getClaims().get("username");
+        return claims;
 
 
-    	}
+    }
 
 }

+ 19 - 3
chuanyi_server/src/main/java/com/judong/chuanyiserver/util/UserUtil.java

@@ -3,14 +3,16 @@ package com.judong.chuanyiserver.util;
 import com.judong.chuanyiserver.dao.UserDao;
 import com.judong.chuanyiserver.entity.User;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
 
+@Component
 public class UserUtil {
 
     @Autowired
-    private static RedisUtil redisUtil;
+    private RedisUtil redisUtil;
 
     @Autowired
-    private static UserDao userDao;
+    private UserDao userDao;
 
     /**
      * 通过token获取当前登录人信息
@@ -18,11 +20,25 @@ public class UserUtil {
      * @param token
      * @return
      */
-    public static User getCurrentUser(String token) {
+    public User getCurrentUser(String token) {
         String userId = (String) redisUtil.get(token);
         if (Blank.isEmpty(userId)) {
             return null;
         }
         return userDao.getUserById(userId);
     }
+
+    /**
+     * 通过token获取当前登录人Id
+     *
+     * @param token
+     * @return
+     */
+    public String getCurrentUserId(String token){
+        String userId = (String) redisUtil.get(token);
+        if (Blank.isEmpty(userId)) {
+            return null;
+        }
+        return userId;
+    }
 }

+ 36 - 4
chuanyi_server/src/main/resources/mapper/ConnectDao.xml

@@ -3,13 +3,45 @@
 <mapper namespace="com.judong.chuanyiserver.dao.ConnectDao">
 
     <insert id="saveConnect">
-        insert into server_information(use_id, ip_address, ip_user_name, ip_password, agreement_type)
-            value (#{userId}, #{ipAddress}, #{ipUserName}, #{ipPassword}, #{agreementType})
+        insert into server_information(user_id, ip_address, ip_user_name, ip_password, agreement_type, create_time)
+            value (#{userId}, #{ipAddress}, #{ipUserName}, #{ipPassword}, #{agreementType}, now())
     </insert>
+    <update id="editConnect">
+        update server_information
+        set ip_address=#{ipAddress},
+            ip_user_name=#{ipUserName},
+            ip_password=#{ipPassword},
+            agreement_type=#{agreementType}
+        where id = #{id}
+    </update>
+    <delete id="deleteConnect">
+        delete
+        from server_information
+        where id = #{id}
+          and user_id = #{userId}
+    </delete>
     <select id="getServerInformation" resultType="com.judong.chuanyiserver.entity.ServerInformation">
-        select id, use_id, ip_address, ip_user_name, ip_password, agreement_type
+        select id, user_id, ip_address, ip_user_name, ip_password, agreement_type, create_time
+        from server_information
+        where user_id = #{userId}
+          and ip_address = #{ipAddress}
+    </select>
+    <select id="getServerInformationById" resultType="com.judong.chuanyiserver.entity.ServerInformation">
+        select id, user_id, ip_address, ip_user_name, ip_password, agreement_type, create_time
+        from server_information
+        where id = #{id}
+    </select>
+    <select id="selectAllConnect" resultType="com.judong.chuanyiserver.entity.ServerInformation">
+        select id, user_id, ip_address, ip_user_name, ip_password, agreement_type, create_time
+        from server_information
+        where user_id = #{userId}
+        order by create_time desc
+    </select>
+    <select id="getServerInformationEdit" resultType="com.judong.chuanyiserver.entity.ServerInformation">
+        select id, user_id, ip_address, ip_user_name, ip_password, agreement_type, create_time
         from server_information
-        where use_id = #{userId}
+        where user_id = #{userId}
           and ip_address = #{ipAddress}
+          and id != #{id}
     </select>
 </mapper>

+ 47 - 16
chuanyi_server/src/main/resources/mapper/UserDao.xml

@@ -53,7 +53,7 @@
     <insert id="addRoleList">
         INSERT INTO user_role (user_id, role_id) VALUES
         <foreach collection="roleList" item="role" index="index" separator=",">
-            (#{userId},#{role})
+            (#{userId},#{role.id})
         </foreach>
     </insert>
 
@@ -63,36 +63,67 @@
         FROM role
         WHERE role_name = #{roleName}
     </select>
-    <select id="getRoleListByUserId" resultType="java.lang.Integer">
+    <select id="getRoleIdListByUserId" resultType="java.lang.Integer">
         SELECT role_id
         from user_role
         where user_id = #{userId}
     </select>
-    <select id="getShareRoleList" resultType="java.lang.Integer">
-        SELECT role_id FROM user_role where user_id=#{userId} and role_id in
+    <select id="getShareRoleList" resultType="com.judong.chuanyiserver.entity.Role">
+        select id, role_name
+        from role where id in
+        ( SELECT role_id FROM user_role where user_id=#{userId} and role_id in
         <foreach collection="roleList" item="role" index="index" open="(" close=")" separator=",">
-            #{role}
-        </foreach>
+            #{role.id}
+        </foreach>)
+
     </select>
 
-    <delete id="deleteNoShareRole">
-        DELETE FROM user_role WHERE user_id=#{userId} AND
-        role_id in
-        (SELECT role_id FROM user_role WHERE user_id=#{userId} AND NOT IN
-        (SELECT role_id FROM user_role where user_id=#{userId}
-        and role_id in
-        <foreach collection="roleList" item="role" index="index" open="(" close=")" separator=",">
+    <delete id="deleteUserRole">
+        delete from user_role where user_id=#{userId}
+        AND role_id in (
+        <foreach collection="roleList" item="role" index="index" separator=",">
             #{role}
         </foreach>)
     </delete>
+    <delete id="deleteUserRoleByUserId">
+        delete
+        from user_role
+        where user_id = #{userId}
+    </delete>
 
     <select id="getResourceByRoleList" resultType="com.judong.chuanyiserver.entity.Resource">
         SELECT id, resource_url, resource_name FROM resource
         WHERE id in (
         SELECT resource_id FROM role_resource WHERE
-        role_id in
-        <foreach collection="roleList" item="role" index="index" open="(" close=")" separator=",">
+        role_id in (
+        <foreach collection="roleList" item="role" index="index" separator=",">
             #{role}
-        </foreach>)
+        </foreach>))
+    </select>
+    <select id="getRoleListByUserId" resultType="com.judong.chuanyiserver.entity.Role">
+        select id, role_name
+        from role
+        where id in (
+            SELECT role_id
+            from user_role
+            where user_id = #{userId})
+    </select>
+    <select id="getUserCount" resultType="java.lang.Long">
+        select count(*)
+        from user;
+    </select>
+    <select id="getUserPage" resultType="com.judong.chuanyiserver.entity.User">
+        select user_id, user_name, password, create_date, last_login_time, state
+        from user
+        order by create_date DESC
+        limit #{startNum},#{limitNum}
+    </select>
+    <select id="getNoShareOldRoleList" resultType="java.lang.Integer">
+        SELECT role_id FROM user_role WHERE user_id=#{userId} AND role_id NOT IN
+        (
+        <foreach collection="roleList" item="role" index="index" separator=",">
+            #{role.id}
+        </foreach>
+        )
     </select>
 </mapper>