|
@@ -33,6 +33,7 @@ public class AppAccountOidcServiceImpl implements AppAccountOidcService {
|
|
|
private static final String BANNED = "BANNED";
|
|
private static final String BANNED = "BANNED";
|
|
|
private static final String DEFAULT_AVATAR = "https://static.public.hobbystock.cn/applet/share/share_logo2.png";
|
|
private static final String DEFAULT_AVATAR = "https://static.public.hobbystock.cn/applet/share/share_logo2.png";
|
|
|
private static final int DEFAULT_ROLE_ID = 0;
|
|
private static final int DEFAULT_ROLE_ID = 0;
|
|
|
|
|
+ private static final String WECHAT_ACCOUNT_PREFIX = "WX_";
|
|
|
private static final SecureRandom SECURE_RANDOM = new SecureRandom();
|
|
private static final SecureRandom SECURE_RANDOM = new SecureRandom();
|
|
|
|
|
|
|
|
@Resource
|
|
@Resource
|
|
@@ -99,20 +100,50 @@ public class AppAccountOidcServiceImpl implements AppAccountOidcService {
|
|
|
@Override
|
|
@Override
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
public EndUserDTO createWeChatUser(WeChatCreateRequest request) {
|
|
public EndUserDTO createWeChatUser(WeChatCreateRequest request) {
|
|
|
- if (request == null) {
|
|
|
|
|
|
|
+ if (request == null || !StringUtils.hasText(request.getOpenId())) {
|
|
|
throw new ServiceException("参数不能为空");
|
|
throw new ServiceException("参数不能为空");
|
|
|
}
|
|
}
|
|
|
- return createUser(request.getOpenId(), null, null, null, request.getOpenId(),
|
|
|
|
|
|
|
+ String openId = request.getOpenId().trim();
|
|
|
|
|
+ EndUserDTO existing = loadUserByLoginId(openId);
|
|
|
|
|
+ if (existing != null) {
|
|
|
|
|
+ return existing;
|
|
|
|
|
+ }
|
|
|
|
|
+ return createUser(createWechatInternalAccount(), null, null, null, openId,
|
|
|
request.getNickName(), request.getAvatarUrl(), "WX_AUTH", null);
|
|
request.getNickName(), request.getAvatarUrl(), "WX_AUTH", null);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
public EndUserDTO createWeChatPhoneUser(WeChatPhoneCreateRequest request) {
|
|
public EndUserDTO createWeChatPhoneUser(WeChatPhoneCreateRequest request) {
|
|
|
- if (request == null) {
|
|
|
|
|
|
|
+ if (request == null || !StringUtils.hasText(request.getOpenId()) || !StringUtils.hasText(request.getPhone())) {
|
|
|
throw new ServiceException("参数不能为空");
|
|
throw new ServiceException("参数不能为空");
|
|
|
}
|
|
}
|
|
|
- return createUser(request.getPhone(), request.getPhone(), null, null, request.getOpenId(),
|
|
|
|
|
|
|
+ String openId = request.getOpenId().trim();
|
|
|
|
|
+ String phone = request.getPhone().trim();
|
|
|
|
|
+ EndUserDTO existingWechatUser = loadUserByLoginId(openId);
|
|
|
|
|
+ if (existingWechatUser != null) {
|
|
|
|
|
+ BindPhoneRequest bindPhoneRequest = new BindPhoneRequest();
|
|
|
|
|
+ bindPhoneRequest.setLoginId(openId);
|
|
|
|
|
+ bindPhoneRequest.setPhone(phone);
|
|
|
|
|
+ return bindPhone(bindPhoneRequest);
|
|
|
|
|
+ }
|
|
|
|
|
+ AppAccount phoneAccount = poyeeAppAccountMapper.selectByLoginId(phone);
|
|
|
|
|
+ if (phoneAccount != null) {
|
|
|
|
|
+ AppBaseUser baseUser = poyeeAppBaseUserMapper.selectByUsername(phoneAccount.getAccount());
|
|
|
|
|
+ if (baseUser != null && StringUtils.hasText(baseUser.getOpenid()) && !openId.equals(baseUser.getOpenid())) {
|
|
|
|
|
+ throw new ServiceException("手机号已绑定");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (baseUser != null && !StringUtils.hasText(baseUser.getOpenid())) {
|
|
|
|
|
+ baseUser.setOpenid(openId);
|
|
|
|
|
+ poyeeAppBaseUserMapper.updateAppBaseUser(baseUser);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!StringUtils.hasText(phoneAccount.getOpenId())) {
|
|
|
|
|
+ poyeeAppAccountMapper.updateOpenId(phoneAccount.getAccount(), openId);
|
|
|
|
|
+ phoneAccount.setOpenId(openId);
|
|
|
|
|
+ }
|
|
|
|
|
+ return toEndUser(phoneAccount.getAccount(), phoneAccount, baseUser);
|
|
|
|
|
+ }
|
|
|
|
|
+ return createUser(phone, phone, null, null, openId,
|
|
|
request.getNickName(), request.getAvatarUrl(), "WECHAT_PHONE", null);
|
|
request.getNickName(), request.getAvatarUrl(), "WECHAT_PHONE", null);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -165,17 +196,35 @@ public class AppAccountOidcServiceImpl implements AppAccountOidcService {
|
|
|
String loginId = request.getLoginId().trim();
|
|
String loginId = request.getLoginId().trim();
|
|
|
String phone = request.getPhone().trim();
|
|
String phone = request.getPhone().trim();
|
|
|
AppAccount account = poyeeAppAccountMapper.selectByLoginId(loginId);
|
|
AppAccount account = poyeeAppAccountMapper.selectByLoginId(loginId);
|
|
|
|
|
+ AppBaseUser baseUser = null;
|
|
|
if (account == null) {
|
|
if (account == null) {
|
|
|
- throw new ServiceException("账号不存在");
|
|
|
|
|
|
|
+ baseUser = poyeeAppBaseUserMapper.selectByExternalLoginId(loginId);
|
|
|
|
|
+ if (baseUser == null) {
|
|
|
|
|
+ throw new ServiceException("账号不存在");
|
|
|
|
|
+ }
|
|
|
|
|
+ account = poyeeAppAccountMapper.selectByLoginId(baseUser.getUsername());
|
|
|
|
|
+ if (account == null) {
|
|
|
|
|
+ throw new ServiceException("账号不存在");
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
AppAccount phoneAccount = poyeeAppAccountMapper.selectByLoginId(phone);
|
|
AppAccount phoneAccount = poyeeAppAccountMapper.selectByLoginId(phone);
|
|
|
if (phoneAccount != null && !isSameAccount(account, phoneAccount)) {
|
|
if (phoneAccount != null && !isSameAccount(account, phoneAccount)) {
|
|
|
throw new ServiceException("手机号已绑定");
|
|
throw new ServiceException("手机号已绑定");
|
|
|
}
|
|
}
|
|
|
- poyeeAppAccountMapper.updatePhone(account.getAccount(), phone);
|
|
|
|
|
|
|
+ String oldAccount = account.getAccount();
|
|
|
|
|
+ poyeeAppAccountMapper.updateAccountAndPhone(oldAccount, phone, phone);
|
|
|
|
|
+ if (!phone.equals(oldAccount)) {
|
|
|
|
|
+ poyeeAppBaseUserMapper.updateUsername(oldAccount, phone);
|
|
|
|
|
+ }
|
|
|
|
|
+ account.setAccount(phone);
|
|
|
account.setPhone(phone);
|
|
account.setPhone(phone);
|
|
|
- AppBaseUser baseUser = poyeeAppBaseUserMapper.selectByUsername(account.getAccount());
|
|
|
|
|
- return toEndUser(account.getAccount(), account, baseUser);
|
|
|
|
|
|
|
+ if (baseUser == null) {
|
|
|
|
|
+ baseUser = resolveBaseUserForAccount(oldAccount, account);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (baseUser != null) {
|
|
|
|
|
+ baseUser.setUsername(phone);
|
|
|
|
|
+ }
|
|
|
|
|
+ return toEndUser(phone, account, baseUser);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
@@ -262,6 +311,14 @@ public class AppAccountOidcServiceImpl implements AppAccountOidcService {
|
|
|
return left.getAccount() != null && left.getAccount().equals(right.getAccount());
|
|
return left.getAccount() != null && left.getAccount().equals(right.getAccount());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ private AppBaseUser resolveBaseUserForAccount(String oldAccount, AppAccount account) {
|
|
|
|
|
+ AppBaseUser baseUser = poyeeAppBaseUserMapper.selectByUsername(oldAccount);
|
|
|
|
|
+ if (baseUser == null && account != null && StringUtils.hasText(account.getAccount())) {
|
|
|
|
|
+ baseUser = poyeeAppBaseUserMapper.selectByUsername(account.getAccount());
|
|
|
|
|
+ }
|
|
|
|
|
+ return baseUser;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
private String resolvePhoneRegisterChannel(PhoneCreateRequest request) {
|
|
private String resolvePhoneRegisterChannel(PhoneCreateRequest request) {
|
|
|
String registerChannel = request.getRegisterChannel();
|
|
String registerChannel = request.getRegisterChannel();
|
|
|
if (!StringUtils.hasText(registerChannel)) {
|
|
if (!StringUtils.hasText(registerChannel)) {
|
|
@@ -288,6 +345,7 @@ public class AppAccountOidcServiceImpl implements AppAccountOidcService {
|
|
|
account.setAccount(username);
|
|
account.setAccount(username);
|
|
|
account.setPhone(phone);
|
|
account.setPhone(phone);
|
|
|
account.setEmail(email);
|
|
account.setEmail(email);
|
|
|
|
|
+ account.setOpenId(openId);
|
|
|
account.setStatus((short) 0);
|
|
account.setStatus((short) 0);
|
|
|
account.setDelFlg((short) 0);
|
|
account.setDelFlg((short) 0);
|
|
|
account.setRoleid(DEFAULT_ROLE_ID);
|
|
account.setRoleid(DEFAULT_ROLE_ID);
|
|
@@ -316,6 +374,10 @@ public class AppAccountOidcServiceImpl implements AppAccountOidcService {
|
|
|
return toEndUser(username, account, baseUser);
|
|
return toEndUser(username, account, baseUser);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ private String createWechatInternalAccount() {
|
|
|
|
|
+ return WECHAT_ACCOUNT_PREFIX + randomSalt();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
private EndUserDTO toEndUser(String loginId, AppAccount account, AppBaseUser baseUser) {
|
|
private EndUserDTO toEndUser(String loginId, AppAccount account, AppBaseUser baseUser) {
|
|
|
if (account == null && baseUser == null) {
|
|
if (account == null && baseUser == null) {
|
|
|
return null;
|
|
return null;
|