From f9d7ed2645427e7f0428e85383f7f59d7b4be164 Mon Sep 17 00:00:00 2001
From: Whitea029 <1664915115@qq.com>
Date: Wed, 14 Feb 2024 10:07:37 +0800
Subject: [PATCH 1/2] =?UTF-8?q?=E5=88=A0=E9=99=A4=E5=BE=AE=E4=BF=A1?=
=?UTF-8?q?=E7=99=BB=E5=BD=95=E4=B8=9A=E5=8A=A1=E4=BB=A3=E7=A0=81?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
pom.xml | 2 +-
.../sast/evento/common/constant/Constant.java | 3 +-
.../sast/evento/common/enums/ErrorEnum.java | 2 -
.../evento/controller/LoginController.java | 27 ---------
.../java/sast/evento/job/WxSubscribeJob.java | 1 +
.../sast/evento/service/LoginService.java | 43 ++++++++++++--
.../java/sast/evento/service/WxService.java | 16 ++++-
.../evento/service/impl/LoginServiceImpl.java | 59 ++-----------------
.../evento/service/impl/WxServiceImpl.java | 30 ----------
9 files changed, 59 insertions(+), 124 deletions(-)
diff --git a/pom.xml b/pom.xml
index 6391d0c..c8f4aef 100644
--- a/pom.xml
+++ b/pom.xml
@@ -37,7 +37,7 @@
sast-link-SDK
0.1.2
-
+·
org.springframework.boot
diff --git a/src/main/java/sast/evento/common/constant/Constant.java b/src/main/java/sast/evento/common/constant/Constant.java
index 5ef79e8..9e30007 100644
--- a/src/main/java/sast/evento/common/constant/Constant.java
+++ b/src/main/java/sast/evento/common/constant/Constant.java
@@ -5,11 +5,10 @@
* @author: feelMoose
* @date: 2023/7/31 21:53
*/
+// todo 待删除
public class Constant {
- public static final String wxAccessTokenURL = "https://api.weixin.qq.com/cgi-bin/token?appid={appid}&secret={secret}&grant_type=client_credential";
public static final String wxStableTokenURL = "https://api.weixin.qq.com/cgi-bin/stable_token";
public static final String wxSubscribeURL = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token={access_token}";
- public static final String jsCode2Session = "https://api.weixin.qq.com/sns/jscode2session?appid={appid}&secret={secret}&js_code={code}&grant_type=authorization_code";
}
diff --git a/src/main/java/sast/evento/common/enums/ErrorEnum.java b/src/main/java/sast/evento/common/enums/ErrorEnum.java
index b0d535e..80f4f8c 100644
--- a/src/main/java/sast/evento/common/enums/ErrorEnum.java
+++ b/src/main/java/sast/evento/common/enums/ErrorEnum.java
@@ -37,8 +37,6 @@ public enum ErrorEnum {
COS_SERVICE_ERROR(10004, "cos service error"),
SAST_LINK_SERVICE_ERROR(10005, "sast link service error"),
STUDENT_NOT_BIND(10006, "bind studentId first"),
- STUDENT_HAS_BEEN_BIND(10007, "studentId has been bind"),
- ACCOUNT_HAS_BEEN_BIND(10008, "account has been bind"),
/* 时间格式错误 */
TIME_ERROR(20001, "time format error");
private final Integer errCode;
diff --git a/src/main/java/sast/evento/controller/LoginController.java b/src/main/java/sast/evento/controller/LoginController.java
index 7a0167e..a9af5a2 100644
--- a/src/main/java/sast/evento/controller/LoginController.java
+++ b/src/main/java/sast/evento/controller/LoginController.java
@@ -48,33 +48,6 @@ public Map linkLogin(@RequestParam String code,
}
}
- /**
- * 使用weChat第三方登录
- * @param code weChat验证code
- * @return Map
- */
- @OperateLog("微信登录")
- @PostMapping("/login/wx")
- @DefaultActionState(ActionState.PUBLIC)
- public Map wxLogin(@RequestParam String code) {
- return loginService.wxLogin(code);
- }
-
- /**
- * weChat登录后绑定学号
- * @param studentId 学号
- * @return Map
- */
- @OperateLog("绑定学号")
- @PostMapping("/bind/student")
- @DefaultActionState(ActionState.LOGIN)
- public Map bindStudentId(@RequestParam String studentId,
- @RequestParam(required = false,defaultValue = "false") Boolean force){
- UserModel user = HttpInterceptor.userHolder.get();
- return loginService.bindStudentOnWechat(user.getId(),studentId,force);
-
- }
-
/**
* 获取授权给新设备登录的ticket
* @return Map
diff --git a/src/main/java/sast/evento/job/WxSubscribeJob.java b/src/main/java/sast/evento/job/WxSubscribeJob.java
index fff16d5..37bb9d1 100644
--- a/src/main/java/sast/evento/job/WxSubscribeJob.java
+++ b/src/main/java/sast/evento/job/WxSubscribeJob.java
@@ -28,6 +28,7 @@
* @date: 2023/7/26 22:11
*/
+// 定时任务
@Slf4j
public class WxSubscribeJob implements Job {
diff --git a/src/main/java/sast/evento/service/LoginService.java b/src/main/java/sast/evento/service/LoginService.java
index cf01eb3..101dff3 100644
--- a/src/main/java/sast/evento/service/LoginService.java
+++ b/src/main/java/sast/evento/service/LoginService.java
@@ -6,22 +6,57 @@
import java.util.Map;
public interface LoginService {
+ /**
+ * sast-link登录
+ * @param code
+ * @param type
+ * @param updateUser
+ * @return
+ * @throws SastLinkException
+ */
Map linkLogin(String code, Integer type, Boolean updateUser) throws SastLinkException;
- Map wxLogin(String code);
-
+ /**
+ * 登出
+ * @param userId
+ * @throws SastLinkException
+ */
void logout(String userId) throws SastLinkException;
+ /**
+ * 检查登录状态
+ * @param userId
+ * @param token
+ */
void checkLoginState(String userId, String token);
- Map bindStudentOnWechat(String userId, String studentId, Boolean force);
-
+ /**
+ * 获取授权给新设备登录的ticket
+ * @param ticket
+ * @return
+ */
Map getLoginTicket(String ticket);
+ /**
+ * 新设备获取ticket后使用学号登录
+ * @param ticket
+ * @param userId
+ */
void checkLoginTicket(String ticket,String userId);
+ /**
+ * 绑定密码
+ * @param studentId
+ * @param password
+ */
void bindPassword(String studentId, String password);
+ /**
+ * 密码登录
+ * @param studentId
+ * @param password
+ * @return
+ */
Map loginByPassword(String studentId, String password);
diff --git a/src/main/java/sast/evento/service/WxService.java b/src/main/java/sast/evento/service/WxService.java
index f0edab7..7b948c1 100644
--- a/src/main/java/sast/evento/service/WxService.java
+++ b/src/main/java/sast/evento/service/WxService.java
@@ -4,14 +4,24 @@
import sast.evento.model.wxServiceDTO.JsCodeSessionResponse;
import sast.evento.model.wxServiceDTO.WxSubscribeResponse;
+// todo 待删除
public interface WxService {
- AccessTokenResponse getStableToken();
- AccessTokenResponse getAccessToken();
- JsCodeSessionResponse login(String code);
+ /**
+ * 获取稳定token
+ * @return
+ */
+ AccessTokenResponse getStableToken();
+ /**
+ * 发送微信订阅消息
+ * @param eventId
+ * @param access_token
+ * @param openId
+ * @return
+ */
WxSubscribeResponse seedSubscribeMessage(Integer eventId, String access_token, String openId);
diff --git a/src/main/java/sast/evento/service/impl/LoginServiceImpl.java b/src/main/java/sast/evento/service/impl/LoginServiceImpl.java
index b4e3455..1875272 100644
--- a/src/main/java/sast/evento/service/impl/LoginServiceImpl.java
+++ b/src/main/java/sast/evento/service/impl/LoginServiceImpl.java
@@ -47,7 +47,7 @@ public class LoginServiceImpl implements LoginService {
private UserMapper userMapper;
@Resource
private UserPasswordMapper userPasswordMapper;
- @Resource
+ @Resource // todo 待删除
private WxService wxService;
@Resource
private JwtUtil jwtUtil;
@@ -100,27 +100,6 @@ public Map linkLogin(String code, Integer type, Boolean updateUs
return Map.of("token", token, "userInfo", user);
}
- @Override
- @Transactional
- public Map wxLogin(String code) {
- //没有学号冲突的风险
- JsCodeSessionResponse jsCodeSessionResponse = wxService.login(code);
- String openId = jsCodeSessionResponse.getOpenid();
- if (openId == null || openId.isEmpty()) {
- throw new LocalRunTimeException(ErrorEnum.WX_SERVICE_ERROR, "wx login failed");
- }
- User user = userMapper.selectOne(Wrappers.lambdaQuery(User.class)
- .eq(User::getOpenId, openId));
- if (user == null) {
- user = new User();
- user.setOpenId(openId);
- user.setUnionId(jsCodeSessionResponse.getUnionid());
- userMapper.insert(user);
- }
- String token = addTokenInCache(user, false);
- return Map.of("unionid", jsCodeSessionResponse.getUnionid(), "userInfo", user, "token", token);
- }
-
@Override
public Map getLoginTicket(@Nullable String ticket) {
if (ticket == null || ticket.isEmpty()) {
@@ -186,39 +165,6 @@ public Map loginByPassword(String studentId, String password) {
return Map.of("token", token, "userInfo", user);
}
- @Override
- @Transactional(rollbackFor = Exception.class)
- public Map bindStudentOnWechat(String userId, String studentId, Boolean force) {
- //此时微信登陆成功已经默认创建新账号,需要将新账号删除并绑定至原有link账号
- //查看本地是否存在此学号
- User user = userMapper.selectOne(Wrappers.lambdaQuery(User.class)
- .eq(User::getStudentId, studentId).last("for update"));
- if (user != null) {
- //若已经存在,则使用第一个账号(本账号已经绑定过也算在这里,所以只可以绑定一次学号,否则去联系管理员)
- if (force) {
- User del = userMapper.selectOne(Wrappers.lambdaQuery(User.class)
- .eq(User::getId, userId).last("for update"));
- if (user.getOpenId() != null || del.getLinkId() != null) {
- //微信已经绑定过学号也在这里报错
- throw new LocalRunTimeException(ErrorEnum.ACCOUNT_HAS_BEEN_BIND, "please contact administrator");
- }
- user.setOpenId(del.getOpenId());
- user.setUnionId(del.getUnionId());
- user.setStudentId(studentId);
- userMapper.deleteById(userId);
- userMapper.updateById(user);
- String token = addTokenInCache(user, true);
- return Map.of("token", token, "userInfo", user);
- } else {
- throw new LocalRunTimeException(ErrorEnum.STUDENT_HAS_BEEN_BIND, "force an overwrite on new account or cancel operation");
- }
- }
- userMapper.bindStudentId(userId, studentId);
- user = userMapper.selectById(userId);
- String token = addTokenInCache(user, true);
- return Map.of("token", token, "userInfo", user);
-
- }
@Override
public void logout(String userId) throws SastLinkException {
@@ -259,8 +205,11 @@ private void checkPassword(String studentId, String password) {
}
private String addTokenInCache(User user, boolean update) {
+ // 构建用户
UserModel userModel = new UserModel(user.getId(), user.getStudentId(), user.getEmail());
+ // 生成token
String token = generateToken(userModel);
+ // 缓存
if (update) {
redisUtil.set(TOKEN + user.getId(), token, jwtUtil.expiration);
} else {
diff --git a/src/main/java/sast/evento/service/impl/WxServiceImpl.java b/src/main/java/sast/evento/service/impl/WxServiceImpl.java
index 96d2671..ca14ccd 100644
--- a/src/main/java/sast/evento/service/impl/WxServiceImpl.java
+++ b/src/main/java/sast/evento/service/impl/WxServiceImpl.java
@@ -57,36 +57,6 @@ public AccessTokenResponse getStableToken() {
throw new LocalRunTimeException(ErrorEnum.WX_SERVICE_ERROR, "response or access_token is empty");
}
- @Override
- public AccessTokenResponse getAccessToken() {
- Map map = restTemplate.getForEntity(Constant.wxAccessTokenURL, Map.class, appid, secret).getBody();
- if (map == null) {
- throw new LocalRunTimeException(ErrorEnum.WX_SERVICE_ERROR, "response is empty");
- }
- AccessTokenResponse response = new AccessTokenResponse();
- response.setAccess_token((String) map.get("access_token"));
- if (response.getAccess_token() == null) {
- log.error("error response: " + map);
- throw new LocalRunTimeException(ErrorEnum.WX_SERVICE_ERROR, "access_token is empty");
- }
- response.setExpires_in((Integer) map.get("expires_in"));
- return response;
- }
-
- @Override
- public JsCodeSessionResponse login(String code) {
- String text = restTemplate.getForEntity(Constant.jsCode2Session, String.class, appid, secret, code).getBody();
- if (text == null) {
- throw new LocalRunTimeException(ErrorEnum.WX_SERVICE_ERROR, "null response from wx");
- }
- JsCodeSessionResponse jsCodeSessionResponse = JsonUtil.fromJson(text, JsCodeSessionResponse.class);
- if (jsCodeSessionResponse == null ||!jsCodeSessionResponse.getErrmsg().isEmpty()) {
- log.error("error get userInfo: " + text);
- throw new LocalRunTimeException(ErrorEnum.WX_SERVICE_ERROR, "error get userInfo from WeChat");
- }
- return jsCodeSessionResponse;
- }
-
@Override
/* 发送wx模板消息内容 */
From 32e9197abddb6a0d8294349f9716c39c7540e0c8 Mon Sep 17 00:00:00 2001
From: whitea <145814986+Whitea029@users.noreply.github.com>
Date: Wed, 14 Feb 2024 10:23:07 +0800
Subject: [PATCH 2/2] Update pom.xml
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
删除之前不小心打了一个点
---
pom.xml | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/pom.xml b/pom.xml
index c8f4aef..6391d0c 100644
--- a/pom.xml
+++ b/pom.xml
@@ -37,7 +37,7 @@
sast-link-SDK
0.1.2
-·
+
org.springframework.boot