1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > 微信开放平台 微信登录-扫码登录

微信开放平台 微信登录-扫码登录

时间:2020-06-20 16:27:24

相关推荐

微信开放平台 微信登录-扫码登录

需求:后台需要添加微信扫码登录。

首先需要阅读官方文档,了解大致过程。介绍有两种微信扫码,一是跳转到微信网页生成的二维码进行扫码,二是把二维码内嵌到自己的网站页面。下面微信文档。

https://developers./doc/oplatform/Website_App/WeChat_Login/Wechat_Login.html

准备:

1.appid 开放平台查找

2.redirect_uri 回调链接 跟开放平台配置一致,使用时需要urlEncode处理。用php 处理直接用urlEncode,用JavaScript处理用urlEncode没反应的话就用,encodeURIComponent。

例一,跳转扫码

<?php$appid = "填写你的APPID";$redirect_uri = UrlEncode("/login.php");$data = time();$state = MD5($data);//跳转页面echo "<script>location.href=\"https://open./connect/qrconnect?appid=$appid&redirect_uri=$redirect_uri&response_type=code&scope=snsapi_login&state=$state#wechat_redirect\";</script>";?>

效果

情形二:把二维码内嵌到网页

<!-- 引入微信扫码登录js文件 --><script type="text/javascript" src="http://res./connect/zh_CN/htmledition/js/wxLogin.js"></script></head><body><!-- 放置二维码的div --><div id="login_container"></div></body><script type="text/javascript">var obj = new WxLogin({self_redirect:true,id:"login_container", //二维码容器idappid: "wxbdc5610cc59c1631", scope: "snsapi_login", redirect_uri: encodeURIComponent("http://"+window.location.host+"/..."), state: Math.ceil(Math.random()*1000), // style: "black",//黑底或者白底 可不填href: ""//样式,公网链接样式,可不填});</script>

效果:

回调处理:

public function callback(){$code = input('code');if($code){$WxLogin = new \WxLogin\WxLogin();//这个是我封装的获取用户信息的类$re = $WxLogin->getOpenid($code);/*//我回调处理主要是用unionid判断1.是否跟之前的用户表已经有了unionid,匹配进入后台2.是否已经绑定过其他账户 ,如果有重新进入扫码页面,提示换号绑定3.如果用户账号登录后 ,微信未绑定过,则绑定后进入页面……*/$UserModel = new \app\common\model\common\User();$MprUser = new \app\common\model\common\MprUser();$user_id = is_login();if($user_id>0){//账号登录后扫码//微信是否已经绑定过其他账号$isexit = $UserModel->where('type',0)->where('unionid',$re['unionid'])->where('status',1)->find();if($isexit){$this->error('此微信已经绑定过,请用另外的微信扫码绑定!','/wxLogin?time='.time());}else{//绑定微信session(md5('wxLogin_session'),1);$exit = $MprUser->where('openid',$re['unionid'])->find();if($exit){$UserModel->save(['mpr_user_id'=>$exit['mpr_user_id'],'unionid'=>$re['unionid']],['id'=>$user_id]);}else{$UserModel->save(['unionid'=>$re['unionid']],['id'=>$user_id]);}$this->success('绑定成功!',url('admin/index/index'));exit;}}else{//扫码登录//是否有用户信息$isexit = $UserModel->where('type',0)->where('unionid',$re['unionid'])->where('status',1)->find();if($isexit){$exit = $MprUser->where('openid',$re['unionid'])->find();if($exit){$UserModel->save(['mpr_user_id'=>$exit['mpr_user_id']],['unionid'=>$re['unionid']]);}$user = new \app\admin\logic\Session();$res = $user->wxLogin($isexit['id']);if($res['code']==1){session(md5('wxLogin_session'),1);$this->success($res['msg'],$res['url']);exit;}}else{//未有绑定微信的账号或者账号不可用,$this->error('未查询到此微信的账号或者账号不可用!','/wxLogin?time='.time());}}}else{return '欢迎光临!这是微信扫码进入的回调方法,亲是不是走错了~~哈哈哈!';}}

下面类似通过code 获取用户信息

<?phpnamespace WxLogin;use think\Db;/*** 微信登录类*/class WxLogin{private $appid = 'wxAPPID';//小程序appid,主要用appidprivate $secret = 'SECRET';//开放平台秘钥,用不到 public function getOpenid($code){//获取access_token和openid$url = "https://api./sns/oauth2/access_token?appid=$this->appid&secret=$this->secret&code=$code&grant_type=authorization_code";//发送请求$result = $this->post($url);//返回接口的数据$arr = json_decode($result,true);$data = $this->getUser($arr['access_token'],$arr['openid']);return $data;}function getUser($token,$openid){//获取用户信息需要openid 和 access_token//获取用户信息$getinfourl = "https://api./sns/userinfo?access_token=$token&openid=$openid";//发送请求获取用户信息$info_result = $this->post($getinfourl);//返回接口的数据// echo $info_result;$info_arr = json_decode($info_result,true);return $info_arr;}function post($url) {$ch = curl_init();curl_setopt($ch, CURLOPT_URL, $url);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);curl_setopt($ch, CURLOPT_POST, 1);curl_setopt($ch, CURLOPT_POSTFIELDS, $data);curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);$rst = curl_exec($ch);curl_close($ch);return $rst;}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。