From 6e3fb0fd9e30142dbf340ed44621e12c81aa9a70 Mon Sep 17 00:00:00 2001 From: Revers <0x00stone@gmail.com> Date: Wed, 27 Mar 2024 12:48:06 +0800 Subject: [PATCH] fix wechat login and share --- .../user/service/WeChatAouth2Service.java | 26 +++++++++++++++++-- luoo_user/src/main/resources/bootstrap.yml | 4 +-- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/luoo_user/src/main/java/com/luoo/user/service/WeChatAouth2Service.java b/luoo_user/src/main/java/com/luoo/user/service/WeChatAouth2Service.java index 7a0f504..6d15328 100644 --- a/luoo_user/src/main/java/com/luoo/user/service/WeChatAouth2Service.java +++ b/luoo_user/src/main/java/com/luoo/user/service/WeChatAouth2Service.java @@ -52,6 +52,12 @@ public class WeChatAouth2Service { @Value("${oauth2.weixin.secret}") private String secret; + @Value("${oauth2.weixin.appidShare}") + private String appid_share; + + @Value("${oauth2.weixin.secretShare}") + private String secret_share; + @Autowired private UserInfoService userInfoService; @@ -62,7 +68,7 @@ public class WeChatAouth2Service { private UserInfoDao userInfoDao; public Result getShareToken(WeChatShareReq weChatShareReq){ - Aouth2WeChatTokenResponse accessToken = getAccessToken(weChatShareReq.getCode()); + Aouth2WeChatTokenResponse accessToken = getAccessShareToken(weChatShareReq.getCode()); if(accessToken == null || accessToken.getAccess_token() == null ) return Result.failed("分享微信失败"); @@ -161,7 +167,23 @@ public class WeChatAouth2Service { url.append("&grant_type=authorization_code"); String string = new String(get(url.toString())); - log.info("refreshAccessToken:" + string); + log.info("getAccessToken:" + string); + Aouth2WeChatTokenResponse aouth2WeChatTokenResponse = JSON.parseObject(string, Aouth2WeChatTokenResponse.class); + saveTokenToRedis(aouth2WeChatTokenResponse); + + return aouth2WeChatTokenResponse; + } + + public Aouth2WeChatTokenResponse getAccessShareToken(String code){ + StringBuffer url = new StringBuffer(); + url.append(Constants.HTTPS_API_WEIXIN_QQ_COM_SNS_OAUTH2_ACCESS_TOKEN); + url.append("?appid=").append(urlEncode(appid_share)); + url.append("&secret=").append(urlEncode(secret_share)); + url.append("&code=").append(urlEncode(code)); + url.append("&grant_type=authorization_code"); + String string = new String(get(url.toString())); + + log.info("getAccessShareToken:" + string); Aouth2WeChatTokenResponse aouth2WeChatTokenResponse = JSON.parseObject(string, Aouth2WeChatTokenResponse.class); saveTokenToRedis(aouth2WeChatTokenResponse); diff --git a/luoo_user/src/main/resources/bootstrap.yml b/luoo_user/src/main/resources/bootstrap.yml index a2c06ec..2cc882b 100644 --- a/luoo_user/src/main/resources/bootstrap.yml +++ b/luoo_user/src/main/resources/bootstrap.yml @@ -12,6 +12,6 @@ spring: oauth2: weixin: appid: wxae6fb76efa147314 -# appid: wx667f580d1605650b secret: 741727f12d1b262ac855b905bf2e60e2 -# secret: 4bb42de98cb422d733ac7d50e7cade3b \ No newline at end of file + appidShare: wx667f580d1605650b + secretShare: 4bb42de98cb422d733ac7d50e7cade3b \ No newline at end of file