1.获取用户code
@Autowired
private RedisTemplate<String, String> redisTemplate;
/**
* 用户同意授权,获取code
*
* @return java.lang.String
* @date 2022/2/15
**/
@Override
public void wxAuthorizeUrl(String redirectUri, HttpServletResponse response) {
String urlString = "";
String url = "";
try{
urlString = URLEncoder.encode(payConfig.getRedirectUri(), "utf-8");
url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+payConfig.getAppId()+"&redirect_uri="+urlString+"&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
response.sendRedirect(url);
//放入缓存
String regMobileKey = RedisKeyConstants.REDIRECT_KEY;
final BoundValueOperations<String, String> regKey = redisTemplate.boundValueOps(regMobileKey);
regKey.set(redirectUri.replace("~","#"), 5, TimeUnit.MINUTES);
}catch (Exception e){
log.error("用户同意授权 异常:",e);
throw new ServiceException("用户同意授权 异常:"+e.toString());
}
}
2.创建实体
@Data
public class WxAuthorizeDto {
@JSONField(name = "access_token")
private String accessToken;
@JSONField(name = "expires_in")
private String expiresIn;
@JSONField(name = "refresh_token")
private String refreshToken;
@JSONField(name = "openid")
private String openId;
@JSONField(name = "scope")
private String scope;
@JSONField(name = "errcode")
private String errCode;
@JSONField(name = "errmsg")
private String errMsg;
}
3.通过code获取openId
/**
* 微信授权获取openId;
* @param code :
*
* @date 2022/2/15
**/
@Override
public WxAuthorizeDto wxAuthorizeOpenId(String code,HttpServletResponse response) {
try {
//通过code换取网页授权access_token
String result = HttpUtil.get("https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + payConfig.getAppId() + "&secret=" + payConfig
.getSecret() + "&code=" + code + "&grant_type=authorization_code");
if (StringUtils.isEmpty(result)) {
throw new ServiceException(PayEnum.WX_AUTHORIZE_IS_NULL.getValue(),
PayEnum.WX_AUTHORIZE_IS_NULL.getDescription());
}
WxAuthorizeDto wxAuthorizeDto = JSONObject.parseObject(result, WxAuthorizeDto.class);
if (StringUtils.isNotEmpty(wxAuthorizeDto.getErrCode())) {
throw new ServiceException(Integer.parseInt(wxAuthorizeDto.getErrCode()), wxAuthorizeDto.getErrMsg());
}
//拉取用户信息
String resultUser = HttpUtil.get("https://api.weixin.qq.com/sns/userinfo?access_token="+ wxAuthorizeDto.getAccessToken() +"&openid="+ wxAuthorizeDto.getOpenId() +"&lang=zh_CN");
WxUserInfoDto wxUserInfoDto = JSONObject.parseObject(resultUser, WxUserInfoDto.class);
if (StringUtils.isNotEmpty(wxUserInfoDto.getErrCode())) {
throw new ServiceException(Integer.parseInt(wxUserInfoDto.getErrCode()), wxUserInfoDto.getErrMsg());
}
//获取缓存
final BoundValueOperations<String, String> regKey = redisTemplate.boundValueOps(RedisKeyConstants.REDIRECT_KEY);
if(ObjectUtil.isEmpty(regKey)){
return null;
}
//如果回调url需要带参数需要这样处理
String url = regKey.get();
if(url.contains("?")){
url = url + "&openId="+wxAuthorizeDto.getOpenId();
}else {
url = url + "?openId="+wxAuthorizeDto.getOpenId();
}
response.sendRedirect(url);
return wxAuthorizeDto;
}catch (Exception e){
log.error("微信授权获取openId 异常:",e);
throw new ServiceException("微信授权获取openId 异常:"+e.toString());
}
}