LoginService.java
10.8 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package cn.com.glsx.app.modules.service;
import cn.binarywang.wx.miniapp.api.WxMaService;
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.param.LoginByAccountBO;
import cn.com.glsx.auth.api.AuthFeignClient;
import cn.com.glsx.auth.model.SimpleUser;
import cn.hutool.json.JSONUtil;
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 me.chanjar.weixin.common.error.WxErrorException;
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;
@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 WxMaService wxMaService;
@Autowired
private AuthFeignClient authFeignClient;
@Autowired
private BCryptPasswordEncoder bCryptPasswordEncoder;
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.setCuserId(user.getId());
appUser.setUserType(MiniUserType.NORMAL.getType());
return appUser;
}
// else if (loginBO.getLoginType() == 2) {
// //验证码校验
// smsService.verifyCode(loginBO.getAccount(), loginBO.getCode());
// }
throw new AppServerException("登录失败");
}
/**
* c端用户账号注册
*
* @param simpleUser
*/
@Transactional(rollbackFor = Exception.class)
public User register(SimpleUser simpleUser) {
UserLocalAuth localAuth = userLocalAuthMapper.selectByAccount(simpleUser.getAccount());
if (localAuth == null) {
localAuth = new UserLocalAuth();
localAuth.setAccount(simpleUser.getAccount());
String encryptedPasswd = bCryptPasswordEncoder.encode(simpleUser.getAccount());
localAuth.setPassword(encryptedPasswd);
userLocalAuthMapper.insert(localAuth);
}
//生成本地账号
UserLocalAccount localAccount = userLocalAccountMapper.selectByAuthId(localAuth.getId());
if (localAccount == null) {
localAccount = new UserLocalAccount();
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);
}
}
//自定义用户主体
User user = null;
UserAuthRel userAuthRel = userAuthRelMapper.selectByAuthTypeAndAuthId(AuthType.LOCAL.getType(), localAuth.getId());
if (userAuthRel == null) {
user = new User();
user.setRelUserId(simpleUser.getId());
user.setBlacklist(simpleUser.getBlacklist());
userMapper.saveOrUpdate(user);
userAuthRel = new UserAuthRel();
userAuthRel.setUserId(user.getId());
userAuthRel.setAuthId(localAuth.getId());
userAuthRel.setAuthType(AuthType.LOCAL.getType());
userAuthRel.setCreatedDate(new Date());
userAuthRelMapper.insertUseGeneratedKeys(userAuthRel);
return user;
} else {
user = userMapper.selectById(userAuthRel.getUserId());
if (user == null) {
throw new AppServerException("登录失败,请联系管理员");
}
}
return user;
}
public void linkWxAuth(String code, AppUser user) throws WxErrorException {
if (StringUtils.isNotEmpty(code)) {
WxMaJscode2SessionResult sessionResult = wxMaService.getUserService().getSessionInfo(code);
if (sessionResult != null) {
UserThirdAuth userThirdAuthByOpenid = userThirdAuthMapper.selectByOpenid(sessionResult.getOpenid());
UserThirdAuth userThirdAuthByUserId = userThirdAuthMapper.selectByLoginTypeAndUserId(LoginType.WECHAT.getType(), user.getCuserId());
log.info("code={},thirdAuthByUserId:{},thirdAuthByOpenid:{}", code, JSONUtil.toJsonStr(userThirdAuthByUserId), JSONUtil.toJsonStr(userThirdAuthByOpenid));
if (userThirdAuthByUserId == null) {
//关联c端用户openid
wechatAuth(sessionResult, user.getCuserId());
} else {
log.info("用户{}已关联微信,Openid为{},新Openid为{}", user.getAccount(), userThirdAuthByUserId.getOpenid(), sessionResult.getOpenid());
//更新用户openid
if (!sessionResult.getOpenid().equals(userThirdAuthByUserId.getOpenid())) {
// userThirdAuthByUserId.setOpenid(sessionResult.getOpenid());
// userThirdAuthByUserId.setUpdatedDate(new Date());
// userThirdAuthMapper.updateByPrimaryKeySelective(userThirdAuthByUserId);
}
//todo 当c端账号先存在,常规账号后存在时,需要更新账号信息(比如:当某个用户通过被分享车辆链接登录产生了c端账号(账号信息是随机的),后来平台给开了常规账号,这种情况两种账号可以通过openid联系起来)
UserLocalAuth userLocalAuth = userLocalAuthMapper.selectByUserId(user.getCuserId());
if (userLocalAuth != null) {
UserLocalAccount userLocalAccount = userLocalAccountMapper.selectByAuthId(userLocalAuth.getId());
}
}
}
}
}
@Transactional(rollbackFor = Exception.class)
public UserAuthRel wechatAuth(WxMaJscode2SessionResult sessionInfo, Long userId) {
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);
}
UserAuthRel authRel = userAuthRelMapper.selectByAuthTypeAndAuthId(AuthType.THIRD.getType(), thirdAuth.getId());
if (authRel == null) {
authRel = new UserAuthRel();
authRel.setUserId(userId);
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;
}
@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);
}
UserAuthRel authRel = userAuthRelMapper.selectByAuthTypeAndAuthId(AuthType.THIRD.getType(), thirdAuth.getId());
if (authRel == null) {
User user = null;
//如果是分享链接登录,没有存在C端账号,则新建C端账号
if (MiniUserType.SHARED.getFlag().equals(accessMode)) {
SimpleUser simpleUser = new SimpleUser();
simpleUser.setAccount(thirdAuth.getLoginType() + "_user" + thirdAuth.getId());
simpleUser.setUsername(thirdAuth.getLoginType() + "_user");
simpleUser.setBlacklist(SysConstants.EnableStatus.enable.getCode());
user = register(simpleUser);
}
authRel = new UserAuthRel();
authRel.setUserId(user != null ? user.getId() : 0L);
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;
}
}