WechatNotificationService.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace App\Services;
  3. use EasyWeChat\Factory;
  4. use Illuminate\Support\Facades\Log;
  5. class WechatNotificationService
  6. {
  7. protected $app;
  8. public function __construct()
  9. {
  10. $this->app = app('wechat.official_account');
  11. }
  12. /**
  13. * 发送模板消息
  14. *
  15. *
  16. * @param string $openid 接收者openid
  17. * @param string $templateId 模板ID
  18. * @param array $data 模板数据
  19. * @param string|null $url 点击跳转链接
  20. * @param array|null $miniprogram 跳小程序配置
  21. * @return array
  22. */
  23. public function sendTemplateMessage(
  24. string $openid,
  25. string $templateId,
  26. array $data,
  27. ?string $url = null,
  28. ?array $miniprogram = null
  29. ): array {
  30. try {
  31. $message = [
  32. 'touser' => $openid,
  33. 'template_id' => $templateId,
  34. 'data' => $data,
  35. ];
  36. // 添加跳转链接
  37. if ($url) {
  38. $message['url'] = $url;
  39. }
  40. // 添加小程序跳转配置
  41. if ($miniprogram) {
  42. $message['miniprogram'] = $miniprogram;
  43. }
  44. $result = $this->app->template_message->send($message);
  45. if ($result['errcode'] === 0) {
  46. Log::info('微信模板消息发送成功', [
  47. 'openid' => $openid,
  48. 'template_id' => $templateId,
  49. 'data' => $data,
  50. ]);
  51. return [
  52. 'success' => true,
  53. 'message' => '发送成功',
  54. ];
  55. }
  56. Log::error('微信模板消息发送失败', [
  57. 'openid' => $openid,
  58. 'template_id' => $templateId,
  59. 'data' => $data,
  60. 'error' => $result['errmsg'],
  61. ]);
  62. return [
  63. 'success' => false,
  64. 'message' => $result['errmsg'],
  65. ];
  66. } catch (\Exception $e) {
  67. Log::error('微信模板消息发送异常', [
  68. 'openid' => $openid,
  69. 'template_id' => $templateId,
  70. 'data' => $data,
  71. 'error' => $e->getMessage(),
  72. ]);
  73. return [
  74. 'success' => false,
  75. 'message' => '发送失败:' . $e->getMessage(),
  76. ];
  77. }
  78. }
  79. /**
  80. * 发送订单状态变更通知
  81. *
  82. * $result = $this->wechatService->sendOrderStatusNotification(
  83. * $order->user->wechat_openid,
  84. * [
  85. * 'order_no' => $order->order_no,
  86. * 'status' => $order->status_text,
  87. * 'time' => now()->format('Y-m-d H:i:s')
  88. * ]
  89. * );
  90. *
  91. * @param string $openid
  92. * @param array $orderInfo
  93. * @return array
  94. */
  95. public function sendOrderStatusNotification(string $openid, array $orderInfo): array
  96. {
  97. $templateId = env('WECHAT_ORDER_STATUS_TEMPLATE_ID');
  98. $data = [
  99. 'first' => ['value' => '您的订单状态有更新'],
  100. 'keyword1' => ['value' => $orderInfo['order_no']],
  101. 'keyword2' => ['value' => $orderInfo['status']],
  102. 'keyword3' => ['value' => $orderInfo['time']],
  103. 'remark' => ['value' => '感谢您的使用!'],
  104. ];
  105. return $this->sendTemplateMessage($openid, $templateId, $data);
  106. }
  107. }