Fan management is essential in daily life, and it is necessary to participate in a party or in daily life.
1. First create a fan module 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="">Please select a label</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>avatar</th> <th>nickname</th> <th>Following time</th> <th>Openid</th> <th>tag</th> <th>Action</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; //Various event-based operations, which will be further introduced below form.on('select(tag)', function(data){ // console.log(data.elem); //get the select original DOM object // console.log(data.value); //get the selected value // console.log(data.othis); //Get the beautified DOM object window.location.href="{:U(index)}"+"?tagid="+data.value; }); }); </script> </block>
2. Let it be displayed on the page first
//display page 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. Realize synchronous fans (in large events, there are many fans to follow)
//sync fans 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('Synchronization completed', U('index')); }
4. Generate a labeled QR code ( one official account can create up to 100 labels. )
interface
http请求方式:POST(请使用https协议)
https://api.weixin.qq.com/cgi-bin/tags/create?access_token=ACCESS_TOKEN
Returned numeric type
{ "tag" : { "name" : "广东"//标签名 } }
5. Create tags
//create label public function createTag($tagname=''){ $mp= $this->mp; // $tagname="May Day"; / / Query whether the database is automatically created if there is no $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. Get already created tags (tag followers)
//Give fans to tags public function OpenidTag(){ $openid="oZuNt1oSMWluxOthMFC7JKxAq6D0"; // tag fans $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. Hit the label to return
{ "tags":[{ "id":1, "name":"每天一罐可乐星人", "count":0 //此标签下粉丝数 },{ "id":2, "name":"星标组", "count":0 },{ "id":127, "name":"广东", "count":5 } ] }
8. Get the list of followers under the tag
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 'No such tag' . $tagname; } }
9. Assign tags to followers
public static function eventQrsceneSubscribe(&$request){ /* *Users scan the QR code with parameters for automatic grouping *Adding this code here is that most of the requirements are to automatically group users after scanning the QR code with parameters */ $scene_str = str_replace("qrscene_","",$request['eventkey']); //Move the user to the corresponding group, the $sceneid here depends on the parameters that were created before $ret = self::fansGroup($request['fromusername'],$scene_str); if($ret===true){ $content = 'You are a new fan, assigned to:' . $scene_str; }else{ $content = 'Failed,' .json_encode($ret,JSON_UNESCAPED_UNICODE); } return ResponsePassive::text($request['fromusername'],$request['tousername'],$content,$request['eventkey']); } /** * @descrpition scan the QR code (while already following) * @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 = 'Assigned to:' . $scene_str; }else{ $content = 'Failed,' .json_encode($ret,JSON_UNESCAPED_UNICODE); } }else{ $content='You have already followed'; } return ResponsePassive::text($request['fromusername'],$request['tousername'],$content,$request['eventkey']); }