|
@@ -83,14 +83,38 @@ public class UserService {
|
|
|
userRepo.save(user);
|
|
userRepo.save(user);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public User loginByPhone(String phone) {
|
|
|
|
|
- return userRepo.findByPhoneAndDelFalse(phone);
|
|
|
|
|
|
|
+ public User loginByPhone(String phone, String code) {
|
|
|
|
|
+ smsService.verify(phone, code);
|
|
|
|
|
+ User user = userRepo.findByPhoneAndDelFalse(phone).orElse(null);
|
|
|
|
|
+ ;
|
|
|
|
|
+ if (user == null) {
|
|
|
|
|
+ String name = "9th_" + RandomStringUtils.randomAlphabetic(8);
|
|
|
|
|
+ user = create(UserRegister.builder()
|
|
|
|
|
+ .authorities(Collections.singleton(Authority.get(AuthorityName.ROLE_USER)))
|
|
|
|
|
+ .username(name)
|
|
|
|
|
+ .nickname(name)
|
|
|
|
|
+ .avatar(Constants.DEFAULT_AVATAR)
|
|
|
|
|
+ .phone(phone)
|
|
|
|
|
+ .build());
|
|
|
|
|
+ }
|
|
|
|
|
+ return user;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public User loginByPhonePwd(String phone, String password) {
|
|
|
|
|
+ User user = userRepo.findByPhoneAndDelFalse(phone).orElseThrow(new BusinessException("账号或密码错误"));
|
|
|
|
|
+
|
|
|
|
|
+ if (StringUtils.isNoneEmpty(user.getPassword()) &&
|
|
|
|
|
+ new BCryptPasswordEncoder().matches(password, user.getPassword())) {
|
|
|
|
|
+ throw new BusinessException("账号或密码错误");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return user;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public User loginMp(String code) throws WxErrorException {
|
|
public User loginMp(String code) throws WxErrorException {
|
|
|
WxMpOAuth2AccessToken accessToken = wxMpService.oauth2getAccessToken(code);
|
|
WxMpOAuth2AccessToken accessToken = wxMpService.oauth2getAccessToken(code);
|
|
|
WxMpUser wxMpUser = wxMpService.oauth2getUserInfo(accessToken, null);
|
|
WxMpUser wxMpUser = wxMpService.oauth2getUserInfo(accessToken, null);
|
|
|
- User user = userRepo.findByOpenIdAndDelFalse(wxMpUser.getOpenId());
|
|
|
|
|
|
|
+ User user = userRepo.findByOpenIdAndDelFalse(wxMpUser.getOpenId()).orElse(null);
|
|
|
if (user == null) {
|
|
if (user == null) {
|
|
|
user = User.builder()
|
|
user = User.builder()
|
|
|
.username(UUID.randomUUID().toString())
|
|
.username(UUID.randomUUID().toString())
|
|
@@ -114,7 +138,8 @@ public class UserService {
|
|
|
WxMaJscode2SessionResult result = wxMaService.jsCode2SessionInfo(code);
|
|
WxMaJscode2SessionResult result = wxMaService.jsCode2SessionInfo(code);
|
|
|
String openId = result.getOpenid();
|
|
String openId = result.getOpenid();
|
|
|
String sessionKey = result.getSessionKey();
|
|
String sessionKey = result.getSessionKey();
|
|
|
- User userInfo = userRepo.findByOpenIdAndDelFalse(openId);
|
|
|
|
|
|
|
+ User userInfo = userRepo.findByOpenIdAndDelFalse(openId).orElse(null);
|
|
|
|
|
+ ;
|
|
|
if (userInfo != null) {
|
|
if (userInfo != null) {
|
|
|
return userInfo;
|
|
return userInfo;
|
|
|
}
|
|
}
|
|
@@ -142,7 +167,8 @@ public class UserService {
|
|
|
|
|
|
|
|
// 解密用户信息
|
|
// 解密用户信息
|
|
|
WxMaUserInfo wxUserInfo = wxMaService.getUserService().getUserInfo(sessionKey, encryptedData, iv);
|
|
WxMaUserInfo wxUserInfo = wxMaService.getUserService().getUserInfo(sessionKey, encryptedData, iv);
|
|
|
- User user = userRepo.findByOpenIdAndDelFalse(wxUserInfo.getOpenId());
|
|
|
|
|
|
|
+ User user = userRepo.findByOpenIdAndDelFalse(wxUserInfo.getOpenId()).orElse(null);
|
|
|
|
|
+ ;
|
|
|
|
|
|
|
|
String avatarUrl = Constants.DEFAULT_AVATAR;
|
|
String avatarUrl = Constants.DEFAULT_AVATAR;
|
|
|
try {
|
|
try {
|
|
@@ -196,4 +222,19 @@ public class UserService {
|
|
|
}
|
|
}
|
|
|
return setPassword(userId, password);
|
|
return setPassword(userId, password);
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ public void bindPhone(Long userId, String phone) {
|
|
|
|
|
+ User user = userRepo.findByIdAndDelFalse(userId).orElseThrow(new BusinessException("用户不存在"));
|
|
|
|
|
+ if (StringUtils.isNoneEmpty(user.getPhone())) {
|
|
|
|
|
+ throw new BusinessException("该账号已绑定手机");
|
|
|
|
|
+ }
|
|
|
|
|
+ userRepo.findByPhoneAndDelFalse(phone).ifPresent(user1 -> {
|
|
|
|
|
+ if (!user1.getId().equals(userId)) {
|
|
|
|
|
+ throw new BusinessException("该手机号已绑定其他账号");
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ user.setPhone(phone);
|
|
|
|
|
+ userRepo.save(user);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|