二维码app扫码后登录其它形态系统

今天我们就介绍一下,如何使用微信应用内二维码扫描功能,扫描PC端网站  弹出二维码   实现PC端网站自动登录功能。

我们直入主题。

PC端微信二维码扫码登录实现思路

1. 首先要保证每次弹出的二维码是唯一的。例如我们可以在程序内部,通过session_id 与与弹出的二维码做一定关联。(即微信oauth授权网址 callback 中要带有当前PC端用户session_id,且callback URL应与 PC网站同域)---未登录何来当前用户?

2. 其次当展现二维码的同时,要运行一个PC端网页异步轮询xmlhttprequest (ajax ) ,定时轮询后台  断手机微信端是否有成功登录标记。(此标记是标记进入数据库还是其它存储介质?)

3. 当用户在微信扫码,并授权登录后,微信携带openid 信息跳转到 callback URL,此时callback URL 参数中带有PC端session_id ,根据session_id ,关联设置用户登录状态。(通过执行的call-back URL,URL根据二维码中的参数?才能知道当前扫码是什么场景?)

4. PC端异步轮询请求获得已登录消息,刷新\跳转PC端网页。

=====

php 微信扫码 pc端自动登陆注册 用的接口scope 是snsapi_userinfo,微信登陆一个是网页授权登陆,另一个是微信联合登陆

网页授权登陆:http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html

微信联合登陆:https://open.weixin.qq.com/cgi-bin/frame?t=home/web_tmpl&lang=zh_CN

一:首先把微信链接带个标识生成二维码

比如链接为 https://open.weixin.qq.com/connect/oauth2/authorize?appid=’.$appid.’&redirect_uri=’.$url.’&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect’  

我们可以在state上做文章,因为state你传入什么微信那边返回什么

可以作为服务器与微信 的一个标识


public function creatqrAction(){
 
if ( $_GET [ 'app' ]){
$wtoken = $_COOKIE [ 'wtoken' ];
$postdata = $_SESSION [ 'w_state' ];
if ( $wtoken ){
$postdata = $wtoken ;
}
<span style= "color: #3366ff;" > include CONFIG_PATH . 'phpqrcode/' . 'phpqrcode.php' ;</span>
$sh = $this- shar1();
$value = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx138697ef383a9167&amp;redirect_uri=http://www.xxx.net/login/wcallback&amp;response_type=code&amp;scope=snsapi_userinfo&amp;state=" . $postdata . "&amp;connect_redirect=1#wechat_redirect" ;
 
$errorCorrectionLevel = "L" ;
 
$matrixPointSize = "5" ;
微信扫码后得到URL,其本质是请求url 
QRcode::png( $value , false, $errorCorrectionLevel , $matrixPointSize );
}
 
}

这样我们设置了回调地址http://www.xxx.net/login/wcallback

就可以在wcallback方法里面处理数据 插入用户 生成session,跳转登陆,pc端可以设置几秒钟ajax请求服务器,一旦获取到了

state,即实现调整,微信浏览器里处理完后可以关闭窗口,微信js可实现

document.addEventListener( 'WeixinJSBridgeReady' , function onBridgeReady() {
WeixinJSBridge.call( 'closeWindow' );
 
}, false);


可以授权登陆成功后跳转到微信服务号关注页面::::::
header("Location: weixin://profile/gh_a5e1959f9a4e");

回调的页面获得如下信息,并解析处理::
$code = $_GET [ 'code' ];
$state = $_GET [ 'state' ];
$setting = include CONFIG_PATH . 'setting.php' ;
$appid = $setting [ 'weixin' ][ 'appid' ];
$appsecret = $setting [ 'weixin' ][ 'appsecret' ];
 
if ( empty ( $code )) $this ->showMessage( '授权失败' );
try {
 
$token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . $appid . '&secret=' . $appsecret . '&code=' . $code . '&grant_type=authorization_code' ;
 
$token = json_decode( $this ->https_request( $token_url ));
 
} catch (Exception $e )
{
print_r( $e );
}
 
if (isset( $token ->errcode)) {
echo '<h1>错误:</h1>' . $token ->errcode;
echo '<br/><h2>错误信息:</h2>' . $token ->errmsg;
exit ;
}
 
$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( $this ->https_request( $access_token_url ));
if (isset( $access_token ->errcode)) {
echo '<h1>错误:</h1>' . $access_token ->errcode;
echo '<br/><h2>错误信息:</h2>' . $access_token ->errmsg;
exit ;
}
$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( $this ->https_request( $user_info_url ));
if (isset( $user_info ->errcode)) {
echo '<h1>错误:</h1>' . $user_info ->errcode;
echo '<br/><h2>错误信息:</h2>' . $user_info ->errmsg;
exit ;
}
//打印用户信息
// echo '<pre>';
// print_r($user_info);
// echo '</pre>';

====》如果是自己开发的应用呢?扫码操作URL跳转,回调设置标记,PC登录成功提示?


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值