添加到那儿了小程序登录接口

调整小程序code换用户信息接口,兼容多小程序
This commit is contained in:
walker-sheng 2021-06-16 17:31:15 +08:00
parent 4ef20d7d62
commit 6256f6b8b3
9 changed files with 76 additions and 16 deletions

View File

@ -25,6 +25,10 @@ public class WeChatConfig {
private String spAppSecret; private String spAppSecret;
private String spApp2Id;
private String spApp2Secret;
/** 微信模块基础url */ /** 微信模块基础url */
private String wxModuleUrl; private String wxModuleUrl;
@ -92,6 +96,14 @@ public class WeChatConfig {
.append("&grant_type=authorization_code").toString(); .append("&grant_type=authorization_code").toString();
} }
public String getMiniApp2Code2SessionUrl(String wmCode) {
return new StringBuilder(this.domainUri)
.append("/sns/jscode2session?appid=").append(spApp2Id)
.append("&secret=").append(this.spApp2Secret)
.append("&js_code=").append(wmCode)
.append("&grant_type=authorization_code").toString();
}
public String getWxModuleBatchGetUserInfoUrl() { public String getWxModuleBatchGetUserInfoUrl() {
return this.wxModuleUrl + "/api/user/batchget"; return this.wxModuleUrl + "/api/user/batchget";
} }

View File

@ -87,12 +87,22 @@ public class LoginController {
return this.iAuthenticateService.getLoginUserInfoByToken(token); return this.iAuthenticateService.getLoginUserInfoByToken(token);
} }
@ApiOperation(value = "微信小程序code换取token") @ApiOperation(value = "<玖琏科技>微信小程序code换取token")
@GetMapping(path = "/wm/token") @GetMapping(path = "/wm/token")
public String getTokenByWmCode(String code) { public String getTokenByWmCode(String code) {
return this.iAuthenticateService.getTokenByWmCode(code); return this.iAuthenticateService.getTokenByWmCode(code);
} }
/**
* 到那儿了小程序code换token
* @param code
* @return
*/
@GetMapping(path = "/wm2/token")
public String getTokenByWmCode2(String code) {
return this.iAuthenticateService.getTokenByWmCode2(code);
}
@ApiOperation(value = "token是否过期") @ApiOperation(value = "token是否过期")
@GetMapping(path = "/{token}/isExpired") @GetMapping(path = "/{token}/isExpired")
public boolean isTokenExpired(@PathVariable String token) { public boolean isTokenExpired(@PathVariable String token) {

View File

@ -9,6 +9,6 @@ public interface IWxApiService {
* @param wmCode * @param wmCode
* @return * @return
*/ */
WmUserSession getWmUserSession(String wmCode); WmUserSession getWmUserSession(WxApiService.MiniApp miniApp, String wmCode);
} }

View File

@ -316,7 +316,7 @@ public class PermissionDistributeService implements IPermissionDistributeService
@Override @Override
@Transactional @Transactional
public List<UserPermissionVO> wmGetPermission(String code, Long id) { public List<UserPermissionVO> wmGetPermission(String code, Long id) {
AccountVO accountVO = this.iAuthenticateService.getOrCreateUserByWmcode(code); AccountVO accountVO = this.iAuthenticateService.getOrCreateUserByWmcode(WxApiService.MiniApp.JoyLink, code);
this.getUserPermission(id, accountVO); this.getUserPermission(id, accountVO);
List<UserPermissionVO> voList = iUserPermissionService.queryValidByUserIdAndDistributeId(accountVO.getId(), id); List<UserPermissionVO> voList = iUserPermissionService.queryValidByUserIdAndDistributeId(accountVO.getId(), id);
return voList; return voList;

View File

@ -376,7 +376,7 @@ public class SysUserService implements ISysUserService {
BusinessExceptionAssertEnum.INVALID_OPERATION.assertNotHasText(account.getWmOpenId(), BusinessExceptionAssertEnum.INVALID_OPERATION.assertNotHasText(account.getWmOpenId(),
String.format("用户[%s]已经绑定微信小程序", account.getNickname())); String.format("用户[%s]已经绑定微信小程序", account.getNickname()));
// 如果之前已经存在绑定解除之前的绑定 // 如果之前已经存在绑定解除之前的绑定
WmUserSession userSession = this.iWxApiService.getWmUserSession(code); WmUserSession userSession = this.iWxApiService.getWmUserSession(WxApiService.MiniApp.JoyLink, code);
String openid = userSession.getOpenid(); String openid = userSession.getOpenid();
SysAccountExample example = new SysAccountExample(); SysAccountExample example = new SysAccountExample();
example.createCriteria() example.createCriteria()

View File

@ -22,9 +22,7 @@ import java.util.concurrent.TimeUnit;
public class WxApiService implements IWxApiService { public class WxApiService implements IWxApiService {
private final RestTemplate restTemplate; private final RestTemplate restTemplate;
private final WeChatConfig weChatConfig; private final WeChatConfig weChatConfig;
private final ICacheService iCacheService; private final ICacheService iCacheService;
@Autowired @Autowired
@ -34,17 +32,34 @@ public class WxApiService implements IWxApiService {
this.iCacheService = iCacheService; this.iCacheService = iCacheService;
} }
public enum MiniApp {
/** 玖琏科技小程序 */
JoyLink,
/** 到那儿了小程序 */
DNL
}
/** /**
* 根据小程序code获取微信用户id信息 * 根据小程序code获取微信用户id信息
* @param wmCode * @param wmCode
* @return * @return
*/ */
@Override @Override
public WmUserSession getWmUserSession(String wmCode) { public WmUserSession getWmUserSession(MiniApp miniApp, String wmCode) {
BusinessExceptionAssertEnum.ARGUMENT_ILLEGAL.assertNotNull(miniApp);
BusinessExceptionAssertEnum.ARGUMENT_ILLEGAL.assertHasText(wmCode,"wmCode不能为空"); BusinessExceptionAssertEnum.ARGUMENT_ILLEGAL.assertHasText(wmCode,"wmCode不能为空");
WmUserSession wmUserSession = (WmUserSession) this.iCacheService.get(WmUserSession.getCacheKey(wmCode)); WmUserSession wmUserSession = (WmUserSession) this.iCacheService.get(WmUserSession.getCacheKey(wmCode));
if(Objects.isNull(wmUserSession)) { if(Objects.isNull(wmUserSession)) {
wmUserSession = restTemplate.getForObject(weChatConfig.getCode2SessionUrl(wmCode), WmUserSession.class); String url = null;
switch (miniApp) {
case JoyLink:
url = this.weChatConfig.getCode2SessionUrl(wmCode);
break;
case DNL:
url = this.weChatConfig.getMiniApp2Code2SessionUrl(wmCode);
break;
}
wmUserSession = restTemplate.getForObject(url, WmUserSession.class);
BusinessExceptionAssertEnum.WECHAT_CODE_EXPIRED.assertHasText(wmUserSession.getOpenid(), BusinessExceptionAssertEnum.WECHAT_CODE_EXPIRED.assertHasText(wmUserSession.getOpenid(),
String.format("小程序code为%s微信返回的错误消息errcode:'%s', errmsg:%s", wmCode, wmUserSession.getErrcode(), wmUserSession.getErrmsg())); String.format("小程序code为%s微信返回的错误消息errcode:'%s', errmsg:%s", wmCode, wmUserSession.getErrcode(), wmUserSession.getErrmsg()));
iCacheService.putExpired(WmUserSession.getCacheKey(wmCode), wmUserSession, 5, TimeUnit.MINUTES); iCacheService.putExpired(WmUserSession.getCacheKey(wmCode), wmUserSession, 5, TimeUnit.MINUTES);

View File

@ -11,6 +11,7 @@ import club.joylink.rtss.exception.BusinessExceptionAssertEnum;
import club.joylink.rtss.services.ISysUserService; import club.joylink.rtss.services.ISysUserService;
import club.joylink.rtss.services.IWxApiService; import club.joylink.rtss.services.IWxApiService;
import club.joylink.rtss.services.LoginSessionManager; import club.joylink.rtss.services.LoginSessionManager;
import club.joylink.rtss.services.WxApiService;
import club.joylink.rtss.services.project.DeviceService; import club.joylink.rtss.services.project.DeviceService;
import club.joylink.rtss.services.simulation.ProjectSimulationService; import club.joylink.rtss.services.simulation.ProjectSimulationService;
import club.joylink.rtss.simulation.cbtc.ProjectJointSimulationService; import club.joylink.rtss.simulation.cbtc.ProjectJointSimulationService;
@ -106,15 +107,15 @@ public class AuthenticateService implements IAuthenticateService {
LoginScanParam param = LoginScanParam.parse(state); LoginScanParam param = LoginScanParam.parse(state);
LoginStatusVO loginStatusVo = getLoginStatus(param.getSessionId()); LoginStatusVO loginStatusVo = getLoginStatus(param.getSessionId());
BusinessExceptionAssertEnum.LOGIN_EXPIRED.assertTrue(null != loginStatusVo && loginStatusVo.isWaiting()); BusinessExceptionAssertEnum.LOGIN_EXPIRED.assertTrue(null != loginStatusVo && loginStatusVo.isWaiting());
AccountVO accountVO = getOrCreateUserByWmcode(code); AccountVO accountVO = getOrCreateUserByWmcode(WxApiService.MiniApp.JoyLink, code);
loginStatusVo.setStatus(LoginStatusVO.ScanLoginStatus.SCAN); loginStatusVo.setStatus(LoginStatusVO.ScanLoginStatus.SCAN);
return accountVO; return accountVO;
} }
@Override @Override
public AccountVO getOrCreateUserByWmcode(String code) { public AccountVO getOrCreateUserByWmcode(WxApiService.MiniApp miniApp, String code) {
WmUserSession wmUserSession = this.iWxApiService.getWmUserSession(code); WmUserSession wmUserSession = this.iWxApiService.getWmUserSession(miniApp, code);
AccountVO accountVO = null; AccountVO accountVO = null;
if (!StringUtils.isEmpty(wmUserSession.getUnionid())) { if (!StringUtils.isEmpty(wmUserSession.getUnionid())) {
accountVO = this.iSysUserService.findUserByUnionId(wmUserSession.getUnionid()); accountVO = this.iSysUserService.findUserByUnionId(wmUserSession.getUnionid());
@ -135,7 +136,19 @@ public class AuthenticateService implements IAuthenticateService {
@Override @Override
public String getTokenByWmCode(String code) { public String getTokenByWmCode(String code) {
AccountVO accountVO = this.getOrCreateUserByWmcode(code); AccountVO accountVO = this.getOrCreateUserByWmcode(WxApiService.MiniApp.JoyLink, code);
LoginUserInfoVO loginInfoVO = this.handleMiniAppLogin(accountVO);
return loginInfoVO.getToken();
}
@Override
public String getTokenByWmCode2(String code) {
AccountVO accountVO = this.getOrCreateUserByWmcode(WxApiService.MiniApp.DNL, code);
LoginUserInfoVO loginInfoVO = this.handleMiniAppLogin(accountVO);
return loginInfoVO.getToken();
}
private LoginUserInfoVO handleMiniAppLogin(AccountVO accountVO) {
// 移除之前登录信息 // 移除之前登录信息
List<LoginUserInfoVO> infoVOList = this.loginSessionManager.queryLoginInfoByUserId(accountVO.getId()); List<LoginUserInfoVO> infoVOList = this.loginSessionManager.queryLoginInfoByUserId(accountVO.getId());
if (!CollectionUtils.isEmpty(infoVOList)) { if (!CollectionUtils.isEmpty(infoVOList)) {
@ -152,7 +165,7 @@ public class AuthenticateService implements IAuthenticateService {
Project.DEFAULT, null); Project.DEFAULT, null);
String token = loginUserInfoVO.getToken(); String token = loginUserInfoVO.getToken();
this.loginSessionManager.addLoginUserInfo(loginUserInfoVO); this.loginSessionManager.addLoginUserInfo(loginUserInfoVO);
return token; return loginUserInfoVO;
} }
@Override @Override
@ -202,7 +215,7 @@ public class AuthenticateService implements IAuthenticateService {
synchronized (loginStatusVo) { synchronized (loginStatusVo) {
if (!loginStatusVo.isSuccess()) { if (!loginStatusVo.isSuccess()) {
// 获取用户 // 获取用户
WmUserSession wmUserSession = this.iWxApiService.getWmUserSession(code); WmUserSession wmUserSession = this.iWxApiService.getWmUserSession(WxApiService.MiniApp.JoyLink, code);
AccountVO user = this.iSysUserService.getUserByWmOpenId(wmUserSession.getOpenid()); AccountVO user = this.iSysUserService.getUserByWmOpenId(wmUserSession.getOpenid());
if (StringUtils.isEmpty(loginStatusVo.getToken())) { //正常扫码登陆 if (StringUtils.isEmpty(loginStatusVo.getToken())) { //正常扫码登陆
// 构造登陆用户信息 // 构造登陆用户信息

View File

@ -1,6 +1,7 @@
package club.joylink.rtss.services.auth; package club.joylink.rtss.services.auth;
import club.joylink.rtss.constants.Project; import club.joylink.rtss.constants.Project;
import club.joylink.rtss.services.WxApiService;
import club.joylink.rtss.vo.AccountVO; import club.joylink.rtss.vo.AccountVO;
import club.joylink.rtss.vo.LoginUserInfoVO; import club.joylink.rtss.vo.LoginUserInfoVO;
import club.joylink.rtss.vo.client.LoginStatusVO; import club.joylink.rtss.vo.client.LoginStatusVO;
@ -71,15 +72,22 @@ public interface IAuthenticateService {
* @param code * @param code
* @return * @return
*/ */
AccountVO getOrCreateUserByWmcode(String code); AccountVO getOrCreateUserByWmcode(WxApiService.MiniApp miniApp, String code);
/** /**
* * 玖琏科技小程序code换token
* @param code * @param code
* @return * @return
*/ */
String getTokenByWmCode(String code); String getTokenByWmCode(String code);
/**
* 到那儿了小程序code换token
* @param code
* @return
*/
String getTokenByWmCode2(String code);
boolean isTokenExpired(String token); boolean isTokenExpired(String token);
/** /**

View File

@ -57,6 +57,8 @@ wechat:
wx-api-url: https://open.weixin.qq.com/connect/oauth2/authorize?appid=${wechat.app-id}&redirect_uri=http://joylink.club/wx/%s&response_type=code&scope=snsapi_base&state=%s#wechat_redirect wx-api-url: https://open.weixin.qq.com/connect/oauth2/authorize?appid=${wechat.app-id}&redirect_uri=http://joylink.club/wx/%s&response_type=code&scope=snsapi_base&state=%s#wechat_redirect
sp-app-id: wxe9150dbbcbf9440b sp-app-id: wxe9150dbbcbf9440b
sp-app-secret: 4b5d453e5ec246a3f1b72360c59e4fab sp-app-secret: 4b5d453e5ec246a3f1b72360c59e4fab
sp-app2-id: wxecb0321367be529c
sp-app2-secret: 3c31cb41588f27a78160092249123766
wm-base-url: https://joylink.club/oss/joylink/%s?state=%s wm-base-url: https://joylink.club/oss/joylink/%s?state=%s
wx-module-url: http://localhost:9001 wx-module-url: http://localhost:9001
mini: mini: