js使用ajax获取微信openid

2018-07-24 16:15:00
1147533288
转贴:
csdn
7243
摘要:登录页面增加js,异步获取openid存放到cookie中;

1.登录界面改造,引入getwxopenid.js,文件内容如下:


function addcookie(name,value,expireHours){
    var cookieString=name+"="+escape(value)+"; path=/";
    //判断是否设置过期时间
    if(expireHours>0){
        var date=new Date();
        date.setTime(date.getTime+expireHours*3600*1000);
        cookieString=cookieString+"; expire="+date.toGMTString();
    }
    document.cookie=cookieString;
}

function getcookie(name){
    var strcookie=document.cookie;
    var arrcookie=strcookie.split("; ");
    for(var i=0;i<arrcookie.length;i++){
        var arr=arrcookie[i].split("=");
        if(arr[0]==name)return decodeURIComponent(arr[1]); //增加对特殊字符的解析
    }
    return "";
}

function delCookie(name) {//删除cookie
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval = getcookie(name);
    if (cval != null) document.cookie = name + "=" + cval + "; path=/;expires=" + exp.toGMTString();
}
function GetQueryString(name) {
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if(r!=null)return  unescape(r[2]); return null;
}
$(function () {
    var wxid=getcookie('wxid');
    var fromurl;
    if (wxid==''){
        var access_code=GetQueryString('code');
        if (access_code==null){
            fromurl=location.href;
            var url='https://open.weixin.qq.com/connect/oauth2/authorize?appid=你自己的appid&redirect_uri='+encodeURIComponent(fromurl)+'&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect';
            location.href=url;
        }else{
            $.ajax({
                type:'get',
                url:'index.php?c=login&a=getOpenidByCode',
                async:false,
                cache:false,
                data:{code:access_code},
                dataType:'json',
                success:function(result){
                    debugger;
                    if (result!=null && result.hasOwnProperty('openid') && result.openid!=""){
                        addcookie('wxid',result.openid,86400);
                        // alert(result.openid);
                        //getlogininfo(result.openid);
                    }
                    else
                    {
                        alert('微信身份识别失败 \n '+result);
                        //location.href=fromurl;
                    }
                }
            });
        }
    }
});
2.后台php代码,根据code换微信openid,返回页面:
function getOpenidByCode_action(){
        if($_GET['code']){
            //获取微信用户openid
            require_once APP_PATH."wxsdk/example/"."WxPay.JsApiPay.php";
            $tools = new JsApiPay();
            $openId = $tools->GetOpenid();
            if($openId){
                echo json_encode(array('openid'=>$openId));
            }
        }
    }

注意WxPay.JsApiPay.php来自微信支付jsApi对应官方demo;

3.登录成功时,从cookie中读取wxid写入session,用于其他页面如微信支付等。

文章参考https://blog.csdn.net/baronyang/article/details/44489841实现。

文章分类
联系我
联系人: meepo
电话: *****
Email: 1147533288@qq.com
QQ: 1147533288