![](https://i-blog.csdnimg.cn/blog_migrate/e47c642d111149ff65e9b457f505b74a.png)
![](https://i-blog.csdnimg.cn/blog_migrate/58a948982db9e08e9d01449e47c2d8b4.png)
![](https://i-blog.csdnimg.cn/blog_migrate/e0e4a8003950150627e8b865e1780474.png)
![](https://i-blog.csdnimg.cn/blog_migrate/6241614ecb7ccef9ad91605286449200.png)
@Data
public class SocialUser {
private String access_token;
private String remind_in;
private long expires_in;
private String uid;
private String isRealName;
}
![](https://i-blog.csdnimg.cn/blog_migrate/d6090ee2960ac40110cd42b2e00f5892.png)
![](https://i-blog.csdnimg.cn/blog_migrate/9d11fa58653a240f9643ca3ded81f50c.png)
![](https://i-blog.csdnimg.cn/blog_migrate/34d155b9d0961f78ecb79aebd1bc4869.png)
![](https://i-blog.csdnimg.cn/blog_migrate/a03d0fc6ad019a536464472926fbcf81.png)
@Override
public MemberEntity login(SocialUser socialUser) throws Exception {
//登录和注册合并逻辑
String uid = socialUser.getUid();
//1、判断当前社交用户是否已经登陆过系统
MemberDao memberDao = this.baseMapper;
MemberEntity memberEntity = memberDao.selectOne(new QueryWrapper<MemberEntity>().eq("social_uid", uid));
if(memberEntity != null){
// 这个用户已经注册
MemberEntity update = new MemberEntity();
update.setId(memberEntity.getId());
update.setAccessToken(socialUser.getAccess_token());
update.setExpiresIn(socialUser.getExpires_in());
memberDao.updateById(update);
memberEntity.setAccessToken(socialUser.getAccess_token());
memberEntity.setExpiresIn(socialUser.getExpires_in());
return memberEntity;
} else {
//2、 没有查到当前社交用户对应记录,我们就需要注册一个
MemberEntity regist = new MemberEntity();
try {
//3、查询当前社交用户的社交账号信息(昵称,性别等)
Map<String, String> query = new HashMap<String, String>();
query.put("access_token", socialUser.getAccess_token());
query.put("uid", socialUser.getUid());
HttpResponse response = HttpUtils.doGet("https://api.weibo.com", "/2/users/show.json", "get", new HashMap<String, String>(), query);
if(response.getStatusLine().getStatusCode() == 200){
//查询成功
String json = EntityUtils.toString(response.getEntity());
JSONObject jsonObject = JSON.parseObject(json);
// 昵称
String name = jsonObject.getString("name");
String gender = jsonObject.getString("gender");
// ....
regist.setNickname(name);
regist.setGender("m".equals(gender)?1:0);
}
} catch (Exception e) {
}
regist.setSocialUid(socialUser.getUid());
regist.setAccessToken(socialUser.getAccess_token());
regist.setExpiresIn(socialUser.getExpires_in());
memberDao.insert(regist);
return regist;
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/5650798b39c5e1bf17d62cc32f2497ff.png)