thinkcmf5 微信扫码登录

 

1.前端:

<a href="{:url('user/login/wxlogin')}">微信扫码登录</a>

2.二维码页面

<!DOCTYPE html>
<html>
<head>
    <title>微信扫码登录</title>
    <meta name="keywords" content=""/>
    <meta name="description" content=""> 
</head>

<body style="text-align: center;">
	<span id="login_container"></span> 
	<script src="http://res.wx.qq.com/connect/zh_CN/htmledition/js/wxLogin.js"></script> 
	<script> 
		var obj = new WxLogin({ 
		  id: "login_container", 
		  appid: "{$config['appid']}", 
		  scope: "{$config['scope']}", 
		  redirect_uri: encodeURIComponent("{$config['redirect_uri']}"), 
		  state: Math.ceil(Math.random()*1000), 
		  style: "black", 
		  href: ""}); 

	</script> 
</body>
</html>

3.后端:

#微信登录
    public function wxlogin()
    {
        $config = cmf_get_plugin_config('WxLogin');
        $this->assign('config',$config);
        return $this->fetch();
    }
    public function wxredirect(){
        import('wxlogin.wxlogin');
        $Wechat = new \Wxlogin();
        $token = $Wechat->get_access_token($_GET['code']); //确认授权后会,根据返回的code获取token
        session('token',$token); //保存授权信息
        $user_info = $Wechat->get_user_info($token['access_token'],$token['openid']); //获取用户信息

        $user = Db::name('user')->where(['wx_open_id'=>$user_info['openid']])->find();
        if ($user) {
            session('user',$user);
             Db::name('user')->where('id',cmf_get_current_user_id())->update(['last_login_time'=>time(),'last_login_ip'=>get_client_ip(0, true)]);
            Db::name('user_login_log')->insert(['user_id'=>cmf_get_current_user_id(),'ip'=>get_client_ip(),'create_time'=>time(),'type'=>2]);
            $this->redirect('portal/index/index');
        }else{
            $data = [];
            $data['wx_open_id'] = $user_info['openid']; // 微信openid
            $data['login_type'] = 2;    // 用户帐号类型  2为微信
            $data['user_type'] = 2;
            $data['create_time'] = time();
            $data['user_nickname'] = $user_info['nickname'];      // 用户昵称
            $data['sex']= $user_info['sex'];    // 性别
            $data['avatar']  = $user_info['headimgurl']; // 用户头像
            $result = Db::name('user')
                    ->insertGetId($data);
            if(is_numeric($result)){
                $user = Db::name('user')->where('id',$result)->find();
                session('user',$user);
                 Db::name('user')->where('id',cmf_get_current_user_id())->update(['last_login_time'=>time(),'last_login_ip'=>get_client_ip(0, true)]);
                Db::name('user_login_log')->insert(['user_id'=>cmf_get_current_user_id(),'ip'=>get_client_ip(),'create_time'=>time(),'type'=>2]);
                $this->redirect('portal/index/index');
            }
        }
        
    }

4.配置参数:例如:array(
        "appid"=> "123456",
        "app_secret"=> "123456",
        "scope"=>  "snsapi_login",
        "redirect_uri"=>  "http://www.weixin/user/login/wxredirect"
    )

5.第三方类 放在simplewind/extend/wxlogin/wxlogin.php

<?php 
/**
 * 
 */
class Wxlogin 
{
	# 你自己的
    private $app_id;
    # 也是你自己的
    private $app_secret;
    public function __construct()
    {
        $config = cmf_get_plugin_config('WxLogin');
        $this->app_id = $config['appid'];
        $this->app_secret =  $config['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='POST', $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);
    }
}

?>

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值