标签:def stat 参数 targe define get href test connect
有关微信网页授权
let wechat = { getCode:function(appids){ /** * 获取微信code */ let appid = appids; let href = window.location.href; let redirect_uri = encodeURI(href.split("#")[0]); redirect_uri = redirect_uri.replace(/&/g, ‘%26‘); window.location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appid + "&redirect_uri=" + redirect_uri+"&response_type=code&scope=snsapi_userinfo&state=abcdefghigklmnopqrstuvwxyz#wechat_redirect"; }, getUrlStr : function(name){ /** * 获取地址栏参数 * * */ let reg = new RegExp("(^|\\?|&)" + name + "=([^&]*)(\\s|&|$)","i"); if(reg.test(window.location.href)){ return unescape(RegExp.$2.replace(/\+/g," ")) } return undefined } } export default wechat;
第一步:用户同意授权,获取code
第二步:通过code换取网页授权access_token
后台处理,前端只需把code传个后台就行了,然后后台可以根据code获取微信的oppenid,需要注意的是code是有时效的
标签:def stat 参数 targe define get href test connect
原文地址:http://www.cnblogs.com/DivHao/p/7712398.html