ThirdLoginController.java
3.81 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
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.common.enums.LoginType;
import cn.com.glsx.app.common.enums.MiniUserType;
import cn.com.glsx.app.common.exception.AppServerException;
import cn.com.glsx.app.modules.entity.UserAuthRel;
import cn.com.glsx.app.modules.model.AppUser;
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 io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
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.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
/**
* 第三方登录
*
* @author liuyf
*/
@Slf4j
@Api(value = "登录(第三方)", tags = {"登录(第三方)"})
@RestController
public class ThirdLoginController {
@Resource
private UserService userService;
@Autowired
private LoginService loginService;
@Autowired
private WxMaService wxMaService;
@NoLogin
@ApiOperation(value = "微信授权登录")
@GetMapping(value = "/wechatAuth")
public R wechatAuth(@RequestParam String code, @RequestParam Integer accessMode) 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, accessMode);
Map<String, Object> rtnMap = thirdLogin(LoginType.WECHAT, accessMode, authRel);
return R.ok().data(rtnMap);
}
/**
* 第三方登录
*
* @param loginType
* @param accessMode 针对loginType而言更具体的访问模式,比如微信小程序登录,通过常规入口方式,还是分享链接方式
* @param authRel
* @return
*/
private Map<String, Object> thirdLogin(LoginType loginType, Integer accessMode, UserAuthRel authRel) {
Map<String, Object> rtnMap = new HashMap<>();
Optional<Long> optUserId = Optional.ofNullable(authRel.getUserId());
if (!optUserId.isPresent() || optUserId.get() == 0L) {
throw new AppServerException("登录失败,请联系管理员");
}
//分两种情况:
if (MiniUserType.NORMAL.getFlag().equals(accessMode)) {
//1、常规免登录(通过小程序正常入口)
//已绑定本地账号返回引用账号
AppUser user = userService.getRelUserByUserId(authRel.getUserId());
String token = userService.createToken(user);
rtnMap.put("user", user);
rtnMap.put("token", token);
} else if (MiniUserType.SHARED.getFlag().equals(accessMode)) {
//2、通过分享车辆地址登录,也分两种情况
//a、存在账号的openid发起免登录
//b、不存在账号的openid发起免登录
AppUser user = userService.getLocalUserByUserId(authRel.getUserId());
user.setUserType(MiniUserType.SHARED.getType());
String token = userService.createToken(user);
rtnMap.put("user", user);
rtnMap.put("token", token);
} else {
throw new AppServerException("登录方式参数有误");
}
return rtnMap;
}
}