diff --git a/play-admin/src/main/java/com/starry/admin/modules/weichat/service/WxMpApi.java b/play-admin/src/main/java/com/starry/admin/modules/weichat/service/WxMpApi.java index f0d9dc4..80ef20a 100644 --- a/play-admin/src/main/java/com/starry/admin/modules/weichat/service/WxMpApi.java +++ b/play-admin/src/main/java/com/starry/admin/modules/weichat/service/WxMpApi.java @@ -40,12 +40,7 @@ public class WxMpApi { } public String getAccessToken(String appId, String secret, boolean forceUpdate) { - WxMpMapConfigImpl config = new WxMpMapConfigImpl(); - config.setAppId(appId); - config.setSecret(secret); - config.setUseStableAccessToken(true); - mpService.addConfigStorage(appId, config); - WxMpService wxMpService = mpService.switchoverTo(appId); + WxMpService wxMpService = this.proxyWxMpService(appId, secret); try { return wxMpService.getAccessToken(forceUpdate); } catch (WxErrorException e) { @@ -53,4 +48,62 @@ public class WxMpApi { throw new RuntimeException(e); } } + + private WxMpService proxyWxMpService(String appId, String secret) { + WxMpMapConfigImpl config = new WxMpMapConfigImpl(); + config.setAppId(appId); + config.setSecret(secret); + config.setUseStableAccessToken(true); + mpService.addConfigStorage(appId, config); + return mpService.switchoverTo(appId); + } + + public String getOauth2Url(String url) { + if (StringUtils.isEmpty(url)) { + throw new RuntimeException("oauth url不能为空"); + } + return mpService.getOAuth2Service().buildAuthorizationUrl(url, "snsapi_userinfo", "mystate"); + } + + //public String oauth2Callback(String code, String appId) { + // if (StringUtils.isEmpty(code)) { + // throw new RuntimeException("授权码不能为空"); + // } + // synchronized (code.intern()) { + // String openId; + // String key = "mpOauth2code:" + code; + // Object cacheObject = redisCache.getCacheObject(key); + // WxOAuth2UserInfo userInfo = null; + // if (Objects.nonNull(cacheObject)) { + // openId = cacheObject.toString(); + // } else { + // // 通过code换取accessToken + // WxOAuth2AccessToken accessTokenResult; + // try { + // accessTokenResult = mpService.getOAuth2Service().getAccessToken(code); + // userInfo = mpService.getOAuth2Service().getUserInfo(accessTokenResult, null); + // } catch (WxErrorException e) { + // throw new RuntimeException(e); + // } + // openId = accessTokenResult.getOpenId(); + // } + // // 返回token 和 userInfo + // SysUser user = userMapper.selectOne(Wrappers.lambdaQuery(SysUser.class).eq(SysUser::getOpenid, openId).eq(SysUser::getDelFlag, "0")); + // if (Objects.isNull(user)) { + // user = new SysUser(); + // user.setOpenid(openId); + // user.setUserName("mp" + RandomUtil.randomString(6)); + // user.setNickName(userInfo.getNickname()); + // user.setAvartar(userInfo.getHeadImgUrl()); + // user.setAvartar(userInfo.getSex()); + // user.setPassword(SecurityUtils.encryptPassword("123456")); + // userMapper.insert(user); + // } + // // 制作token + // LoginUser loginUser = new LoginUser(user.getUserId(), user.getDeptId(), user, new LinkedHashSet<>()); + // String token = tokenService.createToken(loginUser); + // redisCache.setCacheObject(key, openId, 1, TimeUnit.HOURS); + // return Constants.TOKEN_PREFIX + token; + // } + //} }