diff --git a/src/main/java/club/joylink/rtss/configuration/configProp/WeChatConfig.java b/src/main/java/club/joylink/rtss/configuration/configProp/WeChatConfig.java index d7d8e8e09..d3798ae7b 100644 --- a/src/main/java/club/joylink/rtss/configuration/configProp/WeChatConfig.java +++ b/src/main/java/club/joylink/rtss/configuration/configProp/WeChatConfig.java @@ -25,6 +25,10 @@ public class WeChatConfig { private String spAppSecret; + private String spApp2Id; + + private String spApp2Secret; + /** 微信模块基础url */ private String wxModuleUrl; @@ -92,6 +96,14 @@ public class WeChatConfig { .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() { return this.wxModuleUrl + "/api/user/batchget"; } diff --git a/src/main/java/club/joylink/rtss/controller/LoginController.java b/src/main/java/club/joylink/rtss/controller/LoginController.java index 4b5ee0c75..f6f432f62 100644 --- a/src/main/java/club/joylink/rtss/controller/LoginController.java +++ b/src/main/java/club/joylink/rtss/controller/LoginController.java @@ -87,12 +87,22 @@ public class LoginController { return this.iAuthenticateService.getLoginUserInfoByToken(token); } - @ApiOperation(value = "微信小程序code换取token") + @ApiOperation(value = "<玖琏科技>微信小程序code换取token") @GetMapping(path = "/wm/token") public String getTokenByWmCode(String 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是否过期") @GetMapping(path = "/{token}/isExpired") public boolean isTokenExpired(@PathVariable String token) { diff --git a/src/main/java/club/joylink/rtss/services/IWxApiService.java b/src/main/java/club/joylink/rtss/services/IWxApiService.java index d5a2f950d..283de245f 100644 --- a/src/main/java/club/joylink/rtss/services/IWxApiService.java +++ b/src/main/java/club/joylink/rtss/services/IWxApiService.java @@ -9,6 +9,6 @@ public interface IWxApiService { * @param wmCode * @return */ - WmUserSession getWmUserSession(String wmCode); + WmUserSession getWmUserSession(WxApiService.MiniApp miniApp, String wmCode); } \ No newline at end of file diff --git a/src/main/java/club/joylink/rtss/services/PermissionDistributeService.java b/src/main/java/club/joylink/rtss/services/PermissionDistributeService.java index b8e9ba0b5..6b897baca 100644 --- a/src/main/java/club/joylink/rtss/services/PermissionDistributeService.java +++ b/src/main/java/club/joylink/rtss/services/PermissionDistributeService.java @@ -316,7 +316,7 @@ public class PermissionDistributeService implements IPermissionDistributeService @Override @Transactional public List wmGetPermission(String code, Long id) { - AccountVO accountVO = this.iAuthenticateService.getOrCreateUserByWmcode(code); + AccountVO accountVO = this.iAuthenticateService.getOrCreateUserByWmcode(WxApiService.MiniApp.JoyLink, code); this.getUserPermission(id, accountVO); List voList = iUserPermissionService.queryValidByUserIdAndDistributeId(accountVO.getId(), id); return voList; diff --git a/src/main/java/club/joylink/rtss/services/SysUserService.java b/src/main/java/club/joylink/rtss/services/SysUserService.java index 60c9ded79..571532b3d 100644 --- a/src/main/java/club/joylink/rtss/services/SysUserService.java +++ b/src/main/java/club/joylink/rtss/services/SysUserService.java @@ -376,7 +376,7 @@ public class SysUserService implements ISysUserService { BusinessExceptionAssertEnum.INVALID_OPERATION.assertNotHasText(account.getWmOpenId(), String.format("用户[%s]已经绑定微信小程序", account.getNickname())); // 如果之前已经存在绑定,解除之前的绑定 - WmUserSession userSession = this.iWxApiService.getWmUserSession(code); + WmUserSession userSession = this.iWxApiService.getWmUserSession(WxApiService.MiniApp.JoyLink, code); String openid = userSession.getOpenid(); SysAccountExample example = new SysAccountExample(); example.createCriteria() diff --git a/src/main/java/club/joylink/rtss/services/WxApiService.java b/src/main/java/club/joylink/rtss/services/WxApiService.java index e59d71ef5..b910d0f0a 100644 --- a/src/main/java/club/joylink/rtss/services/WxApiService.java +++ b/src/main/java/club/joylink/rtss/services/WxApiService.java @@ -22,9 +22,7 @@ import java.util.concurrent.TimeUnit; public class WxApiService implements IWxApiService { private final RestTemplate restTemplate; - private final WeChatConfig weChatConfig; - private final ICacheService iCacheService; @Autowired @@ -34,17 +32,34 @@ public class WxApiService implements IWxApiService { this.iCacheService = iCacheService; } + public enum MiniApp { + /** 玖琏科技小程序 */ + JoyLink, + /** 到那儿了小程序 */ + DNL + } + /** * 根据小程序code获取微信用户id信息 * @param wmCode * @return */ @Override - public WmUserSession getWmUserSession(String wmCode) { + public WmUserSession getWmUserSession(MiniApp miniApp, String wmCode) { + BusinessExceptionAssertEnum.ARGUMENT_ILLEGAL.assertNotNull(miniApp); BusinessExceptionAssertEnum.ARGUMENT_ILLEGAL.assertHasText(wmCode,"wmCode不能为空"); WmUserSession wmUserSession = (WmUserSession) this.iCacheService.get(WmUserSession.getCacheKey(wmCode)); 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(), String.format("小程序code为:‘%s’,微信返回的错误消息:errcode:'%s', errmsg:‘%s’", wmCode, wmUserSession.getErrcode(), wmUserSession.getErrmsg())); iCacheService.putExpired(WmUserSession.getCacheKey(wmCode), wmUserSession, 5, TimeUnit.MINUTES); diff --git a/src/main/java/club/joylink/rtss/services/auth/AuthenticateService.java b/src/main/java/club/joylink/rtss/services/auth/AuthenticateService.java index 579434736..00cb9194e 100644 --- a/src/main/java/club/joylink/rtss/services/auth/AuthenticateService.java +++ b/src/main/java/club/joylink/rtss/services/auth/AuthenticateService.java @@ -11,6 +11,7 @@ import club.joylink.rtss.exception.BusinessExceptionAssertEnum; import club.joylink.rtss.services.ISysUserService; import club.joylink.rtss.services.IWxApiService; import club.joylink.rtss.services.LoginSessionManager; +import club.joylink.rtss.services.WxApiService; import club.joylink.rtss.services.project.DeviceService; import club.joylink.rtss.services.simulation.ProjectSimulationService; import club.joylink.rtss.simulation.cbtc.ProjectJointSimulationService; @@ -106,15 +107,15 @@ public class AuthenticateService implements IAuthenticateService { LoginScanParam param = LoginScanParam.parse(state); LoginStatusVO loginStatusVo = getLoginStatus(param.getSessionId()); BusinessExceptionAssertEnum.LOGIN_EXPIRED.assertTrue(null != loginStatusVo && loginStatusVo.isWaiting()); - AccountVO accountVO = getOrCreateUserByWmcode(code); + AccountVO accountVO = getOrCreateUserByWmcode(WxApiService.MiniApp.JoyLink, code); loginStatusVo.setStatus(LoginStatusVO.ScanLoginStatus.SCAN); return accountVO; } @Override - public AccountVO getOrCreateUserByWmcode(String code) { - WmUserSession wmUserSession = this.iWxApiService.getWmUserSession(code); + public AccountVO getOrCreateUserByWmcode(WxApiService.MiniApp miniApp, String code) { + WmUserSession wmUserSession = this.iWxApiService.getWmUserSession(miniApp, code); AccountVO accountVO = null; if (!StringUtils.isEmpty(wmUserSession.getUnionid())) { accountVO = this.iSysUserService.findUserByUnionId(wmUserSession.getUnionid()); @@ -135,7 +136,19 @@ public class AuthenticateService implements IAuthenticateService { @Override 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 infoVOList = this.loginSessionManager.queryLoginInfoByUserId(accountVO.getId()); if (!CollectionUtils.isEmpty(infoVOList)) { @@ -152,7 +165,7 @@ public class AuthenticateService implements IAuthenticateService { Project.DEFAULT, null); String token = loginUserInfoVO.getToken(); this.loginSessionManager.addLoginUserInfo(loginUserInfoVO); - return token; + return loginUserInfoVO; } @Override @@ -202,7 +215,7 @@ public class AuthenticateService implements IAuthenticateService { synchronized (loginStatusVo) { 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()); if (StringUtils.isEmpty(loginStatusVo.getToken())) { //正常扫码登陆 // 构造登陆用户信息 diff --git a/src/main/java/club/joylink/rtss/services/auth/IAuthenticateService.java b/src/main/java/club/joylink/rtss/services/auth/IAuthenticateService.java index ce24e3b89..98a9456a7 100644 --- a/src/main/java/club/joylink/rtss/services/auth/IAuthenticateService.java +++ b/src/main/java/club/joylink/rtss/services/auth/IAuthenticateService.java @@ -1,6 +1,7 @@ package club.joylink.rtss.services.auth; import club.joylink.rtss.constants.Project; +import club.joylink.rtss.services.WxApiService; import club.joylink.rtss.vo.AccountVO; import club.joylink.rtss.vo.LoginUserInfoVO; import club.joylink.rtss.vo.client.LoginStatusVO; @@ -71,15 +72,22 @@ public interface IAuthenticateService { * @param code * @return */ - AccountVO getOrCreateUserByWmcode(String code); + AccountVO getOrCreateUserByWmcode(WxApiService.MiniApp miniApp, String code); /** - * + * 玖琏科技小程序code换token * @param code * @return */ String getTokenByWmCode(String code); + /** + * 到那儿了小程序code换token + * @param code + * @return + */ + String getTokenByWmCode2(String code); + boolean isTokenExpired(String token); /** diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index f4d83f470..2b7a9e366 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -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 sp-app-id: wxe9150dbbcbf9440b sp-app-secret: 4b5d453e5ec246a3f1b72360c59e4fab + sp-app2-id: wxecb0321367be529c + sp-app2-secret: 3c31cb41588f27a78160092249123766 wm-base-url: https://joylink.club/oss/joylink/%s?state=%s wx-module-url: http://localhost:9001 mini: