1.根目录下的/Application/Home/Controller文件夹中的UserController.class.php文件添加创建分组的方法,书写代码为:
~~~
<?php
namespace Home\Controller;
use Think\Controller;
use Com\Wechat;
use Com\WechatAuth;
class UserController extends Controller
{
private $appid="wx165112bf167af76c";
private $appSecret="c65b22bfcf03fdd98504eac299701b03";
private $WechatAuth="";//初始化WechatAuth类
private $access_token="";//缓存token
public function __construct(){
parent::__construct();//可能内部已经有这个构造方法了,因此加上这个
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['access_token']);//缓存token
$this->access_token=$token;
}else{
$token=session('token');
$this->WechatAuth=new WechatAuth($this->appid,$this->appSecret,$token);//初始化WechatAuth类
$this->access_token=$token;//缓存token
}
}
//获取所有用户组
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);
}
}
~~~
2.对比上节添加或修改代码部分
![](https://box.kancloud.cn/1165690ad2dbcfd13a08769fd0f4470a_598x343.png)
3.在微信web开发者工具上测试结果为
![](https://box.kancloud.cn/42d9b597f2dcb1ff3891a728ece3bd93_884x360.png)