新增人员选择控件,修改restTemplate请求方式,修改tree的问题

This commit is contained in:
wenc000 2019-07-30 23:17:41 +08:00
parent 15b7ab0a74
commit 5954efd25e
13 changed files with 519 additions and 9 deletions

View File

@ -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";
}

View File

@ -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;
/**

View File

@ -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<String, Object> 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);
}
}

View File

@ -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<String, Object> 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<String, Object> params = getParams();
params.put("userIds", userIds);
return userService.listUserByIds(params);
}
}

View File

@ -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;
}
}

View File

@ -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<String, Object> params) throws SearchException;
}

View File

@ -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<String, Object> 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);
}
}

View File

@ -34,7 +34,7 @@ public class MenuServiceImpl extends AbstractService implements IMenuService {
@Override
public JSONArray listMenu(Map<String, Object> 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("获取列表失败");
}

View File

@ -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<String, Object> params) throws SearchException;
/**
* 通过id列表获取用户ID
*
* @param params
* @return
* @throws SearchException
*/
JSONArray listUserByIds(Map<String, Object> params) throws SearchException;
}

View File

@ -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<String, Object> 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<String, Object> 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);
}
}

View File

@ -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<String, Object> queryParams = new LinkedMultiValueMap<>();
queryParams.add("access_token", accessToken);
for (Map.Entry<String, Object> kv : params.entrySet()) {
queryParams.add(kv.getKey(), kv.getKey());
queryParams.add(kv.getKey(), kv.getValue());
}
HttpEntity<MultiValueMap<String, Object>> 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<String, Object> queryParams = new HashMap<>(1);
queryParams.put("access_token", accessToken);
for (Map.Entry<String, Object> kv : params.entrySet()) {
queryParams.put(kv.getKey(), kv.getValue());
}
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
HttpEntity<Map<String, Object>> httpEntity = new HttpEntity<>(params, httpHeaders);
HttpEntity<Map<String, Object>> 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;

View File

@ -0,0 +1,193 @@
<!doctype html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<base th:href="${#httpServletRequest.getScheme() + '://' + #httpServletRequest.getServerName() + ':' + #request.getServerPort() + #request.getContextPath() + '/'} ">
<meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=11,chrome=1"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<link rel="stylesheet" type="text/css" href="assets/fonts/font-awesome/css/font-awesome.css"/>
<link rel="stylesheet" type="text/css" href="assets/js/vendor/bootstrap/css/bootstrap.min.css"/>
<link rel="stylesheet" type="text/css" href="assets/js/easyui/themes/default/easyui.css"/>
<link rel="stylesheet" type="text/css" href="assets/js/easyui/themes/icon.css"/>
<link rel="stylesheet" type="text/css" href="assets/js/vendor/zTree3/css/metroStyle/metroStyle.css"/>
<link rel="stylesheet" type="text/css" href="assets/css/minimal.css"/>
<link rel="stylesheet" type="text/css" href="assets/css/system.css">
</head>
<body>
<div id="app" class="easyui-layout easyui-layout-dialog dept-selector">
<div class="selector-title" data-options="region:'north'">
<div id="selectUsers" class="selector-title-wrapper"></div>
</div>
<div class="selector-tree" data-options="region:'west',split:false,collapsible:false,border:true">
<div class="selector-tree-wrapper">
<ul id="leftTree" class="ztree"></ul>
</div>
</div>
<div class="selector-body" data-options="region:'center',border:true">
<div class="selector-body-wrapper">
<div class="selector-body-search">
<input type="text" class="form-control" id="name" placeholder="检索用户名" oninput="searchUser()"/>
</div>
<div id="users" class="selector-body-content list-group"></div>
</div>
</div>
<div class="edit-button-footer2">
<div class="col-sm-offset-4 col-sm-8">
<button type="button" class="btn btn-primary" onclick="confirmBox()">确认</button>
<!--
<button type="button" class="btn btn-default" onclick="closeBox()">关闭</button>
-->
</div>
</div>
</div>
<input type="hidden" id="selectedUserIds"/>
<script type="text/javascript" src="assets/js/jquery-2.1.1.min.js"></script>
<script type="text/javascript" src="assets/js/easyui/jquery.easyui.min.js"></script>
<script type="text/javascript" src="assets/js/easyui/locale/easyui-lang-zh_CN.js"></script>
<script type="text/javascript" src="assets/js/vendor/zTree3/js/jquery.ztree.all.js"></script>
<script type="text/javascript" src="assets/js/common.js"></script>
<script type="text/javascript">
var hrefParams = top.restAjax.params(window.location.href);
$('#selectedUserIds').val(hrefParams.selectedUserIds);
// 是否单选
var single = hrefParams.single;
var selectDepartmentUserArray = [];
function closeBox() {
top.DialogBox.closeBox();
}
function confirmBox() {
top.DialogBox.dialogData.selectedDepartmentUsers = selectDepartmentUserArray;
closeBox();
}
// 人员是否已经选择
function isUserSelected(userId) {
var isSelected = false;
for(var i = 0, item; item = selectDepartmentUserArray[i++];) {
if(item.userId == userId) {
isSelected = true;
break;
}
}
return isSelected;
}
// 删除已经选择的人员
function removeSelectedUser(userId) {
for(var i = 0, item; item = selectDepartmentUserArray[i]; i++) {
if(item.userId == userId) {
selectDepartmentUserArray.splice(i, 1);
$('#selected_user_'+ userId).remove();
break;
}
}
}
// 清空已经选择的人员
function clearSelectedUser() {
selectDepartmentUserArray.splice(0, selectDepartmentUserArray.length);
$('.selected_user').remove();
}
// 添加人员
function addSelectedUser(userId, userName, userUsername) {
selectDepartmentUserArray.push({
userId: userId,
userName: userName,
userUsername: userUsername
});
$('#selectUsers').append('<a id="selected_user_'+ userId +'" href="javascript:;" class="btn btn-success btn-xs selected_user">'+ userName +' <i class="fa fa-close" onclick="removeSelectedUser(\''+ userId +'\')"></i></a>');
}
// 选择人员
function selectUser(userId, userName, userUsername) {
// 如果是单选
if(single == 'true') {
clearSelectedUser();
addSelectedUser(userId, userName, userUsername);
return;
}
if(!isUserSelected(userId)) {
addSelectedUser(userId, userName, userUsername);
}
}
// 筛选人员
function searchUser() {
$('.users').hide().filter(":contains('" + $('#name').val() + "')").show();
}
// 初始化人员列表
function initUsers(parentId) {
var loadLayerIndex;
top.restAjax.get(top.restAjax.path('api/user/listdepartmentusers/{parentId}', [parentId]), {}, null, function(code, data) {
$('#users').empty();
var userDom = '';
for(var i = 0, item; item = data[i++];) {
var avatarDom;
if(null == item.userAvatar || '' == item.userAvatar) {
avatarDom = '<img class="user-avatar" src="assets/images/profile-photo.jpg"/> '
} else {
avatarDom = '<img class="user-avatar" src="route/file/downloadfile/false/'+ item.userAvatar +'"/> ';
}
userDom += '<a id="user_'+ item.userId +'" href="javascript:;" class="users list-group-item" onclick="selectUser(\''+ item.userId +'\', \''+ item.userName +'\', \''+ item.userUsername +'\')">'+ avatarDom + item.userName +' ['+ item.userUsername +']</a>';
}
$('#users').append(userDom);
}, function(code, data) {
top.DialogBox.msg(data.msg);
}, function() {
loadLayerIndex = top.DialogBox.msg(TextMessage.loading, {icon: 16, time: 0, shade: 0.3});
}, function() {
top.DialogBox.close(loadLayerIndex);
});
}
// 初始化树
function initThree() {
var setting = {
async: {
enable: true,
autoLoad: false,
type: 'get',
url: top.restAjax.path('api/department/listdepartments', []),
autoParam:['id'],
otherParam:{},
dataFilter: function(treeId, parentNode, childNodes) {
if (!childNodes) return null;
for (var i=0, l=childNodes.length; i<l; i++) {
childNodes[i].name = childNodes[i].name.replace(/\.n/g, '.');
}
return childNodes;
}
},
callback: {
onClick: function(event, treeId, treeNode) {
initUsers(treeNode.id);
return false;
}
},
view: {
fontCss: {'color': 'black'}
}
};
var zTree = $.fn.zTree.init($("#leftTree"), setting);
zTree.addNodes(null, {id: '0', pId: '-1', name: '未划分人员', url: 'javascript:;', isParent: 'true'})
}
// 初始化选择的人员
function initSelectedUsers() {
if($('#selectedUserIds').val() != '') {
top.restAjax.get(top.restAjax.path('api/user/listuserbyids/{selectedUserIds}', [$('#selectedUserIds').val()]), {}, null, function(code, data) {
for(var i = 0, item; item = data[i++]; ) {
selectUser(item.userId, item.userName, item.userUsername);
}
}, function(code, data) {
top.DialogBox.msg(data.msg);
}, function() {
loadLayerIndex = top.DialogBox.msg(TextMessage.loading, {icon: 16, time: 0, shade: 0.3});
}, function() {
top.DialogBox.close(loadLayerIndex);
});
}
}
$(function() {
$('#app').fadeTo(1000, 1);
initThree();
initUsers(0);
initSelectedUsers();
top.DialogBox.dialogData.selectedDepartmentUsers = null;
});
</script>
</body>
</html>

View File

@ -88,7 +88,7 @@
i--;
continue;
}
$('#selectNodes').append('<span id="selectNode_'+ item[selectTree.primaryKey] +'" class="btn btn-success btn-xs selected-node">'+ item.name +' <i class="fa fa-close" onclick="removeSelectNodes('+ item[selectTree.primaryKey] +')"></i></span>')
$('#selectNodes').append('<span id="selectNode_'+ item[selectTree.primaryKey] +'" class="btn btn-success btn-xs selected-node">'+ item.name +' <i class="fa fa-close" onclick="removeSelectNodes(\''+ item[selectTree.primaryKey] +'\')"></i></span>')
}
}
// 初始化节点