From cf76235123d78c14ee3fc7c2dca7eb424bf99a1f Mon Sep 17 00:00:00 2001 From: shahaibo <1023316923@qq.com> Date: Wed, 5 Feb 2025 12:04:53 +0800 Subject: [PATCH] =?UTF-8?q?TASK:=E8=B0=B7=E6=AD=8C=E5=BE=AE=E4=BF=A1?= =?UTF-8?q?=E5=BF=AB=E6=8D=B7=E5=8C=BA=E5=88=86=E7=99=BB=E5=BD=95=E6=B3=A8?= =?UTF-8?q?=E5=86=8C;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../da/controller/ThirdPartyController.java | 8 +- .../com/ai/da/service/AccountService.java | 4 +- .../da/service/impl/AccountServiceImpl.java | 180 ++++++++++-------- 3 files changed, 110 insertions(+), 82 deletions(-) diff --git a/src/main/java/com/ai/da/controller/ThirdPartyController.java b/src/main/java/com/ai/da/controller/ThirdPartyController.java index c0ff893f..72454016 100644 --- a/src/main/java/com/ai/da/controller/ThirdPartyController.java +++ b/src/main/java/com/ai/da/controller/ThirdPartyController.java @@ -130,14 +130,14 @@ public class ThirdPartyController { } @CrossOrigin @GetMapping("/parseGoogleCredential") - public Response parseGoogleCredential(@RequestParam("credential") String credential) { - return Response.success(accountService.parseGoogleCredential(credential)); + public Response parseGoogleCredential(@RequestParam("credential") String credential, @RequestParam("type") Integer type) { + return Response.success(accountService.parseGoogleCredential(credential, type)); } @CrossOrigin @GetMapping("/parseWeChatCode") - public Response parseWeChatCode(@RequestParam("code") String code) { - return Response.success(accountService.parseWeChatCode(code)); + public Response parseWeChatCode(@RequestParam("code") String code, @RequestParam("type") Integer type) { + return Response.success(accountService.parseWeChatCode(code, type)); } @ApiOperation(value = "接收Design结果") diff --git a/src/main/java/com/ai/da/service/AccountService.java b/src/main/java/com/ai/da/service/AccountService.java index e8a2324b..270d2d66 100644 --- a/src/main/java/com/ai/da/service/AccountService.java +++ b/src/main/java/com/ai/da/service/AccountService.java @@ -206,9 +206,9 @@ public interface AccountService extends IService { 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(); diff --git a/src/main/java/com/ai/da/service/impl/AccountServiceImpl.java b/src/main/java/com/ai/da/service/impl/AccountServiceImpl.java index b3fc9c35..3917722c 100644 --- a/src/main/java/com/ai/da/service/impl/AccountServiceImpl.java +++ b/src/main/java/com/ai/da/service/impl/AccountServiceImpl.java @@ -2235,7 +2235,7 @@ public class AccountServiceImpl extends ServiceImpl 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 impl log.info(userId); log.info(email); log.info(name); - - // 检查数据库中是否已有该用户 - QueryWrapper queryWrapper = new QueryWrapper<>(); - queryWrapper.lambda().eq(AccountExtend::getAuth, userId); // 根据邮箱查询用户 - List 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 accountQueryWrapper = new QueryWrapper<>(); - accountQueryWrapper.lambda().eq(Account::getUserEmail, email); // 根据邮箱查询用户 - List accounts = accountMapper.selectList(accountQueryWrapper); - - // 用户不存在,创建新用户(自动注册) - Account newUser = new Account(); - if (CollectionUtil.isNotEmpty(accounts)) { - newUser = CopyUtil.copyObject(accounts.get(0), Account.class); + if (type == 1) { + // 注册 + QueryWrapper accountExtendQW = new QueryWrapper<>(); + accountExtendQW.lambda().eq(AccountExtend::getAuthType, "Google"); + accountExtendQW.lambda().eq(AccountExtend::getAuth, userId); + List 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 accountQueryWrapper = new QueryWrapper<>(); + accountQueryWrapper.lambda().eq(Account::getUserEmail, email); // 根据邮箱查询用户 + List 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 accountExtendQW = new QueryWrapper<>(); + accountExtendQW.lambda().eq(AccountExtend::getAuthType, "Google"); + accountExtendQW.lambda().eq(AccountExtend::getAuth, userId); + List 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 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 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 queryWrapper = new QueryWrapper<>(); - queryWrapper.lambda().eq(AccountExtend::getAuthType, "WeChat"); - queryWrapper.lambda().eq(AccountExtend::getAuth, unionId); - List 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 queryWrapper = new QueryWrapper<>(); + queryWrapper.lambda().eq(AccountExtend::getAuthType, "WeChat"); + queryWrapper.lambda().eq(AccountExtend::getAuth, unionId); + List 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 queryWrapper = new QueryWrapper<>(); + queryWrapper.lambda().eq(AccountExtend::getAuthType, "WeChat"); + queryWrapper.lambda().eq(AccountExtend::getAuth, unionId); + List 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);