Message.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace app\backend\modules\coupon\services;
  3. use app\common\services\MessageService;
  4. use EasyWeChat\Message\News;
  5. use EasyWeChat\Message\Text;
  6. class Message
  7. {
  8. //默认使用微信"客服消息"通知, 对于超过 48 小时未和平台互动的用户, 使用"模板消息"通知
  9. public static function message($data, $templateId = null, $uid)
  10. {
  11. self::sendTemplateNotice($uid, $templateId, $data);
  12. // try {
  13. // self::sendNotice($openid, $data);
  14. // } catch (\Exception $e) {
  15. // try {
  16. // self::sendTemplateNotice($uid, $templateId, $data);
  17. // } catch (\Exception $e) {
  18. // //
  19. // }
  20. // }
  21. }
  22. //发送微信"客服消息"
  23. /*
  24. * $notice可以是微信文本回复或者微信图文回复
  25. * 文本: $data = new Text(['content' => 'Hello']);
  26. * 图文:
  27. * $data = new News([
  28. 'title' => 'your_title',
  29. 'image' => 'your_image',
  30. 'description' => 'your_description',
  31. 'url' => 'your_url',
  32. ]);
  33. */
  34. public static function sendNotice($openid, $data)
  35. {
  36. $app = app('wechat');
  37. if (array_key_exists('content', $data)) {
  38. $data = new Text($data); //发送文本消息
  39. } else {
  40. $data = new News($data); //发送图文消息
  41. }
  42. $app->staff->message($data)->to($openid)->send();
  43. }
  44. //发送微信"模板消息"
  45. /*
  46. * * 如果只使用模板消息 -- "业务处理通知":
  47. * $data = [
  48. * 'first' => 'your_title',
  49. 'keyword1' => 'your_description',
  50. 'keyword2' => 'your_description',
  51. 'url' => 'your_url',
  52. * ]
  53. * 如何需要和"客服消息"共用数据 (注意模板消息中无法发送图片):
  54. * $data = [
  55. 'title' => 'your_title',
  56. 'image' => 'your_image',
  57. 'description' => 'your_description',
  58. 'url' => 'your_url',
  59. ];
  60. */
  61. public static function sendTemplateNotice($uid, $templateId, $data)
  62. {
  63. // $app = app('wechat');
  64. // $notice = $app->notice;
  65. $url = $data['url'];
  66. if (array_key_exists('description', $data)) { //如果需要和"客服消息"共用数据
  67. $data = [
  68. 'first' => '您好',
  69. 'keyword1' => $data['title'],
  70. 'keyword2' => $data['description'],
  71. 'remark' => '',
  72. ];
  73. }
  74. MessageService::notice($templateId, $data, $uid);
  75. // $notice->to($openid)->uses($templateId)->andUrl($url)->data($data)->send();
  76. }
  77. }