公众号关注
创建一个应用为official,新建控制器Index.php
全局消息类型处理
<?php
// 严格模式
declare (strict_types = 1);
namespace app\official\controller;
// 基础控制器
use app\BaseController;
// 引入视图
use think\facade\View;
use EasyWeChat\Factory;
use think\facade\Config;
use think\facade\Log;
use EasyWeChat\Kernel\Messages\Text;
use EasyWeChat\Kernel\Messages\Image;
use EasyWeChat\Kernel\Messages\News;
use EasyWeChat\Kernel\Messages\NewsItem;
use app\common\model\Order\Order as OrderModel;
use app\common\model\Post\Post as PostModel;
use app\common\model\Config as ConfigModel;
// 继承基础控制器
class Index extends BaseController
{
// 初始化
public function initialize()
{
parent::initialize();
// 读取配置文件
$config = Config::get('wechat');
// 初始化
$this->app = Factory::officialAccount($config);
}
public function index()
{
try{
$this->app->server->push(function ($message) {
switch ($message['MsgType']) {
case 'event':
return $this->event($message);
break;
case 'text':
return $this->text($message['Content']);
break;
case 'image':
return '收到图片消息';
break;
case 'voice':
return '收到语音消息';
break;
case 'video':
return '收到视频消息';
break;
case 'location':
return '收到坐标消息';
break;
case 'link':
return '收到链接消息';
break;
case 'file':
return '收到文件消息';
default:
return '收到其它消息';
break;
}
});
} catch (\Exception $e) {
Log::log('debug', $e->getMessage());
}
// 发送
$response =$this->app->server->serve()->send();
}
关注回复,点赞,扫描二维码
/**
* 回复事件
* @param array $message
* @return Image|string
*/
private function event($message = [])
{
// subscribe: 判断是否是订阅 MsgType:判断是消息
if($message['Event'] === 'subscribe' && $message['MsgType'] == 'event'){
return "欢迎来到测试号\n使用帮助\n1、发送城市名称,可搜索天气\n2、发送预约单号,可搜索预约进度\n3、发送商品订单号,可搜索商品订单进度\n4、发送悬赏问题,可查看解决方案(提问:关键词)";;
}
// 点击事件
if($message['Event'] === 'CLICK' && $message['MsgType'] == 'event')
{
// 微信公众号官方功能,调式方法 Public文件夹查看a.txt
// file_put_contents('a.txt', '0000');
$EventKey = isset($message['EventKey']) ? $message['EventKey'] : '';
return new Text($EventKey);
}
// 扫描二维码
if($message['MsgType'] == 'event' && $message['Event'] === "scancode_waitmsg"){
$EventKey = isset($message['EventKey']) ? $message['EventKey'] : '';
return new Text($EventKey);
}
}
创建自定义菜单
/*
* 菜单创建
*/
public function menu()
{
// 配置菜单
$buttons = [
[
"type" => "view",
"name" => "今日热贴",
"url" => "http://tp6.lyqing.cn/home/index/info.html?postid=9"
],
[
"name" => "菜单",
"sub_button" => [
[
"type" =>"scancode_waitmsg",
"name"=> "扫码带提示",
"key"=> "扫描事件",
"sub_button" => [ ]
],
[
"type" => "view",
"name" => "商城",
"url" => "http://vueshop.lyqing.cn/"
],
[
"type" => "click",
"name" => "赞一下我们",
"key" => "点赞成功"
],
],
],
[
"type" => "view",
"name" => "论坛 ",
"url" => "http://tp6.lyqing.cn"
],
];
// 创建菜单
$result = $this->app->menu->create($buttons);
if ($result) {
echo '创建成功';
} else {
echo '创建失败';
}
}
模板消息回复
1、需要在微信公众号平台模板消息接口,创建测试模板,获取到模板ID(template_id)
2、在TP后台创建view视图,send.html,表单文件填写touser,touser是指粉丝关注的微信号
// 发送模板消息
public function send()
{
if($this->request->isPost())
{
$touser = $this->request->param('touser', 0);
$data = [
'touser' => $touser,
'template_id' => "8SkELTBaa-WzmgtshpoUJZO_55mS9fSAgXsV7dZ6M-w",
'url' => 'http://tp6.lyqing.cn',
'data' => [
'key1' => 'VALUE',
'key2' => 'VALUE2',
]
];
$result = $this->app->template_message->send($data);
if($result == FALSE)
{
echo '发送失败';
} else {
echo '模板消息成功';
}
}
return View::fetch();
}