| | |
| | | package com.zy.asrs.wcs.sys.controller; |
| | | |
| | | import com.zy.asrs.common.web.BaseController; |
| | | import com.zy.asrs.framework.common.Cools; |
| | | import com.zy.asrs.framework.common.R; |
| | | import com.zy.asrs.wcs.common.annotation.OperationLog; |
| | | import com.zy.asrs.wcs.common.config.ConfigProperties; |
| | | import com.zy.asrs.wcs.common.security.JwtSubject; |
| | | import com.zy.asrs.wcs.sys.controller.param.LoginParam; |
| | | import com.zy.asrs.wcs.sys.controller.param.UpdatePasswordParam; |
| | | import com.zy.asrs.wcs.sys.controller.result.LoginResult; |
| | | import com.zy.asrs.wcs.sys.entity.Menu; |
| | | import com.zy.asrs.wcs.sys.entity.User; |
| | | import com.zy.asrs.wcs.sys.entity.UserLogin; |
| | | import com.zy.asrs.wcs.sys.service.RoleMenuService; |
| | | import com.zy.asrs.wcs.sys.service.UserLoginService; |
| | | import com.zy.asrs.wcs.sys.service.UserService; |
| | | import com.zy.asrs.wcs.utils.JwtUtil; |
| | | import org.springframework.web.bind.annotation.PostMapping; |
| | | import org.springframework.web.bind.annotation.RequestBody; |
| | | import org.springframework.web.bind.annotation.RequestMapping; |
| | | import org.springframework.web.bind.annotation.RestController; |
| | | import com.zy.asrs.wcs.utils.Utils; |
| | | 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; |
| | | |
| | | /** |
| | | * 认证控制器 |
| | |
| | | private UserService userService; |
| | | @Resource |
| | | private UserLoginService userLoginService; |
| | | @Resource |
| | | private RoleMenuService roleMenuService; |
| | | |
| | | @PostMapping("/login") |
| | | public R login(@RequestBody LoginParam param, HttpServletRequest request) { |
| | |
| | | return R.error("密码错误"); |
| | | } |
| | | userLoginService.saveAsync(user.getId(), UserLogin.TYPE_LOGIN, null, hostId, request); |
| | | String accessToken = JwtUtil.buildToken(new JwtSubject(username, hostId), |
| | | String accessToken = JwtUtil.buildToken(new JwtSubject(username, user.getHostId()), |
| | | configProperties.getTokenExpireTime(), configProperties.getTokenKey()); |
| | | return R.ok("登录成功").add(new LoginResult(accessToken, user)); |
| | | } |
| | | |
| | | @GetMapping("/auth/user") |
| | | public R userInfo() { |
| | | return R.ok(userService.getByIdRel(getLoginUserId())); |
| | | } |
| | | |
| | | @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)); |
| | | } |
| | | |
| | | @PreAuthorize("hasAuthority('sys:auth:user')") |
| | | @OperationLog |
| | | @PutMapping("/auth/user") |
| | | public R updateInfo(@RequestBody User user) { |
| | | user.setId(getLoginUserId()); |
| | | // 不能修改的字段 |
| | | user.setUsername(null); |
| | | user.setPassword(null); |
| | | user.setEmailVerified(null); |
| | | user.setHostId(null); |
| | | user.setStatus(null); |
| | | if (userService.updateById(user)) { |
| | | return R.ok().add(userService.getByIdRel(user.getId())); |
| | | } |
| | | return R.error("保存失败"); |
| | | } |
| | | |
| | | @PreAuthorize("hasAuthority('sys:auth:password')") |
| | | @OperationLog |
| | | @PutMapping("/auth/password") |
| | | public R updatePassword(@RequestBody UpdatePasswordParam param) { |
| | | if (Cools.isEmpty(param.getOldPassword(), param.getPassword())) { |
| | | return R.error("参数不能为空"); |
| | | } |
| | | Long userId = getLoginUserId(); |
| | | if (userId == null) { |
| | | return R.error("未登录"); |
| | | } |
| | | if (!userService.comparePassword(userService.getById(userId).getPassword(), param.getOldPassword())) { |
| | | return R.error("原密码输入不正确"); |
| | | } |
| | | User user = new User(); |
| | | user.setId(userId); |
| | | user.setPassword(userService.encodePassword(param.getPassword())); |
| | | if (userService.updateById(user)) { |
| | | return R.ok("修改成功"); |
| | | } |
| | | return R.error("修改失败"); |
| | | } |
| | | |
| | | } |