Skip to content

Commit cc70e53

Browse files
authored
Merge pull request #114 from Whitea029/main
取消微信登录
2 parents 874e17a + 32e9197 commit cc70e53

File tree

8 files changed

+58
-123
lines changed

8 files changed

+58
-123
lines changed

src/main/java/sast/evento/common/constant/Constant.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@
55
* @author: feelMoose
66
* @date: 2023/7/31 21:53
77
*/
8+
// todo 待删除
89
public class Constant {
9-
public static final String wxAccessTokenURL = "https://api.weixin.qq.com/cgi-bin/token?appid={appid}&secret={secret}&grant_type=client_credential";
1010
public static final String wxStableTokenURL = "https://api.weixin.qq.com/cgi-bin/stable_token";
1111
public static final String wxSubscribeURL = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token={access_token}";
12-
public static final String jsCode2Session = "https://api.weixin.qq.com/sns/jscode2session?appid={appid}&secret={secret}&js_code={code}&grant_type=authorization_code";
1312

1413

1514
}

src/main/java/sast/evento/common/enums/ErrorEnum.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ public enum ErrorEnum {
3737
COS_SERVICE_ERROR(10004, "cos service error"),
3838
SAST_LINK_SERVICE_ERROR(10005, "sast link service error"),
3939
STUDENT_NOT_BIND(10006, "bind studentId first"),
40-
STUDENT_HAS_BEEN_BIND(10007, "studentId has been bind"),
41-
ACCOUNT_HAS_BEEN_BIND(10008, "account has been bind"),
4240
/* 时间格式错误 */
4341
TIME_ERROR(20001, "time format error");
4442
private final Integer errCode;

src/main/java/sast/evento/controller/LoginController.java

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -48,33 +48,6 @@ public Map<String, Object> linkLogin(@RequestParam String code,
4848
}
4949
}
5050

51-
/**
52-
* 使用weChat第三方登录
53-
* @param code weChat验证code
54-
* @return Map
55-
*/
56-
@OperateLog("微信登录")
57-
@PostMapping("/login/wx")
58-
@DefaultActionState(ActionState.PUBLIC)
59-
public Map<String, Object> wxLogin(@RequestParam String code) {
60-
return loginService.wxLogin(code);
61-
}
62-
63-
/**
64-
* weChat登录后绑定学号
65-
* @param studentId 学号
66-
* @return Map
67-
*/
68-
@OperateLog("绑定学号")
69-
@PostMapping("/bind/student")
70-
@DefaultActionState(ActionState.LOGIN)
71-
public Map<String,Object> bindStudentId(@RequestParam String studentId,
72-
@RequestParam(required = false,defaultValue = "false") Boolean force){
73-
UserModel user = HttpInterceptor.userHolder.get();
74-
return loginService.bindStudentOnWechat(user.getId(),studentId,force);
75-
76-
}
77-
7851
/**
7952
* 获取授权给新设备登录的ticket
8053
* @return Map

src/main/java/sast/evento/job/WxSubscribeJob.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
* @date: 2023/7/26 22:11
2929
*/
3030

31+
// 定时任务
3132
@Slf4j
3233
public class WxSubscribeJob implements Job {
3334

src/main/java/sast/evento/service/LoginService.java

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,57 @@
66
import java.util.Map;
77

88
public interface LoginService {
9+
/**
10+
* sast-link登录
11+
* @param code
12+
* @param type
13+
* @param updateUser
14+
* @return
15+
* @throws SastLinkException
16+
*/
917
Map<String, Object> linkLogin(String code, Integer type, Boolean updateUser) throws SastLinkException;
1018

11-
Map<String, Object> wxLogin(String code);
12-
19+
/**
20+
* 登出
21+
* @param userId
22+
* @throws SastLinkException
23+
*/
1324
void logout(String userId) throws SastLinkException;
1425

26+
/**
27+
* 检查登录状态
28+
* @param userId
29+
* @param token
30+
*/
1531
void checkLoginState(String userId, String token);
1632

17-
Map<String, Object> bindStudentOnWechat(String userId, String studentId, Boolean force);
18-
33+
/**
34+
* 获取授权给新设备登录的ticket
35+
* @param ticket
36+
* @return
37+
*/
1938
Map<String, Object> getLoginTicket(String ticket);
2039

40+
/**
41+
* 新设备获取ticket后使用学号登录
42+
* @param ticket
43+
* @param userId
44+
*/
2145
void checkLoginTicket(String ticket,String userId);
2246

47+
/**
48+
* 绑定密码
49+
* @param studentId
50+
* @param password
51+
*/
2352
void bindPassword(String studentId, String password);
2453

54+
/**
55+
* 密码登录
56+
* @param studentId
57+
* @param password
58+
* @return
59+
*/
2560
Map<String, Object> loginByPassword(String studentId, String password);
2661

2762

src/main/java/sast/evento/service/WxService.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,24 @@
44
import sast.evento.model.wxServiceDTO.JsCodeSessionResponse;
55
import sast.evento.model.wxServiceDTO.WxSubscribeResponse;
66

7+
// todo 待删除
78

89
public interface WxService {
9-
AccessTokenResponse getStableToken();
1010

11-
AccessTokenResponse getAccessToken();
1211

13-
JsCodeSessionResponse login(String code);
12+
/**
13+
* 获取稳定token
14+
* @return
15+
*/
16+
AccessTokenResponse getStableToken();
1417

18+
/**
19+
* 发送微信订阅消息
20+
* @param eventId
21+
* @param access_token
22+
* @param openId
23+
* @return
24+
*/
1525
WxSubscribeResponse seedSubscribeMessage(Integer eventId, String access_token, String openId);
1626

1727

src/main/java/sast/evento/service/impl/LoginServiceImpl.java

Lines changed: 4 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public class LoginServiceImpl implements LoginService {
4747
private UserMapper userMapper;
4848
@Resource
4949
private UserPasswordMapper userPasswordMapper;
50-
@Resource
50+
@Resource // todo 待删除
5151
private WxService wxService;
5252
@Resource
5353
private JwtUtil jwtUtil;
@@ -100,27 +100,6 @@ public Map<String, Object> linkLogin(String code, Integer type, Boolean updateUs
100100
return Map.of("token", token, "userInfo", user);
101101
}
102102

103-
@Override
104-
@Transactional
105-
public Map<String, Object> wxLogin(String code) {
106-
//没有学号冲突的风险
107-
JsCodeSessionResponse jsCodeSessionResponse = wxService.login(code);
108-
String openId = jsCodeSessionResponse.getOpenid();
109-
if (openId == null || openId.isEmpty()) {
110-
throw new LocalRunTimeException(ErrorEnum.WX_SERVICE_ERROR, "wx login failed");
111-
}
112-
User user = userMapper.selectOne(Wrappers.lambdaQuery(User.class)
113-
.eq(User::getOpenId, openId));
114-
if (user == null) {
115-
user = new User();
116-
user.setOpenId(openId);
117-
user.setUnionId(jsCodeSessionResponse.getUnionid());
118-
userMapper.insert(user);
119-
}
120-
String token = addTokenInCache(user, false);
121-
return Map.of("unionid", jsCodeSessionResponse.getUnionid(), "userInfo", user, "token", token);
122-
}
123-
124103
@Override
125104
public Map<String, Object> getLoginTicket(@Nullable String ticket) {
126105
if (ticket == null || ticket.isEmpty()) {
@@ -186,39 +165,6 @@ public Map<String, Object> loginByPassword(String studentId, String password) {
186165
return Map.of("token", token, "userInfo", user);
187166
}
188167

189-
@Override
190-
@Transactional(rollbackFor = Exception.class)
191-
public Map<String, Object> bindStudentOnWechat(String userId, String studentId, Boolean force) {
192-
//此时微信登陆成功已经默认创建新账号,需要将新账号删除并绑定至原有link账号
193-
//查看本地是否存在此学号
194-
User user = userMapper.selectOne(Wrappers.lambdaQuery(User.class)
195-
.eq(User::getStudentId, studentId).last("for update"));
196-
if (user != null) {
197-
//若已经存在,则使用第一个账号(本账号已经绑定过也算在这里,所以只可以绑定一次学号,否则去联系管理员)
198-
if (force) {
199-
User del = userMapper.selectOne(Wrappers.lambdaQuery(User.class)
200-
.eq(User::getId, userId).last("for update"));
201-
if (user.getOpenId() != null || del.getLinkId() != null) {
202-
//微信已经绑定过学号也在这里报错
203-
throw new LocalRunTimeException(ErrorEnum.ACCOUNT_HAS_BEEN_BIND, "please contact administrator");
204-
}
205-
user.setOpenId(del.getOpenId());
206-
user.setUnionId(del.getUnionId());
207-
user.setStudentId(studentId);
208-
userMapper.deleteById(userId);
209-
userMapper.updateById(user);
210-
String token = addTokenInCache(user, true);
211-
return Map.of("token", token, "userInfo", user);
212-
} else {
213-
throw new LocalRunTimeException(ErrorEnum.STUDENT_HAS_BEEN_BIND, "force an overwrite on new account or cancel operation");
214-
}
215-
}
216-
userMapper.bindStudentId(userId, studentId);
217-
user = userMapper.selectById(userId);
218-
String token = addTokenInCache(user, true);
219-
return Map.of("token", token, "userInfo", user);
220-
221-
}
222168

223169
@Override
224170
public void logout(String userId) throws SastLinkException {
@@ -259,8 +205,11 @@ private void checkPassword(String studentId, String password) {
259205
}
260206

261207
private String addTokenInCache(User user, boolean update) {
208+
// 构建用户
262209
UserModel userModel = new UserModel(user.getId(), user.getStudentId(), user.getEmail());
210+
// 生成token
263211
String token = generateToken(userModel);
212+
// 缓存
264213
if (update) {
265214
redisUtil.set(TOKEN + user.getId(), token, jwtUtil.expiration);
266215
} else {

src/main/java/sast/evento/service/impl/WxServiceImpl.java

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -57,36 +57,6 @@ public AccessTokenResponse getStableToken() {
5757
throw new LocalRunTimeException(ErrorEnum.WX_SERVICE_ERROR, "response or access_token is empty");
5858
}
5959

60-
@Override
61-
public AccessTokenResponse getAccessToken() {
62-
Map<String, Object> map = restTemplate.getForEntity(Constant.wxAccessTokenURL, Map.class, appid, secret).getBody();
63-
if (map == null) {
64-
throw new LocalRunTimeException(ErrorEnum.WX_SERVICE_ERROR, "response is empty");
65-
}
66-
AccessTokenResponse response = new AccessTokenResponse();
67-
response.setAccess_token((String) map.get("access_token"));
68-
if (response.getAccess_token() == null) {
69-
log.error("error response: " + map);
70-
throw new LocalRunTimeException(ErrorEnum.WX_SERVICE_ERROR, "access_token is empty");
71-
}
72-
response.setExpires_in((Integer) map.get("expires_in"));
73-
return response;
74-
}
75-
76-
@Override
77-
public JsCodeSessionResponse login(String code) {
78-
String text = restTemplate.getForEntity(Constant.jsCode2Session, String.class, appid, secret, code).getBody();
79-
if (text == null) {
80-
throw new LocalRunTimeException(ErrorEnum.WX_SERVICE_ERROR, "null response from wx");
81-
}
82-
JsCodeSessionResponse jsCodeSessionResponse = JsonUtil.fromJson(text, JsCodeSessionResponse.class);
83-
if (jsCodeSessionResponse == null ||!jsCodeSessionResponse.getErrmsg().isEmpty()) {
84-
log.error("error get userInfo: " + text);
85-
throw new LocalRunTimeException(ErrorEnum.WX_SERVICE_ERROR, "error get userInfo from WeChat");
86-
}
87-
return jsCodeSessionResponse;
88-
}
89-
9060

9161
@Override
9262
/* 发送wx模板消息内容 */

0 commit comments

Comments
 (0)