OutPushListener.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. namespace app\listener\out;
  3. use app\jobs\OutPushJob;
  4. use app\services\out\OutAccountServices;
  5. use crmeb\interfaces\ListenerInterface;
  6. use crmeb\services\CacheService;
  7. use crmeb\services\HttpService;
  8. use think\facade\Log;
  9. class OutPushListener implements ListenerInterface
  10. {
  11. public function handle($event): void
  12. {
  13. [$type, $data] = $event;
  14. /** @var OutAccountServices $outAccountServices */
  15. $outAccountServices = app()->make(OutAccountServices::class);
  16. $outAccountList = $outAccountServices->selectList(['is_del' => 0, 'status' => 1])->toArray();
  17. foreach ($outAccountList as $item) {
  18. if ($item['push_open'] == 1) {
  19. $token = $this->getPushToken($item);
  20. if ($type == 'order_create_push') {
  21. OutPushJob::dispatch('orderCreate', [$data['order_id'], $item['order_create_push'] . '?pushToken=' . $token]);
  22. } elseif ($type == 'order_pay_push') {
  23. OutPushJob::dispatch('paySuccess', [$data['order_id'], $item['order_pay_push'] . '?pushToken=' . $token]);
  24. } elseif ($type == 'refund_create_push') {
  25. OutPushJob::dispatch('refundCreate', [$data['order_id'], $item['refund_create_push'] . '?pushToken=' . $token]);
  26. } elseif ($type == 'refund_cancel_push') {
  27. OutPushJob::dispatch('refundCancel', [$data['order_id'], $item['refund_cancel_push'] . '?pushToken=' . $token]);
  28. } elseif ($type == 'user_update_push') {
  29. OutPushJob::dispatch('userUpdate', [$data, $item['user_update_push'] . '?pushToken=' . $token]);
  30. }
  31. }
  32. }
  33. }
  34. /**
  35. * 获取推送token
  36. * @param array $info
  37. * @return false|mixed
  38. */
  39. public function getPushToken(array $info)
  40. {
  41. $token = CacheService::get('pushToken' . $info['id']);
  42. if (!$token) {
  43. $param = json_encode(['push_account' => $info['push_account'], 'push_password' => $info['push_password']], JSON_UNESCAPED_UNICODE);
  44. $res = HttpService::postRequest($info['push_token_url'], $param, ['Content-Type:application/json', 'Content-Length:' . strlen($param)]);
  45. $res = $res ? json_decode($res, true) : [];
  46. if (!$res || !isset($res['code']) || $res['code'] != 0) {
  47. Log::error(['msg' => $info['title'] . ',获取token失败']);
  48. return false;
  49. }
  50. CacheService::set('pushToken' . $info['id'], $res['token'], $res['time']);
  51. return $res['token'];
  52. } else {
  53. return $token;
  54. }
  55. }
  56. }