WechatService.php
11.0 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
<?php
namespace addons\weixin\library;
use addons\weixin\library\MessageRepositories;
use EasyWeChat\Factory;
use EasyWeChat\Kernel\Messages\Article;
use EasyWeChat\Kernel\Messages\Image;
use EasyWeChat\Kernel\Messages\Material;
use EasyWeChat\Kernel\Messages\News;
use EasyWeChat\Kernel\Messages\NewsItem;
use EasyWeChat\Kernel\Messages\Text;
use EasyWeChat\Kernel\Messages\Video;
use EasyWeChat\Kernel\Messages\Voice;
use EasyWeChat\Kernel\Messages\Media;
use app\admin\model\weixin\Reply as WechatReply;
use think\Response;
class WechatService
{
private static $instance = null;
public static function options()
{
$wechat_data = \app\admin\model\weixin\Config::where(['group' => 'weixin'])->select();
foreach ($wechat_data as $k => $v) {
$value = $v->toArray();
if (in_array($value['type'], ['selects', 'checkbox', 'images', 'files'])) {
$value['value'] = explode(',', $value['value']);
}
if ($value['type'] == 'array') {
$value['value'] = (array)json_decode($value['value'], true);
}
$wechat[$value['name']] = $value['value'];
}
$config = [
'app_id' => isset($wechat['appid']) ? trim($wechat['appid']) : '',
'secret' => isset($wechat['appsecret']) ? trim($wechat['appsecret']) : '',
'token' => isset($wechat['token']) ? trim($wechat['token']) : '',
'guzzle' => [
'timeout' => 10.0, // 超时时间(秒)
'verify' => false
],
];
if (isset($wechat['encode']) && (int)$wechat['encode']>0 && isset($wechat['encodingaeskey']) &&
!empty($wechat['encodingaeskey'])) {
$config['aes_key'] = $wechat['encodingaeskey'];
}
return $config;
}
public static function application($cache = false)
{
(self::$instance === null || $cache === true) && (self::$instance = Factory::officialAccount(self::options()));
return self::$instance;
}
public static function serve():Response
{
$wechat = self::application(true);
$server = $wechat->server;
self::hook($server);
$response = $server->serve();
return response($response->getContent());
}
/**
* 监听行为
* @param Guard $server
*/
private static function hook($server)
{
$server->push(function ($message) {
//微信消息前置操作
switch ($message['MsgType']) {
case 'event':
switch (strtolower($message['Event'])) {
case 'subscribe':
$response = WechatReply::reply('subscribe');//关注回复
break;
case 'unsubscribe':
//用户取消关注公众号前置操作
break;
case 'scan':
$response = WechatReply::reply('subscribe');//扫码关注
break;
case 'location':
$response = MessageRepositories::wechatEventLocation($message);
break;
case 'click':
$response = WechatReply::reply($message['EventKey']);//点击事件
break;
case 'view':
$response = MessageRepositories::wechatEventView($message);
break;
}
break;
case 'text':
$response = WechatReply::reply($message['Content']);
break;
case 'image':
$response = MessageRepositories::wechatMessageImage($message);
break;
case 'voice':
$response = MessageRepositories::wechatMessageVoice($message);
break;
case 'video':
$response = MessageRepositories::wechatMessageVideo($message);
break;
case 'location':
$response = MessageRepositories::wechatMessageLocation($message);
break;
case 'link':
$response = MessageRepositories::wechatMessageLink($message);
break;
// ... 其它消息
default:
$response = MessageRepositories::wechatMessageOther($message);
break;
}
return $response ?? false;
});
}
/**
* 上传永久素材接口
* @return object
*/
public static function materialService()
{
return self::application()->material;
}
/**
* 客服消息接口
* @return object
*/
public static function staffService()
{
return self::application()->customer_service;
}
/**
* 微信公众号菜单接口
* @return object
*/
public static function menuService()
{
return self::application()->menu;
}
/**
* 用户标签接口
* @return object
*/
public static function userTagService()
{
return self::application()->user_tag;
}
/**
* 回复文本消息
* @param string $content 文本内容
* @return Text
*/
public static function textMessage($content)
{
return new Text($content);
}
/**
* 回复图片消息
* @param string $media_id 媒体资源 ID
* @return Image
*/
public static function imageMessage($media_id)
{
return new Image($media_id);
}
/**
* 回复视频消息
* @param string $media_id 媒体资源 ID
* @param string $title 标题
* @param string $description 描述
* @param null $thumb_media_id 封面资源 ID
* @return Video
*/
public static function videoMessage($media_id, $title = '', $description = '...', $thumb_media_id = null)
{
return new Video(
$media_id,
['title' => $title, 'description' => $description, 'thumb_media_id' => $thumb_media_id]
);
}
/**
* 回复声音消息
* @param string $media_id 媒体资源 ID
* @return Voice
*/
public static function voiceMessage($media_id)
{
return new Voice($media_id);
}
/**
* 回复图文消息
* @param string|array $title 标题
* @param string $description 描述
* @param string $url URL
* @param string $image 图片链接
* @return Video
*/
public static function newsMessage($data)
{
$items = [
new NewsItem([
'title' => $data['title'],
'description' => $data['description'],
'url' => $data['url'],
'image' => $data['image'],
]),
];
return new News($items);
}
/**
* 回复素材消息
* @param string $type [mpnews、 mpvideo、voice、image]
* @param string $media_id 素材 ID
* @return Material
*/
public static function materialMessage($type, $media_id)
{
return new Media($media_id, $type);
}
/**
* 作为客服消息发送
* @param $to
* @param $message
* @return bool
*/
public static function staffTo($to, $message)
{
$staff = self::staffService();
$staff = is_callable($message) ? $staff->message($message()) : $staff->message($message);
$res = $staff->to($to)->send();
return $res;
}
/**
* jsSdk
* @return object
*/
public static function jsService()
{
return self::application()->jssdk;
}
public static function jsSdk($url = '')
{
$apiList = [
'editAddress', 'openAddress', 'updateTimelineShareData', 'updateAppMessageShareData', 'onMenuShareTimeline',
'onMenuShareAppMessage', 'onMenuShareQQ', 'onMenuShareWeibo', 'onMenuShareQZone', 'startRecord',
'stopRecord', 'onVoiceRecordEnd', 'playVoice', 'pauseVoice', 'stopVoice', 'onVoicePlayEnd', 'uploadVoice',
'downloadVoice', 'chooseImage', 'previewImage', 'uploadImage', 'downloadImage', 'translateVoice',
'getNetworkType', 'openLocation', 'getLocation', 'hideOptionMenu', 'showOptionMenu', 'hideMenuItems',
'showMenuItems', 'hideAllNonBaseMenuItem', 'showAllNonBaseMenuItem', 'closeWindow', 'scanQRCode',
'chooseWXPay', 'openProductSpecificView', 'addCard', 'chooseCard', 'openCard'
];
$jsService = self::jsService();
if ($url) {
$jsService->setUrl($url);
}
try {
return $jsService->buildConfig($apiList, $debug = false, $beta = false, $json = false);
} catch (\Exception $e) {
return '{}';
}
}
/**
* 用户授权
* @return object
*/
public static function oauthService()
{
return self::application()->oauth;
}
/**
* 用户接口
* @return object
*/
public static function userService()
{
return self::application()->user;
}
/**
* 获得用户信息
* @param array|string $openid
* @return object
*/
public static function getUserInfo($openid)
{
$userService = self::userService();
$userInfo = $userService->get($openid);
return $userInfo;
}
/**
* 获得用户openid列表
* @param array|string $openid
* @return object
*/
public static function getUserList()
{
$userService = self::userService();
$userList = $userService->list();
return $userList;
}
/**
* 获得用户列表信息
* @param array|string $openid
* @return object
*/
public static function getUserListInfo()
{
$userService = self::userService();
$list = self::getUserList();
if (!isset($list['data']['openid'])) {
return [];
}
$userList = $userService->select($list['data']['openid']);
return $userList;
}
/**
* 模板消息接口
* @return \EasyWeChat\Notice\Notice
*/
public static function noticeService()
{
return self::application()->template_message;
}
public static function sendTemplate($openid, $templateId, array $data, $url = null, $defaultColor = null)
{
$data = [
'touser' => $openid,
'template_id' => $templateId,
'url' => $url,
'data' => $data
];
return self::noticeService()->send($data);
}
/**
* 微信二维码生成接口
* @return \EasyWeChat\QRCode\QRCode
*/
public static function qrcodeService()
{
return self::application()->qrcode;
}
/**
* 短链接生成接口
* @return \EasyWeChat\Url\Url
*/
public static function urlService()
{
return self::application()->url;
}
}