# 微信公众号推送接口对接教程 [TOC] ## **第一步 创建一个控制器 继承WchatApi** ~~~ <?php namespace app\api\controller\index; use mikkle\tp_wechat\WechatApi; /** * Created by PhpStorm. * Power By Mikkle * Email:776329498@qq.com * Date: 2017/11/1 * Time: 14:57 */ class Index extends WechatApi { } ~~~ ok 现在微信的接口已经搭建好了 剩余就是配置微信参数了 ## **第二步 配置微信参数** 配置参数详见 https://www.kancloud.cn/mikkle/thinkphp5_study/450540 当然 你也可以直接吧微信参数直接写到接口文件中 ~~~ <?php namespace app\api\controller\index; use mikkle\tp_wechat\WechatApi; /** * Created by PhpStorm. * Power By Mikkle * Email:776329498@qq.com * Date: 2017/11/1 * Time: 14:57 */ class Index extends WechatApi { protected $options=[ 'token'=>'*****', 'appid'=>'******************', 'appsecret'=>'*********************************', 'encodingaeskey'=>'******************************', ]; protected $valid = false; //网站第一次匹配 true 1为匹配 protected $isHook = false; //是否开启钩子 } ~~~ ## **第三步 登录微信网站对接接口即可** >[danger] 设置 protected $valid = true; //网站第一次匹配 true 1为匹配 > 登录微信公众平台对接即可. ## **第四步 根据自己实际需求处理重建回调方法** 例如: ~~~ protected function returnEventUnsubscribe(){ Db::name('WeFans')->where('openid', $this->openid)->update(['subscribe' => 0, 'unsubscribe_time' => time()]); return ['type' => 'text', 'message' => '期待你的再次关注']; } ~~~ ## **推送接口 回调函数列表** 详情你可以参照 WechatApi 文件 文件中已经添加了默认处理方法 | 回调方法名称 | 回调方法作用 | | --- | --- | | returnMessageText | 默认文本消息回复内容 | | returnMessageImage | 默认图片信息回复内容 | | returnMessageVoice | 默认语音信息回复内容处理方法 | | returnMessageMusic | 默认音乐信息回复内容处理方法 | | returnMessageVideo | 默认视频信息回复处理方法 | | returnMessageLocation | 默认发送地理位置回复信息处理方法 | | returnMessageLink | 默认链接回复内容处理方法 | | returnEventSubscribe | 默认关注回复处理方法 | | returnEventUnsubscribe | 默认取消关注回复处理方法 | | returnEventScan | 默认扫码事件处理方法 | | returnEventLocation | 默认上报地理事件处理方法 | | returnEventClick | 默认点击菜单关键字处理方法 | | returnEventMenuScan | 菜单调用扫码事件处理方法 | | returnEventMenuPic | 通过菜单上传图片处理方法 | | returnEventMenuLocation | 菜单上报地理事件处理方法 | | returnEventSendMass | 群发成功推送结果处理方法 | | returnEventSendTemplate | 模版消息接收结果处理方法 | | returnEvenKfSession | 客服事件处理方法 | | returnEventCard | 卡类事件 处理方法 | | returnEventWifiConnected | wifi连一连处理方法 | | returnEventAroundUserSnake | 周围摇一摇事件处理方法 | | returnEventOthers | 其他未知事件处理方法 | >[danger] PS 不是所有的推送 都会返回给顾客信息的 如果你要通知顾客 就要主动发起推送 如模版消息 在回调方法中,我只做了回调结构处理 text news image voice video等媒体信息 message内容要写MediaId * 文本类型 方法一 直接回复string ~~~ return '发送的是文本信息'; ~~~ 方法二 回复数组 ~~~ return ['type' => 'text', 'message' => '感谢你的关注']; ~~~ * 回复新闻 ~~~ $newsarray = [ [ 'Title' => '你的图片发送成功', 'Description' => '这是你发的图片吧', 'PicUrl' => $this->data['PicUrl'], 'Url' => $this->request->domain(), ], ]; $reply = ['type' => 'news', 'message' => $newsarray]; return $reply; ~~~ >[danger] 注意 回复结构 $newsarray是二维数组 支持多条文章 * 回复 image voice video ~~~ //回复 image return ['type' => 'image', 'message' => $MediaId]; //回复 voice return ['type' => 'voice', 'message' => $MediaId]; //回复 video return ['type' => 'video', 'message' => $MediaId]; ~~~ ## **重构微信推送保存方法的接口** 返回true 表示消息不存在 并且保存成功 返回 fasle 表示该消息已经存在 我是分表存储的 如:下面代码 ~~~ saveWeMessage(){ //根据message的获取存储的Model $model_message = $this->getWeMessageModel(); //查询是否已经接受该消息 if (isset($this->data['MsgId'])) { if ($model_message->infoByMsgId($this->data['MsgId'])) { return false; } $model_message->editData($this->data); } } ~~~ 如果你不保存也不查询是否存在 直接返回true ## **微信推送信息的获取** | 属性名称 | 备注 | | --- | --- | | $this->app_id | 微信app_id | | $this->options | 微信参数信息 | | $this->openid | 用户openid | | $this->fans | 微信fans信息 | | $this->type | 推送类型 | | $this->data | 微信推送信息 | 你可以根据需求直接在回调函数中调用查询使用 ## **微信推送信息的调试** ~~~ $this->we_dump($reply); ~~~ >[info] 你只需要使用we_dump方法即可 会把你想要的信息转化成文本信息 ## **重构获取用户信息接口** 默认的获取用户信息方法并未查库处理 推荐重构 进行查库处理 ~~~ /** * 获取用户信息 * 建议根据你的需求重写此方法 * Power: Mikkle * Email:776329498@qq.com * @param string $openid * @return array|bool */ protected function hasSaveFans($openid = '') { try { $openid = $openid ?: $this->openid; if (empty($openid)) { return false; } $fans = $this->weObj->getUserInfo($openid); return $fans; } catch (Exception $e) { Log::error($e->getMessage()); return false; } } ~~~ ## **微信推送钩子(HOOK)事件列表** >[info] 本推送接口埋藏大量的钩子(HOOK),但并没用自动开启 >[danger] 如果要开启钩子 请将$this->isHook 设置为true 或者 1 > 另外 回调函数的参数为 $this->weObj 对象 并不是当前类 ~~~ Hook::listen("wechat_receive", $this->weObj); ~~~ >[info] 你可以根据下面的方法 在hook获取想要的值 ~~~ //分解数据获得常用字段 $get_rev = $this->weObj->getRev(); $this->openid = $get_rev->getRevFrom(); $this->type = $get_rev->getRevType(); $this->data = $get_rev->getRevData(); ~~~ | 钩子名称 | 钩子说明 | | --- | --- | | wechat_receive | 主程序 | | wechat_message_text | | | wechat_message_image | | | wechat_message_voice | | | wechat_message_music | | | wechat_message_video | | | wechat_message_location | | | wechat_message_link | | | wechat_event_subscribe | | | wechat_event_unsubscribe | | | wechat_event_scan | | | wechat_event_location | | | wechat_event_click | | | wechat_event_menu_scan_push | | | wechat_event_menu_scan_waitmsg | | | wechat_event_menu_pic_sys | | | wechat_event_menu_pic_photo | | | wechat_event_menu_pic_weixin | | | wechat_event_menu_location | | | wechat_event_send_mass | | | wechat_event_send_template | | | wechat_event_kf_create | | | wechat_event_kf_close | | | wechat_event_kf_switch | | | wechat_event_card_pass | | | wechat_event_card_notpass | | | wechat_event_card_user_get | | | wechat_event_menu_user_del | | | wechat_event_wifi_connected | | | wechat_event_around_user_shake | | | wechat_event_others | | 注释我不写了 自己猜吧