微信公众号二维码

时间:2022-11-16 21:38:24

 微信生成带参数的时效二维码

常用微信api

//配置文件

//other.php 

'wechat' => [
    'param' => [
        'appid'         => 'wx98cfd0a933247acb',
        'secret'         => 'e997d5b1ab94ef639333158caf9e1ee6',
    ],
    'api'   => [
        'token'         => 'https://api.weixin.qq.com/cgi-bin/token', //获取access_token
        'user'          => 'https://api.weixin.qq.com/cgi-bin/user/info', //获取用户信息
        'qrcode'        => 'https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=', //获取ticket
        'ticket'        => 'https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=',    //获取微信图片
        'template_send'    =>'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=',//【POST】 推送模板信息
        'get_openid'     =>'https://api.weixin.qq.com/cgi-bin/user/get?',  //获取关注微信号的粉丝openid
        'template_id'    =>'https://api.weixin.qq.com/cgi-bin/template/api_add_template?', //获得模板ID【POST】
    ],
    //网页授权登录
    'oauth' => [
        'url_code'         => 'https://open.weixin.qq.com/connect/oauth2/authorize?',   //获取网页授权code码
        'redirect_url'     => 'http://zhangwei.s1.natapp.cc/wechat/Oauth_login',          //回调地址
        'token_url'        => 'https://api.weixin.qq.com/sns/oauth2/access_token',     //获取token(不同于服务器和token)
        'refresh_token_url'=> 'https://api.weixin.qq.com/sns/oauth2/refresh_token',//获取刷新token
        'user_info_url'    => 'https://api.weixin.qq.com/sns/userinfo',           //拉取用户的微信信息
        'url_verify'       =>'https://api.weixin.qq.com/sns/auth',                //授权用户信息验证
    ],
],
****************************************************************************************************

use Curl\Curl;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Redis;
use App\Models\Wxbind;
//生成公众号二维码
public function remind_qrcode()
    {
        $param = Config::get('other.wechat');//
        $params_token = [
            'grant_type' => 'client_credential',
            'appid'      => $param['param']['appid'],
            'secret'     => $param['param']['secret']
        ];
        $key = 'pay:access_token';
        $token = Redis::get($key);
        $curl = new Curl();
    // access_token缓存处理,防止反复刷新,获取达到上限值
        if(!$tokens){
            $url = $param['api']['token'];
            $token = $curl->post($url, $params_token);
            !$token->access_token && $this->errorOutput('NO_ACCESS_TOKEN');
            $tokens = $token->access_token;
            $key = 'pay:access_token';
            Redis::setex($key,4800,$tokens);
        }
        $params_ticket = ['expire_seconds' => 7200, 'action_name' => 'QR_SCENE',];
       // $params_ticket['action_info']['scene']['scene_id'] =rand(100000,999999);  
        $url = $param['api']['qrcode'] . $tokens;
        $ticket = $curl->post($url, json_encode($params_ticket)); //根据access_token获取ticket
        $ticket = $ticket->ticket;
        $this->set_code_redis($ticket);                 //此步骤可以在用户扫码关注公众号时进行绑定账号绑定,
        if (!$ticket) {
            $this->errorOutput('NO_TICKET');
        }
        $url = $param['api']['ticket'] . urlencode($ticket);
        $qrcode = $curl->get($url, []) ? $url : '';
        return response()->json(['url' => $qrcode, 'expire_time' => 4800]);
    }


private function set_code_redis($ticket){
    $session = new Session;
    $info=$session->get('userinfo');
    $key = 'wechat'.':'.'ticket'.$ticket;
    Redis::setex($key,300,$info);
}