123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- package com.yunzhi.demo.controller;
-
- import com.yunzhi.demo.common.*;
- import com.yunzhi.demo.entity.SysLogin;
- import com.yunzhi.demo.entity.SysUser;
- import com.yunzhi.demo.service.ISysLoginService;
- import com.yunzhi.demo.service.ISysUserService;
- import com.yunzhi.demo.vo.ChangePassword;
- import com.yunzhi.demo.vo.LoginParam;
- import com.yunzhi.demo.vo.TokenParam;
- import io.swagger.annotations.Api;
- import io.swagger.annotations.ApiOperation;
- import io.swagger.annotations.ApiParam;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.*;
-
- import java.util.HashMap;
- import java.util.Map;
-
-
-
-
- @Api(tags = "登入/登出")
- @RestController
- public class LoginController extends BaseController {
-
- @Autowired
- ISysLoginService iSysLoginService;
-
- @Autowired
- ISysUserService iSysUserService;
-
- @PostMapping("/admin/login")
- @ApiOperation(value="登录", notes = "登录", httpMethod = "POST", response = ResponseBean.class)
- public ResponseBean login(@ApiParam("登录参数") @RequestBody LoginParam loginParam) throws Exception {
- if (null == loginParam) {
- return ResponseBean.error("账户或密码不能为空", ResponseBean.ERROR_ILLEGAL_PARAMS);
- }
-
- String userName = loginParam.getUserName();
- String password = loginParam.getPassword();
-
- if (StringUtils.isEmpty(userName) || StringUtils.isEmpty(password)) {
- return ResponseBean.error("账户或密码不能为空", ResponseBean.ERROR_ILLEGAL_PARAMS);
- }
-
- SysLogin sysLogin = iSysLoginService.getByLoginName(userName);
- if (null == sysLogin) {
- return ResponseBean.error("账户或密码不正确", ResponseBean.ERROR_ILLEGAL_PARAMS);
- }
-
- SysUser sysUser = iSysUserService.getById(sysLogin.getUserId());
- if (null == sysUser) {
- return ResponseBean.error("账户或密码不正确", ResponseBean.ERROR_ILLEGAL_PARAMS);
- }
-
- if (!checkPassword(password, sysLogin.getPassword(), sysLogin.getUserId())) {
- return ResponseBean.error("账户或密码不正确", ResponseBean.ERROR_ILLEGAL_PARAMS);
- }
-
- if (!Constants.STATUS_NORMAL.equals(sysUser.getStatus())) {
- return ResponseBean.error("用户状态不正确, 请联系管理人员", ResponseBean.ERROR_UNAVAILABLE);
- }
-
-
- TokenParam tokenParam = new TokenParam()
- .setUserId(sysUser.getUserId())
- .setOrgId("0");
- String token = JWTUtils.encode(tokenParam.toMap());
-
- Map<String, Object> res = new HashMap<String, Object>() {{
- put("user", sysUser);
- put("token", token);
- }};
- return ResponseBean.success(res);
- }
-
-
- @PutMapping("/admin/change-password")
- @ApiOperation(value="修改密码", notes = "修改密码", httpMethod = "PUT", response = ResponseBean.class)
- public ResponseBean changePassword(@ApiParam("修改密码参数") @RequestBody ChangePassword param) throws Exception {
- if (StringUtils.isEmpty(param.getOriginPassword()) || StringUtils.isEmpty(param.getNewPassword())) {
- return ResponseBean.error("原始密码或新密码不能为空", ResponseBean.ERROR_ILLEGAL_PARAMS);
- }
-
- SysUser currentUser = getCurrentUser();
- SysLogin sysLogin = iSysLoginService.getByUser(currentUser.getUserId());
-
- if (!checkPassword(param.getOriginPassword(), sysLogin.getPassword(), currentUser.getUserId())) {
- return ResponseBean.error("原始密码不正确", ResponseBean.ERROR_ILLEGAL_PARAMS);
- }
-
- sysLogin.setPassword(EncryptUtils.md5(param.getNewPassword(), currentUser.getUserId()));
- iSysLoginService.updateById(sysLogin);
-
- return ResponseBean.success("密码修改成功");
- }
-
- @PutMapping("/admin/reset-password/{userId}")
- @ApiOperation(value="重置密码", notes = "重置密码", httpMethod = "PUT", response = ResponseBean.class)
- public ResponseBean resetPassword(@ApiParam("用户ID") @PathVariable String userId) throws Exception {
- SysLogin sysLogin = iSysLoginService.getByUser(userId);
-
- String newPassword = EncryptUtils.md5(EncryptUtils.md5(Constants.DEFAULT_PASSWORD), userId);
- sysLogin.setPassword(newPassword);
- iSysLoginService.updateById(sysLogin);
-
- return ResponseBean.success("重置密码成功");
- }
-
- private boolean checkPassword(String src, String targ, String salt) {
- return EncryptUtils.md5(src, salt).equals(targ);
- }
- }
|