SysMenuServiceImpl.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. package com.ruoyi.system.service.impl;
  2. import java.util.ArrayList;
  3. import java.util.Arrays;
  4. import java.util.HashSet;
  5. import java.util.Iterator;
  6. import java.util.LinkedList;
  7. import java.util.List;
  8. import java.util.Set;
  9. import java.util.stream.Collectors;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.stereotype.Service;
  12. import com.ruoyi.common.constant.Constants;
  13. import com.ruoyi.common.constant.UserConstants;
  14. import com.ruoyi.common.core.domain.TreeSelect;
  15. import com.ruoyi.common.core.domain.entity.SysMenu;
  16. import com.ruoyi.common.core.domain.entity.SysRole;
  17. import com.ruoyi.common.core.domain.entity.SysUser;
  18. import com.ruoyi.common.utils.SecurityUtils;
  19. import com.ruoyi.common.utils.StringUtils;
  20. import com.ruoyi.system.domain.vo.MetaVo;
  21. import com.ruoyi.system.domain.vo.RouterVo;
  22. import com.ruoyi.system.mapper.SysMenuMapper;
  23. import com.ruoyi.system.mapper.SysRoleMapper;
  24. import com.ruoyi.system.mapper.SysRoleMenuMapper;
  25. import com.ruoyi.system.service.ISysMenuService;
  26. /**
  27. * 菜单 业务层处理
  28. *
  29. * @author ruoyi
  30. */
  31. @Service
  32. public class SysMenuServiceImpl implements ISysMenuService
  33. {
  34. public static final String PREMISSION_STRING = "perms[\"{0}\"]";
  35. @Autowired
  36. private SysMenuMapper menuMapper;
  37. @Autowired
  38. private SysRoleMapper roleMapper;
  39. @Autowired
  40. private SysRoleMenuMapper roleMenuMapper;
  41. /**
  42. * 根据用户查询系统菜单列表
  43. *
  44. * @param userId 用户ID
  45. * @return 菜单列表
  46. */
  47. @Override
  48. public List<SysMenu> selectMenuList(Long userId)
  49. {
  50. return selectMenuList(new SysMenu(), userId);
  51. }
  52. /**
  53. * 查询系统菜单列表
  54. *
  55. * @param menu 菜单信息
  56. * @return 菜单列表
  57. */
  58. @Override
  59. public List<SysMenu> selectMenuList(SysMenu menu, Long userId)
  60. {
  61. List<SysMenu> menuList = null;
  62. // 管理员显示所有菜单信息
  63. if (SysUser.isAdmin(userId))
  64. {
  65. menuList = menuMapper.selectMenuList(menu);
  66. }
  67. else
  68. {
  69. menu.getParams().put("userId", userId);
  70. menuList = menuMapper.selectMenuListByUserId(menu);
  71. }
  72. return menuList;
  73. }
  74. /**
  75. * 根据用户ID查询权限
  76. *
  77. * @param userId 用户ID
  78. * @return 权限列表
  79. */
  80. @Override
  81. public Set<String> selectMenuPermsByUserId(Long userId)
  82. {
  83. List<String> perms = menuMapper.selectMenuPermsByUserId(userId);
  84. Set<String> permsSet = new HashSet<>();
  85. for (String perm : perms)
  86. {
  87. if (StringUtils.isNotEmpty(perm))
  88. {
  89. permsSet.addAll(Arrays.asList(perm.trim().split(",")));
  90. }
  91. }
  92. return permsSet;
  93. }
  94. /**
  95. * 根据用户ID查询菜单
  96. *
  97. * @param userId 用户名称
  98. * @return 菜单列表
  99. */
  100. @Override
  101. public List<SysMenu> selectMenuTreeByUserId(Long userId)
  102. {
  103. List<SysMenu> menus = null;
  104. if (SecurityUtils.isAdmin(userId))
  105. {
  106. menus = menuMapper.selectMenuTreeAll();
  107. }
  108. else
  109. {
  110. menus = menuMapper.selectMenuTreeByUserId(userId);
  111. }
  112. return getChildPerms(menus, 0);
  113. }
  114. /**
  115. * 根据角色ID查询菜单树信息
  116. *
  117. * @param roleId 角色ID
  118. * @return 选中菜单列表
  119. */
  120. @Override
  121. public List<Long> selectMenuListByRoleId(Long roleId)
  122. {
  123. SysRole role = roleMapper.selectRoleById(roleId);
  124. return menuMapper.selectMenuListByRoleId(roleId, role.isMenuCheckStrictly());
  125. }
  126. /**
  127. * 构建前端路由所需要的菜单
  128. *
  129. * @param menus 菜单列表
  130. * @return 路由列表
  131. */
  132. @Override
  133. public List<RouterVo> buildMenus(List<SysMenu> menus)
  134. {
  135. List<RouterVo> routers = new LinkedList<RouterVo>();
  136. for (SysMenu menu : menus)
  137. {
  138. RouterVo router = new RouterVo();
  139. router.setHidden("1".equals(menu.getVisible()));
  140. router.setName(getRouteName(menu));
  141. router.setPath(getRouterPath(menu));
  142. router.setComponent(getComponent(menu));
  143. router.setQuery(menu.getQuery());
  144. router.setMeta(new MetaVo(menu.getMenuName(), menu.getIcon(), StringUtils.equals("1", menu.getIsCache()), menu.getPath()));
  145. List<SysMenu> cMenus = menu.getChildren();
  146. if (!cMenus.isEmpty() && cMenus.size() > 0 && UserConstants.TYPE_DIR.equals(menu.getMenuType()))
  147. {
  148. router.setAlwaysShow(true);
  149. router.setRedirect("noRedirect");
  150. router.setChildren(buildMenus(cMenus));
  151. }
  152. else if (isMenuFrame(menu))
  153. {
  154. router.setMeta(null);
  155. List<RouterVo> childrenList = new ArrayList<RouterVo>();
  156. RouterVo children = new RouterVo();
  157. children.setPath(menu.getPath());
  158. children.setComponent(menu.getComponent());
  159. children.setName(StringUtils.capitalize(menu.getPath()));
  160. children.setMeta(new MetaVo(menu.getMenuName(), menu.getIcon(), StringUtils.equals("1", menu.getIsCache()), menu.getPath()));
  161. children.setQuery(menu.getQuery());
  162. childrenList.add(children);
  163. router.setChildren(childrenList);
  164. }
  165. else if (menu.getParentId().intValue() == 0 && isInnerLink(menu))
  166. {
  167. router.setMeta(new MetaVo(menu.getMenuName(), menu.getIcon()));
  168. router.setPath("/");
  169. List<RouterVo> childrenList = new ArrayList<RouterVo>();
  170. RouterVo children = new RouterVo();
  171. String routerPath = innerLinkReplaceEach(menu.getPath());
  172. children.setPath(routerPath);
  173. children.setComponent(UserConstants.INNER_LINK);
  174. children.setName(StringUtils.capitalize(routerPath));
  175. children.setMeta(new MetaVo(menu.getMenuName(), menu.getIcon(), menu.getPath()));
  176. childrenList.add(children);
  177. router.setChildren(childrenList);
  178. }
  179. routers.add(router);
  180. }
  181. return routers;
  182. }
  183. /**
  184. * 构建前端所需要树结构
  185. *
  186. * @param menus 菜单列表
  187. * @return 树结构列表
  188. */
  189. @Override
  190. public List<SysMenu> buildMenuTree(List<SysMenu> menus)
  191. {
  192. List<SysMenu> returnList = new ArrayList<SysMenu>();
  193. List<Long> tempList = new ArrayList<Long>();
  194. for (SysMenu dept : menus)
  195. {
  196. tempList.add(dept.getMenuId());
  197. }
  198. for (Iterator<SysMenu> iterator = menus.iterator(); iterator.hasNext();)
  199. {
  200. SysMenu menu = (SysMenu) iterator.next();
  201. // 如果是顶级节点, 遍历该父节点的所有子节点
  202. if (!tempList.contains(menu.getParentId()))
  203. {
  204. recursionFn(menus, menu);
  205. returnList.add(menu);
  206. }
  207. }
  208. if (returnList.isEmpty())
  209. {
  210. returnList = menus;
  211. }
  212. return returnList;
  213. }
  214. /**
  215. * 构建前端所需要下拉树结构
  216. *
  217. * @param menus 菜单列表
  218. * @return 下拉树结构列表
  219. */
  220. @Override
  221. public List<TreeSelect> buildMenuTreeSelect(List<SysMenu> menus)
  222. {
  223. List<SysMenu> menuTrees = buildMenuTree(menus);
  224. return menuTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
  225. }
  226. /**
  227. * 根据菜单ID查询信息
  228. *
  229. * @param menuId 菜单ID
  230. * @return 菜单信息
  231. */
  232. @Override
  233. public SysMenu selectMenuById(Long menuId)
  234. {
  235. return menuMapper.selectMenuById(menuId);
  236. }
  237. /**
  238. * 是否存在菜单子节点
  239. *
  240. * @param menuId 菜单ID
  241. * @return 结果
  242. */
  243. @Override
  244. public boolean hasChildByMenuId(Long menuId)
  245. {
  246. int result = menuMapper.hasChildByMenuId(menuId);
  247. return result > 0;
  248. }
  249. /**
  250. * 查询菜单使用数量
  251. *
  252. * @param menuId 菜单ID
  253. * @return 结果
  254. */
  255. @Override
  256. public boolean checkMenuExistRole(Long menuId)
  257. {
  258. int result = roleMenuMapper.checkMenuExistRole(menuId);
  259. return result > 0;
  260. }
  261. /**
  262. * 新增保存菜单信息
  263. *
  264. * @param menu 菜单信息
  265. * @return 结果
  266. */
  267. @Override
  268. public int insertMenu(SysMenu menu)
  269. {
  270. return menuMapper.insertMenu(menu);
  271. }
  272. /**
  273. * 修改保存菜单信息
  274. *
  275. * @param menu 菜单信息
  276. * @return 结果
  277. */
  278. @Override
  279. public int updateMenu(SysMenu menu)
  280. {
  281. return menuMapper.updateMenu(menu);
  282. }
  283. /**
  284. * 删除菜单管理信息
  285. *
  286. * @param menuId 菜单ID
  287. * @return 结果
  288. */
  289. @Override
  290. public int deleteMenuById(Long menuId)
  291. {
  292. return menuMapper.deleteMenuById(menuId);
  293. }
  294. /**
  295. * 校验菜单名称是否唯一
  296. *
  297. * @param menu 菜单信息
  298. * @return 结果
  299. */
  300. @Override
  301. public String checkMenuNameUnique(SysMenu menu)
  302. {
  303. Long menuId = StringUtils.isNull(menu.getMenuId()) ? -1L : menu.getMenuId();
  304. SysMenu info = menuMapper.checkMenuNameUnique(menu.getMenuName(), menu.getParentId());
  305. if (StringUtils.isNotNull(info) && info.getMenuId().longValue() != menuId.longValue())
  306. {
  307. return UserConstants.NOT_UNIQUE;
  308. }
  309. return UserConstants.UNIQUE;
  310. }
  311. /**
  312. * 获取路由名称
  313. *
  314. * @param menu 菜单信息
  315. * @return 路由名称
  316. */
  317. public String getRouteName(SysMenu menu)
  318. {
  319. String routerName = StringUtils.capitalize(menu.getPath());
  320. // 非外链并且是一级目录(类型为目录)
  321. if (isMenuFrame(menu))
  322. {
  323. routerName = StringUtils.EMPTY;
  324. }
  325. return routerName;
  326. }
  327. /**
  328. * 获取路由地址
  329. *
  330. * @param menu 菜单信息
  331. * @return 路由地址
  332. */
  333. public String getRouterPath(SysMenu menu)
  334. {
  335. String routerPath = menu.getPath();
  336. // 内链打开外网方式
  337. if (menu.getParentId().intValue() != 0 && isInnerLink(menu))
  338. {
  339. routerPath = innerLinkReplaceEach(routerPath);
  340. }
  341. // 非外链并且是一级目录(类型为目录)
  342. if (0 == menu.getParentId().intValue() && UserConstants.TYPE_DIR.equals(menu.getMenuType())
  343. && UserConstants.NO_FRAME.equals(menu.getIsFrame()))
  344. {
  345. routerPath = "/" + menu.getPath();
  346. }
  347. // 非外链并且是一级目录(类型为菜单)
  348. else if (isMenuFrame(menu))
  349. {
  350. routerPath = "/";
  351. }
  352. return routerPath;
  353. }
  354. /**
  355. * 获取组件信息
  356. *
  357. * @param menu 菜单信息
  358. * @return 组件信息
  359. */
  360. public String getComponent(SysMenu menu)
  361. {
  362. String component = UserConstants.LAYOUT;
  363. if (StringUtils.isNotEmpty(menu.getComponent()) && !isMenuFrame(menu))
  364. {
  365. component = menu.getComponent();
  366. }
  367. else if (StringUtils.isEmpty(menu.getComponent()) && menu.getParentId().intValue() != 0 && isInnerLink(menu))
  368. {
  369. component = UserConstants.INNER_LINK;
  370. }
  371. else if (StringUtils.isEmpty(menu.getComponent()) && isParentView(menu))
  372. {
  373. component = UserConstants.PARENT_VIEW;
  374. }
  375. return component;
  376. }
  377. /**
  378. * 是否为菜单内部跳转
  379. *
  380. * @param menu 菜单信息
  381. * @return 结果
  382. */
  383. public boolean isMenuFrame(SysMenu menu)
  384. {
  385. return menu.getParentId().intValue() == 0 && UserConstants.TYPE_MENU.equals(menu.getMenuType())
  386. && menu.getIsFrame().equals(UserConstants.NO_FRAME);
  387. }
  388. /**
  389. * 是否为内链组件
  390. *
  391. * @param menu 菜单信息
  392. * @return 结果
  393. */
  394. public boolean isInnerLink(SysMenu menu)
  395. {
  396. return menu.getIsFrame().equals(UserConstants.NO_FRAME) && StringUtils.ishttp(menu.getPath());
  397. }
  398. /**
  399. * 是否为parent_view组件
  400. *
  401. * @param menu 菜单信息
  402. * @return 结果
  403. */
  404. public boolean isParentView(SysMenu menu)
  405. {
  406. return menu.getParentId().intValue() != 0 && UserConstants.TYPE_DIR.equals(menu.getMenuType());
  407. }
  408. /**
  409. * 根据父节点的ID获取所有子节点
  410. *
  411. * @param list 分类表
  412. * @param parentId 传入的父节点ID
  413. * @return String
  414. */
  415. public List<SysMenu> getChildPerms(List<SysMenu> list, int parentId)
  416. {
  417. List<SysMenu> returnList = new ArrayList<SysMenu>();
  418. for (Iterator<SysMenu> iterator = list.iterator(); iterator.hasNext();)
  419. {
  420. SysMenu t = (SysMenu) iterator.next();
  421. // 一、根据传入的某个父节点ID,遍历该父节点的所有子节点
  422. if (t.getParentId() == parentId)
  423. {
  424. recursionFn(list, t);
  425. returnList.add(t);
  426. }
  427. }
  428. return returnList;
  429. }
  430. /**
  431. * 递归列表
  432. *
  433. * @param list
  434. * @param t
  435. */
  436. private void recursionFn(List<SysMenu> list, SysMenu t)
  437. {
  438. // 得到子节点列表
  439. List<SysMenu> childList = getChildList(list, t);
  440. t.setChildren(childList);
  441. for (SysMenu tChild : childList)
  442. {
  443. if (hasChild(list, tChild))
  444. {
  445. recursionFn(list, tChild);
  446. }
  447. }
  448. }
  449. /**
  450. * 得到子节点列表
  451. */
  452. private List<SysMenu> getChildList(List<SysMenu> list, SysMenu t)
  453. {
  454. List<SysMenu> tlist = new ArrayList<SysMenu>();
  455. Iterator<SysMenu> it = list.iterator();
  456. while (it.hasNext())
  457. {
  458. SysMenu n = (SysMenu) it.next();
  459. if (n.getParentId().longValue() == t.getMenuId().longValue())
  460. {
  461. tlist.add(n);
  462. }
  463. }
  464. return tlist;
  465. }
  466. /**
  467. * 判断是否有子节点
  468. */
  469. private boolean hasChild(List<SysMenu> list, SysMenu t)
  470. {
  471. return getChildList(list, t).size() > 0;
  472. }
  473. /**
  474. * 内链域名特殊字符替换
  475. *
  476. * @return
  477. */
  478. public String innerLinkReplaceEach(String path)
  479. {
  480. return StringUtils.replaceEach(path, new String[] { Constants.HTTP, Constants.HTTPS },
  481. new String[] { "", "" });
  482. }
  483. }