文档手册

微信-接收到消息后-给粉丝设定标签组

2024-12-19 20:30:03

/dayrui/App/Weixin/Controllers/Home.php


// 搜索自定义回复
                            $kws = $this->get_cache('weixin-kws'.(WEIXIN_MORE_ID > 1 ? '_more_'.WEIXIN_MORE_ID : ''));
                            if ($kws) {
                                foreach ($kws as $kw => $id) {
                                    if (strpos($this->data['Content'], (string)$kw) !== false) {
                                        $data = \Phpcmf\Service::M()->table(weixin_wxtable('reply'))->get($id);
                                        if ($data) {
                                            // 记录
                                            \Phpcmf\Service::M()->table(weixin_wxtable('reply'))->update($data['id'], [
                                                'counts' => $data['counts'] + 1,
                                                'updatetime' => SYS_TIME,
                                            ]);
                                            //九戒,如果用户还没有标签,给用户加一个标签  start                                      
                                            $uid = $this->member['id'];
                                            if($uid){
                                                //查询粉丝分组
                                                $wxUser = \Phpcmf\Service::M()->db->table(weixin_wxtable('user'))->where('`uid` = "'.$uid.'"')->get()->getRowArray();  
                                                $groupids = dr_string2array($wxUser['groupids']);
                                                dr_debug("groupids",$groupids);
                                                //查询标签组的tag
                                                $groups = \Phpcmf\Service::M()->db->table(weixin_wxtable('group'))->where('`name` = "'.$kw.'"')->get()->getRowArray();
                                                $tag = $groups['tag'];  
                                                //如果粉丝分组不包含这个类别
                                                if (!empty($tag) && !dr_in_array($tag, $groupids)) {
                                                    $groupids[] = (string)$tag;
                                                    // 更改本地库
                                                    \Phpcmf\Service::M()->db->table(weixin_wxtable('user'))->where('id', $wxUser['id'])->update(array(
                                                        'groupids' => dr_array2string($groupids),
                                                    )); 
 

                                                    //通知微信平台给用户打标签
                                                    $openid = $wxUser['openid'];
                                                    $param = [
                                                        'openid_list' => $openid,
                                                        'tagid' => $tag,
                                                    ];                                               
                                                    $rt = wx_post_https_json_data(
                                                        'https://api.weixin.qq.com/cgi-bin/tags/members/batchtagging?access_token=' . $this->access_token,
                                                        $param
                                                    );                                                  
                                                }
                                                                                             
                                            }    
                                            //九戒,如果用户还没有标签,给用户加一个标签 end                                      

                                            if ($data['tid']) {
                                                // 回复素材
                                                return $this->_to_weixin_content($data['content']);
                                            } else {
                                                // 回复文本
                                                return $this->_to_weixin_text($data['content']);
                                            }
                                            break;
                                        }
                                    }
                                }
                            }