From 5954efd25ea4517f6f1f597045e5ede81ac978cd Mon Sep 17 00:00:00 2001 From: wenc000 <450292408@qq.com> Date: Tue, 30 Jul 2019 23:17:41 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E4=BA=BA=E5=91=98=E9=80=89?= =?UTF-8?q?=E6=8B=A9=E6=8E=A7=E4=BB=B6=EF=BC=8C=E4=BF=AE=E6=94=B9restTempl?= =?UTF-8?q?ate=E8=AF=B7=E6=B1=82=E6=96=B9=E5=BC=8F=EF=BC=8C=E4=BF=AE?= =?UTF-8?q?=E6=94=B9tree=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/cm/common/plugin/IApiConsts.java | 17 +- .../controller/apis/auth/AuthController.java | 2 + .../apis/department/DepartmentController.java | 46 +++++ .../controller/apis/user/UserController.java | 58 ++++++ .../routes/user/UserRouteController.java | 35 ++++ .../department/IDepartmentService.java | 29 +++ .../impl/DepartmentServiceImpl.java | 42 ++++ .../service/menu/impl/MenuServiceImpl.java | 2 +- .../oauth/service/user/IUserService.java | 36 ++++ .../service/user/impl/UserServiceImpl.java | 51 +++++ .../common/plugin/utils/RestTemplateUtil.java | 15 +- .../user/select-department-user.html | 193 ++++++++++++++++++ .../main/resources/templates/tree/tree.html | 2 +- 13 files changed, 519 insertions(+), 9 deletions(-) create mode 100644 cloud-common-plugin-oauth/src/main/java/com/cm/common/plugin/oauth/controller/apis/department/DepartmentController.java create mode 100644 cloud-common-plugin-oauth/src/main/java/com/cm/common/plugin/oauth/controller/apis/user/UserController.java create mode 100644 cloud-common-plugin-oauth/src/main/java/com/cm/common/plugin/oauth/controller/routes/user/UserRouteController.java create mode 100644 cloud-common-plugin-oauth/src/main/java/com/cm/common/plugin/oauth/service/department/IDepartmentService.java create mode 100644 cloud-common-plugin-oauth/src/main/java/com/cm/common/plugin/oauth/service/department/impl/DepartmentServiceImpl.java create mode 100644 cloud-common-plugin-oauth/src/main/java/com/cm/common/plugin/oauth/service/user/IUserService.java create mode 100644 cloud-common-plugin-oauth/src/main/java/com/cm/common/plugin/oauth/service/user/impl/UserServiceImpl.java create mode 100644 cloud-common-plugin-oauth/src/main/resources/templates/user/select-department-user.html diff --git a/cloud-common-plugin-oauth/src/main/java/com/cm/common/plugin/IApiConsts.java b/cloud-common-plugin-oauth/src/main/java/com/cm/common/plugin/IApiConsts.java index 5e5229d..2f97687 100644 --- a/cloud-common-plugin-oauth/src/main/java/com/cm/common/plugin/IApiConsts.java +++ b/cloud-common-plugin-oauth/src/main/java/com/cm/common/plugin/IApiConsts.java @@ -12,6 +12,21 @@ public interface IApiConsts { /** * 菜单列表 */ - String MENU_LIST = "%s/resource/menu/listmenubyclientid/%s"; + String LIST_MENU = "%s/resource/menu/listmenubyclientid/%s"; + + /** + * 父ID获取部门列表 + */ + String LIST_DEPARTMENT = "%s/resource/department/listdepartments/%s"; + + /** + * 组织部门用户列表 + */ + String LIST_DEPARTMENT_USER = "%s/resource/user/listdepartmentusers/%s"; + + /** + * 用户列表,通过ID获取 + */ + String LIST_USER_BY_ID = "%s/resource/user/listuserbyids"; } diff --git a/cloud-common-plugin-oauth/src/main/java/com/cm/common/plugin/oauth/controller/apis/auth/AuthController.java b/cloud-common-plugin-oauth/src/main/java/com/cm/common/plugin/oauth/controller/apis/auth/AuthController.java index 1025bd6..63db1e0 100644 --- a/cloud-common-plugin-oauth/src/main/java/com/cm/common/plugin/oauth/controller/apis/auth/AuthController.java +++ b/cloud-common-plugin-oauth/src/main/java/com/cm/common/plugin/oauth/controller/apis/auth/AuthController.java @@ -4,6 +4,7 @@ import com.cm.common.base.AbstractController; import com.cm.common.constants.ISystemConstant; import com.cm.common.result.SuccessResultData; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.security.oauth2.client.OAuth2ClientContext; import org.springframework.security.oauth2.common.OAuth2AccessToken; import org.springframework.web.bind.annotation.GetMapping; @@ -22,6 +23,7 @@ import org.springframework.web.bind.annotation.RestController; public class AuthController extends AbstractController { @Autowired + @Qualifier("oauth2ClientContext") private OAuth2ClientContext oAuth2ClientContext; /** diff --git a/cloud-common-plugin-oauth/src/main/java/com/cm/common/plugin/oauth/controller/apis/department/DepartmentController.java b/cloud-common-plugin-oauth/src/main/java/com/cm/common/plugin/oauth/controller/apis/department/DepartmentController.java new file mode 100644 index 0000000..9c03711 --- /dev/null +++ b/cloud-common-plugin-oauth/src/main/java/com/cm/common/plugin/oauth/controller/apis/department/DepartmentController.java @@ -0,0 +1,46 @@ +package com.cm.common.plugin.oauth.controller.apis.department; + +import com.alibaba.fastjson.JSONArray; +import com.cm.common.base.AbstractController; +import com.cm.common.constants.ISystemConstant; +import com.cm.common.exception.SearchException; +import com.cm.common.plugin.oauth.service.department.IDepartmentService; +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.Map; + +/** + * When you feel like quitting. Think about why you started + * 当你想要放弃的时候,想想当初你为何开始 + * + * @ClassName: DepartmentController + * @Description: 组织部门 + * @Author: WangGeng + * @Date: 2019-07-29 21:30 + * @Version: 1.0 + **/ +@RestController +@RequestMapping(ISystemConstant.API_PREFIX + "/department") +public class DepartmentController extends AbstractController { + + @Autowired + private IDepartmentService departmentService; + + @GetMapping("listdepartments") + public JSONArray listDepartments() throws SearchException { + Map params = requestParams(); + String parentId = "0"; + if (!StringUtils.isBlank(params.get("id") == null ? null : params.get("id").toString())) { + parentId = params.get("id").toString(); + } + params.put("parentId", parentId); + return departmentService.listDepartments(params); + } + +} diff --git a/cloud-common-plugin-oauth/src/main/java/com/cm/common/plugin/oauth/controller/apis/user/UserController.java b/cloud-common-plugin-oauth/src/main/java/com/cm/common/plugin/oauth/controller/apis/user/UserController.java new file mode 100644 index 0000000..0000719 --- /dev/null +++ b/cloud-common-plugin-oauth/src/main/java/com/cm/common/plugin/oauth/controller/apis/user/UserController.java @@ -0,0 +1,58 @@ +package com.cm.common.plugin.oauth.controller.apis.user; + +import com.alibaba.fastjson.JSONArray; +import com.cm.common.base.AbstractController; +import com.cm.common.constants.ISystemConstant; +import com.cm.common.exception.SearchException; +import com.cm.common.plugin.oauth.service.user.IUserService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +import java.util.Map; + +/** + * When you feel like quitting. Think about why you started + * 当你想要放弃的时候,想想当初你为何开始 + * + * @ClassName: UserController + * @Description: 用户 + * @Author: WangGeng + * @Date: 2019-07-29 21:30 + * @Version: 1.0 + **/ +@RestController +@RequestMapping(ISystemConstant.API_PREFIX + "/user") +public class UserController extends AbstractController { + + @Autowired + private IUserService userService; + + /** + * 部门人员列表 + * + * @param departmentId + * @return + * @throws SearchException + */ + @GetMapping("listdepartmentusers/{departmentId}") + public JSONArray listDepartmentUsers(@PathVariable("departmentId") String departmentId) throws SearchException { + Map params = getParams(); + params.put("departmentId", departmentId); + return userService.listDepartmentUsers(params); + } + + /** + * 通过用户ID获取用户列表 + * + * @param userIds + * @return + * @throws SearchException + */ + @GetMapping("listuserbyids/{userIds}") + public JSONArray listUserByIds(@PathVariable("userIds") String userIds) throws SearchException { + Map params = getParams(); + params.put("userIds", userIds); + return userService.listUserByIds(params); + } + +} diff --git a/cloud-common-plugin-oauth/src/main/java/com/cm/common/plugin/oauth/controller/routes/user/UserRouteController.java b/cloud-common-plugin-oauth/src/main/java/com/cm/common/plugin/oauth/controller/routes/user/UserRouteController.java new file mode 100644 index 0000000..7f757de --- /dev/null +++ b/cloud-common-plugin-oauth/src/main/java/com/cm/common/plugin/oauth/controller/routes/user/UserRouteController.java @@ -0,0 +1,35 @@ +package com.cm.common.plugin.oauth.controller.routes.user; + +import com.cm.common.base.AbstractController; +import com.cm.common.constants.ISystemConstant; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.servlet.ModelAndView; + +/** + * When you feel like quitting. Think about why you started + * 当你想要放弃的时候,想想当初你为何开始 + * + * @ClassName: UserRouteController + * @Description: 用户 + * @Author: WangGeng + * @Date: 2019-07-29 21:22 + * @Version: 1.0 + **/ +@Controller +@RequestMapping(ISystemConstant.ROUTE_PREFIX + "/user") +public class UserRouteController extends AbstractController { + + /** + * 部门用户页面 + * + * @return + */ + @GetMapping("departmentuser") + public ModelAndView departmentUser() { + ModelAndView mv = new ModelAndView("user/select-department-user"); + return mv; + } + +} diff --git a/cloud-common-plugin-oauth/src/main/java/com/cm/common/plugin/oauth/service/department/IDepartmentService.java b/cloud-common-plugin-oauth/src/main/java/com/cm/common/plugin/oauth/service/department/IDepartmentService.java new file mode 100644 index 0000000..ab9559e --- /dev/null +++ b/cloud-common-plugin-oauth/src/main/java/com/cm/common/plugin/oauth/service/department/IDepartmentService.java @@ -0,0 +1,29 @@ +package com.cm.common.plugin.oauth.service.department; + +import com.alibaba.fastjson.JSONArray; +import com.cm.common.exception.SearchException; + +import java.util.Map; + +/** + * When you feel like quitting. Think about why you started + * 当你想要放弃的时候,想想当初你为何开始 + * + * @ClassName: IDepartmentService + * @Description: 组织部门 + * @Author: WangGeng + * @Date: 2019-07-29 21:45 + * @Version: 1.0 + **/ +public interface IDepartmentService { + + /** + * 组织部门列表 + * + * @param params + * @return + * @throws SearchException + */ + JSONArray listDepartments(Map params) throws SearchException; + +} diff --git a/cloud-common-plugin-oauth/src/main/java/com/cm/common/plugin/oauth/service/department/impl/DepartmentServiceImpl.java b/cloud-common-plugin-oauth/src/main/java/com/cm/common/plugin/oauth/service/department/impl/DepartmentServiceImpl.java new file mode 100644 index 0000000..a2b73c7 --- /dev/null +++ b/cloud-common-plugin-oauth/src/main/java/com/cm/common/plugin/oauth/service/department/impl/DepartmentServiceImpl.java @@ -0,0 +1,42 @@ +package com.cm.common.plugin.oauth.service.department.impl; + +import com.alibaba.fastjson.JSONArray; +import com.cm.common.base.AbstractService; +import com.cm.common.config.properties.ApiPathProperties; +import com.cm.common.exception.SearchException; +import com.cm.common.plugin.IApiConsts; +import com.cm.common.plugin.oauth.service.department.IDepartmentService; +import com.cm.common.plugin.utils.RestTemplateUtil; +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.Map; + +/** + * When you feel like quitting. Think about why you started + * 当你想要放弃的时候,想想当初你为何开始 + * + * @ClassName: DepartmentServiceImpl + * @Description: 组织部门 + * @Author: WangGeng + * @Date: 2019-07-29 21:46 + * @Version: 1.0 + **/ +@Service +public class DepartmentServiceImpl extends AbstractService implements IDepartmentService { + + @Autowired + private RestTemplateUtil restTemplateUtil; + @Autowired + private ApiPathProperties apiPathProperties; + + @Override + public JSONArray listDepartments(Map params) throws SearchException { + String result = restTemplateUtil.doPostForm(String.format(IApiConsts.LIST_DEPARTMENT, apiPathProperties.getUserCenter(), params.get("parentId").toString()), params); + if (StringUtils.isBlank(result)) { + throw new SearchException("获取组织部门列表失败"); + } + return JSONArray.parseArray(result); + } +} diff --git a/cloud-common-plugin-oauth/src/main/java/com/cm/common/plugin/oauth/service/menu/impl/MenuServiceImpl.java b/cloud-common-plugin-oauth/src/main/java/com/cm/common/plugin/oauth/service/menu/impl/MenuServiceImpl.java index 3ffdd0d..fd79e4c 100644 --- a/cloud-common-plugin-oauth/src/main/java/com/cm/common/plugin/oauth/service/menu/impl/MenuServiceImpl.java +++ b/cloud-common-plugin-oauth/src/main/java/com/cm/common/plugin/oauth/service/menu/impl/MenuServiceImpl.java @@ -34,7 +34,7 @@ public class MenuServiceImpl extends AbstractService implements IMenuService { @Override public JSONArray listMenu(Map params) throws SearchException { - String result = restTemplateUtil.doPostForm(String.format(IApiConsts.MENU_LIST, apiPathProperties.getUserCenter(), oAuth2ClientProperties.getClientId()), params); + String result = restTemplateUtil.doPostForm(String.format(IApiConsts.LIST_MENU, apiPathProperties.getUserCenter(), oAuth2ClientProperties.getClientId()), params); if (StringUtils.isBlank(result)) { throw new SearchException("获取列表失败"); } diff --git a/cloud-common-plugin-oauth/src/main/java/com/cm/common/plugin/oauth/service/user/IUserService.java b/cloud-common-plugin-oauth/src/main/java/com/cm/common/plugin/oauth/service/user/IUserService.java new file mode 100644 index 0000000..48d8563 --- /dev/null +++ b/cloud-common-plugin-oauth/src/main/java/com/cm/common/plugin/oauth/service/user/IUserService.java @@ -0,0 +1,36 @@ +package com.cm.common.plugin.oauth.service.user; + +import com.alibaba.fastjson.JSONArray; +import com.cm.common.exception.SearchException; + +import java.util.Map; + +/** + * When you feel like quitting. Think about why you started + * 当你想要放弃的时候,想想当初你为何开始 + * + * @ClassName: IUserService + * @Description: 用户 + * @Author: WangGeng + * @Date: 2019-07-29 21:23 + * @Version: 1.0 + **/ +public interface IUserService { + /** + * 组织部门人员列表 + * + * @param params + * @return + * @throws SearchException + */ + JSONArray listDepartmentUsers(Map params) throws SearchException; + + /** + * 通过id列表获取用户ID + * + * @param params + * @return + * @throws SearchException + */ + JSONArray listUserByIds(Map params) throws SearchException; +} diff --git a/cloud-common-plugin-oauth/src/main/java/com/cm/common/plugin/oauth/service/user/impl/UserServiceImpl.java b/cloud-common-plugin-oauth/src/main/java/com/cm/common/plugin/oauth/service/user/impl/UserServiceImpl.java new file mode 100644 index 0000000..4e3b1ea --- /dev/null +++ b/cloud-common-plugin-oauth/src/main/java/com/cm/common/plugin/oauth/service/user/impl/UserServiceImpl.java @@ -0,0 +1,51 @@ +package com.cm.common.plugin.oauth.service.user.impl; + +import com.alibaba.fastjson.JSONArray; +import com.cm.common.base.AbstractService; +import com.cm.common.config.properties.ApiPathProperties; +import com.cm.common.exception.SearchException; +import com.cm.common.plugin.IApiConsts; +import com.cm.common.plugin.oauth.service.user.IUserService; +import com.cm.common.plugin.utils.RestTemplateUtil; +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.Map; + +/** + * When you feel like quitting. Think about why you started + * 当你想要放弃的时候,想想当初你为何开始 + * + * @ClassName: UserServiceImpl + * @Description: 用户 + * @Author: WangGeng + * @Date: 2019-07-29 21:23 + * @Version: 1.0 + **/ +@Service +public class UserServiceImpl extends AbstractService implements IUserService { + + @Autowired + private RestTemplateUtil restTemplateUtil; + @Autowired + private ApiPathProperties apiPathProperties; + + @Override + public JSONArray listDepartmentUsers(Map params) throws SearchException { + String result = restTemplateUtil.doPostForm(String.format(IApiConsts.LIST_DEPARTMENT_USER, apiPathProperties.getUserCenter(), params.get("departmentId").toString()), params); + if (StringUtils.isBlank(result)) { + throw new SearchException("获取人员列表失败"); + } + return JSONArray.parseArray(result); + } + + @Override + public JSONArray listUserByIds(Map params) throws SearchException { + String result = restTemplateUtil.doPostJson(String.format(IApiConsts.LIST_USER_BY_ID, apiPathProperties.getUserCenter()), params); + if (StringUtils.isBlank(result)) { + throw new SearchException("获取人员列表失败"); + } + return JSONArray.parseArray(result); + } +} diff --git a/cloud-common-plugin-oauth/src/main/java/com/cm/common/plugin/utils/RestTemplateUtil.java b/cloud-common-plugin-oauth/src/main/java/com/cm/common/plugin/utils/RestTemplateUtil.java index 7622e2c..b08845b 100644 --- a/cloud-common-plugin-oauth/src/main/java/com/cm/common/plugin/utils/RestTemplateUtil.java +++ b/cloud-common-plugin-oauth/src/main/java/com/cm/common/plugin/utils/RestTemplateUtil.java @@ -4,6 +4,7 @@ import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.http.*; import org.springframework.http.client.SimpleClientHttpRequestFactory; import org.springframework.security.oauth2.client.OAuth2ClientContext; @@ -26,6 +27,7 @@ import java.util.Map; public class RestTemplateUtil { @Autowired + @Qualifier("oauth2ClientContext") private OAuth2ClientContext oAuth2ClientContext; private static Logger LOG = LoggerFactory.getLogger(RestTemplateUtil.class); @@ -68,16 +70,15 @@ public class RestTemplateUtil { httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED); MultiValueMap queryParams = new LinkedMultiValueMap<>(); - queryParams.add("access_token", accessToken); for (Map.Entry kv : params.entrySet()) { - queryParams.add(kv.getKey(), kv.getKey()); + queryParams.add(kv.getKey(), kv.getValue()); } HttpEntity> httpEntity = new HttpEntity<>(queryParams, httpHeaders); RestTemplate restTemplate = getRestTemplate(); try { - return getResponse(restTemplate.postForEntity(url, httpEntity, String.class)); + return getResponse(restTemplate.postForEntity(String.format("%s?access_token=%s", url, accessToken), httpEntity, String.class)); } catch (Exception e) { LOG.error(e.getMessage(), e); return null; @@ -97,16 +98,18 @@ public class RestTemplateUtil { return null; } Map queryParams = new HashMap<>(1); - queryParams.put("access_token", accessToken); + for (Map.Entry kv : params.entrySet()) { + queryParams.put(kv.getKey(), kv.getValue()); + } HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8); - HttpEntity> httpEntity = new HttpEntity<>(params, httpHeaders); + HttpEntity> httpEntity = new HttpEntity<>(queryParams, httpHeaders); RestTemplate restTemplate = getRestTemplate(); try { - return getResponse(restTemplate.postForEntity(String.format("%s?%s", url, queryParams(queryParams)), httpEntity, String.class)); + return getResponse(restTemplate.postForEntity(String.format("%s?access_token=%s", url, accessToken), httpEntity, String.class)); } catch (Exception e) { LOG.error(e.getMessage(), e); return null; diff --git a/cloud-common-plugin-oauth/src/main/resources/templates/user/select-department-user.html b/cloud-common-plugin-oauth/src/main/resources/templates/user/select-department-user.html new file mode 100644 index 0000000..4957c9a --- /dev/null +++ b/cloud-common-plugin-oauth/src/main/resources/templates/user/select-department-user.html @@ -0,0 +1,193 @@ + + + + + + + + + + + + + + + + +
+
+
+
+
+
+
    +
    +
    +
    +
    + +
    +
    +
    +
    +
    + + +
    +
    +
    + + + + + + + + + \ No newline at end of file diff --git a/cloud-common-plugin/src/main/resources/templates/tree/tree.html b/cloud-common-plugin/src/main/resources/templates/tree/tree.html index f50ef92..5e96015 100644 --- a/cloud-common-plugin/src/main/resources/templates/tree/tree.html +++ b/cloud-common-plugin/src/main/resources/templates/tree/tree.html @@ -88,7 +88,7 @@ i--; continue; } - $('#selectNodes').append(''+ item.name +' ') + $('#selectNodes').append(''+ item.name +' ') } } // 初始化节点