微信开放平台 微信登录-扫码登录[通俗易懂]

微信开放平台 微信登录-扫码登录[通俗易懂]需求:后台需要添加微信扫码登录,要求能跟小程序用户绑定,同步用户信息。

大家好,又见面了,我是你们的朋友全栈君。

需求:后台需要添加微信扫码登录。
首先需要阅读官方文档,了解大致过程。介绍有两种微信扫码,一是跳转到微信网页生成的二维码进行扫码,二是把二维码内嵌到自己的网站页面。下面微信文档。
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;
	}
	
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/146074.html原文链接:https://javaforall.net

(0)
上一篇 2022年5月15日 下午9:40
下一篇 2022年5月15日 下午10:00


相关推荐

  • 【转载】How browsers work–Behind the scenes of modern web browsers (前端必读)

    【转载】How browsers work–Behind the scenes of modern web browsers (前端必读)

    2021年11月18日
    41
  • 看板娘制作

    看板娘制作https www cnblogs com yjlblog p 8724881 html utm source gold browser extension 转载于 https www cnblogs com 1129 tangqiyuan p 9838291 html

    2026年3月18日
    2
  • 手机来电通核心模块——归属地数据库设计(Winsym原创)「建议收藏」

    手机来电通核心模块——归属地数据库设计(Winsym原创)「建议收藏」说到Symbian,确实让人头痛。不仅开发平台和SDK版本众多,难以选择,而且对程序员确实要求很高,光是SymbianC++的熟悉就要花上很长时间,更麻烦的是测试和调试。模拟器只能提供一部分功能,和电话通信有关的全部要在真机上测试。很多时候,在模拟器上能跑的代码,放到真机上就不行了,这其中的心酸想必开发过得朋友深有体会。小弟我因为工程实践项目的要求,和几位嵌入式的高手一起搞了Symbian来电通项目。其实来电通项目已经有很多人做了,比较有名的是CallMaster和柳丁,但是这方面的关键技术和源码至今没有

    2022年7月22日
    19
  • 计算机win7卡顿如何解决方法,win7卡顿严重解决方法_win7运行卡顿严重最流畅设置方法-win7之家…[通俗易懂]

    计算机win7卡顿如何解决方法,win7卡顿严重解决方法_win7运行卡顿严重最流畅设置方法-win7之家…[通俗易懂]在使用win7系统电脑的时间一长,出现的电脑故障也就会越多,这大多数都是用户自己所造成的,例如有用户的win7系统在运行过程中总是会出现严重卡顿的情况,这让许多用户都感到很难受,那么win7卡顿严重怎么解决呢?下面小编就来告诉大家win7运行卡顿严重最流畅设置方法。具体方法:1、右击【计算机】选择属性;2、在出现的面板的左侧栏选择【高级系统设置】;3、依次点击【高级】【设置】;4、默认是【让系统选…

    2025年11月1日
    4
  • Ruby On Rails 4 hello world,Ruby On Rails上手

    Ruby On Rails 4 hello world,Ruby On Rails上手

    2021年11月29日
    47
  • linux中nmap命令,Linux中nmap命令起什么作用呢?

    linux中nmap命令,Linux中nmap命令起什么作用呢?摘要:下文讲述Linux中nmap的功能说明,如下所示;nmap是一个网络探测和安全审核的工具,它目前是开放源代码模式nmap命令功能:用于网络探测工具和安全和端口扫描器它可以快速扫描大型网络它运用原始的ip报文的方式发现网络上的主机nmap命令的语法格式:nmap[参数]—–常用参数说明——–traceroute:扫描主机端口并跟踪路由-p:扫描指定端口和端口范围-sP:对目标…

    2022年5月28日
    45

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号