|
|
@@ -8,6 +8,8 @@ import com.izouma.awesomeAdmin.domain.User;
|
|
|
import com.izouma.awesomeAdmin.exception.BusinessException;
|
|
|
import com.izouma.awesomeAdmin.repo.UserRepo;
|
|
|
import com.izouma.awesomeAdmin.security.Authority;
|
|
|
+import com.izouma.awesomeAdmin.security.JwtTokenUtil;
|
|
|
+import com.izouma.awesomeAdmin.security.JwtUserFactory;
|
|
|
import com.izouma.awesomeAdmin.service.sms.SmsService;
|
|
|
import com.izouma.awesomeAdmin.service.storage.StorageService;
|
|
|
import lombok.AllArgsConstructor;
|
|
|
@@ -28,11 +30,12 @@ import java.util.*;
|
|
|
@Slf4j
|
|
|
@AllArgsConstructor
|
|
|
public class UserService {
|
|
|
- private UserRepo userRepo;
|
|
|
- private WxMaService wxMaService;
|
|
|
- private WxMpService wxMpService;
|
|
|
- private SmsService smsService;
|
|
|
+ private UserRepo userRepo;
|
|
|
+ private WxMaService wxMaService;
|
|
|
+ private WxMpService wxMpService;
|
|
|
+ private SmsService smsService;
|
|
|
private StorageService storageService;
|
|
|
+ private JwtTokenUtil jwtTokenUtil;
|
|
|
|
|
|
public User loginByPhone(String phone) {
|
|
|
return userRepo.findByPhone(phone);
|
|
|
@@ -44,18 +47,18 @@ public class UserService {
|
|
|
User user = userRepo.findByOpenId(wxMpUser.getOpenId());
|
|
|
if (user == null) {
|
|
|
user = User.builder()
|
|
|
- .username(UUID.randomUUID().toString())
|
|
|
- .nickname(wxMpUser.getNickname())
|
|
|
- .avatar(wxMpUser.getHeadImgUrl())
|
|
|
- .sex(wxMpUser.getSexDesc())
|
|
|
- .country(wxMpUser.getCountry())
|
|
|
- .province(wxMpUser.getProvince())
|
|
|
- .city(wxMpUser.getCity())
|
|
|
- .openId(wxMpUser.getOpenId())
|
|
|
- .language(wxMpUser.getLanguage())
|
|
|
- .enabled(true)
|
|
|
- .authorities(Collections.singleton(Authority.builder().name("ROLE_USER").build()))
|
|
|
- .build();
|
|
|
+ .username(UUID.randomUUID().toString())
|
|
|
+ .nickname(wxMpUser.getNickname())
|
|
|
+ .avatar(wxMpUser.getHeadImgUrl())
|
|
|
+ .sex(wxMpUser.getSexDesc())
|
|
|
+ .country(wxMpUser.getCountry())
|
|
|
+ .province(wxMpUser.getProvince())
|
|
|
+ .city(wxMpUser.getCity())
|
|
|
+ .openId(wxMpUser.getOpenId())
|
|
|
+ .language(wxMpUser.getLanguage())
|
|
|
+ .enabled(true)
|
|
|
+ .authorities(Collections.singleton(Authority.builder().name("ROLE_USER").build()))
|
|
|
+ .build();
|
|
|
userRepo.save(user);
|
|
|
}
|
|
|
return user;
|
|
|
@@ -71,13 +74,13 @@ public class UserService {
|
|
|
return userInfo;
|
|
|
}
|
|
|
userInfo = User.builder()
|
|
|
- .username(UUID.randomUUID().toString())
|
|
|
- .nickname("用户" + RandomStringUtils.randomAlphabetic(6))
|
|
|
- .openId(openId)
|
|
|
- .avatar(Constants.DEFAULT_AVATAR)
|
|
|
- .enabled(true)
|
|
|
- .authorities(Collections.singleton(Authority.builder().name("ROLE_USER").build()))
|
|
|
- .build();
|
|
|
+ .username(UUID.randomUUID().toString())
|
|
|
+ .nickname("用户" + RandomStringUtils.randomAlphabetic(6))
|
|
|
+ .openId(openId)
|
|
|
+ .avatar(Constants.DEFAULT_AVATAR)
|
|
|
+ .enabled(true)
|
|
|
+ .authorities(Collections.singleton(Authority.builder().name("ROLE_USER").build()))
|
|
|
+ .build();
|
|
|
userInfo = userRepo.save(userInfo);
|
|
|
return userInfo;
|
|
|
} catch (WxErrorException e) {
|
|
|
@@ -111,17 +114,17 @@ public class UserService {
|
|
|
if (user == null) {
|
|
|
|
|
|
user = User.builder()
|
|
|
- .username(UUID.randomUUID().toString())
|
|
|
- .nickname(wxUserInfo.getNickName())
|
|
|
- .openId(wxUserInfo.getOpenId())
|
|
|
- .avatar(avatarUrl)
|
|
|
- .sex(wxUserInfo.getGender())
|
|
|
- .country(wxUserInfo.getCountry())
|
|
|
- .province(wxUserInfo.getProvince())
|
|
|
- .city(wxUserInfo.getCity())
|
|
|
- .enabled(true)
|
|
|
- .authorities(Collections.singleton(Authority.builder().name("ROLE_USER").build()))
|
|
|
- .build();
|
|
|
+ .username(UUID.randomUUID().toString())
|
|
|
+ .nickname(wxUserInfo.getNickName())
|
|
|
+ .openId(wxUserInfo.getOpenId())
|
|
|
+ .avatar(avatarUrl)
|
|
|
+ .sex(wxUserInfo.getGender())
|
|
|
+ .country(wxUserInfo.getCountry())
|
|
|
+ .province(wxUserInfo.getProvince())
|
|
|
+ .city(wxUserInfo.getCity())
|
|
|
+ .enabled(true)
|
|
|
+ .authorities(Collections.singleton(Authority.builder().name("ROLE_USER").build()))
|
|
|
+ .build();
|
|
|
user = userRepo.save(user);
|
|
|
|
|
|
} else {
|
|
|
@@ -145,14 +148,21 @@ public class UserService {
|
|
|
authorities.add(new Authority(Authority.NAMES.ROLE_ADMIN.name()));
|
|
|
authorities.add(new Authority(Authority.NAMES.ROLE_USER.name()));
|
|
|
root = User.builder()
|
|
|
- .nickname("管理员")
|
|
|
- .username("root")
|
|
|
- .password(new BCryptPasswordEncoder().encode("123456"))
|
|
|
- .avatar(Constants.DEFAULT_AVATAR)
|
|
|
- .enabled(true)
|
|
|
- .authorities(authorities)
|
|
|
- .build();
|
|
|
+ .nickname("管理员")
|
|
|
+ .username("root")
|
|
|
+ .password(new BCryptPasswordEncoder().encode("123456"))
|
|
|
+ .avatar(Constants.DEFAULT_AVATAR)
|
|
|
+ .enabled(true)
|
|
|
+ .authorities(authorities)
|
|
|
+ .build();
|
|
|
userRepo.save(root);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ public String setPassword(Long userId, String password) {
|
|
|
+ User user = userRepo.findById(userId).orElseThrow(new BusinessException("用户不存在"));
|
|
|
+ user.setPassword(new BCryptPasswordEncoder().encode(password));
|
|
|
+ user = userRepo.save(user);
|
|
|
+ return jwtTokenUtil.generateToken(JwtUserFactory.create(user));
|
|
|
+ }
|
|
|
}
|