Laravel集成EasyWeChat接收普通消息和事件推送

Laravel快速集成EasyWeChat:http://www.appblog.cn/2018/02/11/Laravel快速集成EasyWeChat/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
/**
* Created by PhpStorm.
* User: yezhou
* Date: 2018/2/7
* Time: 13:20
*/

namespace App\Http\Controllers;


use Illuminate\Support\Facades\Log;

class WeChatController extends Controller
{

/**
* 处理微信的请求消息
*
* @return string
*/
public function serve()
{
Log::info('request arrived.'); # 注意:Log 为 Laravel 组件,所以它记的日志去 Laravel 日志看,而不是 EasyWeChat 日志

$app = app('wechat.official_account');
$app->server->push(function($message) {
switch ($message['MsgType']) {
case 'event':
Log::info('收到事件消息');
//接收事件推送: https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140454
switch ($message['Event']) {
case 'subscribe': //关注事件, 扫描带参数二维码事件(用户未关注时,进行关注后的事件推送)
return "欢迎关注 HTTP://WWW.AppBlog.CN!";
break;
case 'unsubscribe': //取消关注事件

break;
case 'SCAN': //扫描带参数二维码事件(用户已关注时的事件推送)
return "欢迎关注 HTTP://WWW.AppBlog.CN!";
break;
case 'LOCATION': //上报地理位置事件
return "经度: " . $message['Longitude'] . "\n纬度: " . $message['Latitude'] . "\n精度: " . $message['Precision'];
break;
case 'CLICK': //自定义菜单事件(点击菜单拉取消息时的事件推送)
return "事件KEY值: " . $message['EventKey'];
break;
case 'VIEW': //自定义菜单事件(点击菜单拉取消息时的事件推送)
return "跳转URL: " . $message['EventKey'];
break;
case 'ShakearoundUserShake':
Log::info('摇一摇周边事件');
//摇一摇事件通知: https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1443448066
return 'ChosenBeacon\n' . 'Uuid: ' . $message['ChosenBeacon']['Uuid'] . 'Major: ' . $message['ChosenBeacon']['Major'] . 'Minor: ' . $message['ChosenBeacon']['Minor'] . 'Distance: ' . $message['ChosenBeacon']['Distance'];
break;
default:
return $message['Event'];
break;
}
break;
//接收普通消息: https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140453
case 'text':
Log::info('收到文字消息');
return "Content: " . $message['Content'];
break;
case 'image':
Log::info('收到图片消息');
return "MediaId: " . $message['MediaId'] . "\nPicUrl: " . $message['PicUrl'];
break;
case 'voice':
Log::info('收到语音消息');
return "MediaId: " . $message['MediaId'] . "\nFormat: " . $message['Format'] . "\nRecognition: " . $message['Recognition'];
break;
case 'video':
Log::info('收到视频消息');
return "MediaId: " . $message['MediaId'] . "\nThumbMediaId: " . $message['ThumbMediaId'];
break;
case 'shortvideo':
Log::info('收到小视频消息');
return "MediaId: " . $message['MediaId'] . "\nThumbMediaId: " . $message['ThumbMediaId'];
break;
case 'location':
return "Location_X: " . $message['Location_X'] . "\nLocation_Y: " . $message['Location_Y'] . "\nScale: " . $message['Scale'] . "\nLabel: " . $message['Label'];
Log::info('收到坐标消息');
break;
case 'link':
Log::info('收到链接消息');
return "Title: " . $message['Title'] . "\nDescription: " . $message['Description'] . "\nUrl: " . $message['Url'];
break;
default:
Log::info('收到其它消息');
return $message['MsgType'];
break;
}
});

return $app->server->serve();
}

}

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :