首页 > 微信 > 详细

微信网页授权

时间:2017-10-23 00:18:47      阅读:358      评论:0      收藏:0      [点我收藏+]
有关微信网页授权

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是有时效的


微信网页授权

原文:http://www.cnblogs.com/DivHao/p/7712398.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!