我现在正在开发三方网站绑定微信的功能,客户端请求后,生成绑定的链接,这是服务端代码;
@Override
@NoCheck
public Response getQRCodeUrl(String appId,String userAction) throws UnsupportedEncodingException {
List<AppMap> appMaps = sysAppMapService.getAppMapByAppId("bind".equals(userAction)? RequestContext.getAppId() :appId, AccountType.WECHAT);
if (CollectionUtils.isEmpty(appMaps) || appMaps.size() > 1) {
throw GlobalException.PARAM_VERIFY_EXCEPTION;
}
String referer = request.getHeader("Referer");
String redirectUrl = appMaps.get(0).getLoginSuccessRedirectUrl();
if (!HttpUtil.verifyRefer(referer, redirectUrl)) {
throw GlobalException.PARAM_VERIFY_EXCEPTION;
}
wxMpService.switchoverTo(appMaps.get(0).getThirdAppId());
if("bind".equals(userAction)){
String url = wxMpService.getOAuth2Service()
.buildAuthorizationUrl(appMaps.get(0).getLoginRedirectUrl(),"snsapi_userinfo", URLEncoder.encode("bind_wechat", "UTF-8"));
return new Response(url);
}else{
String url = wxMpService.buildQrConnectUrl(appMaps.get(0).getLoginRedirectUrl(), "snsapi_login", MD5Utils.getRandomCode());
return new Response(url);
}
}
为什么生成的链接提示需要在手机上打开?我需要让用户扫描生成的二维码。这是生成的链接:
这个是公众号的网页授权,只能在微信中打开。
你想要的应该是网站应用的扫码登录,参考下文档:https://developers.weixin.qq.com/doc/oplatform/Website_App/WeChat_Login/Wechat_Login.html 。