关注回复多图文实现

application/mp/controller/Entr.php 替换对应的函数即可
    /**
* 关键词回复规则
* @param $keyword 消息文本
* @author geeson myrhzq@qq.com
* @param $msg array 微信消息
* case 回复类型 text,addon,images,news,voice,music,video
*
*/
public function keyword($keyword, $msg = [])
{
$rules = [];$contents = [];
// 查看是否存在对应关键词的订阅事件 公司需求目前只有订阅公众号 可自行拓展
$existSubscribe = Db::name('mp_rule')->where(['mpid' => $this->mid, 'keyword' => $keyword, 'status' => '1'])
->where('event', 'subscribe')
->order('id Desc')->find();
// 如果存在对应事件 获取所有对应规则
if (!empty($existSubscribe)){
$rules = Db::name('mp_rule')->where(['mpid' => $this->mid, 'keyword' => $keyword, 'status' => '1'])
->where('event', '')
->order('id Desc')->select();
if (is_array($rules)){
foreach ($rules as $ruleKey => $rule) {
$currentRes = Db::name('mp_reply')->where(['reply_id' => $rule['reply_id']])->select();
array_push($contents,$currentRes);
}
}
} else {
// 存在多个对应规则的时候也只取最大一个 id
$rule = Db::name('mp_rule')->where(['mpid' => $this->mid, 'keyword' => $keyword, 'status' => '1'])
->where('event', '')
->order('id Desc')->find();
}
trace(json_encode($rules).'$rules');

if (!empty($rule)) {
switch ($rule['type']) {//text,addon,images,news,voice,music,video
case 'addon'://该关键词是插件应用响应的
loadAdApi($rule['addon'], $msg, ['mid' => $this->mid, 'addon' => $rule['addon']]);
break;
case 'text'://文本
$content = Db::name('mp_reply')->where(['reply_id' => $rule['reply_id']])->field('content')->find();
replyText($content['content']);
break;
case 'image'://图片
$result = Db::name('mp_reply')->where(['reply_id' => $rule['reply_id']])->find();
if (!empty($result)) {
replyImage($result['media_id']);
}
break;
case 'news'://图文
trace(json_encode($contents).'$contents');
$news = [];
// 如果不存在 subscribe 事件 或者 subscribe 事件对应的规则为空 则降级为普通消息推送
if(empty($existSubscribe) || empty($contents)){
$content = Db::name('mp_reply')->where(['reply_id' => $rule['reply_id']])->select();
foreach ($content as $key1 => $v) {
foreach ($v as $key2 => $v) {
if ($key2 == 'title') {
$news[$key1]['Title'] = $v;;
}
if ($key2 == 'content') {
$news[$key1]['Description'] = $v;
}
if ($key2 == 'url') {
$news[$key1]['PicUrl'] = $v;
}
if ($key2 == 'link') {
$news[$key1]['Url'] = $v . '?openid=' . getOrSetOpenid();
}
}
}
} else {
foreach ($contents as $contentKey => $content) {
foreach ($content as $key1 => $item) {
foreach ($item as $itemKey => $itemValue) {
if ($itemKey == 'title') {
$news[$contentKey]['Title'] = $itemValue;;
}
if ($itemKey == 'content') {
$news[$contentKey]['Description'] = $itemValue;
}
if ($itemKey == 'url') {
$news[$contentKey]['PicUrl'] = $itemValue;
}
if ($itemKey == 'link') {
$news[$contentKey]['Url'] = $itemValue . '?openid=' . getOrSetOpenid();
}
}
}
}
}
replyNews($news);
trace(json_encode($news).'$news');
break;

case 'voice'://语音
$result = Db::name('mp_reply')->where(['reply_id' => $rule['reply_id']])->find();
if (!empty($result)) {
replyVoice($result['media_id']);
}
break;
case 'music'://音乐
$result = Db::name('mp_reply')->where(['reply_id' => $rule['reply_id']])->find();
if (is_array($result)) {
replyMusic($result['title'], $result['content'], $result['url'], $result['link']);
}
break;
case 'video'://视频
$result = Db::name('mp_reply')->where(['reply_id' => $rule['reply_id']])->find();
if (!empty($result)) {
replyVideo($result['media_id'], $result['title'], $result['content']);
}
break;
case 'member'://会员登录
$content = Db::name('mp_reply')->where(['reply_id' => $rule['reply_id']])->select();
$news = [];
foreach ($content as $key1 => $v) {
foreach ($v as $key2 => $v) {
if ($key2 == 'title') {
$news[$key1]['Title'] = '会员登录';
}
if ($key2 == 'url') {
$news[$key1]['PicUrl'] = $v;
}
if ($key2 == 'link') {
$news[$key1]['Url'] = $v . '?openid=' . getOrSetOpenid();
}
}
}
replyNews($news);
break;
default:
//
break;
}
} else {
$rule = Db::name('mp_rule')
->where(['mpid' => $this->mid, 'event' => 'unidentified', 'status' => '1'])->find();
if (!empty($rule)) {
if ($rule['keyword']) {
$this->keyword($rule['keyword'], $msg);
}
if ($rule['addon']) {
loadAdApi($rule['addon'], $msg, ['mid' => $this->mid, 'addon' => $rule['addon']]);
}
} else {

$this->mpMsg($msg);
}

}

}
已邀请:

xiaosa

赞同来自:

本人在接触这个项目之前没有了解过php 抛砖引玉,也希望作者大大能诶给出更好地实现
 
此外 微信官方限制在回复文字时只能回复一条图文,所以这个用户发送文字时是不可用的。语音等同理。
 
修改代码后添加多条对应订阅关键字的图文回复即可,为避免用户回复关键字导致副作用,请在关键字中添加特殊字符。
 
根据时间倒序,最后一条添加的图文显示在最上面。
 
 

要回复问题请先登录注册