package com.product.admin.entity; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.beust.jcommander.internal.Lists; import com.beust.jcommander.internal.Sets; import com.product.admin.config.CmnConst; import com.product.common.lang.StringUtils; import com.product.core.cache.DataPoolCacheImpl; import com.product.core.entity.DataTableEntity; import com.product.core.entity.FieldSetEntity; import com.product.util.BaseUtil; import java.io.Serializable; import java.util.*; import java.util.stream.Collectors; public class RouterEntity implements Serializable { private static final long serialVersionUID = 1L; /** * 根据按钮存放路由 */ private Map buttonRouterMap = new HashMap<>(); /** * 根据功能存放路由 */ private Map> functionRouterMap = new HashMap<>(); /** * 根据按钮&功能存放路由 */ private Map functionButtonRouterMap = new HashMap<>(); /** * 根据 功能&按钮名称 存放的路由信息 */ private Map routerMetaMap = new HashMap<>(); /** * 根据角色存放路由信息 */ private Map>> roleRouterMap; public Map getButtonRouterMap() { return buttonRouterMap; } public void setButtonRouterMap(Map buttonRouterMap) { this.buttonRouterMap = buttonRouterMap; } public Map> getFunctionRouterMap() { return functionRouterMap; } public void setFunctionRouterMap(Map> functionRouterMap) { this.functionRouterMap = functionRouterMap; } public Map getFunctionButtonRouterMap() { return functionButtonRouterMap; } public void setFunctionButtonRouterMap(Map functionButtonRouterMap) { this.functionButtonRouterMap = functionButtonRouterMap; } public Map getRouterMetaMap() { return routerMetaMap; } public void setRouterMetaMap(Map routerMetaMap) { this.routerMetaMap = routerMetaMap; } public Map>> getRoleRouterMap() { return roleRouterMap; } public void setRoleRouterMap(Map>> roleRouterMap) { this.roleRouterMap = roleRouterMap; } public void updateRouter(String... role_uuid) { if (role_uuid != null && role_uuid.length > 0) { routerGroup(role_uuid); } } public void updateRouter(String function_uuid, DataTableEntity dt) { List temp = getRouterByFunction(function_uuid); List routerByFunction = null; if (temp != null) { routerByFunction = Lists.newArrayList(temp); } for (int i = 0; i < dt.getRows(); i++) { String button_uuid = dt.getString(i, "button_uuid"); RouterMeta routerMeta = buttonRouterMap.get(button_uuid); if (routerMeta == null) { // 新增路由 routerMeta = new RouterMeta(dt.getFieldSetEntity(i)); reload(routerMeta); } else { // 历史路由 需要更新 routerMeta.reload(dt.getFieldSetEntity(i)); routerByFunction.remove(routerMeta); } } if (routerByFunction != null && routerByFunction.size() > 0) { removeRouter(function_uuid, routerByFunction); } } /** * 删除缓存中的路由 * * @param function_uuid * @param routerMetas */ public void removeRouter(String function_uuid, List routerMetas) { if (function_uuid != null && routerMetas != null) { List routerByFunction = getRouterByFunction(function_uuid); for (int i = 0; i < routerMetas.size(); i++) { RouterMeta routerMeta = routerMetas.get(0); if (routerByFunction != null) { routerMetas.remove(i); i--; continue; } routerMetaMap.values().remove(routerMeta); functionButtonRouterMap.values().remove(routerMeta); functionRouterMap.values().remove(routerMeta); buttonRouterMap.values().remove(routerMeta); if (roleRouterMap != null) { for (List> value : roleRouterMap.values()) { if (value.get(0).contains(routerMeta)) { value.get(0).remove(routerMeta); } if (value.get(1).contains(routerMeta)) { value.get(1).remove(routerMeta); } } } } } } public void routerGroup(String... roleUuid) { DataPoolCacheImpl dataPoolCache = DataPoolCacheImpl.getInstance(); if (dataPoolCache == null) { // 跳出 return; } String[] role = null; if (roleUuid != null && roleUuid.length > 0) { role = roleUuid; } if (this.roleRouterMap == null) { this.roleRouterMap = new HashMap<>(); } DataTableEntity permissionInfo; if (role != null) { permissionInfo = new DataTableEntity(); StringBuilder sql = new StringBuilder(); sql.append(" SELECT "); sql.append(" role_uuid,function_uuid,button_uuid,role_uuid as uuid "); sql.append(" FROM product_sys_function_permission where "); sql.append(" role_uuid in (select uuid from product_sys_role where "); sql.append(BaseUtil.buildQuestionMarkFilter("uuid", role, true)); sql.append(" ) "); DataTableEntity permissionTemp = dataPoolCache.getBaseDao().listTable(sql.toString(), new Object[]{}); // DataTableEntity permissionTemp = dataPoolCache.getCacheData("功能权限按角色分组", new String[]{uuid}); if (permissionTemp != null && permissionTemp.getRows() > 0) { BaseUtil.dataTableMerge(permissionInfo, permissionTemp); } } else { permissionInfo = dataPoolCache.getCacheData("功能权限按角色分组", role); } Map> fieldSets = permissionInfo.getFieldSets(); //角色uuid集合 Set roleSet = fieldSets.keySet(); // 遍历角色 for (Object role_uuid : roleSet) { //嵌套路由 Set nestMetaList = Sets.newHashSet(); //非嵌套路由 Set newTableMeatList = Sets.newHashSet(); //权限 List permissionList = fieldSets.get(role_uuid); //循环权限信息 for (FieldSetEntity fse : permissionList) { String function_uuid = fse.getString("function_uuid"); String button_uuid = fse.getString("button_uuid"); if (StringUtils.isEmpty(button_uuid) || StringUtils.isEmpty(function_uuid)) { continue; } //按钮uuid集合 String[] buttons = button_uuid.split(","); for (String key : buttons) { RouterMeta routerMeta = getRouterByFunctionButton(function_uuid, key); if (routerMeta == null) { continue; } else if (routerMeta.isNewTabs()) { newTableMeatList.add(routerMeta); } else { nestMetaList.add(routerMeta); } } } roleRouterMap.put(String.valueOf(role_uuid), Lists.newArrayList(nestMetaList, newTableMeatList)); } } /** * 获取路由根据功能 * * @param function_uuid * @return */ public List getRouterByFunction(String function_uuid) { return function_uuid != null ? functionRouterMap.get(function_uuid) : null; } /** * 获取路由根据功能按钮 * * @param function_uuid * @param button_uuid * @return */ private RouterMeta getRouterByFunctionButton(String function_uuid, String button_uuid) { return button_uuid == null ? null : functionButtonRouterMap.get(function_uuid + "/" + button_uuid); } /** * 获取路由 根据按钮 * * @param button_uuid * @return */ public RouterMeta getRouterByButton(String button_uuid) { return button_uuid == null ? null : buttonRouterMap.get(button_uuid); } /** * 根据功能&按钮uuid获取routeName * * @param function_uuid * @param button_uuid * @param role_uuid 角色uuid数组验证传入角色是否拥有权限 * @return */ public String getRouteNameByUuid(String function_uuid, String button_uuid, String[] role_uuid) { if (roleRouterMap == null || role_uuid == null) { return null; } RouterMeta routerByFunctionButton = getRouterByFunctionButton(function_uuid, button_uuid); if (routerByFunctionButton == null) { return null; } if (!routerPermission(role_uuid, routerByFunctionButton)) { return null; } return routerByFunctionButton.getName(); } /** * 判断是否拥有权限 * * @param role_uuid * @param routerEntity * @return */ public boolean routerPermission(String[] role_uuid, RouterMeta routerEntity) { if (role_uuid == null || role_uuid.length <= 0 || routerEntity == null) { return false; } for (String key : role_uuid) { List> sets = roleRouterMap.get(key); if (sets != null) { for (Set set : sets) { if (set.contains(routerEntity)) { return true; } } } } return false; } /** * 根据功能&按钮名称获取routeName * * @param function_uuid * @param button_name * @param role_uuid 角色uuid数组验证传入角色是否拥有权限 * @return */ public String getRouteNameByName(String function_uuid, String button_name, String[] role_uuid) { if (roleRouterMap == null || role_uuid == null) { //参数异常 return null; } RouterMeta routerByFunctionButton = routerMetaMap.get(function_uuid + "/" + button_name); if (routerByFunctionButton == null) { //路由不存在 return null; } if (routerPermission(role_uuid, routerByFunctionButton)) { return routerByFunctionButton.getName(); } // 没有权限 return null; } /** * 路由顶级模板 * * @param openWith 打开方式 (新窗口) * @return */ private JSONObject getRouterParentTemp(boolean openWith) { JSONObject temp = new JSONObject(); temp.put(CmnConst.NAME, ""); temp.put(CmnConst.PATH, ""); temp.put("redirect", "noRedirect"); if (!openWith) { temp.put(CmnConst.COMPONENT, "Layout"); } else { temp.put(CmnConst.COMPONENT, "ParentView"); } temp.put("alwaysShow", true); return temp; } protected List getFixRouter() { FieldSetEntity fse = new FieldSetEntity(); fse.setTableName("product_sys_router_v"); fse.setValue("component", "error/404.vue"); fse.setValue("name", "404"); fse.setValue("path", "/404"); return Lists.newArrayList(new RouterMeta(fse)); } public List getMobileRouterList(String[] role_uuid) { Set routerMetas = new HashSet<>(); if (role_uuid != null && roleRouterMap != null) { for (String key : role_uuid) { List> sets = roleRouterMap.get(key); if (sets != null) { routerMetas.addAll(sets.get(0)); routerMetas.addAll(sets.get(1)); } } } return routerMetas.parallelStream().filter(routerMeta -> routerMeta.clientType.indexOf("App") != -1).collect(Collectors.toList()); } /** * 获取路由 * * @param role_uuid * @return */ public JSONArray getRouterList(String[] role_uuid) { JSONArray result = new JSONArray(); Set nestMetaList = Sets.newHashSet(); Set newTabMetas = Sets.newHashSet(); if (role_uuid != null && roleRouterMap != null) { for (String key : role_uuid) { List> sets = roleRouterMap.get(key); if (sets != null) { nestMetaList.addAll(sets.get(0)); newTabMetas.addAll(sets.get(1)); } } } JSONObject routerParentTemp; if (nestMetaList.size() > 0) { routerParentTemp = getRouterParentTemp(false); routerParentTemp.put("children", nestMetaList); result.add(routerParentTemp); } if (newTabMetas.size() > 0) { routerParentTemp = getRouterParentTemp(true); nestMetaList.addAll(getFixRouter()); routerParentTemp.put("children", newTabMetas); result.add(routerParentTemp); } // if (newTabMetas.size() > 0) { // routerParentTemp = getRouterParentTemp(false); // nestMetaList.addAll(getFixRouter()); // routerParentTemp.put("children", newTabMetas); // result.add(routerParentTemp); // } return result; } /** * 初始构造函数 * * @param dt */ public RouterEntity(DataTableEntity dt) { if (dt != null && dt.getRows() > 0) { for (int i = 0; i < dt.getRows(); i++) { RouterMeta routerMeta = new RouterMeta(dt.getFieldSetEntity(i)); reload(routerMeta); } routerGroup(); } } /** * 重新装载 * * @param routerMeta */ public void reload(RouterMeta routerMeta) { String function_uuid = routerMeta.getMeta().getString("group"); String button_uuid = routerMeta.getMeta().getString("button_uuid"); String buttonName = routerMeta.getButton(); this.buttonRouterMap.put(button_uuid, routerMeta); functionButtonRouterMap.put(function_uuid + "/" + button_uuid, routerMeta); if (this.functionRouterMap.get(function_uuid) == null) { this.functionRouterMap.put(function_uuid, new ArrayList<>()); } routerMetaMap.put(function_uuid + "/" + buttonName, routerMeta); List functionRouter = this.functionRouterMap.get(function_uuid); functionRouter.add(routerMeta); } /** * 单个路由 */ public class RouterMeta implements Serializable { private static final long serialVersionUID = 1L; /** * 名称 */ private String name; /** * 路由 */ private String path; /** * 组件路径 */ private String component; private boolean newTabs; private String buttonName; private JSONObject meta; /** * 页面类型 */ private String pageType; /** * 客户端类型 */ private String clientType; public void setName(String name) { this.name = name; } public void setPath(String path) { this.path = path; } public void setComponent(String component) { this.component = component; } public void setNewTabs(boolean newTabs) { this.newTabs = newTabs; } public String getButtonName() { return buttonName; } public void setButtonName(String buttonName) { this.buttonName = buttonName; } public void setMeta(JSONObject meta) { this.meta = meta; } public void setPageType(String pageType) { this.pageType = pageType; } public String getClientType() { return clientType; } public void setClientType(String clientType) { this.clientType = clientType; } public RouterMeta(FieldSetEntity fse) { reload(fse); } public String getButton() { return this.buttonName; } public boolean isNewTabs() { return newTabs; } public Object getMeta(String key) { return this.meta.get(key); } public String getName() { return name; } public String getPath() { return path; } public String getComponent() { return component; } public String getPageType() { return pageType; } public JSONObject getMeta() { return meta; } public void reload(FieldSetEntity fse) { this.name = fse.getString("name"); this.path = fse.getString("path"); this.component = fse.getString("component"); this.newTabs = fse.getBoolean("open_with"); this.buttonName = fse.getString("button_name"); this.pageType = fse.getString("page_type"); this.clientType = fse.getString("client_type_uuid"); meta = new JSONObject(); meta.put("icon", fse.getString("icon")); meta.put("group", fse.getString("group")); meta.put("title", fse.getString("title")); meta.put("face_uuid", fse.getString("face_uuid")); meta.put("noCache", fse.getBoolean("noCache")); meta.put("mvc_page_uuid", fse.getString("mvc_page_uuid")); meta.put("menu_icon", fse.getString("menu_icon")); meta.put("button_uuid", fse.getString("button_uuid")); meta.put("activeMenu", fse.getString("activeMenu")); if (fse.getString("face_number") != null) { meta.put("face_number", fse.getString("face_number")); } } } }