RoutineListener.php 4.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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\order\StoreOrderTakeServices;
  13. use crmeb\services\wechat\MiniProgram;
  14. use EasyWeChat\Kernel\Contracts\EventHandlerInterface;
  15. use EasyWeChat\Kernel\Messages\Image;
  16. use EasyWeChat\Kernel\Messages\News;
  17. use EasyWeChat\Kernel\Messages\Text;
  18. use EasyWeChat\Kernel\Messages\Transfer;
  19. use EasyWeChat\Kernel\Messages\Voice;
  20. use think\facade\Event;
  21. use think\facade\Log;
  22. /**
  23. * 小程序消息处理事件
  24. * Class RoutineListener
  25. * @package app\listener\wechat
  26. */
  27. class RoutineListener implements EventHandlerInterface
  28. {
  29. /**
  30. * 事件回调
  31. * @param null $payload
  32. * @return array|bool|Image|News|Text|Transfer|Voice|string|void
  33. */
  34. public function handle($payload = null)
  35. {
  36. try {
  37. switch ($payload['MsgType']) {
  38. case 'event':
  39. switch (strtolower($payload['Event'])) {
  40. case 'funds_order_pay':
  41. $prefix = substr($payload['order_info']['trade_no'],0,2);
  42. //处理一下参数
  43. switch ($prefix) {
  44. case 'wx':
  45. case 'cp':
  46. $data['attach'] = 'Product';
  47. break;
  48. case 'hy':
  49. $data['attach'] = 'Member';
  50. break;
  51. case 'cz':
  52. $data['attach'] = 'UserRecharge';
  53. break;
  54. }
  55. $data['out_trade_no'] = $payload['order_info']['trade_no'];
  56. $data['transaction_id'] = $payload['order_info']['transaction_id'];
  57. $data['opneid'] = $payload['FromUserName'];
  58. if (Event::until('pay.notify', [$data])) {
  59. $response = 'success';
  60. } else {
  61. $response = 'faild';
  62. }
  63. Log::error(['data'=>$data,'res'=>$response,'message'=>$payload]);
  64. break;
  65. case 'trade_manage_remind_access_api': // 小程序完成账期授权时 小程序产生第一笔交易时 已产生交易但从未发货的小程序,每天一次
  66. break;
  67. case 'trade_manage_remind_shipping': // 曾经发过货的小程序,订单超过48小时未发货时
  68. break;
  69. case 'trade_manage_order_settlement': // 订单完成发货时 订单结算时
  70. if (isset($payload['estimated_settlement_time'])) { //订单完成发货时
  71. MiniProgram::notifyConfirmByTradeNo($payload['merchant_trade_no'], time());
  72. }
  73. if (isset($payload['confirm_receive_method'])) { // 订单结算时
  74. /** @var StoreOrderTakeServices $StoreOrderTakeServices */
  75. $storeOrderTakeServices = app()->make(StoreOrderTakeServices::class);
  76. $storeOrderTakeServices->miniOrderTakeOrder($payload['merchant_trade_no']);
  77. }
  78. break;
  79. }
  80. break;
  81. // ... 其它消息
  82. default:
  83. break;
  84. }
  85. } catch (\Throwable $e) {
  86. \think\facade\Log::error(['title' => '微信消息服务端消息执行错误', 'message' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()]);
  87. }
  88. return $response ?? false;
  89. }
  90. }