WxMiniProgram.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. namespace addons\shopro\library\notify\channel;
  3. use addons\shopro\notifications\Notification;
  4. class WxMiniProgram
  5. {
  6. public function __construct()
  7. {
  8. }
  9. /**
  10. * 发送 微信模板消息
  11. *
  12. * @param mixed $notifiable // 通知用户
  13. * @param 通知内容
  14. * @return void
  15. */
  16. public function send($notifiable, Notification $notification)
  17. {
  18. $data = [];
  19. if (method_exists($notification, 'toWxMiniProgram')) {
  20. $data = $notification->toWxMiniProgram($notifiable);
  21. if ($data && isset($data['openid']) && isset($data['template_id'])) {
  22. $data['touser'] = $data['openid'];
  23. unset($data['openid']);
  24. // 发送模板消息
  25. $result = (new \addons\shopro\library\Wechat('wxMiniProgram'))->getApp()->subscribe_message->send($data);
  26. if ($result['errcode'] != 0) {
  27. // 短信发送失败
  28. \think\Log::write('小程序模板消息发送失败:用户:'. $notifiable['id'] . ';类型:' . get_class($notification) . ";发送类型:" . $notification->event . ";错误信息:" . json_encode($result));
  29. } else {
  30. // 发送成功
  31. $notification->sendOk('wxMiniProgram');
  32. }
  33. return true;
  34. }
  35. // 没有openid
  36. \think\Log::write('小程序模板消息发送失败,没有 openid:用户:' . $notifiable['id'] . ';类型:' . get_class($notification) . ";发送类型:" . $notification->event);
  37. }
  38. return true;
  39. }
  40. }