微信开发网页授权登录——2018年6月2日

2018年06月02日 18:55:09阅读数:1030博客 / 沈斌的博客 / 微信公众号开发

微信网页授权登录

  1. 用户同意授权,获取code

  2. 通过code,获取网页的access_token

  3. 拉取用户信息(scope 为snsapi_userinfo)


application\index\controller\Weixin.php


实例

<?php
namespace app\index\controller;
use think\Controller;
use think\facade\Cache;
class Weixin extends Controller
{
    public function __construct(){
        parent::__construct();
        $this->model=model('Weixin');
    }
    public function index()
    {
        $valid=$this->model->valid();

        if(!$valid){
            exit('sigature error');
        }

        exit(input('get.echostr'));
    }

    public function get_access_token($iscache=true){
        $cache_key='access_token';
        if(!$iscache){
            Cache::rm($cache_key);
        }
        $data=Cache::get($cache_key);
        if($data && $iscache){
            return $data;
        }


        $appid=config('app.appid');
        $appsecret=config('app.APPSECRET');
        $url='https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$appsecret;
        $res=http_Get($url);
        // dump($res);
        $res=json_decode($res,true);
        // dump($res);
        if(!isset($res[$cache_key])){
            return false;
        }
        Cache::set($cache_key,$res['access_token'],($res['expires_in']-600));

        return $res[$cache_key];
    }

    public function custom_menu(){
        $access_token=$this->get_access_token();
        if(!$access_token){
            exit('access_token get failed');
        }
        $url='https://api.weixin.qq.com/cgi-bin/menu/create?access_token='.$access_token;
        $data = '{
                    "button":[
                        {
                            "type":"view",
                            "name":"Home",
                            "url":"http://m.php.cn/"
                        },
                        {
                            "type":"view",
                            "name":"视频教程2",
                            "url":"http://m.php.cn/course.html"
                        },
                        {
                            "name":"接口demo",
                            "sub_button":[
                                {
                                    "type":"view",
                                    "name":"用户信息",
                                    "url":"http://881c65b6.ngrok.io/index.php/index/weixin/auth"
                                },
                                {
                                   "type":"view",
                                   "name":"用户地理位置",
                                   "url":"http://tests.php.cn/index.php/weixintest/get_location"
                                }]
                        }]
                }';

        $res=http_Post($url,$data);
        dump($res);

    }

    public function auth(){
        // halt(config('app.APPID'));
        $appid=config('app.appid');
        $redirect='http://881c65b6.ngrok.io/index.php/index/weixin/userinfo';
        $url_code = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$appid.'&redirect_uri='.urlEncode($redirect)
        .'&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect';


        header('Location:'.$url_code);

    }

    public function userinfo(){
        // dump(config('app.APPID'));
        $code=input('get.code');
        // dump($code);
        $res=$this->model->auth_access_token($code);
        // dump($res);

        $auth_access_token=$res['access_token'];
        $openid=$res['openid'];
        // dump($auth_access_token);
        $userinfo=$this->model->get_userinfo($auth_access_token,$openid);
        dump($userinfo);

    }

    public function get_location(){

    }

}

运行实例 »

点击 "运行实例" 按钮查看在线实例

application\index\model\Weixin.php

实例

<?php
namespace app\index\model;
use think\Model;
use think\Db;
use think\facace\Cache;

class Weixin extends model
{
    public function valid()
    {
        $signature=input('get.signature');
        // $signature='c5052967c11f8d7e59c3b4011407b94c697b1ceb';
        // var_dump($signature);
        $timestamp=input('get.timestamp');
        // $timestamp='1527896867';

        $nonce=input('get.nonce');
        // $nonce='3010175004';

        $echostr=input('get.echostr');
        // $echostr='9044276551450221539';
        $token=config('app.token');

        file_put_contents('d://data.txt','signature='.$signature.' timestmp='.$timestamp.' nonce='.$nonce.' echostr='.$echostr);
        $tmpArr=array($timestamp,$nonce,$token);
        sort($tmpArr,SORT_STRING);

        // dump($tmpArr);
        $tmpStr=implode($tmpArr);
        // echo $tmpStr;
        // $tmpStr=sha1($tmpStr);
        //
        if(sha1($tmpStr) != $signature){
            // exit('error');
            return false;
        }
        // 首次接入成功需要输出$echostr
        // exit($echostr);

        return true;
    }
    //网页授权access_tonken
    public function auth_access_token($code){
        $appid=config('app.appid');
        $appsecret=config('app.APPSECRET');
        $url='https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.
        $appsecret.'&code='.$code.'&grant_type=authorization_code';

        $res=http_Get($url);
        $res=json_decode($res,true);

        if(!isset($res['access_token'])){
            return false;
        }

        return $res;
    }

    public function get_userinfo($auth_access_token,$openid){
        $url='https://api.weixin.qq.com/sns/userinfo?access_token='.
            $auth_access_token.'&openid='.
            $openid.'&lang=zh_CN';
        $res=http_Get($url);
        $res=json_decode($res,true);
        return $res;
    }


}

运行实例 »

点击 "运行实例" 按钮查看在线实例

点击用户信息,会打印出$userinfo

批改状态:未批改

老师批语:

版权申明:本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!

全部评论

文明上网理性发言,请遵守新闻评论服务协议

条评论
  • 博主信息
    沈斌的博客
    博文
    56
    粉丝
    3
    评论
    1
    访问量
    29043
    积分:0
    P豆:417