|
@@ -1,10 +1,11 @@
|
|
|
package com.ims.eval.service.custom;
|
|
|
|
|
|
+import cn.hutool.core.collection.CollectionUtil;
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.ims.eval.config.ImaConfig;
|
|
|
-import com.ims.eval.entity.custom.DoAction;
|
|
|
+import com.ims.eval.entity.custom.Menu;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.http.*;
|
|
@@ -12,8 +13,10 @@ import org.springframework.stereotype.Service;
|
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
-import java.util.LinkedHashMap;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
import java.util.Map;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* <p>
|
|
@@ -35,32 +38,31 @@ public class MenuOperationService {
|
|
|
|
|
|
/**
|
|
|
* 获取权限菜单
|
|
|
+ *
|
|
|
* @param parentId
|
|
|
* @return
|
|
|
*/
|
|
|
- public JSONArray menuList(String parentId, HttpServletRequest request){
|
|
|
+ public JSONArray menuList(String parentId, HttpServletRequest request) {
|
|
|
HttpHeaders headers = new HttpHeaders();
|
|
|
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
|
|
|
headers.add("Blade-Auth", request.getHeader("Blade-Auth"));
|
|
|
headers.add("code", request.getHeader("code"));
|
|
|
-
|
|
|
-
|
|
|
HttpEntity<Map> param = new HttpEntity<>(null, headers);
|
|
|
- ResponseEntity<String> responseEntity2 = restTemplate.exchange(imaConfig.getGatewayUrl()+"ims-ext/sys/sysMenu/menuList?parentId={1}", HttpMethod.POST,param,String.class,parentId);
|
|
|
+ ResponseEntity<String> responseEntity2 = restTemplate.exchange(imaConfig.getGatewayUrl() + "ims-ext/sys/sysMenu/menuList?parentId={1}", HttpMethod.POST, param, String.class, parentId);
|
|
|
log.info("\n code:{}\n header:{}\n body:{}\n", responseEntity2.getStatusCodeValue(), responseEntity2.getHeaders(), responseEntity2.getBody());
|
|
|
- if(200== responseEntity2.getStatusCodeValue()){
|
|
|
+ if (200 == responseEntity2.getStatusCodeValue()) {
|
|
|
return JSON.parseArray(responseEntity2.getBody());
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
|
|
|
-
|
|
|
/**
|
|
|
* 获取用户权限
|
|
|
+ *
|
|
|
* @return
|
|
|
*/
|
|
|
- public JSONObject getPrivilegesOfCurrentUserAll(HttpServletRequest request){
|
|
|
+ public JSONObject getPrivilegesOfCurrentUserAll(HttpServletRequest request) {
|
|
|
HttpHeaders headers = new HttpHeaders();
|
|
|
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
|
|
|
headers.add("Blade-Auth", request.getHeader("Blade-Auth"));
|
|
@@ -68,13 +70,78 @@ public class MenuOperationService {
|
|
|
|
|
|
|
|
|
HttpEntity<Map> param = new HttpEntity<>(null, headers);
|
|
|
- ResponseEntity<String> responseEntity2 = restTemplate.exchange(imaConfig.getGatewayUrl()+"ims-ext/sys/sysRole/getPrivilegesOfCurrentUserAll", HttpMethod.POST,param,String.class);
|
|
|
+ ResponseEntity<String> responseEntity2 = restTemplate.exchange(imaConfig.getGatewayUrl() + "ims-ext/sys/sysRole/getPrivilegesOfCurrentUserAll", HttpMethod.POST, param, String.class);
|
|
|
log.info("\n code:{}\n header:{}\n body:{}\n", responseEntity2.getStatusCodeValue(), responseEntity2.getHeaders(), responseEntity2.getBody());
|
|
|
- if(200== responseEntity2.getStatusCodeValue()){
|
|
|
+ if (200 == responseEntity2.getStatusCodeValue()) {
|
|
|
return JSON.parseObject(responseEntity2.getBody());
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
+ * 获取菜单树结构
|
|
|
+ *
|
|
|
+ * @param parentId
|
|
|
+ * @param request
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public List<Menu> getMenuTree(String parentId, HttpServletRequest request) {
|
|
|
+ JSONArray arr = menuList(parentId, request);
|
|
|
+ if (null == arr) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ List<Menu> list = arr.toJavaList(Menu.class);
|
|
|
+ List<Menu> menuTr = convert(list);
|
|
|
+ return menuTr;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 转换为有树形结构的列表
|
|
|
+ *
|
|
|
+ * @param source 源数据所有数据
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public List<Menu> convert(List<Menu> source) {
|
|
|
+ if (CollectionUtil.isEmpty(source)) {
|
|
|
+ return new ArrayList<>();
|
|
|
+ }
|
|
|
+ List<Menu> result = new ArrayList<>();
|
|
|
+ List<String> idList = source.stream().map(Menu::getId).collect(Collectors.toList());
|
|
|
+ // 设置最外层顶级
|
|
|
+ for (Menu menuVO : source) {
|
|
|
+ // 判断所有列表里面父级未在集合中放置到一级
|
|
|
+ if (!idList.contains(menuVO.getParentId())) {
|
|
|
+ result.add(menuVO);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ for (Menu menuVO : result) {
|
|
|
+ // 循环一级之后子级
|
|
|
+ getChildren(source, menuVO);
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 循环设置子级
|
|
|
+ *
|
|
|
+ * @param list 所有元素列表
|
|
|
+ * @param treeSelectVO 父级元素
|
|
|
+ * @return 父级元素
|
|
|
+ */
|
|
|
+ public static Menu getChildren(List<Menu> list, Menu treeSelectVO) {
|
|
|
+ for (Menu Menu : list) {
|
|
|
+ if (treeSelectVO.getId().equals(Menu.getParentId())) {
|
|
|
+ List<Menu> children = CollectionUtil.isEmpty(treeSelectVO.getChildren()) ? new ArrayList<>() : treeSelectVO.getChildren();
|
|
|
+ children.add(getChildren(list, Menu));
|
|
|
+ treeSelectVO.setChildren(children);
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return treeSelectVO;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
}
|