Skip to content

Commit

Permalink
Merge pull request #114 from Whitea029/main
Browse files Browse the repository at this point in the history
取消微信登录
  • Loading branch information
feellmoose authored Feb 14, 2024
2 parents 874e17a + 32e9197 commit cc70e53
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 123 deletions.
3 changes: 1 addition & 2 deletions src/main/java/sast/evento/common/constant/Constant.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";


}
2 changes: 0 additions & 2 deletions src/main/java/sast/evento/common/enums/ErrorEnum.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
27 changes: 0 additions & 27 deletions src/main/java/sast/evento/controller/LoginController.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,33 +48,6 @@ public Map<String, Object> linkLogin(@RequestParam String code,
}
}

/**
* 使用weChat第三方登录
* @param code weChat验证code
* @return Map
*/
@OperateLog("微信登录")
@PostMapping("/login/wx")
@DefaultActionState(ActionState.PUBLIC)
public Map<String, Object> wxLogin(@RequestParam String code) {
return loginService.wxLogin(code);
}

/**
* weChat登录后绑定学号
* @param studentId 学号
* @return Map
*/
@OperateLog("绑定学号")
@PostMapping("/bind/student")
@DefaultActionState(ActionState.LOGIN)
public Map<String,Object> 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
Expand Down
1 change: 1 addition & 0 deletions src/main/java/sast/evento/job/WxSubscribeJob.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
* @date: 2023/7/26 22:11
*/

// 定时任务
@Slf4j
public class WxSubscribeJob implements Job {

Expand Down
43 changes: 39 additions & 4 deletions src/main/java/sast/evento/service/LoginService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,57 @@
import java.util.Map;

public interface LoginService {
/**
* sast-link登录
* @param code
* @param type
* @param updateUser
* @return
* @throws SastLinkException
*/
Map<String, Object> linkLogin(String code, Integer type, Boolean updateUser) throws SastLinkException;

Map<String, Object> wxLogin(String code);

/**
* 登出
* @param userId
* @throws SastLinkException
*/
void logout(String userId) throws SastLinkException;

/**
* 检查登录状态
* @param userId
* @param token
*/
void checkLoginState(String userId, String token);

Map<String, Object> bindStudentOnWechat(String userId, String studentId, Boolean force);

/**
* 获取授权给新设备登录的ticket
* @param ticket
* @return
*/
Map<String, Object> 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<String, Object> loginByPassword(String studentId, String password);


Expand Down
16 changes: 13 additions & 3 deletions src/main/java/sast/evento/service/WxService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);


Expand Down
59 changes: 4 additions & 55 deletions src/main/java/sast/evento/service/impl/LoginServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -100,27 +100,6 @@ public Map<String, Object> linkLogin(String code, Integer type, Boolean updateUs
return Map.of("token", token, "userInfo", user);
}

@Override
@Transactional
public Map<String, Object> 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<String, Object> getLoginTicket(@Nullable String ticket) {
if (ticket == null || ticket.isEmpty()) {
Expand Down Expand Up @@ -186,39 +165,6 @@ public Map<String, Object> loginByPassword(String studentId, String password) {
return Map.of("token", token, "userInfo", user);
}

@Override
@Transactional(rollbackFor = Exception.class)
public Map<String, Object> 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 {
Expand Down Expand Up @@ -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 {
Expand Down
30 changes: 0 additions & 30 deletions src/main/java/sast/evento/service/impl/WxServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Object> 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模板消息内容 */
Expand Down

0 comments on commit cc70e53

Please sign in to comment.