TASK:AiDA
This commit is contained in:
@@ -314,14 +314,14 @@ public class AccountController {
|
||||
@CrossOrigin
|
||||
@GetMapping("/bindGoogle")
|
||||
@ApiOperation(value = "绑定谷歌")
|
||||
public Response<AccountLoginVO> bindGoogle(@RequestParam("credential") String credential) {
|
||||
public Response<Boolean> bindGoogle(@RequestParam("credential") String credential) {
|
||||
return Response.success(accountService.bindGoogle(credential));
|
||||
}
|
||||
|
||||
@CrossOrigin
|
||||
@GetMapping("/bindWeChat")
|
||||
@ApiOperation(value = "绑定微信")
|
||||
public Response<AccountLoginVO> bindWeChat(@RequestParam("code") String code) {
|
||||
public Response<Boolean> bindWeChat(@RequestParam("code") String code) {
|
||||
return Response.success(accountService.bindWeChat(code));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.ai.da.mapper.primary.entity;
|
||||
|
||||
import com.ai.da.common.response.PageResponse;
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
@@ -26,5 +27,9 @@ public class AccountExtend implements Serializable {
|
||||
|
||||
private String authType;
|
||||
|
||||
private String headImgUrl;
|
||||
|
||||
private String name;
|
||||
|
||||
private String auth;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.ai.da.model.vo;
|
||||
|
||||
import com.ai.da.mapper.primary.entity.AccountExtend;
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import io.swagger.models.auth.In;
|
||||
@@ -8,6 +9,7 @@ import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import java.util.List;
|
||||
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@@ -40,4 +42,6 @@ public class AccountLoginVO {
|
||||
|
||||
private Long followerCount;
|
||||
|
||||
private List<AccountExtend> accountExtendList;
|
||||
|
||||
}
|
||||
|
||||
@@ -209,7 +209,7 @@ public interface AccountService extends IService<Account> {
|
||||
|
||||
AccountLoginVO getAccountDetail();
|
||||
|
||||
AccountLoginVO bindGoogle(String credential);
|
||||
Boolean bindGoogle(String credential);
|
||||
|
||||
AccountLoginVO bindWeChat(String code);
|
||||
Boolean bindWeChat(String code);
|
||||
}
|
||||
|
||||
@@ -2179,6 +2179,7 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> impl
|
||||
String userId = payload.getSubject();
|
||||
String email = payload.getEmail();
|
||||
String name = (String) payload.get("name");
|
||||
String pictureUrl = (String) payload.get("picture");
|
||||
log.info(userId);
|
||||
log.info(email);
|
||||
log.info(name);
|
||||
@@ -2191,6 +2192,13 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> impl
|
||||
if (CollectionUtil.isNotEmpty(accounts)) {
|
||||
account = accounts.get(0);
|
||||
} else {
|
||||
|
||||
AccountExtend accountExtendInsert = new AccountExtend();
|
||||
accountExtendInsert.setAuth(userId);
|
||||
accountExtendInsert.setAuthType("Google");
|
||||
accountExtendInsert.setHeadImgUrl(pictureUrl);
|
||||
accountExtendInsert.setName(name);
|
||||
|
||||
// 用户不存在,创建新用户(自动注册)
|
||||
Account newUser = new Account();
|
||||
newUser.setUserEmail(email);
|
||||
@@ -2206,6 +2214,9 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> impl
|
||||
newUser.setSystemUser(3);
|
||||
accountMapper.insert(newUser);
|
||||
|
||||
accountExtendInsert.setAccountId(newUser.getId());
|
||||
accountExtendMapper.insert(accountExtendInsert);
|
||||
|
||||
account = newUser;
|
||||
}
|
||||
|
||||
@@ -2264,6 +2275,7 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> impl
|
||||
// 提取 unionid 和 nickname
|
||||
String unionId = userInfoResponse.getString("unionid");
|
||||
String userName = userInfoResponse.getString("nickname");
|
||||
String headimgurl = userInfoResponse.getString("headimgurl");
|
||||
if (unionId == null) {
|
||||
throw new IllegalArgumentException("无法获取 unionid,请检查微信开发平台配置");
|
||||
}
|
||||
@@ -2278,6 +2290,8 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> impl
|
||||
AccountExtend accountExtendInsert = new AccountExtend();
|
||||
accountExtendInsert.setAuth(unionId);
|
||||
accountExtendInsert.setAuthType("WeChat");
|
||||
accountExtendInsert.setHeadImgUrl(headimgurl);
|
||||
accountExtendInsert.setName(userName);
|
||||
|
||||
// 用户不存在,创建新用户(自动注册)
|
||||
Account newUser = new Account();
|
||||
@@ -2325,21 +2339,6 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> impl
|
||||
response.setFolloweeCount(portfolioService.getFolloweeCount(account.getId()));
|
||||
response.setFollowerCount(portfolioService.getFollowerCount(account.getId()));
|
||||
return response;
|
||||
|
||||
// // 2. 根据 unionid 检查用户是否存在
|
||||
// User user = userRepository.findByUnionid(unionid);
|
||||
// if (user == null) {
|
||||
// // 用户不存在,进行注册
|
||||
// user = new User();
|
||||
// user.setUnionid(unionid);
|
||||
// user.setOpenid(weChatResponse.getString("openid"));
|
||||
// user.setNickname(weChatResponse.getString("nickname"));
|
||||
// user.setAvatar(weChatResponse.getString("headimgurl"));
|
||||
// userRepository.save(user);
|
||||
// }
|
||||
//
|
||||
// // 3. 返回 unionid
|
||||
// return unionid;
|
||||
}
|
||||
|
||||
private static final String WECHAT_USER_INFO_URL = "https://api.weixin.qq.com/sns/userinfo";
|
||||
@@ -2410,21 +2409,94 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> impl
|
||||
response.setAvatar(minioUtil.getPreSignedUrl(avatar, CommonConstant.MINIO_IMAGE_EXPIRE_TIME));
|
||||
response.setFolloweeCount(portfolioService.getFolloweeCount(account.getId()));
|
||||
response.setFollowerCount(portfolioService.getFollowerCount(account.getId()));
|
||||
response.setSystemUser(1);
|
||||
//判断是否常用ip 不是则发邮件提示
|
||||
// calculateExceptionIp(RequestInfoUtil.getIpAddress(request), account);
|
||||
|
||||
QueryWrapper<AccountExtend> qw = new QueryWrapper<>();
|
||||
qw.lambda().eq(AccountExtend::getAccountId, response.getUserId());
|
||||
List<AccountExtend> accountExtends = accountExtendMapper.selectList(qw);
|
||||
if (CollectionUtil.isNotEmpty(accountExtends)) {
|
||||
response.setAccountExtendList(accountExtends);
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
@Override
|
||||
public AccountLoginVO bindGoogle(String credential) {
|
||||
return null;
|
||||
public Boolean bindGoogle(String credential) {
|
||||
try {
|
||||
// 配置 Google ID Token 验证器
|
||||
GoogleIdTokenVerifier verifier = new GoogleIdTokenVerifier.Builder(
|
||||
new NetHttpTransport(),
|
||||
JacksonFactory.getDefaultInstance())
|
||||
.setAudience(Collections.singletonList(CLIENT_ID))
|
||||
.build();
|
||||
|
||||
// 验证并解析 ID Token
|
||||
GoogleIdToken idToken = verifier.verify(credential);
|
||||
|
||||
|
||||
if (idToken != null) {
|
||||
GoogleIdToken.Payload payload = idToken.getPayload();
|
||||
|
||||
// 提取用户信息
|
||||
String userId = payload.getSubject();
|
||||
String email = payload.getEmail();
|
||||
String name = (String) payload.get("name");
|
||||
String pictureUrl = (String) payload.get("picture");
|
||||
|
||||
|
||||
AccountExtend accountExtendInsert = new AccountExtend();
|
||||
accountExtendInsert.setAuth(userId);
|
||||
accountExtendInsert.setAuthType("Google");
|
||||
accountExtendInsert.setHeadImgUrl(pictureUrl);
|
||||
accountExtendInsert.setName(name);
|
||||
|
||||
|
||||
AuthPrincipalVo authPrincipalVo = UserContext.getUserHolder();
|
||||
accountExtendInsert.setAccountId(authPrincipalVo.getId());
|
||||
accountExtendMapper.insert(accountExtendInsert);
|
||||
|
||||
return Boolean.TRUE;
|
||||
} else {
|
||||
throw new IllegalArgumentException("Invalid ID token.");
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
throw new RuntimeException("Failed to verify ID token: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public AccountLoginVO bindWeChat(String code) {
|
||||
return null;
|
||||
public Boolean bindWeChat(String code) {
|
||||
// 1. 获取 access_token 和 openid
|
||||
JSONObject accessTokenResponse = getAccessTokenFromWeChat(code);
|
||||
String accessToken = accessTokenResponse.getString("access_token");
|
||||
String openId = accessTokenResponse.getString("openid");
|
||||
|
||||
if (StringUtils.isEmpty(accessToken) || StringUtils.isEmpty(openId)) {
|
||||
throw new RuntimeException("微信接口返回数据缺失: " + accessTokenResponse.toJSONString());
|
||||
}
|
||||
|
||||
// 2. 获取用户信息
|
||||
JSONObject userInfoResponse = getUserInfoFromWeChat(accessToken, openId);
|
||||
|
||||
// 提取 unionid 和 nickname
|
||||
String unionId = userInfoResponse.getString("unionid");
|
||||
String userName = userInfoResponse.getString("nickname");
|
||||
String headimgurl = userInfoResponse.getString("headimgurl");
|
||||
if (unionId == null) {
|
||||
throw new IllegalArgumentException("无法获取 unionid,请检查微信开发平台配置");
|
||||
}
|
||||
AccountExtend accountExtendInsert = new AccountExtend();
|
||||
accountExtendInsert.setAuth(unionId);
|
||||
accountExtendInsert.setAuthType("WeChat");
|
||||
accountExtendInsert.setHeadImgUrl(headimgurl);
|
||||
accountExtendInsert.setName(userName);
|
||||
|
||||
AuthPrincipalVo authPrincipalVo = UserContext.getUserHolder();
|
||||
accountExtendInsert.setAccountId(authPrincipalVo.getId());
|
||||
accountExtendMapper.insert(accountExtendInsert);
|
||||
|
||||
return Boolean.TRUE;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user