参考文档
基础配置
代码测试说明测试公众号配置你本地的ip
完整的代码在文字末尾
下载微信的开发者工具
公众号AppId以及AppSecret的申请,并可以获取到token
注意
回调地址需要和js安全域名配置为同一个
授权地址必须在微信执行(本地在微信的开发工具进行执行)
授权地址需要进行urlEncode编码
步骤如下
授权地址参考(静默授权)
https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxbb0f4798d5d78b80&redirect_uri=http://10.168.1.110:8080/crm/view/wechat/common_init.html?repairType=outside&response_type=code&scope=snsapi_base&state=123&connect_redirect=1#wechat_redirect
https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxbb0f4798d5d78b80&redirect_uri=http%3A%2F%2F10.168.1.110%3A8080%2Fcrm%2Fview%2Fwechat%2Fcommon_init.html%3FrepairType%3Doutside&response_type=code&scope=snsapi_base&state=123&connect_redirect=1#wechat_redirect
根据code获取用户的openId
前端代码
var wechatCode = getUrlParam('code');
if(null==wechatCode){
return false;
}
var data="wechatCode="+wechatCode;
$.ajax({
url: "../../wechat/sendCode",
type: "POST",
data: data,
dataType: "json",
success: function(result) {
console.log(result.code);
if(result.code == "success") {
debugger;
openId = result.map.openId;
localStorage.setItem("openId", result.map.openId);
location.href="repair_outside.html";
} else {
common.comPopGo(result.msg);
$('.container_text').fadeOut();
}
},
error: function() {
common.comPopGo('系统错误!');
$('.container_text').fadeOut();
}
})
后端代码文章来源:https://www.toymoban.com/news/detail-635735.html
public static String WeChatOpenIdUrl="https://api.weixin.qq.com/sns/oauth2/access_token?grant_type=authorization_code&appid="+WeChatAppId+"&secret="+WeChatAppSecret+"&code=";
public String getOpenId(String wechatCode) {
String openId = null;
if (null == Config.WeChatOpenIdUrl) {
this.getTokenTieckt();
}
HttpGet get = new HttpGet(Config.WeChatOpenIdUrl + wechatCode);
String res = HttpClientUtil.get(get, "");
logger.info("====>getOpenId resulet 是 " + "\n" + res);
if (StringUtils.isNotBlank(res)) {
JSONObject json = JSONObject.fromObject(res);
Object openIdObj = json.get("openid");
if (null != openIdObj) {
openId = openIdObj.toString();
}
}
return openId;
};
代码地址文章来源地址https://www.toymoban.com/news/detail-635735.html
到了这里,关于微信-网页授权的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!