|  |  | 
 |  |  | package com.zy.acs.manager.system.controller; | 
 |  |  |  | 
 |  |  | import com.zy.acs.common.utils.Utils; | 
 |  |  | import com.zy.acs.framework.common.Cools; | 
 |  |  | import com.zy.acs.framework.common.R; | 
 |  |  | import com.zy.acs.manager.common.annotation.OperationLog; | 
 |  |  | import com.zy.acs.manager.common.config.ConfigProperties; | 
 |  |  | import com.zy.acs.manager.common.security.JwtSubject; | 
 |  |  | 
 |  |  | import com.zy.acs.manager.system.controller.param.LoginParam; | 
 |  |  | import com.zy.acs.manager.system.controller.param.UpdatePasswordParam; | 
 |  |  | import com.zy.acs.manager.system.controller.result.LoginResult; | 
 |  |  | import com.zy.acs.manager.system.controller.result.MenuVo; | 
 |  |  | import com.zy.acs.manager.system.entity.Menu; | 
 |  |  | import com.zy.acs.manager.system.entity.User; | 
 |  |  | import com.zy.acs.manager.system.entity.UserLogin; | 
 |  |  | 
 |  |  | import com.zy.acs.manager.system.service.TenantService; | 
 |  |  | import com.zy.acs.manager.system.service.UserLoginService; | 
 |  |  | import com.zy.acs.manager.system.service.UserService; | 
 |  |  | import com.zy.acs.framework.common.Cools; | 
 |  |  | import com.zy.acs.framework.common.R; | 
 |  |  | import org.springframework.security.access.prepost.PreAuthorize; | 
 |  |  | import org.springframework.web.bind.annotation.*; | 
 |  |  |  | 
 |  |  | import javax.annotation.Resource; | 
 |  |  | import javax.servlet.http.HttpServletRequest; | 
 |  |  | import java.util.List; | 
 |  |  | import java.util.stream.Collectors; | 
 |  |  |  | 
 |  |  | /** | 
 |  |  |  * 认证控制器 | 
 |  |  | 
 |  |  |  | 
 |  |  |     @PostMapping("/login") | 
 |  |  |     public R login(@RequestBody LoginParam param, HttpServletRequest request) { | 
 |  |  | //        System.out.println(userService.encodePassword("123456")); | 
 |  |  |         String username = param.getUsername(); | 
 |  |  |         Long tenantId = param.getTenantId(); | 
 |  |  |         User user = userService.getByUsername(username, tenantId); | 
 |  |  |         if (user == null) { | 
 |  |  |             return R.error("账号不存在"); | 
 |  |  |             return R.error("Username Does Not Exist"); | 
 |  |  |         } | 
 |  |  |         if (!user.getStatus().equals(1)) { | 
 |  |  |             return R.error("账号被冻结"); | 
 |  |  |             return R.error("Account Frozen"); | 
 |  |  |         } | 
 |  |  |         if (!userService.comparePassword(user.getPassword(), param.getPassword())) { | 
 |  |  |             return R.error("密码错误"); | 
 |  |  |             return R.error("Wrong Password"); | 
 |  |  |         } | 
 |  |  |         String accessToken = JwtUtil.buildToken(new JwtSubject(username, user.getTenantId()), | 
 |  |  |                 configProperties.getTokenExpireTime(), configProperties.getTokenKey()); | 
 |  |  |         userLoginService.saveAsync(user.getId(), accessToken, UserLogin.TYPE_LOGIN, tenantId, null, request); | 
 |  |  |         return R.ok("登录成功").add(new LoginResult(accessToken, user)); | 
 |  |  |         return R.ok("Login Success").add(new LoginResult(accessToken, user)); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @GetMapping("/auth/user") | 
 |  |  | 
 |  |  |     @GetMapping("/auth/menu") | 
 |  |  |     public R userMenu() { | 
 |  |  |         List<Menu> menus = roleMenuService.listMenuByUserId(getLoginUserId(), Menu.TYPE_MENU); | 
 |  |  |         return R.ok().add(Utils.toTreeData(menus, 0L, Menu::getParentId, Menu::getId, Menu::setChildren)); | 
 |  |  | //        return R.ok().add(Utils.toTreeData(menus, 0L, Menu::getParentId, Menu::getId, Menu::setChildren)); | 
 |  |  |         List<MenuVo> voList = menus.stream().map(this::convertToVo).collect(Collectors.toList()); | 
 |  |  |         return R.ok().add(Utils.toTreeData(voList, 0L, MenuVo::getParentId, MenuVo::getId, MenuVo::setChildren)); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     @GetMapping("/auth/tenant") | 
 |  |  |     public R authHost() { | 
 |  |  | 
 |  |  |         if (userService.updateById(user)) { | 
 |  |  |             return R.ok().add(userService.getByIdRel(user.getId())); | 
 |  |  |         } | 
 |  |  |         return R.error("保存失败"); | 
 |  |  |         return R.error("Save Fail"); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @PreAuthorize("hasAuthority('sys:auth:password')") | 
 |  |  | 
 |  |  |     @PutMapping("/auth/password") | 
 |  |  |     public R updatePassword(@RequestBody UpdatePasswordParam param) { | 
 |  |  |         if (Cools.isEmpty(param.getOldPassword(), param.getPassword())) { | 
 |  |  |             return R.error("参数不能为空"); | 
 |  |  |             return R.error("Parameters Cannot Be Empty"); | 
 |  |  |         } | 
 |  |  |         Long userId = getLoginUserId(); | 
 |  |  |         if (userId == null) { | 
 |  |  |             return R.error("未登录"); | 
 |  |  |             return R.error("Please Login First"); | 
 |  |  |         } | 
 |  |  |         if (!userService.comparePassword(userService.getById(userId).getPassword(), param.getOldPassword())) { | 
 |  |  |             return R.error("原密码输入不正确"); | 
 |  |  |             return R.error("The Origin Password Was Incorrect"); | 
 |  |  |         } | 
 |  |  |         User user = new User(); | 
 |  |  |         user.setId(userId); | 
 |  |  |         user.setPassword(userService.encodePassword(param.getPassword())); | 
 |  |  |         if (userService.updateById(user)) { | 
 |  |  |             return R.ok("修改成功"); | 
 |  |  |             return R.ok("Update Success"); | 
 |  |  |         } | 
 |  |  |         return R.error("修改失败"); | 
 |  |  |         return R.error("Update Fail"); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     // ---------------------------------------------------- | 
 |  |  |  | 
 |  |  |     private MenuVo convertToVo(Menu menu) { | 
 |  |  |         if (menu == null) { | 
 |  |  |             return null; | 
 |  |  |         } | 
 |  |  |         MenuVo vo = new MenuVo(); | 
 |  |  |         vo.setId(menu.getId()); | 
 |  |  |         vo.setName(menu.getName()); | 
 |  |  |         vo.setParentId(menu.getParentId()); | 
 |  |  | //        vo.setParentName(menu.getParentName()); | 
 |  |  |         vo.setPath(menu.getPath()); | 
 |  |  | //        vo.setPathName(menu.getPathName()); | 
 |  |  |         vo.setRoute(menu.getRoute()); | 
 |  |  |         vo.setComponent(menu.getComponent()); | 
 |  |  |         vo.setType(menu.getType()); | 
 |  |  |         vo.setIcon(menu.getIcon()); | 
 |  |  |         vo.setSort(menu.getSort()); | 
 |  |  |         if (menu.getChildren() != null && !menu.getChildren().isEmpty()) { | 
 |  |  |             List<MenuVo> childDTOs = menu.getChildren().stream() | 
 |  |  |                     .map(this::convertToVo) | 
 |  |  |                     .collect(Collectors.toList()); | 
 |  |  |             vo.setChildren(childDTOs); | 
 |  |  |         } | 
 |  |  |         return vo; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  | } |