WxMiniProgram.php
1.6 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
<?php
namespace addons\groupon\library\notify\channel;
use addons\groupon\notifications\Notification;
class WxMiniProgram
{
public function __construct()
{
}
/**
* 发送 微信模板消息
*
* @param mixed $notifiable // 通知用户
* @param 通知内容
* @return void
*/
public function send($notifiable, Notification $notification)
{
$data = [];
if (method_exists($notification, 'toWxMiniProgram')) {
$data = $notification->toWxMiniProgram($notifiable);
if ($data && isset($data['openid']) && isset($data['template_id'])) {
$data['touser'] = $data['openid'];
unset($data['openid']);
// 发送模板消息
$result = (new \addons\groupon\library\Wechat('wxMiniProgram'))->getApp()->subscribe_message->send($data);
if ($result['errcode'] != 0) {
// 短信发送失败
\think\Log::write('小程序模板消息发送失败:用户:'. $notifiable['id'] . ';类型:' . get_class($notification) . ";发送类型:" . $notification->event . ";错误信息:" . json_encode($result));
} else {
// 发送成功
$notification->sendOk('wxMiniProgram');
}
return true;
}
// 没有openid
\think\Log::write('小程序模板消息发送失败,没有 openid:用户:' . $notifiable['id'] . ';类型:' . get_class($notification) . ";发送类型:" . $notification->event);
}
return true;
}
}