使用标签的原因是为了用个性化菜单,但是发现有时候就可以,有时候就24小时,48小时过去了还是没有变。根据微信的说明公众号每5分钟就会获取一次用户信息是否有更新,但是这个又是为什么呢
用户标签单独弄了一个类UserTag,框架是ThinkPHP 5.0.10
<?php
namespace app\common;
use Fuwuhao\API;
class UserTag {
private $tagId,
$accessToken,
$tagName,
$errmsg = NULL;
function __construct(string $accessToken){
$this->accessToken = $accessToken;
}
/**
* 提供 tag id
*
* @param $tagid
*
* @return $this
*/
public
function id($tagid){
$this->tagId = $tagid;
return $this;
}
/**
* 提供 tag name
*
* @param string $tagname
*
* @return $this
*/
public
function name(string $tagname){
$this->tagName = $tagname;
return $this;
}
/**
* 添加标签, 结合name()
*/
public
function add(){
$url = appendParamter(API::$CREATE_USER_TAG, 'access_token', $this->accessToken);
$rst = json_decode(http_post($url, [
'tag' => [
'name' => $this->tagName,
],
])['content'], TRUE);
# 判断是否添加成功
if(array_key_exists('errcode', $rst) && $rst['errcode'] != 0) # 出错了
{
return FALSE;
}
# 添加到数据表
$user_tag = new WxUserTag();
$user_tag->id = $rst['tag']['id'];
$user_tag->name = $rst['tag']['name'];
$user_tag->comment = ["T" . time() => 'init'];
$user_tag->save();
return TRUE;
}
/**
* 删除tag, 结合id()
* @return bool
*/
public
function delete(){
$url = create_url(API::$DELETE_USER_TAG, ['access_token' => $this->accessToken]);
$rst = json_decode(http_post($url, [
'tag' => ['id' => $this->tagId],
])['content'], TRUE);
if($rst['errcode'] != 0){
return FALSE;
}
# 删除数据表中item
WxUserTag::destroy($this->tagId, TRUE);
return TRUE;
}
/**
* 更新名字,, 结合id()和name()
*/
public
function update(){
$url = appendParamter(API::$UPDATE_USER_TAG, 'access_token', $this->accessToken);
$rst = json_decode(http_post($url, [
'tag' => ['id' => $this->tagId, 'name' => $this->tagName],
])['content'], TRUE);
if($rst['errcode'] != 0){
return FALSE;
}
# 更新数据表
$user_tag = WxUserTag::get($this->tagId);
$user_tag->name = $this->tagName;
$user_tag->save();
}
/**
* 获取所有的tag
* @return bool
*/
public
function list($where = 'local'){
$url = appendParamter(API::$FETCH_USER_TAG, 'access_token', $this->accessToken);
$rst = json_decode(http_get($url)['content'], TRUE);
if(array_key_exists('errcode', $rst) && $rst['errcode'] != 0){
return FALSE;
}
# 更新数据库
$new = [];
foreach($rst['tags'] as $tag){
array_push($new, ['id' => $tag['id'], 'name' => codepoint_decode($tag['name']), 'count' => $tag['count']]);
if(WxUserTag::get($tag['id']) != NULL){
$wut = WxUserTag::get($tag['id']);
}else{
$wut = new WxUserTag();// 没有get到
$wut->id = $tag['id'];
}
$wut->name = codepoint_decode($tag['name']); // 是\uXXXX格式的
$wut->comment = ['T' . time() => 'list'];
$wut->save();
}
return $new;
}
/**
* 获取某个tag下的所有用户的openid, 结合id()
*/
public
function list_followers(){
$url = appendParamter(API::$PULL_USERS_BY_TAG, 'access_token', $this->accessToken);
$rst = json_decode(http_post($url, [
'tagid' => $this->tagId,
'next_openid' => '',
])['content'], TRUE);
# 存入数据表
$user_tag = WxUserTag::get($this->tagId);
$user_tag->followers = $rst['data']['openid'];
$user_tag->comment = ['T' . time() => 'openid_list'];
$user_tag - save();
}
/**
* 添某个加用户到某个tag下,结合id()
* 无法使用,必须至少2个openid
*
* @param $openid string
*
* @return bool
*/
public
function tagUser(string $openid){
$openid_list = [$openid, 'oAzFZwYFsunZFrJqd0fHY7phDFAs']; // 李锐小号
// 这里必须{1}至少两个{2}有效的{3}可重复性的{4}openid
// 所以凑一对儿
# 刷新用户 tagid_list
// $user = new Follower($openid);
// $user->freshTagList(); # 2017-06-28
return $this->_batchTagUsers($openid_list);
}
/**
* 批量 tag 用户
*
* @param $openid_list
*
* @return bool
*/
private
function _batchTagUsers($openid_list){
return $this->_batchTagOrUntagUsers($openid_list, API::$BATCH_TAG_USER);
# 更新数据库
# 不用更新了吧,之后直接list_openid()就可以刷新了
}
/**
* 批量处理tag分组下的用户,加入和拽出
*
* @param $openid_list
* @param $does
*
* @return bool
*/
private
function _batchTagOrUntagUsers(array $openid_list, $does){
$url = appendParamter($does, 'access_token', $this->accessToken);
$rst = json_decode(http_post($url, [
'openid_list' => $openid_list,
'tagid' => $this->tagId,
])['content'], TRUE);
if($rst['errcode'] != 0){
return $rst;
return FALSE;
}
return TRUE;
}
/**
* 添加多个用户到某个tag下,结合id()
*
* @param $openid_list array
*
* @return bool
*/
public
function tagUsers($openid_list){
return $this->_batchTagUsers($openid_list);
}
/**
* 从tag分组中去掉某个用户
*
* @param $openid
*/
public
function untagUser($openid){
$openid_list = [$openid, 'oAzFZwYFsunZFrJqd0fHY7phDFAs'];
// 这里必须{1}至少两个{2}有效的{3}可重复性的{4}openid
return $this->_batchUntagUsers($openid_list);
}
/**
* 批量 untag 用户
*
* @param $openid_list
*
* @return bool
*/
private
function _batchUntagUsers($openid_list){
return $this->_batchTagOrUntagUsers($openid_list, API::$BATCH_UNTAG_USER);
}
/**
* 从tag分组中去掉多个用户
*
* @param $openid_list
*/
public
function untagUsers($openid_list){
return $this->_batchUntagUsers($openid_list);
}
public
function getTagIdListOfUser($openid){
$url = appendParamter(API::$PULL_TAGS_BY_USER, 'access_token', $this->accessToken);
$rst = json_decode(http_post($url, [
'openid' => $openid,
])['content'], TRUE);
if(array_key_exists('errcode', $rst) && $rst['errcode'] != 0)
return FALSE;
return $rst;
}
public function getError(){
return $this->errmsg;
}
}
完了回答刚开始的问题了,
因为,
.
.
.
.
.
.
.
.
微信开发者页面有提示,只不过没有注意看
个性化菜单接口支持用户标签,请开发者注意,当用户身上的标签超过1个时,以最后打上的标签为匹配
在个性化菜单接口说明中有