新版微擎2.x新增粉丝消息回复打标签功能
首先在ims_rule表中添加一个字段
ALTER TABLE ims_rule ADD tags VARCHAR(255) NOT NULL COMMENT "粉丝标签";
=====================================================================================================================
修改文件: /web/source/platform/reply.ctrl.php
在
$module['title'] = '关键字自动回复';
下方新增
//dcr 191121 获取公众号标签列表
$tags = mc_fans_groups(true);
在
if (!empty($reply['keywords'])) {
foreach ($reply['keywords'] as &$keyword) {
$keyword = array_elements(array('type', 'content'), $keyword);
}
unset($keyword);
}
下方新增
$reply['tags'] = explode(',',$reply['tags']); //dcr 20191120 粉丝tags
在
if (empty($containtype) && in_array($m, $sysmods) && $m != 'userapi') {
itoast('必须填写有效的回复内容!');
}
下方新增
//dcr163 191120 新增粉丝tag
$tags = '';
if( !empty($_GPC['tags']) ){
if( count($_GPC['tags']) > 20 ){
itoast('为用户不能超过20个标签!');
}
$tags = implode(',',$_GPC['tags']);
}
//dcr163 191120 新增粉丝e
在
'displayorder' => intval($_GPC['displayorder_rule'])
下方新增
'tags' => $tags, //dcr163
=====================================================================================================================
修改文件:/web/themes/default/platform/reply-post.html
在
上方新增
粉丝打标签
{loop $tags $v}
{/loop}
=====================================================================================================================
修改文件:D:/WWW/we7/framework/class/weixin.account.class.php
新增方法
/**
* dcr163 190508 单个粉丝追加标签
* @param $openid
* @param $tagids
* @return array|mixed|void
*/
public function fansTagAppend($openid, $tagids) {
$openid = (string) $openid;
$tagids = (array) $tagids;
if(empty($openid)){
return error(-1, '没有填写用户openid');
}
if(empty($tagids)) {
return error(-1, '没有填写标签');
}
if(count($tagids) > 20) {
return error(-1, '最多20个标签');
}
$token = $this->getAccessToken();
if (is_error($token)) {
return $token;
}
$fetch_result = $this->fansTagFetchOwnTags($openid);
if(is_error($fetch_result)) {
return $fetch_result;
}
if( count($fetch_result['tagid_list']) > 20 ){
return error(-1, '该用户已经有20个标签了');
}
$url = "https://api.weixin.qq.com/cgi-bin/tags/members/batchtagging?access_token={$token}";
foreach($tagids as $tagid) {
if( !empty($fetch_result['tagid_list']) && in_array($tagid,$fetch_result['tagid_list']) ) continue; //已经存在标签就跳过
$data = array(
'openid_list' => $openid,
'tagid' => $tagid
);
$data = json_encode($data);
$result = $this->requestApi($url, $data);
if(is_error($result)) {
return $result;
}
}
return error(0, "单个粉丝打标签成功");
}
=====================================================================================================================
修改文件:/framework/function/global.func.php
新增函数:
/**
* dcr163 190502 回复关键字为用户打标签
* @param $rule_id
* @param $openid
* @return bool
*/
function tagTofans($openid,$rule_id){
global $_W;
//查询回复绑定的标签详情
$sql = 'SELECT tags FROM '.tablename('rule').' WHERE id=:id';
$rule = pdo_fetchcolumn( $sql,array(':id'=>$rule_id) );
if( empty($rule) ) return false;
$tags = explode(',',$rule);
load()->classs('weixin.account');
$wx = WeAccount::create($_W['account']);
$res = $wx->fansTagAppend( $openid,$tags ); //为粉丝批量打标签
if( $res['errno'] == 0 ){
//查询粉丝信息
$sql = 'SELECT fanid,groupid FROM '.tablename('mc_mapping_fans').' WHERE openid=:openid';
$fans = pdo_fetch( $sql,array(':openid'=>$openid) );
if( !$fans['fanid'] ) return false;
$groupid = '';
if( $fans['groupid']){
$groupid = $fans['groupid'];
$temArr = explode(',',$fans['groupid']);
foreach ($tags as $v){
if( !in_array($v,$temArr) ){
$groupid .= ','.$v;
}
}
}else{
$groupid = $rule;
}
//更新粉丝表
if( $groupid != $fans['groupid']){
pdo_update('mc_mapping_fans',array( 'groupid'=>$groupid),array('openid'=>$openid) );
}
//更新系统粉丝标签表
foreach ($tags as $tagid) {
$sql = 'SELECT id FROM '.tablename('mc_fans_tag_mapping').' WHERE fanid=:fanid AND tagid=:tid';
$id = pdo_fetchcolumn($sql,array(':fanid'=>$fans['fanid'],':tid'=>$tagid));
if( !$id ){
//插入系统标签表
$data = array('fanid'=>$fans['fanid'],'tagid'=>$tagid);
pdo_insert('mc_fans_tag_mapping',$data);
}
}
}
}
=====================================================================================================================
修改文件:/framework/builtin/core/processor.php
在
$type = $reply_type[$key];
下方新增
tagTofans($this->message['fromusername'],$this->rule); //dcr163 20191120 回复关键字为用户添加标签
如果回复还没打上标签,那就把目录 /framework/builtin/ 子目录下的processor.php 找到respond()方法内合适的位置加上 tagTofans($this->message['fromusername'],$this->rule);
Dcr163的博客
http://dcr163.cn/233.html(转载时请注明本文出处及文章链接)