diff --git a/luoo_user/src/main/java/com/luoo/user/controller/LoginController.java b/luoo_user/src/main/java/com/luoo/user/controller/LoginController.java index c0a97ef..bbf2a1b 100644 --- a/luoo_user/src/main/java/com/luoo/user/controller/LoginController.java +++ b/luoo_user/src/main/java/com/luoo/user/controller/LoginController.java @@ -2,10 +2,7 @@ package com.luoo.user.controller; import java.io.ByteArrayOutputStream; import java.io.IOException; -import java.util.Arrays; -import java.util.Base64; -import java.util.Date; -import java.util.List; +import java.util.*; import java.util.concurrent.TimeUnit; import javax.servlet.http.HttpServletRequest; @@ -65,6 +62,9 @@ public class LoginController extends BaseController { @Autowired private WeChatAouth2Service weChatAouth2Service; + @Autowired + private JwtUtil jwtUtil; + /** * 更新好友粉丝数和用户关注数 * @@ -360,4 +360,29 @@ public class LoginController extends BaseController { public Result logout(@RequestHeader(value = "Authorization", required = true) String authorization){ return Result.success(); } + + /** + * 通过用户名/密码登录 + * + * @param userName 用户名 + * @param password 密码 + * @return 登录结果 + */ + @ApiOperation(value = "通过用户名/密码登录") + @PostMapping("/login/{userName}/{password}") + public Result loginByUserName(@PathVariable String userName, @PathVariable String password) { + UserInfo userInfo = userInfoService.findByUserNameAndPassword(userName, password); + if (userInfo !=null) { + //生成token + String token = jwtUtil.createJWT(userInfo.getId(),userInfo.getUserName(),"artist",""); + Map map = new HashMap<>(); + map.put("token",token); + map.put("roles","artist"); + map.put("name",userInfo.getUserName()); + return Result.success(map); + } else { + return Result.failed(StatusCode.USER_NAME_OR_PASSWORD_FAILED); + } + } + } diff --git a/luoo_user/src/main/java/com/luoo/user/dto/ArtistRegisterDto.java b/luoo_user/src/main/java/com/luoo/user/dto/ArtistRegisterDto.java index e990c92..3a66bcc 100644 --- a/luoo_user/src/main/java/com/luoo/user/dto/ArtistRegisterDto.java +++ b/luoo_user/src/main/java/com/luoo/user/dto/ArtistRegisterDto.java @@ -4,6 +4,7 @@ import io.swagger.annotations.ApiModelProperty; import lombok.Data; import javax.validation.constraints.NotBlank; +import javax.validation.constraints.NotNull; import java.io.Serializable; /** @@ -21,7 +22,7 @@ public class ArtistRegisterDto implements Serializable { private String nickName; @ApiModelProperty(name = "艺人类型") - @NotBlank(message = "艺人类型必填") + @NotNull(message = "艺人类型必填") private Integer type; @ApiModelProperty(name = "艺人背景图片") @@ -48,7 +49,7 @@ public class ArtistRegisterDto implements Serializable { private String responsibleName; @ApiModelProperty(name = "性别") - @NotBlank(message = "负责人性别必填") + @NotNull(message = "负责人性别必填") private Integer sex; @ApiModelProperty(name = "负责人手机") diff --git a/luoo_user/src/main/java/com/luoo/user/service/ArtistService.java b/luoo_user/src/main/java/com/luoo/user/service/ArtistService.java index 62618d9..0b8a454 100644 --- a/luoo_user/src/main/java/com/luoo/user/service/ArtistService.java +++ b/luoo_user/src/main/java/com/luoo/user/service/ArtistService.java @@ -11,6 +11,7 @@ import constants.Constants; import enums.UserStatusEnum; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import util.IdWorker; @@ -44,6 +45,9 @@ public class ArtistService { @Autowired private IdWorker idWorker; + @Autowired + private BCryptPasswordEncoder encoder; + /** * 音乐人注册 * @@ -52,7 +56,7 @@ public class ArtistService { public void artistRegister(ArtistRegisterDto artistRegisterDto) { UserInfo userInfoByUserName = userInfoDao.findUserInfoByUserName(artistRegisterDto.getUserName()); - if(userInfoByUserName == null) { + if(userInfoByUserName != null) { throw new RuntimeException("该用户名已存在,请重新输入!"); } @@ -66,7 +70,7 @@ public class ArtistService { .thumbnail(Constants.DEFAULT_USER_THUMBNAIL) .signature(Constants.DEFAULT_USER_SIGNATURE) .userName(artistRegisterDto.getUserName()) - .password(artistRegisterDto.getPassword()) + .password(encoder.encode(artistRegisterDto.getPassword())) .build(); userInfoDao.save(userInfo); @@ -83,6 +87,7 @@ public class ArtistService { // 新增音乐人负责人信息 ArtistResponsible artistResponsible = ArtistResponsible.builder() + .id(String.valueOf(idWorker.nextId())) .userId(userInfo.getId()) .sex(artistRegisterDto.getSex()) .name(artistRegisterDto.getNickName()) diff --git a/luoo_user/src/main/java/com/luoo/user/service/UserInfoService.java b/luoo_user/src/main/java/com/luoo/user/service/UserInfoService.java index 985f9ea..b8b6de5 100644 --- a/luoo_user/src/main/java/com/luoo/user/service/UserInfoService.java +++ b/luoo_user/src/main/java/com/luoo/user/service/UserInfoService.java @@ -413,4 +413,19 @@ public class UserInfoService { userInfoDao.save(userInfo); } + /** + * 校验用户名密码是否匹配 + * + * @param userName 用户名 + * @param password 密码 + * @return 成功返回该用户,失败返回null + */ + public UserInfo findByUserNameAndPassword(String userName, String password) { + UserInfo userInfoByUserName = userInfoDao.findUserInfoByUserName(userName); + if(userInfoByUserName != null && encoder.matches(password, userInfoByUserName.getPassword())) { + return userInfoByUserName; + } else { + return null; + } + } }