ecshop第三方登陆之微信登录

发布时间:2018-05-02 00:02:55编辑:丝画阁阅读(1494)

ecshop第三方登陆之微信登录

ecshop现成的第三方登陆是没有微信登录,最近添加了微信登录(示例:http://www.doute365.com/DTYHZX),注意:这个微信登录不像原来那种可以在后台设置key和AppSecret,程序写死的。

大概说一下步骤:

1.去微信开发平台申请App Key。

2.前台添加微信登录连接。在user_passport.dwt文件中添加XXXXXX&response_type=code&scope=snsapi_login&redirect_uri=http%3A%2F%2Fwww.doute365.com%2Fuser.php%3Fact%3Dweixin&state=XXXXXXXX"> 微信
;其中appid=XXXXXX把XXXXXX替换成你的appid,state=XXXXXXXX把XXXXXX替换成你的AppSecret,redirect_uri=http%3A%2F%2Fwww.doute365.com%2Fuser.php%3Fact%3Dweixin替换成你的回调地址(注意其中转码的url,不能直接写url)

3.回调程序中写对应的处理函数

if(isset($_REQUEST['code'])&&isset($_REQUEST['state'])&&$action == 'weixin'){
    include_once(ROOT_PATH . 'includes/website/jntoo.php');
   
    $code = $_GET['code'];
    $state = $_GET['state'];
    //xxxx修改成自己的appID和AppSecret

    $appid = 'XXXXX';
    $appsecret = 'XXXXXXX';

    if (empty($code))
    show_message('授权失败', '返回首页', '', 'wrong');
    
    $token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$appsecret.'&code='.$code.'&grant_type=authorization_code';
    $token = json_decode(file_get_contents($token_url));
    if (isset($token->errcode))
    {
        show_message($token->errmsg, '返回首页', '', 'wrong');
    }
    $access_token_url = 'https://api.weixin.qq.com/sns/oauth2/refresh_token?appid='.$appid.'&grant_type=refresh_token&refresh_token='.$token->refresh_token;

    $access_token = json_decode(file_get_contents($access_token_url));
    if (isset($access_token->errcode))
    {
        show_message($access_token->errmsg, '返回首页', '', 'wrong');
    }
    $user_info_url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token->access_token.'&openid='.$access_token->openid.'&lang=zh_CN';

    $user_info = json_decode(file_get_contents($user_info_url));
    if (isset($user_info->errcode)) {
        show_message($user_info->errmsg, '返回首页', '', 'wrong');
    }
    
    setcookie('user_info',$user_info);
    $info = $user_info;
    $type='weixin';
    $info_user_id = $type .'_'.$info->openid; //  加个标识!!!防止 其他的标识 一样  // 以后的ID 标识 将以这种形式 辨认
    $info->nickname= str_replace("'" , "" ,$info->nickname);
    
    
    $sql = 'SELECT user_name,password,aite_id FROM '.$ecs->table('users').' WHERE aite_id = \''.$info_user_id.'\' OR aite_id=\''.$info->openid.'\'';
    
    $count = $db->getRow($sql);
    $login_name = $info->nickname;
    if(!$count)   // 没有当前数据
    {
        if($user->check_user($info->nickname))  // 重名处理
        {
            $info->nickname = $info->nickname.'_'.$type.(rand()*1000);
        }
        $login_name = $info->nickname;
        $user_pass = $user->compile_password(array('password'=>$info->openid));
        $sql = 'INSERT INTO '.$ecs->table('users').'(user_name , password, aite_id , sex , reg_time , user_rank , is_validated) VALUES '.
                "('$info->nickname' , '$user_pass' , '$info_user_id' , '$info->sex' , '".gmtime()."' , '0' , '1')" ;
        $db->query($sql);
    }
    else
    {
        $login_name = $count['user_name'];
        $sql = '';
        if($count['aite_id'] == $info->openid)
        {
            $sql = 'UPDATE '.$ecs->table('users')." SET aite_id = '$info_user_id' WHERE aite_id = '$count[aite_id]'";
            $db->query($sql);
        }
    }
    
   
    $user->set_session($login_name);
    $user->set_cookie($login_name);
    update_user_info();
    
    $redirect_url =  "http://".$_SERVER["HTTP_HOST"].str_replace("user.php", "index.php", $_SERVER["REQUEST_URI"]);
    header('Location: '.$redirect_url);
}


返回参数详解:

参数
描述
openid 用户的唯一标识
nickname 用户昵称
sex 用户的性别,值为1时是男性,值为2时是女性,值为0时是未知
province 用户个人资料填写的省份
city 普通用户个人资料填写的城市
country 国家,如中国为CN
headimgurl 用户头像,最后一个数值代表正方形头像大小(有0、46、64、96、132数值可选,0代表640*640正方形头像),用户没有头像时该项为空
privilege 用户特权信息,json 数组,如微信沃卡用户为(chinaunicom)
unionid 只有在用户将公众号绑定到微信开放平台帐号后,才会出现该字段。详见:获取用户个人信息(UnionID机制)

个人分类: ecshop

关键字