tp5微信开发(三)----thinkphp5接入微信登陆

版权声明: https://blog.csdn.net/qq_27987023/article/details/83044201

数据库说明:因为是集成的tp5,所以公众号相关信息是通过后台管理的,该代码中的setup表即为配置信息表,其中

appid:公众号appid;

appsecret:公众号秘钥;

back_url:回调地址,由于我接收code是在getUserOpentId方法里,所以我这个back_url 值为:http://域名/home/index/getUserOpentId 【开启伪静态情况下可以把入口文件省略】;

↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓流程风格线↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

微信企鹅文档说的比较复杂,简化流程来说就是:

1,公众平台配置回调域名,则通过微信访问域名会先跳转到微信 oauth2 接口,访问成功微信会返回一个用户code给回调地址

请求地址:$url ="https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appid."&redirect_uri=".$redirect_uri."&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";

2,根据返回的code获取access_token和用户唯一标识openid 【此处一个大大的注意:微信总共会涉及到两个access_token,很多人在第一次接触的时候总是会搞混,一个access_token是有7200秒有效期,该token是通过公众号 appid 和 秘钥获取到的,注意该参数获取接口是有调用限制,一般日限2000,该access_token是整个微信接口调用的凭证,包括模板消息推送,微信jsapi分享接口封装等,都是以该access_token为主要参数;另外一个就是通过授权登陆获取到的access_token,这个token没有调用次数限制;另外补充知识点:同一个用户openid在对同一个微信公众号是唯一的,不管是第几次授权登陆,生成的openid都是唯一并相同,所以无需担心用户多次授权会重新生成一个openid,这个知识点是用以判断该用户以前是否授权登陆过非常有用(可存入数据库)】

请求地址:$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$secret."&code=".$code."&grant_type=authorization_code";

3,通过获取到的access_token 和 openid 即可获取到用户信息

【划重点:微信有两种登陆方式,一种是需要用户点击授权登陆,一种是静默授权。顾名思义,静默授权意思即用户无感知,不需要用户点击授权,但是该方式获取到的信息有限,仅能获取到用户openid,其他诸如昵称,头像,城市是无法获取到的,这种一般仅作为用户统计,此处以用户点击授权登陆为demo】

请求地址:$urltoc = "https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$getopenid."&lang=zh_CN";

↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓实操代码分割线↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

<?php
namespace app\home\controller;
use think\Controller;
use think\Db;
use think\Session;
use think\Request;

class Index extends Controller{
     public function index(){
        // 访问域名会优先执行index方法,用以获取到code
        $dbres = Db::name("setup")->find();
        $appid = $dbres['appid'];
        $redirect_uri = urlencode($dbres['back_url']);
        $url ="https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appid."&redirect_uri=".$redirect_uri."&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
        header("Location:".$url);exit;
    }

    public function http_curl($url,$type='get',$res='json',$arr=''){
        //1.初始化curl
        $ch = curl_init();
        //2.设置curl的参数
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //不验证证书
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //不验证证书
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        if ($type == 'post') {
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $arr);
        }
        //3.采集
        $output = curl_exec($ch);
        //4.关闭
        curl_close($ch);
        if ($res == 'json') {
            return json_decode($output,true);
        }
    }

    public function getUserOpentId(){
        //回调地址会传回一个code,则我们根据code去获取openid和授权获取到的access_token
        $code = $_GET['code'];
        $dbres = Db::name("setup")->find();
        $appid = $dbres['appid'];
        $secret = $dbres['appsecret'];

        $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$secret."&code=".$code."&grant_type=authorization_code";

        $res = $this->http_curl($url);
        $access_token = $res['access_token'];

        $getopenid = $res['openid'];
        //获取用户授权信息
        $urltoc = "https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$getopenid."&lang=zh_CN";
        $resinfos = $this->http_curl($urltoc);
        $openid = $resinfos['openid'];
        $check_member = Db::name("member")->where('openid',$openid)->find();
        if(empty($check_member)){
            //首次进入,则获取用户信息,插入数据库
            $resinfo['openid'] = $openid;
            $insert_data = [
                'openid' => $openid,
                'create_time' => time()
            ];
            Db::name("member")->insert($insert_data);
            $userId = Db::name('member')->getLastInsID();
            Session::set('wx_member_info', $resinfo);
            $this->redirect('home/index/index_html');
        }else{
            //说明是已经是公众号成员,则调用用户信息存到session即可
            $wx_member_info = Db::name('member')->where("openid",$openid)->find();
            Session::set('wx_member_info', $wx_member_info);
            $this->redirect('home/index/index_html');
        }
    }

    public function index_html(){
        if(!Session::has("wx_member_info")){
            action("home/index/index");
        }else{
            return $this->fetch('index');
        }
    }

}

代码复制粘贴应该就可以直接弹出授权框并顺利跳入网站正式页面 index_html

如果出现异常,请检查:

1,微信公众号授权获取用户信息域名是否已提交成功

2,网站是否开启了伪静态,检测方法:在index随机echo一个字符串,并exit掉,通过域名访问该地址看是否能正常显示

猜你喜欢

转载自blog.csdn.net/qq_27987023/article/details/83044201