1. 程式人生 > >微信網頁授權

微信網頁授權

cde 註意 connect encode res str abc 有關 href

有關微信網頁授權

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


微信網頁授權