pc网站微信扫面二维码登陆

微信

15-10-12 17:23:59

    在进行微信OAuth2.在进行微信OAuth2.0授权登录接入之前,在微信开放平台注册开发者帐号,并拥有一个已审核通过的网站应用,并获得相应的AppID和AppSecret,申请微信登录且通过审核后,可开始接入流程。


AppID:wxfb3**84**ea9e0

AppSecret:**


1. 第三方发起微信授权登录请求,微信用户允许授权第三方应用后,微信会拉起应用或重定向到第三方网站,并且带上授权临时票据code参数;
2. 通过code参数加上AppID和AppSecret等,通过API换取access_token;
3. 通过access_token进行接口调用,获取用户基本数据资源或帮助用户实现基本操作。


链接地址:

https://open.weixin.qq.com/connect/qrconnect?appid=wxfb3b1849dea9e0&redirect_uri=http://xxx.xxx.cn/100.php&response_type=code&scope=snsapi_login&state=STATE#wechat_redirect

---------------------------------------------------------------------------

100.php

define('IN_ECS', true);
require(dirname(__FILE__) . '/includes/init.php');
require(dirname(__FILE__) . '/mobile/wxi.php');
//var_dump($_GET);  //array(2) { ["code"]=> string(32) "011ab40901f95beaf916bd4e0e95eb9Q" ["state"]=> string(5) "STATE" } 
$code=isset($_GET['code'])?$_GET['code']:'';
if(empty($code)){ die('你已经拒绝授权.'); }
//AppID:wxfb3b1849dea9e0
//AppSecret:b7be214b3dce5f5481ed2a1bed69d6
//通过code 获取access_token 和openid
$rs=curl_file_get_contents("https://api.weixin.qq.com/sns/oauth2/access_token?appid=wxfb3b1849dea9e0&secret=b7be214b3dce5f5481ed2a1bed69d6&code=$code&grant_type=authorization_code");
$data=json_decode($rs);
//print_r($rs);
//{"access_token":"OezXcEiiBSKSxW0eoylIeKT5F6RtkWhKCdgEtCveun5HLgQOyJ1Lr1AyoK8-ROIj8ovWWQilzy4xQdB9k95e61XYDqPhAIIEGFBU-drt_hALo33pJ8pKxGN-cVWVA0-sh-hWTBaq2pvHEFUUF2nR7g","expires_in":7200,"refresh_token":"OezXcEiiBSKSxW0eoylIeKT5F6RtkWhKCdgEtCveun5HLgQOyJ1Lr1AyoK8-ROIj4m6LX4j_UrRZy4RzGdkz9xzl3je0vM8y_0GARmGQeQPbdBkGWrUfu6yXJqKHxJIrNuoWMBQkpzRw8rzT1LlZnQ","openid":"oKzYJs-zbVuQ78w3ujfx65EgPFQU","scope":"snsapi_login","unionid":"o5WuXjnj6vIa3DCw-8_ymSGATBg8"}
//通过access_token和openid  获取 用户详细信息 
$rs=curl_file_get_contents("https://api.weixin.qq.com/sns/userinfo?access_token=".$data->access_token."&openid=".$data->openid);
$rs=json_decode($rs);
//print_r($rs);
//{"openid":"oKzYJs-zbVuQ78w3ujfx65EgPFQU","nickname":"盛","sex":1,"language":"zh_CN","city":"Liaoyang","province":"Liaoning","country":"CN","headimgurl":"http:\/\/wx.qlogo.cn\/mmopen\/O6rMopK5WTcB88GuPr2PKzNRGstrwm0ZfvEK0zF4ibCz7guwiaPTbNxUb6dFnX7mSA1BM1TTjQiaianAkl1YOfLYbrKYTwQGht1k\/0","privilege":[],"unionid":"o5WuXjnj6vIa3DCw-8_ymSGATBg8"}

$openid=$rs->openid;
$nickname=$rs->nickname;
$sex=$rs->sex;
$language=$rs->language;
$city=$rs->city;
$province=$rs->province;
$headimgurl=$rs->headimgurl;
$privilege=serialize($rs->privilege);
$unionid=$rs->unionid;
$datetime=date("Y-n-j G:i:s");

$rs = $GLOBALS['db']->getRow("select * from esc_wx_oid where unionid='$unionid'");
if($rs['id']){  //如果有id  模拟登录
//echo '我又回来了';
$rs = $GLOBALS['db']->getRow("select user_id,user_name from ecs_users where user_id='$rs[uid]'");
$_SESSION['user_id'] = $rs['user_id'];
$_SESSION['user_name'] = $rs['user_name'];
update_user_info();
}else{ //没有id 模拟注册 入库
//echo '首次登陆';
$user_pass=rand(100000,999999);
$name = 'wx_'.rand(100,999).'_'.rand(1000,9999);
$email=$info['name'].'@nongpingou.cn';
$rank_id=0;
$sql = "INSERT INTO ecs_users (alias,aite_id,email,user_name , password,  sex , reg_time , user_rank , is_validated,pay_points ,rank_points,froms ) VALUES  ('','','$email','$name' , '$user_pass' , '0'  , '".gmtime()."' , '$rank_id' , '1',500,500,'wx_pc')" ;
$GLOBALS['db']->query($sql);
$nid=$GLOBALS['db']->insert_id();
$sql = "INSERT INTO esc_wx_oid (uid,addsj,openid,nickname,sex,language,city,province,country,headimgurl,privilege,unionid ) VALUES ('$nid' , '$datetime','$openid','$nickname','$sex','$language','$city','$province','$country','$headimgurl','$privilege','$unionid')" ;
$GLOBALS['db']->query($sql);
$_SESSION['user_id'] = $nid;
    $_SESSION['user_name'] = $name;
    update_user_info();
}

ecs_header("Location: user.php");