微信接口开发之高级篇系列【用户分组接口和生成带参数的二维码】

<?php
/**
 * Created by PhpStorm.
 * User: Tinywan
 * Date: 2016/9/11
 * Time: 10:00
 */

namespace Home\Controller;

use Org\Util\WechatAuth;
use Think\Controller;

class UserController extends Controller
{
    private $appid;
    private $appSecret;
    private $WechatAuth = "";//初始化WechatAuth类
    private $accsess_token = "";//缓存token

    public function __construct()
    {
        parent::__construct();
        $this->appid = C('WECHAT_OAUTH2.APPID');
        $this->appSecret = C('WECHAT_OAUTH2.APPSECRET');
        // 缓存起来
        if (!session('token')) {
            $this->WechatAuth = new WechatAuth($this->appid, $this->appSecret);//初始化WechatAuth类
            $WechatAuth = $this->WechatAuth;
            $token = $WechatAuth->getAccessToken();
            session(array('expire' => $token['expires_in']));//设置过期时间
            session('token', $token['accsess_token']);//缓存token
            $this->accsess_token = $token;
        } else {
            $token = session('token');
            $this->WechatAuth = new WechatAuth($this->appid, $this->appSecret, $token);//初始化WechatAuth类
            $this->accsess_token = $token;//缓存token
        }

    }

    public function test()
    {
        echo 'selectGroupsGet';
    }


    //获取所有用户组
    public function selectGroupsGet()
    {
        header('Content-type:text/html;charset=utf-8;');
        $WechatAuth = $this->WechatAuth;
        $data = $WechatAuth->groupsGet();
        var_dump($data);
    }

    //创建一个分组
    public function createGroup()
    {
        header('Content-type:text/html;charset=utf-8;');
        $WechatAuth = $this->WechatAuth;
        $data = $WechatAuth->groupsCreate('万少波分组');
        var_dump($data);
    }

    //查询一下指定用户的分组
    public function GroupId()
    {
        $m = M('users');
        $user = $m->find(3);
        $openid = $user['openid'];
        $WechatAuth = $this->WechatAuth;
        $data = $WechatAuth->groupsGetid($openid);
        var_dump($data);
    }
}

 带参数的二维码:

 // 生成带参数的二维码
    public function createCode()
    {
        $WechatAuth = $this->WechatAuth;
        $ticket = $WechatAuth->qrcodeCreate("10086"); //返回临时票据ticket
        $src = $WechatAuth->showqrcode($ticket['ticket']); //根据ticket获取二维码URL
        echo "<img width='40%' src='{$src}'>";

    }

 

上一篇:ThinkPHP 3.2 vendor()方法的深入研究及Phpqrcode的正确扩展


下一篇:事件节流和防抖