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

需求:后台需要添加微信扫码登录。
首先需要阅读官方文档,了解大致过程。介绍有两种微信扫码,一是跳转到微信网页生成的二维码进行扫码,二是把二维码内嵌到自己的网站页面。下面微信文档。
https://developers.weixin.qq.com/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("http://www.baidu.com/login.php");
$data = time();
$state = MD5($data);
//跳转页面
echo "<script>location.href=\"https://open.weixin.qq.com/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.wx.qq.com/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", //二维码容器id
  appid: "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 获取用户信息

<?php
namespace WxLogin;

use think\Db;

/**
 * 微信登录类
 */

class WxLogin{
	private $appid = 'wxAPPID';//小程序appid,主要用appid
    private $secret = 'SECRET';//开放平台秘钥,用不到 
	
	public function getOpenid($code){
		//获取access_token和openid
		$url = "https://api.weixin.qq.com/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.weixin.qq.com/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;
	}
	
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值