LoginService.java
7.15 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package cn.com.glsx.app.modules.service;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.com.glsx.app.common.enums.AuthType;
import cn.com.glsx.app.common.enums.LoginType;
import cn.com.glsx.app.common.exception.AppServerException;
import cn.com.glsx.app.modules.converter.UserConverter;
import cn.com.glsx.app.modules.entity.*;
import cn.com.glsx.app.modules.mapper.*;
import cn.com.glsx.app.modules.model.*;
import cn.com.glsx.auth.api.AuthFeignClient;
import cn.com.glsx.auth.model.SimpleUser;
import com.glsx.plat.core.web.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
@Service
public class LoginService {
@Resource
private UserAuthRelMapper authRelMapper;
@Resource
private UserLocalAuthMapper localAuthMapper;
@Resource
private UserThirdAuthMapper thirdAuthMapper;
@Resource
private UserWechatAccountMapper wechatAccountMapper;
@Resource
private UserQQAccountMapper qqAccountMapper;
@Autowired
private AuthFeignClient authFeignClient;
@Transactional(rollbackFor = Exception.class)
public AppUser localAuth(LoginByAccountBO loginBO) {
//内部登录转成appUser返回前端
if (loginBO.getLoginType() == 1) {
//密码登录
R<SimpleUser> userR = authFeignClient.login(loginBO.getAccount(), loginBO.getCode());
SimpleUser simpleUser = userR.getData(SimpleUser.class);
if (!userR.isSuccess() || simpleUser == null) {
throw new AppServerException(userR.getMessage());
}
UserAuthRel userAuthRel = authRelMapper.selectLocalAuthRelByUserId(simpleUser.getId());
UserLocalAuth localAuth = localAuthMapper.selectById(userAuthRel.getAuthId());
if (localAuth == null) {
throw new AppServerException("请先开通登录权限");
}
return UserConverter.INSTANCE.su2au(simpleUser);
} else if (loginBO.getLoginType() == 2) {
//验证码校验
//smsService.verifyCode(loginBO.getAccount(), loginBO.getCode());
}
return null;
}
@Transactional(rollbackFor = Exception.class)
public UserAuthRel qqAuth(QQOAuth2AccessToken accessToken) {
UserThirdAuth thirdAuth = thirdAuthMapper.selectByOpenid(accessToken.getOpenId());
if (thirdAuth == null) {
thirdAuth = new UserThirdAuth();
thirdAuth.setOpenid(accessToken.getOpenId());
thirdAuth.setAccessToken(accessToken.getAccessToken());
thirdAuth.setLoginType(LoginType.QQ.getType());
thirdAuthMapper.insertUseGeneratedKeys(thirdAuth);
}
UserQQAccount qqAccount = qqAccountMapper.selectByAuthId(thirdAuth.getId());
if (qqAccount == null) {
qqAccount = new UserQQAccount();
qqAccount.setAuthId(thirdAuth.getId());
qqAccount.setUnionId(accessToken.getUnionId());
qqAccountMapper.insertUseGeneratedKeys(qqAccount);
}
UserAuthRel authRel = authRelMapper.selectByAuthTypeAndAuthId(AuthType.THIRD.getType(), thirdAuth.getId());
if (authRel == null) {
authRel = new UserAuthRel();
authRel.setAuthId(thirdAuth.getId());
authRel.setAuthType(AuthType.THIRD.getType());
authRelMapper.insertUseGeneratedKeys(authRel);
}
return authRel;
}
@Transactional(rollbackFor = Exception.class)
public UserAuthRel qqLogin(QQLoginBO loginBO) {
UserThirdAuth thirdAuth = thirdAuthMapper.selectByOpenid(loginBO.getOpenId());
if (thirdAuth == null) {
thirdAuth = new UserThirdAuth();
thirdAuth.setOpenid(loginBO.getOpenId());
thirdAuth.setLoginType(LoginType.QQ.getType());
thirdAuthMapper.insertUseGeneratedKeys(thirdAuth);
}
UserQQAccount qqAccount = qqAccountMapper.selectByAuthId(thirdAuth.getId());
if (qqAccount == null) {
qqAccount = new UserQQAccount();
qqAccount.setAuthId(thirdAuth.getId());
qqAccountMapper.insertUseGeneratedKeys(qqAccount);
}
UserAuthRel authRel = authRelMapper.selectByAuthTypeAndAuthId(AuthType.THIRD.getType(), thirdAuth.getId());
if (authRel == null) {
authRel = new UserAuthRel();
authRel.setAuthId(thirdAuth.getId());
authRel.setAuthType(AuthType.THIRD.getType());
authRelMapper.insertUseGeneratedKeys(authRel);
}
return authRel;
}
@Transactional(rollbackFor = Exception.class)
public UserAuthRel wechatAuth(WxMaJscode2SessionResult sessionInfo) {
UserThirdAuth thirdAuth = thirdAuthMapper.selectByOpenid(sessionInfo.getOpenid());
if (thirdAuth == null) {
thirdAuth = new UserThirdAuth();
thirdAuth.setOpenid(sessionInfo.getOpenid());
thirdAuth.setAccessToken(sessionInfo.getSessionKey());
thirdAuth.setLoginType(LoginType.WECHAT.getType());
thirdAuthMapper.insertUseGeneratedKeys(thirdAuth);
}
UserWechatAccount wechatAccount = wechatAccountMapper.selectByAuthId(thirdAuth.getId());
if (wechatAccount == null) {
wechatAccount = new UserWechatAccount();
wechatAccount.setAuthId(thirdAuth.getId());
wechatAccount.setUnionId(sessionInfo.getUnionid());
wechatAccountMapper.insertUseGeneratedKeys(wechatAccount);
}
UserAuthRel authRel = authRelMapper.selectByAuthTypeAndAuthId(AuthType.THIRD.getType(), thirdAuth.getId());
if (authRel == null) {
authRel = new UserAuthRel();
authRel.setAuthId(thirdAuth.getId());
authRel.setAuthType(AuthType.THIRD.getType());
authRelMapper.insertUseGeneratedKeys(authRel);
}
return authRel;
}
@Transactional(rollbackFor = Exception.class)
public UserAuthRel wechatLogin(WechatLoginBO loginBO) {
UserThirdAuth thirdAuth = thirdAuthMapper.selectByOpenid(loginBO.getOpenId());
if (thirdAuth == null) {
thirdAuth = new UserThirdAuth();
thirdAuth.setOpenid(loginBO.getOpenId());
thirdAuth.setLoginType(LoginType.WECHAT.getType());
thirdAuthMapper.insertUseGeneratedKeys(thirdAuth);
}
UserWechatAccount wechatAccount = wechatAccountMapper.selectByAuthId(thirdAuth.getId());
if (wechatAccount == null) {
wechatAccount = new UserWechatAccount();
wechatAccount.setAuthId(thirdAuth.getId());
wechatAccount.setUnionId(loginBO.getUnionId());
wechatAccountMapper.insertUseGeneratedKeys(wechatAccount);
}
UserAuthRel authRel = authRelMapper.selectByAuthTypeAndAuthId(AuthType.THIRD.getType(), thirdAuth.getId());
if (authRel == null) {
authRel = new UserAuthRel();
authRel.setAuthId(thirdAuth.getId());
authRel.setAuthType(AuthType.THIRD.getType());
authRelMapper.insertUseGeneratedKeys(authRel);
}
return authRel;
}
}