From ea8164e7e71c5a3440aac520231983085f7c0c7a Mon Sep 17 00:00:00 2001 From: x ronger Date: Tue, 17 Nov 2020 00:18:54 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E8=AE=BE=E7=BD=AE-=E8=B4=A6?= =?UTF-8?q?=E6=88=B7=E5=AE=89=E5=85=A8=E6=A8=A1=E5=9D=97:=201.=E6=8D=A2?= =?UTF-8?q?=E7=BB=91=E9=82=AE=E7=AE=B1=202.=E4=BF=AE=E6=94=B9=E5=AF=86?= =?UTF-8?q?=E7=A0=81=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../vertical/config/BaseExceptionHandler.java | 4 +- .../vertical/config/BaseSessionManager.java | 3 ++ .../config/Swagger2Configuration.java | 39 --------------- .../core/exception/CaptchaException.java | 3 +- .../core/exception/ServiceException.java | 1 + .../rymcu/vertical/dto/ChangeEmailDTO.java | 17 +++++++ .../rymcu/vertical/dto/UpdatePasswordDTO.java | 15 ++++++ .../com/rymcu/vertical/mapper/UserMapper.java | 18 +++++++ .../rymcu/vertical/service/UserService.java | 18 +++++-- .../service/impl/UserServiceImpl.java | 50 ++++++++++++++----- .../web/api/common/CommonApiController.java | 3 -- .../web/api/user/UserInfoController.java | 15 +++++- src/main/java/mapper/UserMapper.xml | 8 ++- 13 files changed, 131 insertions(+), 63 deletions(-) delete mode 100644 src/main/java/com/rymcu/vertical/config/Swagger2Configuration.java create mode 100644 src/main/java/com/rymcu/vertical/dto/ChangeEmailDTO.java create mode 100644 src/main/java/com/rymcu/vertical/dto/UpdatePasswordDTO.java diff --git a/src/main/java/com/rymcu/vertical/config/BaseExceptionHandler.java b/src/main/java/com/rymcu/vertical/config/BaseExceptionHandler.java index 9ebe05e..1f7d6b5 100644 --- a/src/main/java/com/rymcu/vertical/config/BaseExceptionHandler.java +++ b/src/main/java/com/rymcu/vertical/config/BaseExceptionHandler.java @@ -81,7 +81,7 @@ public class BaseExceptionHandler { }else { ModelAndView mv = new ModelAndView(); FastJsonJsonView view = new FastJsonJsonView(); - Map attributes = new HashMap(); + Map attributes = new HashMap(2); if (ex instanceof BaseApiException){ attributes.put("code", "401"); attributes.put("message", "用户未登录"); @@ -128,7 +128,7 @@ public class BaseExceptionHandler { private boolean isAjax(HttpServletRequest request) { String requestedWith = request.getHeader("x-requested-with"); - if (requestedWith != null && requestedWith.equalsIgnoreCase("XMLHttpRequest")) { + if (requestedWith != null && "XMLHttpRequest".equalsIgnoreCase(requestedWith)) { return true; } else { return false; diff --git a/src/main/java/com/rymcu/vertical/config/BaseSessionManager.java b/src/main/java/com/rymcu/vertical/config/BaseSessionManager.java index 50d2d03..aab4e3d 100644 --- a/src/main/java/com/rymcu/vertical/config/BaseSessionManager.java +++ b/src/main/java/com/rymcu/vertical/config/BaseSessionManager.java @@ -9,6 +9,9 @@ import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import java.io.Serializable; +/** + * @author ronger + */ public class BaseSessionManager extends DefaultWebSessionManager { private static final String AUTHORIZATION = "Authorization"; diff --git a/src/main/java/com/rymcu/vertical/config/Swagger2Configuration.java b/src/main/java/com/rymcu/vertical/config/Swagger2Configuration.java deleted file mode 100644 index 471fdef..0000000 --- a/src/main/java/com/rymcu/vertical/config/Swagger2Configuration.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.rymcu.vertical.config; - -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import springfox.documentation.builders.ApiInfoBuilder; -import springfox.documentation.builders.PathSelectors; -import springfox.documentation.builders.RequestHandlerSelectors; -import springfox.documentation.service.ApiInfo; -import springfox.documentation.spi.DocumentationType; -import springfox.documentation.spring.web.plugins.Docket; -import springfox.documentation.swagger2.annotations.EnableSwagger2; - -/** - * @author ronger - */ -@Configuration -@EnableSwagger2 -public class Swagger2Configuration { - - @Bean - public Docket createRestApi() { - return new Docket(DocumentationType.SWAGGER_2) - .apiInfo(apiInfo()) - .select() - .apis(RequestHandlerSelectors.basePackage("com.rymcu.vertical.web.api")) - .paths(PathSelectors.any()) - .build(); - } - - private ApiInfo apiInfo() { - return new ApiInfoBuilder() - .title("Vertical项目接口文档") - .description("Vertical项目相关接口的文档") - .termsOfServiceUrl("http://www.rymcu.com") - .version("1.0") - .build(); - } - -} diff --git a/src/main/java/com/rymcu/vertical/core/exception/CaptchaException.java b/src/main/java/com/rymcu/vertical/core/exception/CaptchaException.java index 463ca5c..e20d4d5 100644 --- a/src/main/java/com/rymcu/vertical/core/exception/CaptchaException.java +++ b/src/main/java/com/rymcu/vertical/core/exception/CaptchaException.java @@ -4,7 +4,8 @@ import org.apache.shiro.authc.AuthenticationException; /** * 验证码错误异常类 - * + * + * @author ronger */ public class CaptchaException extends AuthenticationException { diff --git a/src/main/java/com/rymcu/vertical/core/exception/ServiceException.java b/src/main/java/com/rymcu/vertical/core/exception/ServiceException.java index fc95680..cf57b64 100644 --- a/src/main/java/com/rymcu/vertical/core/exception/ServiceException.java +++ b/src/main/java/com/rymcu/vertical/core/exception/ServiceException.java @@ -5,6 +5,7 @@ import com.rymcu.vertical.core.result.ResultCode; /** * 服务(业务)异常如“ 账号或密码错误 ”,该异常只做INFO级别的日志记录 @see WebMvcConfigurer + * @author ronger */ public class ServiceException extends Exception { private int code; diff --git a/src/main/java/com/rymcu/vertical/dto/ChangeEmailDTO.java b/src/main/java/com/rymcu/vertical/dto/ChangeEmailDTO.java new file mode 100644 index 0000000..e1d7cdd --- /dev/null +++ b/src/main/java/com/rymcu/vertical/dto/ChangeEmailDTO.java @@ -0,0 +1,17 @@ +package com.rymcu.vertical.dto; + +import lombok.Data; + +/** + * @author ronger + */ +@Data +public class ChangeEmailDTO { + + private Integer idUser; + + private String email; + + private String code; + +} diff --git a/src/main/java/com/rymcu/vertical/dto/UpdatePasswordDTO.java b/src/main/java/com/rymcu/vertical/dto/UpdatePasswordDTO.java new file mode 100644 index 0000000..3a42008 --- /dev/null +++ b/src/main/java/com/rymcu/vertical/dto/UpdatePasswordDTO.java @@ -0,0 +1,15 @@ +package com.rymcu.vertical.dto; + +import lombok.Data; + +/** + * @author ronger + */ +@Data +public class UpdatePasswordDTO { + + private Integer idUser; + + private String password; + +} diff --git a/src/main/java/com/rymcu/vertical/mapper/UserMapper.java b/src/main/java/com/rymcu/vertical/mapper/UserMapper.java index de1eae8..4bb394b 100644 --- a/src/main/java/com/rymcu/vertical/mapper/UserMapper.java +++ b/src/main/java/com/rymcu/vertical/mapper/UserMapper.java @@ -7,6 +7,8 @@ import com.rymcu.vertical.dto.UserInfoDTO; import com.rymcu.vertical.entity.User; import org.apache.ibatis.annotations.Param; +import java.util.Map; + /** * @author ronger */ @@ -120,4 +122,20 @@ public interface UserMapper extends Mapper { * @return */ Integer updateLastLoginTime(@Param("idUser") Integer idUser); + + /** + * 更换邮箱 + * @param idUser + * @param email + * @return + */ + Integer updateEmail(@Param("idUser") Integer idUser, @Param("email") String email); + + /** + * 更新密码 + * @param idUser + * @param password + * @return + */ + Integer updatePasswordById(@Param("idUser") Integer idUser, @Param("password") String password); } \ No newline at end of file diff --git a/src/main/java/com/rymcu/vertical/service/UserService.java b/src/main/java/com/rymcu/vertical/service/UserService.java index aabaa32..54f8246 100644 --- a/src/main/java/com/rymcu/vertical/service/UserService.java +++ b/src/main/java/com/rymcu/vertical/service/UserService.java @@ -1,9 +1,7 @@ package com.rymcu.vertical.service; import com.rymcu.vertical.core.service.Service; -import com.rymcu.vertical.dto.Author; -import com.rymcu.vertical.dto.UserDTO; -import com.rymcu.vertical.dto.UserInfoDTO; +import com.rymcu.vertical.dto.*; import com.rymcu.vertical.entity.User; import com.rymcu.vertical.entity.UserExtend; import org.apache.ibatis.exceptions.TooManyResultsException; @@ -123,4 +121,18 @@ public interface UserService extends Service { * @return */ UserExtend selectUserExtendByNickname(String nickname); + + /** + * 更换邮箱 + * @param changeEmailDTO + * @return + */ + Map updateEmail(ChangeEmailDTO changeEmailDTO); + + /** + * 更新密码 + * @param updatePasswordDTO + * @return + */ + Map updatePassword(UpdatePasswordDTO updatePasswordDTO); } diff --git a/src/main/java/com/rymcu/vertical/service/impl/UserServiceImpl.java b/src/main/java/com/rymcu/vertical/service/impl/UserServiceImpl.java index 80b47dc..3a007ee 100644 --- a/src/main/java/com/rymcu/vertical/service/impl/UserServiceImpl.java +++ b/src/main/java/com/rymcu/vertical/service/impl/UserServiceImpl.java @@ -2,10 +2,7 @@ package com.rymcu.vertical.service.impl; import com.rymcu.vertical.core.service.AbstractService; import com.rymcu.vertical.core.service.redis.RedisService; -import com.rymcu.vertical.dto.Author; -import com.rymcu.vertical.dto.TokenUser; -import com.rymcu.vertical.dto.UserDTO; -import com.rymcu.vertical.dto.UserInfoDTO; +import com.rymcu.vertical.dto.*; import com.rymcu.vertical.entity.Role; import com.rymcu.vertical.entity.User; import com.rymcu.vertical.entity.UserExtend; @@ -47,7 +44,8 @@ public class UserServiceImpl extends AbstractService implements UserServic @Resource private UserExtendMapper userExtendMapper; - private final static String avatarSvgType = "1"; + private final static String AVATAR_SVG_TYPE = "1"; + private final static String DEFAULT_AVATAR = "https://static.rymcu.com/article/1578475481946.png"; @Override public User findByAccount(String account) throws TooManyResultsException{ @@ -59,22 +57,23 @@ public class UserServiceImpl extends AbstractService implements UserServic public Map register(String email, String password, String code) { Map map = new HashMap(2); map.put("message","验证码无效!"); - String vcode = redisService.get(email); - if(StringUtils.isNotBlank(vcode)){ - if(vcode.equals(code)){ + String vCode = redisService.get(email); + if(StringUtils.isNotBlank(vCode)){ + if(vCode.equals(code)){ User user = userMapper.findByAccount(email); if(user != null){ map.put("message","该邮箱已被注册!"); } else { user = new User(); - user.setAccount(email); String nickname = email.split("@")[0]; nickname = checkNickname(nickname); user.setNickname(nickname); + user.setAccount(nickname); user.setEmail(email); user.setPassword(Utils.entryptPassword(password)); user.setCreatedTime(new Date()); user.setUpdatedTime(user.getCreatedTime()); + user.setAvatarUrl(DEFAULT_AVATAR); userMapper.insertSelective(user); user = userMapper.findByAccount(email); Role role = roleMapper.selectRoleByInputCode("user"); @@ -100,9 +99,7 @@ public class UserServiceImpl extends AbstractService implements UserServic @Override public Map login(String account, String password) { Map map = new HashMap(1); - User user = new User(); - user.setAccount(account); - user = userMapper.selectOne(user); + User user = userMapper.findByAccount(account); if(user != null){ if(Utils.comparePwd(password, user.getPassword())){ userMapper.updateLastLoginTime(user.getIdUser()); @@ -191,7 +188,7 @@ public class UserServiceImpl extends AbstractService implements UserServic map.put("message", "该昵称已使用!"); return map; } - if (StringUtils.isNotBlank(user.getAvatarType()) && avatarSvgType.equals(user.getAvatarType())) { + if (StringUtils.isNotBlank(user.getAvatarType()) && AVATAR_SVG_TYPE.equals(user.getAvatarType())) { String avatarUrl = UploadController.uploadBase64File(user.getAvatarUrl(), 0); user.setAvatarUrl(avatarUrl); user.setAvatarType("0"); @@ -242,4 +239,31 @@ public class UserServiceImpl extends AbstractService implements UserServic public UserExtend selectUserExtendByNickname(String nickname) { return userExtendMapper.selectUserExtendByNickname(nickname); } + + @Override + public Map updateEmail(ChangeEmailDTO changeEmailDTO) { + Map map = new HashMap(2); + map.put("message","验证码无效!"); + Integer idUser = changeEmailDTO.getIdUser(); + String email = changeEmailDTO.getEmail(); + String code = changeEmailDTO.getCode(); + String vCode = redisService.get(email); + if(StringUtils.isNotBlank(vCode)){ + if(vCode.equals(code)){ + userMapper.updateEmail(idUser, email); + map.put("message","更新成功!"); + map.put("email", email); + } + } + return map; + } + + @Override + public Map updatePassword(UpdatePasswordDTO updatePasswordDTO) { + Map map = new HashMap(1); + String password = Utils.entryptPassword(updatePasswordDTO.getPassword()); + userMapper.updatePasswordById(updatePasswordDTO.getIdUser(), password); + map.put("message", "更新成功!"); + return map; + } } diff --git a/src/main/java/com/rymcu/vertical/web/api/common/CommonApiController.java b/src/main/java/com/rymcu/vertical/web/api/common/CommonApiController.java index 34f4971..89f91de 100644 --- a/src/main/java/com/rymcu/vertical/web/api/common/CommonApiController.java +++ b/src/main/java/com/rymcu/vertical/web/api/common/CommonApiController.java @@ -14,7 +14,6 @@ import com.rymcu.vertical.service.PortfolioService; import com.rymcu.vertical.service.UserService; import com.rymcu.vertical.util.UserUtils; import com.rymcu.vertical.util.Utils; -import io.swagger.annotations.ApiOperation; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; @@ -39,7 +38,6 @@ public class CommonApiController { @Resource private PortfolioService portfolioService; - @ApiOperation(value = "获取邮件验证码") @GetMapping("/get-email-code") public GlobalResult> getEmailCode(@RequestParam("email") String email) throws MessagingException { Map map = new HashMap<>(1); @@ -56,7 +54,6 @@ public class CommonApiController { return GlobalResultGenerator.genSuccessResult(map); } - @ApiOperation(value = "获取找回密码邮件") @GetMapping("/get-forget-password-email") public GlobalResult> getForgetPasswordEmail(@RequestParam("email") String email) throws MessagingException { Map map = new HashMap<>(1); diff --git a/src/main/java/com/rymcu/vertical/web/api/user/UserInfoController.java b/src/main/java/com/rymcu/vertical/web/api/user/UserInfoController.java index 0aeb21b..24c7a69 100644 --- a/src/main/java/com/rymcu/vertical/web/api/user/UserInfoController.java +++ b/src/main/java/com/rymcu/vertical/web/api/user/UserInfoController.java @@ -2,7 +2,8 @@ package com.rymcu.vertical.web.api.user; import com.rymcu.vertical.core.result.GlobalResult; import com.rymcu.vertical.core.result.GlobalResultGenerator; -import com.rymcu.vertical.dto.UserInfoDTO; +import com.rymcu.vertical.dto.*; +import com.rymcu.vertical.entity.User; import com.rymcu.vertical.entity.UserExtend; import com.rymcu.vertical.service.UserService; import org.springframework.web.bind.annotation.*; @@ -44,4 +45,16 @@ public class UserInfoController { return GlobalResultGenerator.genSuccessResult(map); } + @PatchMapping("/update-email") + public GlobalResult updateEmail(@RequestBody ChangeEmailDTO changeEmailDTO) { + Map map = userService.updateEmail(changeEmailDTO); + return GlobalResultGenerator.genSuccessResult(map); + } + + @PatchMapping("/update-password") + public GlobalResult updatePassword(@RequestBody UpdatePasswordDTO updatePasswordDTO) { + Map map = userService.updatePassword(updatePasswordDTO); + return GlobalResultGenerator.genSuccessResult(map); + } + } diff --git a/src/main/java/mapper/UserMapper.xml b/src/main/java/mapper/UserMapper.xml index 2874d2c..72bed5c 100644 --- a/src/main/java/mapper/UserMapper.xml +++ b/src/main/java/mapper/UserMapper.xml @@ -68,9 +68,15 @@ update vertical_user set last_login_time = sysdate() where id = #{idUser} + + update vertical_user set email = #{email} where id = #{idUser} + + + update vertical_user set password = #{password} where id = #{idUser} +