LoginService.java
8.03 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
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.enums.MiniUserType;
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.AppUser;
import cn.com.glsx.app.modules.model.LoginByAccountBO;
import cn.com.glsx.app.modules.model.QQOAuth2AccessToken;
import cn.com.glsx.auth.api.AuthFeignClient;
import cn.com.glsx.auth.model.SimpleUser;
import com.glsx.plat.common.utils.StringUtils;
import com.glsx.plat.core.enums.SysConstants;
import com.glsx.plat.core.web.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.Date;
import java.util.Optional;
@Slf4j
@Service
public class LoginService {
@Resource
private UserMapper userMapper;
@Resource
private UserAuthRelMapper userAuthRelMapper;
@Resource
private UserLocalAuthMapper userLocalAuthMapper;
@Resource
private UserLocalAccountMapper userLocalAccountMapper;
@Resource
private UserThirdAuthMapper userThirdAuthMapper;
@Resource
private UserWechatAccountMapper wechatAccountMapper;
@Resource
private UserQQAccountMapper qqAccountMapper;
@Autowired
private AuthFeignClient authFeignClient;
@Autowired
private BCryptPasswordEncoder bCryptPasswordEncoder;
@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());
}
User user = register(simpleUser);
AppUser appUser = UserConverter.INSTANCE.su2au(simpleUser);
appUser.setUserType(MiniUserType.NORMAL.getType());
} else if (loginBO.getLoginType() == 2) {
//验证码校验
//smsService.verifyCode(loginBO.getAccount(), loginBO.getCode());
}
return null;
}
/**
* c端用户账号注册
*
* @param simpleUser
*/
public User register(SimpleUser simpleUser) {
User user = Optional.ofNullable(userMapper.selectByAccount(simpleUser.getAccount())).orElse(new User());
user.setLinkerId(simpleUser.getId());
user.setAccount(simpleUser.getAccount());
user.setBlacklist(simpleUser.getBlacklist());
userMapper.saveOrUpdate(user);
UserLocalAuth localAuth = userLocalAuthMapper.selectByUserId(user.getId());
if (localAuth == null) {
localAuth = new UserLocalAuth();
localAuth.setAccount(user.getAccount());
String encryptedPasswd = bCryptPasswordEncoder.encode(user.getAccount());
localAuth.setPassword(encryptedPasswd);
userLocalAuthMapper.insertUseGeneratedKeys(localAuth);
}
UserAuthRel userAuthRel = userAuthRelMapper.selectByAuthTypeAndAuthId(AuthType.LOCAL.getType(), localAuth.getId());
if (userAuthRel == null) {
userAuthRel = new UserAuthRel();
userAuthRel.setUserId(user.getId());
userAuthRel.setAuthId(localAuth.getId());
userAuthRel.setAuthType(AuthType.LOCAL.getType());
userAuthRelMapper.insertUseGeneratedKeys(userAuthRel);
}
//生成本地账号
UserLocalAccount localAccount = userLocalAccountMapper.selectByAuthId(user.getId());
if (localAccount == null) {
localAccount = new UserLocalAccount();
//localAccount.setUserId(user.getId());
localAccount.setAuthId(localAuth.getId());
localAccount.setUsername(simpleUser.getUsername());
//生成平台unionId
String unionId = null;//generateUnionId();
localAccount.setUnionId(unionId);
localAccount.setCreatedDate(new Date());
userLocalAccountMapper.insert(localAccount);
} else {
if (StringUtils.isNotEmpty(localAccount.getUsername()) && !localAccount.getUsername().equals(simpleUser.getUsername())) {
localAccount.setUsername(simpleUser.getUsername());
localAccount.setUpdatedDate(new Date());
userLocalAccountMapper.updateByPrimaryKeySelective(localAccount);
}
}
return user;
}
@Transactional(rollbackFor = Exception.class)
public UserAuthRel qqAuth(QQOAuth2AccessToken accessToken) {
UserThirdAuth thirdAuth = userThirdAuthMapper.selectByOpenid(accessToken.getOpenId());
if (thirdAuth == null) {
thirdAuth = new UserThirdAuth();
thirdAuth.setOpenid(accessToken.getOpenId());
thirdAuth.setAccessToken(accessToken.getAccessToken());
thirdAuth.setLoginType(LoginType.QQ.getType());
userThirdAuthMapper.insertUseGeneratedKeys(thirdAuth);
}
UserAuthRel authRel = userAuthRelMapper.selectByAuthTypeAndAuthId(AuthType.THIRD.getType(), thirdAuth.getId());
if (authRel == null) {
authRel = new UserAuthRel();
authRel.setAuthId(thirdAuth.getId());
authRel.setAuthType(AuthType.THIRD.getType());
userAuthRelMapper.insertUseGeneratedKeys(authRel);
}
UserQQAccount qqAccount = qqAccountMapper.selectByAuthId(thirdAuth.getId());
if (qqAccount == null) {
qqAccount = new UserQQAccount();
qqAccount.setAuthId(thirdAuth.getId());
qqAccount.setUnionId(accessToken.getUnionId());
qqAccountMapper.insertUseGeneratedKeys(qqAccount);
}
return authRel;
}
@Transactional(rollbackFor = Exception.class)
public UserAuthRel wechatAuth(WxMaJscode2SessionResult sessionInfo, Integer accessMode) {
UserThirdAuth thirdAuth = userThirdAuthMapper.selectByOpenid(sessionInfo.getOpenid());
if (thirdAuth == null) {
thirdAuth = new UserThirdAuth();
thirdAuth.setOpenid(sessionInfo.getOpenid());
thirdAuth.setAccessToken(sessionInfo.getSessionKey());
thirdAuth.setLoginType(LoginType.WECHAT.getType());
userThirdAuthMapper.insertUseGeneratedKeys(thirdAuth);
}
User user = null;
if (MiniUserType.SHARED.getFlag().equals(accessMode)) {
SimpleUser simpleUser = new SimpleUser();
// simpleUser.setId();
// simpleUser.setAccount();
// simpleUser.setUsername();
simpleUser.setBlacklist(SysConstants.EnableStatus.enable.getCode());
user = register(simpleUser);
}
UserAuthRel authRel = userAuthRelMapper.selectByAuthTypeAndAuthId(AuthType.THIRD.getType(), thirdAuth.getId());
if (authRel == null) {
authRel = new UserAuthRel();
authRel.setUserId(user != null ? user.getId() : null);
authRel.setAuthId(thirdAuth.getId());
authRel.setAuthType(AuthType.THIRD.getType());
userAuthRelMapper.insertUseGeneratedKeys(authRel);
}
UserWechatAccount wechatAccount = wechatAccountMapper.selectByAuthId(thirdAuth.getId());
if (wechatAccount == null) {
wechatAccount = new UserWechatAccount();
wechatAccount.setAuthId(thirdAuth.getId());
wechatAccount.setUnionId(sessionInfo.getUnionid());
wechatAccountMapper.insertUseGeneratedKeys(wechatAccount);
}
return authRel;
}
}