|  |  |  | 
|---|
|  |  |  | import io.swagger.annotations.ApiOperation; | 
|---|
|  |  |  | import lombok.extern.slf4j.Slf4j; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import java.io.IOException; | 
|---|
|  |  |  | import java.util.Map; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import javax.servlet.http.HttpServletRequest; | 
|---|
|  |  |  | import javax.servlet.http.HttpServletResponse; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
|---|
|  |  |  | import org.springframework.beans.factory.annotation.Value; | 
|---|
|  |  |  | import org.springframework.util.ObjectUtils; | 
|---|
|  |  |  | import org.springframework.web.bind.annotation.RequestBody; | 
|---|
|  |  |  | import org.springframework.web.bind.annotation.RequestMapping; | 
|---|
|  |  |  | import org.springframework.web.bind.annotation.RequestMethod; | 
|---|
|  |  |  | import org.springframework.web.bind.annotation.RestController; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.moral.api.entity.Group; | 
|---|
|  |  |  | import com.moral.api.entity.User; | 
|---|
|  |  |  | import com.moral.api.service.GroupService; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | 
|---|
|  |  |  | import com.moral.api.entity.SysConfig; | 
|---|
|  |  |  | import com.moral.api.service.SysConfigService; | 
|---|
|  |  |  | import com.moral.api.service.UserService; | 
|---|
|  |  |  | import com.moral.constant.Constants; | 
|---|
|  |  |  | import com.moral.constant.ResponseCodeEnum; | 
|---|
|  |  |  | import com.moral.constant.ResultMessage; | 
|---|
|  |  |  | import com.moral.util.AESUtils; | 
|---|
|  |  |  | import com.moral.util.MD5Utils; | 
|---|
|  |  |  | import com.moral.pojo.VerificationCode; | 
|---|
|  |  |  | import com.moral.util.KaptchaUtils; | 
|---|
|  |  |  | import com.moral.util.TokenUtils; | 
|---|
|  |  |  | import com.moral.util.WebUtils; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Slf4j | 
|---|
|  |  |  | @Api(tags = {"登陆"}) | 
|---|
|  |  |  | 
|---|
|  |  |  | private UserService userService; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private GroupService groupService; | 
|---|
|  |  |  | private SysConfigService sysConfigService; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Value("${AES.KEY}") | 
|---|
|  |  |  | private String AESKey; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @ApiOperation(value = "登陆", notes = "登陆") | 
|---|
|  |  |  | @ApiImplicitParams({ | 
|---|
|  |  |  | @ApiImplicitParam(name = "account", value = "账户", required = false, paramType = "query", dataType = "String"), | 
|---|
|  |  |  | @ApiImplicitParam(name = "password", value = "密码", required = false, paramType = "query", dataType = "String") | 
|---|
|  |  |  | }) | 
|---|
|  |  |  | @ApiOperation(value = "登陆信息", notes = "登陆信息") | 
|---|
|  |  |  | @RequestMapping(value = "login", method = RequestMethod.POST) | 
|---|
|  |  |  | public ResultMessage login(HttpServletRequest request) { | 
|---|
|  |  |  | Map<String, Object> parameters = WebUtils.getParametersStartingWith(request, null); | 
|---|
|  |  |  | public ResultMessage login(@RequestBody Map<String, Object> parameters) { | 
|---|
|  |  |  | if (!(parameters.containsKey("account") && parameters.containsKey("password"))) { | 
|---|
|  |  |  | return ResultMessage.fail(ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode(), ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg()); | 
|---|
|  |  |  | return ResultMessage.fail(ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode(), | 
|---|
|  |  |  | ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //接收参数 | 
|---|
|  |  |  | String password = parameters.get("password").toString(); | 
|---|
|  |  |  | //密码解密 | 
|---|
|  |  |  | password = AESUtils.decrypt(password, AESKey); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | User user = userService.selectUserInfo(parameters); | 
|---|
|  |  |  | //校验账户 | 
|---|
|  |  |  | if (user == null) { | 
|---|
|  |  |  | return ResultMessage.fail(ResponseCodeEnum.ACCOUNT_NOT_EXIST.getCode(), ResponseCodeEnum.ACCOUNT_NOT_EXIST.getMsg()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //校验密码 | 
|---|
|  |  |  | if (!MD5Utils.saltMD5Verify(password, user.getPassword())) { | 
|---|
|  |  |  | return ResultMessage.fail(ResponseCodeEnum.PASSWORD_INVALID.getCode(), ResponseCodeEnum.PASSWORD_INVALID.getMsg()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //校验是否删除 | 
|---|
|  |  |  | if ("1".equals(user.getIsDelete())) { | 
|---|
|  |  |  | return ResultMessage.fail(ResponseCodeEnum.ACCOUNT_IS_DELETE.getCode(), ResponseCodeEnum.ACCOUNT_IS_DELETE.getMsg()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //校验是否过期 | 
|---|
|  |  |  | if (user.getExpireTime() != null && user.getExpireTime().getTime() < System.currentTimeMillis()) { | 
|---|
|  |  |  | return ResultMessage.fail(ResponseCodeEnum.ACCOUNT_IS_EXPIRE.getCode(), ResponseCodeEnum.ACCOUNT_IS_EXPIRE.getMsg()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | Map<String, Object> result = userService.login(user); | 
|---|
|  |  |  | Map<String, Object> result = userService.login(parameters); | 
|---|
|  |  |  | if (!result.containsKey("token")) { | 
|---|
|  |  |  | return ResultMessage.fail(ResponseCodeEnum.TOKEN_CREATE_ERROR.getCode(), ResponseCodeEnum.TOKEN_CREATE_ERROR.getMsg()); | 
|---|
|  |  |  | return ResultMessage.fail((int) result.get("code"), (String) result.get("msg")); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return ResultMessage.ok(); | 
|---|
|  |  |  | return ResultMessage.ok(result); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @ApiOperation(value = "注销", notes = "注销") | 
|---|
|  |  |  | @RequestMapping(value = "logout", method = RequestMethod.POST) | 
|---|
|  |  |  | public ResultMessage logout(HttpServletRequest request) { | 
|---|
|  |  |  | Map<String, Object> parameters = WebUtils.getParametersStartingWith(request, null); | 
|---|
|  |  |  | if (!parameters.containsKey("uid")) { | 
|---|
|  |  |  | return ResultMessage.fail(ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode(), ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | String userId = request.getParameter("uid"); | 
|---|
|  |  |  | String token = request.getHeader("token"); | 
|---|
|  |  |  | TokenUtils.destoryToken(userId, token); | 
|---|
|  |  |  | return ResultMessage.ok(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @ApiOperation(value = "添加组", notes = "添加组") | 
|---|
|  |  |  | @ApiOperation(value = "获取用户信息", notes = "获取用户信息") | 
|---|
|  |  |  | @ApiImplicitParams({ | 
|---|
|  |  |  | @ApiImplicitParam(name = "groupName", value = "组名", required = true, paramType = "query", dataType = "String") | 
|---|
|  |  |  | @ApiImplicitParam(name = "token", value = "token", required = true, paramType = "header", dataType = "String") | 
|---|
|  |  |  | }) | 
|---|
|  |  |  | @RequestMapping(value = "addGroup", method = RequestMethod.POST) | 
|---|
|  |  |  | private ResultMessage addGroup(Group group, HttpServletRequest request) { | 
|---|
|  |  |  | String currentUserId = request.getHeader("uid"); | 
|---|
|  |  |  | Map<String, Object> map = groupService.addGroup(group, currentUserId); | 
|---|
|  |  |  | String msg = map.get("msg").toString(); | 
|---|
|  |  |  | boolean flag = Boolean.parseBoolean(map.get("flag").toString()); | 
|---|
|  |  |  | if (flag) { | 
|---|
|  |  |  | return ResultMessage.ok(msg); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return ResultMessage.fail(msg); | 
|---|
|  |  |  | @RequestMapping(value = "getUserInfo", method = RequestMethod.POST) | 
|---|
|  |  |  | public ResultMessage getUserInfo() { | 
|---|
|  |  |  | Map<String, Object> result = userService.getCurrentUserInfo(); | 
|---|
|  |  |  | return ResultMessage.ok(result); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @ApiOperation(value = "退出", notes = "退出") | 
|---|
|  |  |  | @RequestMapping(value = "logout", method = RequestMethod.GET) | 
|---|
|  |  |  | @ApiImplicitParams({ | 
|---|
|  |  |  | @ApiImplicitParam(name = "token", value = "token", required = true, paramType = "header", dataType = "String") | 
|---|
|  |  |  | }) | 
|---|
|  |  |  | public ResultMessage logout(Integer userId, HttpServletRequest request) { | 
|---|
|  |  |  | if (userId == null) { | 
|---|
|  |  |  | return ResultMessage.fail(ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode(), | 
|---|
|  |  |  | ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | String token = request.getHeader("token"); | 
|---|
|  |  |  | TokenUtils.destoryToken(userId.toString(), token); | 
|---|
|  |  |  | return ResultMessage.ok(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @ApiOperation(value = "获取验证码是否开启", notes = "获取验证码是否开启") | 
|---|
|  |  |  | @RequestMapping(value = "verificationCode/config", method = RequestMethod.GET) | 
|---|
|  |  |  | public ResultMessage verifyConfig(HttpServletResponse response) { | 
|---|
|  |  |  | QueryWrapper<SysConfig> queryWrapper = new QueryWrapper<>(); | 
|---|
|  |  |  | queryWrapper.eq(Constants.FRONT_VERIFICATIONCODE_COLUMN, Constants.FRONT_ERIFICATIONCODE_VALUE); | 
|---|
|  |  |  | SysConfig sysConfig = sysConfigService.getOne(queryWrapper); | 
|---|
|  |  |  | if (Constants.VERFICATIONCODE_OPEN.equals(sysConfig.getCode())) { | 
|---|
|  |  |  | return new ResultMessage(ResponseCodeEnum.VERIFICATION_OPEN, null); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return new ResultMessage(ResponseCodeEnum.VERIFICATION_CLOSE, null); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @ApiOperation(value = "获取验证码", notes = "获取验证码") | 
|---|
|  |  |  | @RequestMapping(value = "verificationCode/get", method = RequestMethod.GET) | 
|---|
|  |  |  | public ResultMessage getVerificationCode() { | 
|---|
|  |  |  | VerificationCode verificationCode = null; | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | verificationCode = KaptchaUtils.createVerificationCode(); | 
|---|
|  |  |  | } catch (IOException e) { | 
|---|
|  |  |  | log.error(e.getMessage()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (ObjectUtils.isEmpty(verificationCode)) | 
|---|
|  |  |  | return ResultMessage.fail(); | 
|---|
|  |  |  | return ResultMessage.ok(verificationCode); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @ApiOperation(value = "校验验证码", notes = "校验验证码") | 
|---|
|  |  |  | @RequestMapping(value = "verificationCode/verify", method = RequestMethod.GET) | 
|---|
|  |  |  | public ResultMessage gverifyVerificationCode(VerificationCode verificationCode) { | 
|---|
|  |  |  | if (!verificationCode.valid()) | 
|---|
|  |  |  | return ResultMessage.fail(); | 
|---|
|  |  |  | if (KaptchaUtils.verify(verificationCode)) | 
|---|
|  |  |  | return ResultMessage.ok(); | 
|---|
|  |  |  | return ResultMessage.fail(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|