openId获取修改

This commit is contained in:
张松 2025-03-07 18:17:16 +08:00
parent db7cf1767c
commit ae0a0e5145
2 changed files with 20 additions and 1 deletions

View File

@ -40,7 +40,7 @@ public class UserAuthorizationServiceImpl implements UserAuthorizationService {
log.info(code);
String openId;
if (UserAuthSourceEnum.WECHAT.getValue().equals(source)) {
openId = wechatAuthUtil.getSessionKeyOrOpenId(code, true);
openId = wechatAuthUtil.getAccountOpenId(code);
}else {
openId = alipayUtil.getOpenId(code, true);
}

View File

@ -47,6 +47,25 @@ public class WechatAuthUtil {
}
public String getAccountOpenId(String code) {
String requestUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?";
Map<String, Object> requestUrlParam = new HashMap<>();
// https://mp.weixin.qq.com/wxopen/devprofile?action=get_profile&token=164113089&lang=zh_CN
//小程序appId
requestUrlParam.put("appid", accountAppId);
//小程序secret
requestUrlParam.put("secret", accountSecrete);
//小程序端返回的code
requestUrlParam.put("code", code);
//默认参数
requestUrlParam.put("grant_type", "authorization_code");
log.info("微信获取openid请求报文{}", requestUrlParam);
//发送post请求读取调用微信接口获取openid用户唯一标识
String resp = HttpUtil.post(requestUrl, requestUrlParam);
log.info("响应报文{}", resp);
return JSONObject.parseObject(resp).getString("openid");
}
public String getSessionKeyOrOpenId(String code, boolean isAccount) {
String requestUrl = "https://api.weixin.qq.com/sns/jscode2session";
Map<String, Object> requestUrlParam = new HashMap<>();