OffcialAccountListener.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\listener\wechat;
  12. use app\services\message\wechat\MessageServices;
  13. use app\services\order\StoreOrderTakeServices;
  14. use app\services\other\QrcodeServices;
  15. use app\services\wechat\WechatMessageServices;
  16. use app\services\wechat\WechatReplyServices;
  17. use crmeb\services\wechat\MiniProgram;
  18. use EasyWeChat\Kernel\Contracts\EventHandlerInterface;
  19. use EasyWeChat\Kernel\Messages\Image;
  20. use EasyWeChat\Kernel\Messages\News;
  21. use EasyWeChat\Kernel\Messages\Text;
  22. use EasyWeChat\Kernel\Messages\Transfer;
  23. use EasyWeChat\Kernel\Messages\Voice;
  24. use think\facade\Event;
  25. use think\facade\Log;
  26. /**
  27. * 公众号消息处理事件
  28. * Class OffcialAccountListener
  29. * @package app\listener\wechat
  30. */
  31. class OffcialAccountListener implements EventHandlerInterface
  32. {
  33. /**
  34. * 事件回调
  35. * @param null $payload
  36. * @return array|bool|Image|News|Text|Transfer|Voice|string|void
  37. */
  38. public function handle($payload = null)
  39. {
  40. try {
  41. /** @var MessageServices $messageService */
  42. $messageService = app()->make(MessageServices::class);
  43. /** @var WechatReplyServices $wechatReplyService */
  44. $wechatReplyService = app()->make(WechatReplyServices::class);
  45. /** @var WechatMessageServices $wechatMessage */
  46. $wechatMessage = app()->make(WechatMessageServices::class);
  47. $spread_uid = 0;
  48. if (isset($payload['Ticket'])) {
  49. /** @var QrcodeServices $qrcodeService */
  50. $qrcodeService = app()->make(QrcodeServices::class);
  51. $qrInfo = $qrcodeService->getQrcode($payload['Ticket'], 'ticket');
  52. if ($qrInfo && isset($qrInfo['third_id'])) $spread_uid = $qrInfo['third_id'];
  53. }
  54. $wechatMessage->wechatMessageBefore($payload, $spread_uid);
  55. switch ($payload['MsgType']) {
  56. case 'event':
  57. switch (strtolower($payload['Event'])) {
  58. case 'subscribe':
  59. $response = $messageService->wechatEventSubscribe($payload, $spread_uid);
  60. break;
  61. case 'unsubscribe':
  62. $messageService->wechatEventUnsubscribe($payload);
  63. break;
  64. case 'scan':
  65. $response = $messageService->wechatEventScan($payload);
  66. break;
  67. case 'location':
  68. $response = $messageService->wechatEventLocation($payload);
  69. break;
  70. case 'click':
  71. $response = $wechatReplyService->reply($payload['EventKey'], $payload['FromUserName']);
  72. break;
  73. case 'view':
  74. $response = $messageService->wechatEventView($payload);
  75. break;
  76. case 'user_get_card'://卡券领取
  77. $response = $messageService->wechatEventUserGetCard($payload);
  78. break;
  79. case 'submit_membercard_user_info'://卡券激活
  80. $response = $messageService->wechatEventSubmitMembercardUserInfo($payload);
  81. break;
  82. case 'user_del_card'://卡券删除
  83. $response = $messageService->wechatEventUserDelCard($payload);
  84. break;
  85. }
  86. break;
  87. case 'text':
  88. $response = $wechatReplyService->reply($payload['Content'], $payload['FromUserName']);
  89. break;
  90. case 'image':
  91. $response = $messageService->wechatMessageImage($payload);
  92. break;
  93. case 'voice':
  94. $response = $messageService->wechatMessageVoice($payload);
  95. break;
  96. case 'video':
  97. $response = $messageService->wechatMessageVideo($payload);
  98. break;
  99. case 'location':
  100. $response = $messageService->wechatMessageLocation($payload);
  101. break;
  102. case 'link':
  103. $response = $messageService->wechatMessageLink($payload);
  104. break;
  105. // ... 其它消息
  106. default:
  107. $response = $messageService->wechatMessageOther($payload);
  108. break;
  109. }
  110. } catch (\Throwable $e) {
  111. \think\facade\Log::error(['title' => '微信消息服务端消息执行错误', 'message' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()]);
  112. }
  113. return $response ?? false;
  114. }
  115. }