TASK:谷歌微信快捷区分登录注册;

This commit is contained in:
shahaibo
2025-02-05 12:04:53 +08:00
parent 9fb8bc989d
commit cf76235123
3 changed files with 110 additions and 82 deletions

View File

@@ -130,14 +130,14 @@ public class ThirdPartyController {
}
@CrossOrigin
@GetMapping("/parseGoogleCredential")
public Response<AccountLoginVO> parseGoogleCredential(@RequestParam("credential") String credential) {
return Response.success(accountService.parseGoogleCredential(credential));
public Response<AccountLoginVO> parseGoogleCredential(@RequestParam("credential") String credential, @RequestParam("type") Integer type) {
return Response.success(accountService.parseGoogleCredential(credential, type));
}
@CrossOrigin
@GetMapping("/parseWeChatCode")
public Response<AccountLoginVO> parseWeChatCode(@RequestParam("code") String code) {
return Response.success(accountService.parseWeChatCode(code));
public Response<AccountLoginVO> parseWeChatCode(@RequestParam("code") String code, @RequestParam("type") Integer type) {
return Response.success(accountService.parseWeChatCode(code, type));
}
@ApiOperation(value = "接收Design结果")

View File

@@ -206,9 +206,9 @@ public interface AccountService extends IService<Account> {
Account accountDetail(Long id);
AccountLoginVO parseGoogleCredential(String credential);
AccountLoginVO parseGoogleCredential(String credential, Integer type);
AccountLoginVO parseWeChatCode(String code);
AccountLoginVO parseWeChatCode(String code, Integer type);
AccountLoginVO getAccountDetail();

View File

@@ -2235,7 +2235,7 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> impl
}
@Override
public AccountLoginVO parseGoogleCredential(String credential) {
public AccountLoginVO parseGoogleCredential(String credential, Integer type) {
try {
// 配置 Google ID Token 验证器
GoogleIdTokenVerifier verifier = new GoogleIdTokenVerifier.Builder(
@@ -2259,52 +2259,67 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> impl
log.info(userId);
log.info(email);
log.info(name);
// 检查数据库中是否已有该用户
QueryWrapper<AccountExtend> queryWrapper = new QueryWrapper<>();
queryWrapper.lambda().eq(AccountExtend::getAuth, userId); // 根据邮箱查询用户
List<AccountExtend> accountExtends = accountExtendMapper.selectList(queryWrapper);
Account account = new Account();
if (CollectionUtil.isNotEmpty(accountExtends)) {
Long accountId = accountExtends.get(0).getAccountId();
account = accountMapper.selectById(accountId);
} else {
AccountExtend accountExtendInsert = new AccountExtend();
accountExtendInsert.setAuth(userId);
accountExtendInsert.setAuthType("Google");
accountExtendInsert.setHeadImgUrl(pictureUrl);
accountExtendInsert.setName(name);
QueryWrapper<Account> accountQueryWrapper = new QueryWrapper<>();
accountQueryWrapper.lambda().eq(Account::getUserEmail, email); // 根据邮箱查询用户
List<Account> accounts = accountMapper.selectList(accountQueryWrapper);
// 用户不存在,创建新用户(自动注册)
Account newUser = new Account();
if (CollectionUtil.isNotEmpty(accounts)) {
newUser = CopyUtil.copyObject(accounts.get(0), Account.class);
if (type == 1) {
// 注册
QueryWrapper<AccountExtend> accountExtendQW = new QueryWrapper<>();
accountExtendQW.lambda().eq(AccountExtend::getAuthType, "Google");
accountExtendQW.lambda().eq(AccountExtend::getAuth, userId);
List<AccountExtend> accountExtends = accountExtendMapper.selectList(accountExtendQW);
if (CollectionUtil.isNotEmpty(accountExtends)) {
throw new BusinessException("This Google account has been registered for AiDA, please use Google Quick Login directly.");
}else {
newUser.setUserEmail(email);
newUser.setUserName(name);
newUser.setUserPassword("Third-000000");
newUser.setLanguage(Language.ENGLISH.name());
newUser.setValidStartTime(System.currentTimeMillis());
newUser.setValidEndTime(toDayEnd(Instant.now().plus(5, ChronoUnit.DAYS).toEpochMilli()));
newUser.setCreateDate(new Date());
newUser.setIsTrial(1);
newUser.setIsBeginner(1);
newUser.setCredits(BigDecimal.valueOf(100));
newUser.setSystemUser(3);
accountMapper.insert(newUser);
QueryWrapper<Account> accountQueryWrapper = new QueryWrapper<>();
accountQueryWrapper.lambda().eq(Account::getUserEmail, email); // 根据邮箱查询用户
List<Account> accounts = accountMapper.selectList(accountQueryWrapper);
if (CollectionUtil.isNotEmpty(accounts)) {
account = accounts.get(0);
AccountExtend accountExtendInsert = new AccountExtend();
accountExtendInsert.setAuth(userId);
accountExtendInsert.setAuthType("Google");
accountExtendInsert.setHeadImgUrl(pictureUrl);
accountExtendInsert.setName(name);
accountExtendInsert.setAccountId(account.getId());
accountExtendMapper.insert(accountExtendInsert);
}else {
Account newUser = new Account();
newUser.setUserEmail(email);
newUser.setUserName(name);
newUser.setUserPassword("Third-000000");
newUser.setLanguage(Language.ENGLISH.name());
newUser.setValidStartTime(System.currentTimeMillis());
newUser.setValidEndTime(toDayEnd(Instant.now().plus(5, ChronoUnit.DAYS).toEpochMilli()));
newUser.setCreateDate(new Date());
newUser.setIsTrial(1);
newUser.setIsBeginner(1);
newUser.setCredits(BigDecimal.valueOf(100));
newUser.setSystemUser(3);
accountMapper.insert(newUser);
account = newUser;
AccountExtend accountExtendInsert = new AccountExtend();
accountExtendInsert.setAuth(userId);
accountExtendInsert.setAuthType("Google");
accountExtendInsert.setHeadImgUrl(pictureUrl);
accountExtendInsert.setName(name);
accountExtendInsert.setAccountId(newUser.getId());
accountExtendMapper.insert(accountExtendInsert);
}
}
}else {
// 登录
QueryWrapper<AccountExtend> accountExtendQW = new QueryWrapper<>();
accountExtendQW.lambda().eq(AccountExtend::getAuthType, "Google");
accountExtendQW.lambda().eq(AccountExtend::getAuth, userId);
List<AccountExtend> accountExtends = accountExtendMapper.selectList(accountExtendQW);
if (CollectionUtil.isNotEmpty(accountExtends)) {
AccountExtend accountExtend = accountExtends.get(0);
account = accountMapper.selectById(accountExtend.getAccountId());
}else {
throw new BusinessException("This Google account has not been bound to an AiDA account yet. Please register an AiDA account and bind it to a Google account, or bind the Google account to an existing AiDA account.");
}
accountExtendInsert.setAccountId(newUser.getId());
accountExtendMapper.insert(accountExtendInsert);
account = newUser;
}
AccountLoginVO response = CopyUtil.copyObject(account, AccountLoginVO.class);
response.setEmail(account.getUserEmail());
String token = LocalCacheUtils.getTokenCache(String.valueOf(account.getId()));
@@ -2344,7 +2359,7 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> impl
private static final String APP_SECRET = "e5592c691756455b2d03ebfd21fc3131";
@Override
public AccountLoginVO parseWeChatCode(String code) {
public AccountLoginVO parseWeChatCode(String code, Integer type) {
// 1. 获取 access_token 和 openid
JSONObject accessTokenResponse = getAccessTokenFromWeChat(code);
String accessToken = accessTokenResponse.getString("access_token");
@@ -2361,45 +2376,58 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> impl
String unionId = userInfoResponse.getString("unionid");
String userName = userInfoResponse.getString("nickname");
String headimgurl = userInfoResponse.getString("headimgurl");
if (unionId == null) {
throw new IllegalArgumentException("无法获取 unionid请检查微信开发平台配置");
}
// 检查数据库中是否已有该unionid
QueryWrapper<AccountExtend> queryWrapper = new QueryWrapper<>();
queryWrapper.lambda().eq(AccountExtend::getAuthType, "WeChat");
queryWrapper.lambda().eq(AccountExtend::getAuth, unionId);
List<AccountExtend> accountExtends = accountExtendMapper.selectList(queryWrapper);
Account account = new Account();
if (CollectionUtil.isEmpty(accountExtends)) {
AccountExtend accountExtendInsert = new AccountExtend();
accountExtendInsert.setAuth(unionId);
accountExtendInsert.setAuthType("WeChat");
accountExtendInsert.setHeadImgUrl(headimgurl);
accountExtendInsert.setName(userName);
if (type == 1) {
// 注册
// 检查数据库中是否已有该unionid
QueryWrapper<AccountExtend> queryWrapper = new QueryWrapper<>();
queryWrapper.lambda().eq(AccountExtend::getAuthType, "WeChat");
queryWrapper.lambda().eq(AccountExtend::getAuth, unionId);
List<AccountExtend> accountExtends = accountExtendMapper.selectList(queryWrapper);
if (CollectionUtil.isNotEmpty(accountExtends)) {
throw new BusinessException("This Wechat account has been registered for AiDA, please use Wechat Quick Login directly.");
}else {
// 创建新用户(自动注册)
Account newUser = new Account();
newUser.setUserName(userName);
newUser.setUserPassword("Third-000000");
newUser.setLanguage(Language.ENGLISH.name());
newUser.setValidStartTime(System.currentTimeMillis());
newUser.setValidEndTime(toDayEnd(Instant.now().plus(5, ChronoUnit.DAYS).toEpochMilli()));
newUser.setCreateDate(new Date());
newUser.setIsTrial(1);
newUser.setIsBeginner(1);
newUser.setCredits(BigDecimal.valueOf(100));
newUser.setSystemUser(3);
accountMapper.insert(newUser);
// 用户不存在,创建新用户(自动注册)
Account newUser = new Account();
// newUser.setUserEmail(email);
newUser.setUserName(userName);
newUser.setUserPassword("Third-000000");
newUser.setLanguage(Language.ENGLISH.name());
newUser.setValidStartTime(System.currentTimeMillis());
newUser.setValidEndTime(toDayEnd(Instant.now().plus(5, ChronoUnit.DAYS).toEpochMilli()));
newUser.setCreateDate(new Date());
newUser.setIsTrial(1);
newUser.setIsBeginner(1);
newUser.setCredits(BigDecimal.valueOf(100));
newUser.setSystemUser(3);
accountMapper.insert(newUser);
AccountExtend accountExtendInsert = new AccountExtend();
accountExtendInsert.setAuth(unionId);
accountExtendInsert.setAuthType("WeChat");
accountExtendInsert.setHeadImgUrl(headimgurl);
accountExtendInsert.setName(userName);
accountExtendInsert.setAccountId(newUser.getId());
accountExtendMapper.insert(accountExtendInsert);
accountExtendInsert.setAccountId(newUser.getId());
accountExtendMapper.insert(accountExtendInsert);
account = newUser;
account = newUser;
}
}else {
AccountExtend accountExtend = accountExtends.get(0);
account = accountMapper.selectById(accountExtend.getAccountId());
// 登录
// 检查数据库中是否已有该unionid
QueryWrapper<AccountExtend> queryWrapper = new QueryWrapper<>();
queryWrapper.lambda().eq(AccountExtend::getAuthType, "WeChat");
queryWrapper.lambda().eq(AccountExtend::getAuth, unionId);
List<AccountExtend> accountExtends = accountExtendMapper.selectList(queryWrapper);
if (CollectionUtil.isNotEmpty(accountExtends)) {
AccountExtend accountExtend = accountExtends.get(0);
account = accountMapper.selectById(accountExtend.getAccountId());
}else {
throw new BusinessException("This WeChat account has not been bound to an AiDA account yet. Please register an AiDA account and bind it to the WeChat account, or bind the WeChat account to an existing AiDA account.");
}
}
AccountLoginVO response = CopyUtil.copyObject(account, AccountLoginVO.class);