Skip to content

Commit

Permalink
feat: 增加微信登录功能
Browse files Browse the repository at this point in the history
  • Loading branch information
fengyw committed May 28, 2024
1 parent 5356ec0 commit b1e523b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.roncoo.education.user.service.api.biz.ApiUsersBiz;
import com.roncoo.education.user.service.api.req.*;
import com.roncoo.education.user.service.api.resp.UsersLoginResp;
import com.roncoo.education.user.service.api.resp.WxAuthResp;
import com.roncoo.education.user.service.api.resp.WxCodeResp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -73,10 +73,10 @@ public Result<String> wxLogin(@RequestBody WxLoginReq req) {
}

@ApiOperation(value = "微信登录", notes = "返回用户信息")
@PostMapping(value = "/wx/auth")
public Result<WxAuthResp> wxAuth(@RequestBody WxAuthReq req) {
@PostMapping(value = "/wx/code")
public Result<WxCodeResp> wxCode(@RequestBody WxCodeReq req) {
try {
return biz.wxAuth(req);
return biz.wxCode(req);
} catch (WxErrorException e) {
log.error("请重试", e);
Thread.currentThread().interrupt();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import com.roncoo.education.user.dao.impl.mapper.entity.UsersAccount;
import com.roncoo.education.user.service.api.req.*;
import com.roncoo.education.user.service.api.resp.UsersLoginResp;
import com.roncoo.education.user.service.api.resp.WxAuthResp;
import com.roncoo.education.user.service.api.resp.WxCodeResp;
import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.bean.WxOAuth2UserInfo;
Expand Down Expand Up @@ -305,14 +305,14 @@ public Result<String> wxLogin(WxLoginReq req) {
return Result.error("登录类型暂没支持");
}

public Result<WxAuthResp> wxAuth(WxAuthReq req) throws WxErrorException {
public Result<WxCodeResp> wxCode(WxCodeReq req) throws WxErrorException {
LoginConfig loginConfig = feignSysConfig.getLogin();
WxAuthResp authResp = new WxAuthResp();
WxCodeResp codeResp = new WxCodeResp();
if (req.getLoginAuthType().equals(LoginAuthTypeEnum.PC.getCode())) {
// 网页应用
WxOAuth2Service wxOAuth2Service = new WxOpenOAuth2ServiceImpl(loginConfig.getWxPcLoginAppId(), loginConfig.getWxPcLoginAppSecret(), new WxOpenInMemoryConfigStorage());
WxOAuth2AccessToken accessToken = wxOAuth2Service.getAccessToken(req.getCode());
authResp.setAuthInfo(getAuthInfo(wxOAuth2Service, accessToken));
codeResp.setAuthInfo(getAuthInfo(wxOAuth2Service, accessToken));
} else if (req.getLoginAuthType().equals(LoginAuthTypeEnum.MP.getCode())) {
// 公众号
WxMpService wxMpService = new WxMpServiceImpl();
Expand All @@ -321,7 +321,7 @@ public Result<WxAuthResp> wxAuth(WxAuthReq req) throws WxErrorException {
mpMapConfig.setSecret(loginConfig.getWxMpLoginAppSecret());
wxMpService.setWxMpConfigStorage(mpMapConfig);
WxOAuth2AccessToken accessToken = wxMpService.getOAuth2Service().getAccessToken(req.getCode());
authResp.setAuthInfo(getAuthInfo(wxMpService.getOAuth2Service(), accessToken));
codeResp.setAuthInfo(getAuthInfo(wxMpService.getOAuth2Service(), accessToken));
} else if (req.getLoginAuthType().equals(LoginAuthTypeEnum.MA.getCode())) {
// 小程序
WxMaService wxMaService = new WxMaServiceImpl();
Expand All @@ -330,13 +330,13 @@ public Result<WxAuthResp> wxAuth(WxAuthReq req) throws WxErrorException {
wxMaConfig.setSecret(loginConfig.getWxMaLoginAppSecret());
wxMaService.setWxMaConfig(wxMaConfig);
}
return Result.success(authResp);
return Result.success(codeResp);
}


private WxAuthResp.AuthInfo getAuthInfo(WxOAuth2Service wxOAuth2Service, WxOAuth2AccessToken accessToken) throws WxErrorException {
private WxCodeResp.AuthInfo getAuthInfo(WxOAuth2Service wxOAuth2Service, WxOAuth2AccessToken accessToken) throws WxErrorException {
WxOAuth2UserInfo userInfo = wxOAuth2Service.getUserInfo(accessToken, null);
WxAuthResp.AuthInfo authInfo = new WxAuthResp.AuthInfo();
WxCodeResp.AuthInfo authInfo = new WxCodeResp.AuthInfo();
authInfo.setUnionId(userInfo.getUnionId());
authInfo.setHeadImg(userInfo.getHeadImgUrl());
authInfo.setNickname(userInfo.getNickname());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.roncoo.education.user.service.api.req;

import com.roncoo.education.common.core.enums.ClientTypeEnum;
import com.roncoo.education.common.core.enums.LoginAuthTypeEnum;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;
Expand All @@ -13,16 +15,22 @@
*/
@Data
@Accessors(chain = true)
public class WxAuthReq implements Serializable {
public class WxCodeReq implements Serializable {

private static final long serialVersionUID = -2877781106821535513L;

@ApiModelProperty(value = "授权回调code值")
private String code;

/**
* @see LoginAuthTypeEnum
*/
@ApiModelProperty(value = "授权类型,参考:LoginAuthTypeEnum", required = true)
private Integer loginAuthType;

/**
* @see ClientTypeEnum
*/
@ApiModelProperty(value = "客户端类型,参考:ClientTypeEnum")
private Integer clientType;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/
@Data
@Accessors(chain = true)
public class WxAuthResp implements Serializable {
public class WxCodeResp implements Serializable {

private static final long serialVersionUID = 2621609267080102065L;

Expand Down

0 comments on commit b1e523b

Please sign in to comment.