Thinkphp 微信网页授权登录

1,建一个vendor类代码如下

<?php

class Wxlogin {
    
    # 你自己的
    private $app_id     = '';
    
    # 也是你自己的
    private $app_secret = '';
 
    /**
     * # +========================================================================
     * # | - @name        获取微信授权链接
     * # | - @author     cq <just_leaf@foxmail.com> 
     * # | - @copyright zmtek 2018-11-07
     * # +------------------------------------------------------------------------
     * # | - 1.获取微信授权链接
     * # +========================================================================
     */
    public function get_authorize_url($redirect_uri = '', $state = ''){
        $redirect_uri = urlencode($redirect_uri);
        return "https://open.weixin.qq.com/connect/qrconnect?appid={$this->app_id}&redirect_uri={$redirect_uri}&response_type=code&scope=snsapi_login&state={$state}#wechat_redirect";
    }
    
    /**
     * # +========================================================================
     * # | - @name        获取授权token
     * # | - @author     cq <just_leaf@foxmail.com> 
     * # | - @copyright zmtek 2018-11-07
     * # +------------------------------------------------------------------------
     * # | - 1.通过get_authorize_url获取到的code
     * # +========================================================================
     */
    public function get_access_token($code = ''){
        $token_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$this->app_id}&secret={$this->app_secret}&code={$code}&grant_type=authorization_code";
        $token_data = $this->http($token_url);
        
        if($token_data[0] == 200){
            return json_decode($token_data[1], TRUE);
        }
        return FALSE;
    }
    
    /**
     * # +========================================================================
     * # | - @name        获取授权后的微信用户信息
     * # | - @author     cq <just_leaf@foxmail.com> 
     * # | - @copyright zmtek 2018-11-07
     * # +------------------------------------------------------------------------
     * # | - 1.获取授权后的微信用户信息
     * # +========================================================================
     */
    public function get_user_info($access_token = '', $open_id = ''){
        if($access_token && $open_id){
            $info_url = "https://api.weixin.qq.com/sns/userinfo?access_token={$access_token}&openid={$open_id}&lang=zh_CN";
            $info_data = $this->http($info_url);
            
            if($info_data[0] == 200){
                return json_decode($info_data[1], TRUE);
            }
        }
        return FALSE;
    }

    /**
     * # +========================================================================
     * # | - @name        验证授权
     * # | - @author     cq <just_leaf@foxmail.com> 
     * # | - @copyright zmtek 2018-11-07
     * # +------------------------------------------------------------------------
     * # | - 1.验证授权
     * # +========================================================================
     */
    public function check_access_token($access_token = '', $open_id = ''){
        if($access_token && $open_id){
            $info_url = "https://api.weixin.qq.com/sns/auth?access_token={$access_token}&openid={$open_id}&lang=zh_CN";
            $info_data = $this->http($info_url);
            
            if($info_data[0] == 200){
                return json_decode($info_data[1], TRUE);
            }
        }
        return FALSE;
    }
    
    /**
     * # +========================================================================
     * # | - @name        请求
     * # | - @author     cq <just_leaf@foxmail.com> 
     * # | - @copyright zmtek 2018-11-07
     * # +------------------------------------------------------------------------
     * # | - 1.请求
     * # +========================================================================
     */
    public function http($url, $method, $postfields = null, $headers = array()){
        $ci = curl_init();
        curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
        curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 30);
        curl_setopt($ci, CURLOPT_TIMEOUT, 30);
        curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);
        switch ($method) {
            case 'POST':
                curl_setopt($ci, CURLOPT_POST, true);
                if (!empty($postfields)) {
                    curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
                    $this->postdata = $postfields;
                }
                break;
        }
        curl_setopt($ci, CURLOPT_URL, $url);
        curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ci, CURLINFO_HEADER_OUT, true);
 
        $response = curl_exec($ci);
        $http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);
 
        curl_close($ci);
        return array($http_code, $response);
    }
}

html页面如下

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
          <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"/>
        <title>扫码登录</title>
        <script src="http://res.wx.qq.com/connect/zh_CN/htmledition/js/wxLogin.js" type="text/javascript" charset="utf-8"></script>
    </head>
    <body>
        <div id="wxqrcode"></div>
        
    </body>
    <script type="text/javascript">
        var obj = new WxLogin({
         self_redirect:false,
         id:"wxqrcode", 
         appid: "你的appid", 
         scope: "snsapi_login", 
         redirect_uri: "授权域名下的页面,扫码授权后会跳转",
         state: "随机加密MD5就行了",//自己存session
         style: "black或者white",
         // 加密之后的样式,要改自己解密去
         href: "data:text/css;base64,LmltcG93ZXJCb3ggLnFyY29kZSB7d2lkdGg6IDIwMHB4O30NCi5pbXBvd2VyQm94IC50aXRsZSB7ZGlzcGxheTogbm9uZTt9DQouaW1wb3dlckJveCAuaW5mbyB7d2lkdGg6IDIwMHB4O30NCi5zdGF0dXNfaWNvbiB7ZGlzcGxheTogbm9uZX0NCi5pbXBvd2VyQm94IC5zdGF0dXMge3RleHQtYWxpZ246IGNlbnRlcjt9"
         });
    </script>
</html>

你的redirect_uri所填下的页面如下,我以authorization为名

/**
     * # +========================================================================
     * # | - @name        授权处理
     * # | - @author     cq <just_leaf@foxmail.com> 
     * # | - @copyright zmtek 2018-11-07
     * # +------------------------------------------------------------------------
     * 
     * # +========================================================================
     */    
    public function authorization() {
        
        $code  = $_GET['code'];
        $state = $_GET['state'];
        
        if(!$code || !$state) die('参数不能为空');

        # 验证参数,防刷
        if($state != session('state')){
            die('错误state');
        }
Vendor('Wxlogin.wxlogin');
$Wx = new \Wxlogin(); # 确认授权后会,根据返回的code获取token $token = $Wx->get_access_token($_GET['code']); # 获取用户信息 $user_info = $Wx->get_user_info($token['access_token'],$token['openid']);      var_dump($user_info); }

还有第二种方法,自己去看文档。不懂再问吧 qq137121172 备注微信网页授权登录

转载于:https://www.cnblogs.com/leaf-cq/p/10278213.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值