在日常生活中粉丝管理是必不可少的,在参加一场晚会或者日常生活中都需要用的到.
1.先创建一个粉丝的模块 Friends
<extend name="Common:base" /> <block name="body"> <div style="padding: 15px;"> <div class="content-hd"> <a href="{:U(sycfriends)}" class="layui-btn layui-btn-normal layui-btn-sm">同步粉丝</a> </div> <div class="layui-col-md3" style="float:right;margin-right:120px;margin-top:-30px"> <form class="layui-form"> <div class="layui-form-item"> <select name="tag" lay-filter="tag"> <option value="">请选择标签</option> <volist name="tag" id="row"> <option value="{$row.tag_id}">{$row.tag}({$row.tag_id})</option> </volist> </select> </div> </form> </div> <table class="layui-table" lay-skin="line"> <col width="120"> <col width="200"> <col width="150"> <col width="100" > <thead> <tr> <th>头像</th> <th>昵称</th> <th>关注时间</th> <th>Openid</th> <th>标签</th> <th>操作</th> </tr> </thead> <tbody> <volist name="data" id="vo"> <tr> <td> <div style="padding: 5px; border: #e6e6e6 solid 1px; width:120px; float: left; "> <img class="form_logo" src="{$vo.headimgurl}" style="max-width: none" width="120" height="110"> </div> </td> <td>{$vo.nickname}</td> <td>{$vo.subscribe_time|date="Y-m-d",###}</td> <td>{$vo.openid}</td> <td>{$vo.tagid_list}</td> <td> <a class="rha-bt-a" href="javascript:;" onclick="editReply('{$vo.id}')">修改</a> <if condition="$vo.status=='1'"> <a class="rha-bt-a" href="javascript:;" onclick="updateReply('{$vo.id}','0')">停用</a> <else/> <a class="rha-bt-a" href="javascript:;" onclick="updateReply('{$vo.id}','1')">开启</a> </if> <a class="rha-bt-a" href="javascript:;" onclick="delReply('{$vo.id}')">删除</a> </td> </tr> </volist> </table> <script> layui.use('form', function(){ var form = layui.form; //各种基于事件的操作,下面会有进一步介绍 form.on('select(tag)', function(data){ // console.log(data.elem); //得到select原始DOM对象 // console.log(data.value); //得到被选中的值 // console.log(data.othis); //得到美化后的DOM对象 window.location.href="{:U(index)}"+"?tagid="+data.value; }); }); </script> </block>
2.先让到显示到页面上
//显示页面 public function index($tagid=''){ $mp = $this->mp; $where['mp_id'] = $mp['id']; if (!empty($tagid)) { $where['tagid_list'] = array('like',"%{$tagid}%"); } $data = M('mp_friends')->where($where)->field('id,headimgurl,openid,nickname,subscribe_time,tagid_list')->order()->select(); $tag = M('tags')->where($where)->select(); $this->assign('tag',$tag); $this->assign('data',$data); $this->display(); }
3.实现同步粉丝(在大型活动中,有很多粉丝进行关注)
//同步粉丝 public function sycfriends(){ $mp = $this->mp; $where['mp_id'] = $mp['id']; M('mp_friends')->where($where)->delete(); include APP_PATH . 'LaneWeChat/lanewechat.php'; $ret = UserManage::getFansList(); // print_r($ret); $openids = $ret['data']['openid']; print_r($data); $arr = array(); foreach ($openids as $value) { $row = array(); $row['openid'] = $value; $row['lang'] = 'zh-CN'; $arr[] = $row; } $ret = UserManage::getManyUserInfo($arr); // print_r($ret); if (isset($ret['user_info_list'])) { $data = $ret['user_info_list']; foreach ($data as &$value) { $value['mp_id'] = $mp['id']; $value['tagid_list'] = implode(',',$value['tagid_list']); } M('mp_friends')->addAll($data); } $this->success('同步完成',U('index')); }
4.生成带标签的二维码(一个公众号,最多可以创建100个标签。)
接口
http请求方式:POST(请使用https协议)
https://api.weixin.qq.com/cgi-bin/tags/create?access_token=ACCESS_TOKEN
返回的数值类型
{ "tag" : { "name" : "广东"//标签名 } }
5.创建标签
//创建标签 public function createTag($tagname=''){ $mp= $this->mp; // $tagname="五一"; //查询数据库 是否有 没有的话自动创建 $where['mp_id']=$mp['id']; $where['tag']=$tagname; $data=M("tags")->where($where)->find(); // dump($data); // exit; if (empty($data)) { $api="https://api.weixin.qq.com/cgi-bin/tags/create?access_token=".getAccess_token(); $arr=array(); $arr['tag']['name']=$tagname; $json=json_encode($arr,JSON_UNESCAPED_UNICODE); // echo $json; // exit; include APP_PATH .'LaneWeChat/lanewechat.php'; $ret=\LaneWeChat\Core\Curl::callWebServer($api,$json,'POST'); if ($ret['tag']) { $row['mp_id']=$mp['id']; $row['tag_id']=$ret['tag']['id']; $row['tag']=$ret['tag']['name']; M('tags')->add($row); } } }
6.获取已经创建的标签(给粉丝打上标签)
//给粉丝到标签 public function OpenidTag(){ $openid="oZuNt1oSMWluxOthMFC7JKxAq6D0"; //给粉丝打标签 $api="https://api.weixin.qq.com/cgi-bin/tags/members/batchtagging?access_token=".getAccess_token(); $arr['openid_list']=array($openid); $arr['tagid']=101; $json=json_encode($arr); // echo $json; // exit; include APP_PATH .'LaneWeChat/lanewechat.php'; $ret=\LaneWeChat\Core\Curl::callWebServer($api,$json,'POST'); print_r($ret); }
7.打标签返回
{ "tags":[{ "id":1, "name":"每天一罐可乐星人", "count":0 //此标签下粉丝数 },{ "id":2, "name":"星标组", "count":0 },{ "id":127, "name":"广东", "count":5 } ] }
8.获取标签下的粉丝列表
private static function fansGroup($openid,$tagname){ include APP_PATH . 'LaneWeChat/lanewechat.php'; $id = $_GET['id']; $where['mpid']=$id; $where['tag'] = $tagname; $data = M("tags")->where($where)->find(); if(!empty($data)){ $api = "https://api.weixin.qq.com/cgi-bin/tags/members/batchtagging?access_token=".getAccess_token(); $arr = array(); $arr = ['openid_list'=>array($openid),'tagid'=>(int)$data['tag_id']]; $json = json_encode($arr); //dump($json); $ret=\LaneWeChat\Core\Curl::callWebServer($api,$json,'POST'); if(isset($ret['errcode']) && $ret['errcode']==0){ return true; }else{ return $ret; } }else{ return '无此标签' . $tagname; } }
9.给粉丝分配标签
public static function eventQrsceneSubscribe(&$request){ /* *用户扫描带参数二维码进行自动分组 *此处添加此代码是大多数需求是在扫描完带参数二维码之后对用户自动分组 */ $scene_str = str_replace("qrscene_","",$request['eventkey']); //移动用户到相应分组中去,此处的$sceneid依赖于之前创建时带的参数 $ret = self::fansGroup($request['fromusername'],$scene_str); if($ret===true){ $content = '您是新粉丝,分配到:' . $scene_str; }else{ $content = '失败了,' .json_encode($ret,JSON_UNESCAPED_UNICODE); } return ResponsePassive::text($request['fromusername'],$request['tousername'],$content,$request['eventkey']); } /** * @descrpition 扫描二维码(已关注时) * @param $request * @return array */ public static function eventScan(&$request){ if(isset($request['eventkey']) && isset($request['ticket'])){ $scene_str = $request['eventkey']; $ret = self::fansGroup($request['fromusername'],$scene_str); if($ret===true){ $content = '分配到:' . $scene_str; }else{ $content = '失败了,' .json_encode($ret,JSON_UNESCAPED_UNICODE); } }else{ $content='您已经关注了哦'; } return ResponsePassive::text($request['fromusername'],$request['tousername'],$content,$request['eventkey']); }