ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
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); } //查询指定用户的分组 public function GroupId(){ header('Content-type:text/html;charset=utf-8;'); $m=M('users'); $user=$m->find(1);//查询users表中1d为1的数据 $openid=$user['openid']; $WechatAuth=$this->WechatAuth; $data=$WechatAuth->groupsGetid($openid); var_dump($data); } } ~~~ 2.对比上节添加或修改代码部分 ![](https://box.kancloud.cn/9f67c863eb6bce5a0294c0dadfa31f1e_566x310.png) 3.在微信web开发者工具上测试结果为 ![](https://box.kancloud.cn/6cd8056705f42e2cac4b8860f5534d21_872x302.png) 4.本节源码下载(下载密码:2eee) [源码下载](https://pan.baidu.com/s/1cMUNzYzlL8-MR196vpgofg)