| | |
| | | package com.vincent.rsf.server.system.controller; |
| | | |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.vincent.rsf.common.enums.SystemModeType; |
| | | import com.vincent.rsf.common.utils.Utils; |
| | | import com.vincent.rsf.framework.common.BaseRes; |
| | |
| | | import com.vincent.rsf.server.common.security.JwtSubject; |
| | | import com.vincent.rsf.server.common.service.EmailService; |
| | | import com.vincent.rsf.server.common.service.RedisService; |
| | | import com.vincent.rsf.server.common.utils.JSONUtil; |
| | | import com.vincent.rsf.server.common.utils.JwtUtil; |
| | | import com.vincent.rsf.server.system.controller.param.LoginParam; |
| | | import com.vincent.rsf.server.system.controller.param.RegisterParam; |
| | |
| | | import com.vincent.rsf.server.system.controller.result.LoginResult; |
| | | import com.vincent.rsf.server.system.controller.result.MenuVo; |
| | | import com.vincent.rsf.server.system.controller.result.SystemInfoVo; |
| | | import com.vincent.rsf.server.system.controller.result.UserMenuRouteVo; |
| | | import com.vincent.rsf.server.system.entity.Menu; |
| | | import com.vincent.rsf.server.system.entity.Tenant; |
| | | import com.vincent.rsf.server.system.entity.User; |
| | |
| | | import com.vincent.rsf.server.system.service.UserService; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.web.bind.annotation.*; |
| | | import com.vincent.rsf.server.manager.utils.buildPageRowsUtils; |
| | | |
| | | import javax.annotation.Resource; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import jakarta.annotation.Resource; |
| | | import jakarta.servlet.http.HttpServletRequest; |
| | | import java.util.LinkedHashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | |
| | | configProperties.getTokenExpireTime(), configProperties.getTokenKey()); |
| | | userLoginService.saveAsync(user.getId(), accessToken, UserLogin.TYPE_LOGIN, user.getTenantId(), null, request); |
| | | Tenant tenant = tenantService.getById(user.getTenantId()); |
| | | return R.ok("Sign In Success").add(new LoginResult(accessToken, user, tenant.getName())); |
| | | user = userService.fillRoleIdsForResponse(user); |
| | | return R.ok("Sign In Success").add(buildPageRowsUtils.rowsMap(new LoginResult(accessToken, user, tenant.getName()))); |
| | | } |
| | | |
| | | @GetMapping("/email/code") |
| | |
| | | configProperties.getTokenExpireTime(), configProperties.getTokenKey()); |
| | | userLoginService.saveAsync(user.getId(), accessToken, UserLogin.TYPE_LOGIN, tenantId, null, request); |
| | | Tenant tenant = tenantService.getById(user.getTenantId()); |
| | | return R.ok("Sign Up Success").add(new LoginResult(accessToken, user, tenant.getName())); |
| | | return R.ok("Sign Up Success").add(buildPageRowsUtils.rowsMap(new LoginResult(accessToken, user, tenant.getName()))); |
| | | } |
| | | |
| | | @GetMapping("/system/info") |
| | |
| | | |
| | | @GetMapping("/tenant/list") |
| | | public R tenantList() { |
| | | return R.ok().add(tenantService.list()); |
| | | return R.ok().add(buildPageRowsUtils.rowsMap(tenantService.list())); |
| | | } |
| | | |
| | | @GetMapping("/auth/user") |
| | |
| | | && !vo.getName().equals("menu.menu") |
| | | ).collect(Collectors.toList()); |
| | | } |
| | | return R.ok().add(Utils.toTreeData(voList, 0L, MenuVo::getParentId, MenuVo::getId, MenuVo::setChildren)); |
| | | return R.ok().add(buildPageRowsUtils.rowsMap(Utils.toTreeData(voList, 0L, MenuVo::getParentId, MenuVo::getId, MenuVo::setChildren))); |
| | | } |
| | | |
| | | @GetMapping("/auth/menu/v2") |
| | | public R userMenuV2() { |
| | | List<Menu> menus = roleMenuService.listMenuByUserId(getLoginUserId(), Menu.TYPE_MENU); |
| | | Map<Long, List<Menu>> buttonMap = roleMenuService.listButtonByUserId(getLoginUserId()) |
| | | .stream() |
| | | .collect(Collectors.groupingBy(Menu::getParentId)); |
| | | List<UserMenuRouteVo> voList = menus.stream().map(menu -> convertToRouteVo(menu, buttonMap)).collect(Collectors.toList()); |
| | | if (!configProperties.getSuperUserList().contains(getLoginUser().getUsername())) { |
| | | voList = voList.stream().filter(vo -> |
| | | !vo.getName().equals("menu.tenant") |
| | | && !vo.getName().equals("menu.menu") |
| | | ).collect(Collectors.toList()); |
| | | } |
| | | return R.ok().add(buildPageRowsUtils.rowsMap(Utils.toTreeData(voList, 0L, UserMenuRouteVo::getParentId, UserMenuRouteVo::getId, UserMenuRouteVo::setChildren))); |
| | | } |
| | | |
| | | @OperationLog("Update UserInfo") |
| | |
| | | } |
| | | } |
| | | if (userService.updateById(user)) { |
| | | return R.ok("Save Success").add(userService.getById(user.getId())); |
| | | return R.ok("Save Success").add(buildPageRowsUtils.rowsMap(userService.getById(user.getId()))); |
| | | } |
| | | return R.error("Save Fail"); |
| | | } |
| | |
| | | return vo; |
| | | } |
| | | |
| | | private UserMenuRouteVo convertToRouteVo(Menu menu, Map<Long, List<Menu>> buttonMap) { |
| | | if (menu == null) { |
| | | return null; |
| | | } |
| | | UserMenuRouteVo vo = new UserMenuRouteVo(); |
| | | vo.setId(menu.getId()); |
| | | vo.setParentId(menu.getParentId()); |
| | | vo.setName(menu.getName()); |
| | | vo.setPath(resolveRoutePath(menu)); |
| | | vo.setComponent(resolveComponent(menu)); |
| | | vo.setMeta(buildMenuMeta(menu, buttonMap.get(menu.getId()))); |
| | | if (menu.getChildren() != null && !menu.getChildren().isEmpty()) { |
| | | vo.setChildren(menu.getChildren().stream().map(child -> convertToRouteVo(child, buttonMap)).collect(Collectors.toList())); |
| | | } |
| | | return vo; |
| | | } |
| | | |
| | | private String resolveComponent(Menu menu) { |
| | | if (menu.getParentId() == null || menu.getParentId() == 0L) { |
| | | return "/index/index"; |
| | | } |
| | | return "/"+menu.getComponent(); |
| | | } |
| | | |
| | | private String resolveRoutePath(Menu menu) { |
| | | String route = menu.getRoute(); |
| | | if (Cools.isEmpty(route)) { |
| | | return route; |
| | | } |
| | | if (menu.getParentId() != null && menu.getParentId() > 0 && route.startsWith("/")) { |
| | | return route.substring(1 + route.lastIndexOf("/", route.length() - 2)); |
| | | } |
| | | return route; |
| | | } |
| | | |
| | | private Map<String, Object> buildMenuMeta(Menu menu, List<Menu> buttons) { |
| | | Map<String, Object> meta = parseMeta(menu.getMeta()); |
| | | if (Cools.isEmpty(meta.get("title")) && !Cools.isEmpty(menu.getName())) { |
| | | meta.put("title", menu.getName()); |
| | | } |
| | | if (Cools.isEmpty(meta.get("icon")) && !Cools.isEmpty(menu.getIcon())) { |
| | | meta.put("icon", menu.getIcon()); |
| | | } |
| | | if (!Cools.isEmpty(buttons)) { |
| | | meta.put("authList", buttons.stream().map(this::convertButtonAuth).collect(Collectors.toList())); |
| | | } |
| | | return meta; |
| | | } |
| | | |
| | | private Map<String, Object> convertButtonAuth(Menu button) { |
| | | Map<String, Object> authItem = new LinkedHashMap<>(); |
| | | authItem.put("title", button.getName()); |
| | | authItem.put("authMark", extractAuthMark(button.getAuthority())); |
| | | return authItem; |
| | | } |
| | | |
| | | private String extractAuthMark(String authority) { |
| | | if (Cools.isEmpty(authority)) { |
| | | return null; |
| | | } |
| | | int lastIndex = authority.lastIndexOf(":"); |
| | | if (lastIndex < 0 || lastIndex == authority.length() - 1) { |
| | | return authority; |
| | | } |
| | | return authority.substring(lastIndex + 1); |
| | | } |
| | | |
| | | private Map<String, Object> parseMeta(String metaJson) { |
| | | if (Cools.isEmpty(metaJson)) { |
| | | return new LinkedHashMap<>(); |
| | | } |
| | | try { |
| | | JSONObject jsonObject = JSONUtil.parseObject(metaJson, JSONObject.class); |
| | | if (jsonObject == null) { |
| | | return new LinkedHashMap<>(); |
| | | } |
| | | return new LinkedHashMap<>(jsonObject); |
| | | } catch (Exception e) { |
| | | return new LinkedHashMap<>(); |
| | | } |
| | | } |
| | | |
| | | } |
| | | |