1. 首先在控制器下创建相应的方法,其中包括粉丝的显示,粉丝的同步。
<?php
namespace Home\Controller;
use Think\Controller;
use LaneWeChat\Core\UserManage;
use LaneWeChat\Core\Curl;
class FriendController extends Controller
{
//显示粉丝
public function index(){
$model = M('mp_friends');
$data = $model->distinct ('openid')->select();
$this->assign('data',$data);
$this->display();
}
//获取粉丝信息
public function getfriend(){
$accessToken = getAccess_token();
$url="https://api.weixin.qq.com/cgi-bin/user/get?access_token=$accessToken";
include APP_PATH . 'LaneWeChat/lanewechat.php';
$ret = Curl::callWebServer($url,'','GET');
$total = $ret['total'];
$count = $ret['count'];
for ($i=0;$i<$count;$i++) {
$openid = $ret['data']['openid'][$i];
$data[] = UserManage::getUserInfo($openid);
}
foreach ($data as $k => $v) {
$map['subscribe']=$v['subscribe'];
$map['openid']=$v['openid'];
$map['nickname']=$v['nickname'];
$map['sex']=$v['sex'];
$map['language']=$v['language'];
$map['city']=$v['city'];
$map['province']=$v['province'];
$map['country']=$v['country'];
$map['headimgurl']=$v['headimgurl'];
$map['subscribe_time']=$v['subscribe_time'];
$map['remark']=$v['remark'];
$map['groupid']=$v['groupid'];
$map['subscribe_scene']=$v['subscribe_scene'];
$map['qr_scene']=$v['qr_scene'];
$mp=getCurrentMp();
$map['mp_id']=$mp['id'];
$con['openid']=$map['openid'];
$data=M('mp_friends')->where($con)->find();
if($data){
$data=M('mp_friends')->select();
$this->assign('data',$data);
$this->display('index');
exit;
}else{
M('mp_friends')->add($map);
}
}
$this->display('index');
}
}
?>
UserManage中的getUserInfo方法如下:
<?php
/**
* @descrpition 创建分组
* @param $groupName 组名 UTF-8
* @return JSON {"group": {"id": 107,"name": "test"}}
*/
public static function createGroup($groupName){
//获取ACCESS_TOKEN
// $accessToken = AccessToken::getAccessToken();
$accessToken = getAccess_token();
$queryUrl = 'https://api.weixin.qq.com/cgi-bin/groups/create?access_token='.$accessToken;
$data = '{"group":{"name":"'.$groupName.'"}}';
return Curl::callWebServer($queryUrl, $data, 'POST');
}
?>
2. 粉丝的数据需要存到数据库friends,数据库内容如下
3. 有了相应的方法和数据库,只需要将数据显示在相应的模板(html)即可。