ThirdLoginController.java
3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package cn.com.glsx.app.modules;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.com.glsx.app.modules.entity.UserAuthRel;
import cn.com.glsx.app.modules.model.AppUser;
import cn.com.glsx.app.modules.model.QQLoginBO;
import cn.com.glsx.app.modules.model.QQOAuth2AccessToken;
import cn.com.glsx.app.modules.model.WechatLoginBO;
import cn.com.glsx.app.modules.service.LoginService;
import cn.com.glsx.app.modules.service.UserService;
import cn.hutool.json.JSONUtil;
import com.glsx.plat.common.annotation.NoLogin;
import com.glsx.plat.core.web.R;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
/**
* 第三方登录
*
* @author liuyf
*/
@Slf4j
@RestController
public class ThirdLoginController {
@Resource
private UserService userService;
@Autowired
private LoginService loginService;
@Autowired
private WxMaService wxMaService;
@NoLogin
@RequestMapping(value = "/wechatAuth")
public R wechatAuth(@RequestParam String code) throws WxErrorException {
//获取openId、unionid、session_key
WxMaJscode2SessionResult sessionInfo = wxMaService.getUserService().getSessionInfo(code);
log.info("微信code[{}]获取的Session信息为{}", code, JSONUtil.toJsonStr(sessionInfo));
UserAuthRel authRel = loginService.wechatAuth(sessionInfo);
Map<String, Object> rtnMap = thirdLogin(authRel);
return R.ok().data(rtnMap);
}
@NoLogin
@PostMapping(value = "/wechatLogin")
public R wechatLogin(@RequestBody WechatLoginBO loginBO) {
log.info("微信授权参数 {}", loginBO.toString());
UserAuthRel wxAuthRel = loginService.wechatLogin(loginBO);
Map<String, Object> rtnMap = thirdLogin(wxAuthRel);
return R.ok().data(rtnMap);
}
@NoLogin
@RequestMapping(value = "/qqAuth")
public R qqAuth(@RequestParam String code) {
QQOAuth2AccessToken accessToken = new QQOAuth2AccessToken();
log.info("QQ code[{}]获取的Session信息为{}", code, JSONUtil.toJsonStr(accessToken));
UserAuthRel authRel = loginService.qqAuth(accessToken);
Map<String, Object> rtnMap = thirdLogin(authRel);
return R.ok().data(rtnMap);
}
@NoLogin
@RequestMapping(value = "/qqLogin")
public R qqLogin(@RequestBody QQLoginBO loginBO) {
log.info("QQ授权参数 {}", loginBO.toString());
UserAuthRel qqAuthRel = loginService.qqLogin(loginBO);
Map<String, Object> rtnMap = thirdLogin(qqAuthRel);
return R.ok().data(rtnMap);
}
private Map<String, Object> thirdLogin(UserAuthRel authRel) {
Map<String, Object> rtnMap = new HashMap<>();
Optional<Long> optUserId = Optional.ofNullable(authRel.getUserId());
if (optUserId.isPresent()) {
//如果用户id存在,理论上User不为空
AppUser user = userService.getAppUserByUserId(optUserId.get());
String token = userService.createToken(user);
rtnMap.put("token", token);
rtnMap.put("user", user);
} else {
//没有绑定用户
rtnMap.put("authRelId", authRel.getId());
}
return rtnMap;
}
}