PushMessageService.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace App\Modules\Push\Services;
  3. use App\Consts\ErrorConst;
  4. use App\Consts\PushConst;
  5. use App\Libs\Push\HuaWei\HwPushCommon;
  6. use App\Libs\Push\OPPOPush\OPPOPushCommon;
  7. use App\Libs\Push\XMPush\MiPushCommon;
  8. use App\Libs\Utils;
  9. use App\Modules\Push\Models\QappPushApp;
  10. use App\Modules\Push\Models\QappPushUser;
  11. use Exception;
  12. use GuzzleHttp\Exception\GuzzleException;
  13. use App\Exceptions\ApiException;
  14. use App\Libs\Push\VPush\VPush;
  15. class PushMessageService
  16. {
  17. const providers = [
  18. 'huawei' => '华为',
  19. 'oppo' => 'OPPO',
  20. 'vivo' => 'VIVO',
  21. 'xiaomi' => '小米',
  22. ];
  23. public static function contentFormat(string $content, string $provider): string
  24. {
  25. return str_replace('{device}', self::providers[$provider], $content);
  26. }
  27. /**
  28. * @param $uid
  29. * @param $title
  30. * @param $content
  31. * @param $url
  32. * @return array|bool|mixed|string|null
  33. * @throws ApiException
  34. * @throws GuzzleException
  35. */
  36. public static function pushMessageToUser($uid, $title, $content, $url)
  37. {
  38. // 获取用户push信息
  39. $pushUser = QappPushUser::getPushUserByUid($uid);
  40. $regId = getProp($pushUser, 'reg_id');
  41. $regIdList = [$regId];
  42. if (empty($regId)) {
  43. Utils::throwError(ErrorConst::PUSH_TOKEN_INVALID);
  44. }
  45. // 获取push app信息
  46. $pushAppId = getProp($pushUser, 'app_id');
  47. $pushApp = QappPushApp::getPushAppByAppId($pushAppId);
  48. $package = getProp($pushApp, 'package');
  49. $appId = getProp($pushApp, 'app_id');
  50. $provider = getProp($pushApp, 'provider');
  51. $appSecret = getProp($pushApp, 'app_secret');
  52. $appKey = getProp($pushApp, 'app_key');
  53. $masterSecret = getProp($pushApp, 'master_secret');
  54. $content = self::contentFormat($content, $provider);
  55. $result = [];
  56. try {
  57. switch ($provider) {
  58. // 华为
  59. case PushConst::PROVIDER_HW:
  60. // 初始化huawei推送
  61. $client = new HwPushCommon($appId, $appSecret);
  62. // 循环推送
  63. $client->setToken($regIdList);
  64. $result = $client->sendPushMessage($title, $content, $url);
  65. break;
  66. // 小米
  67. case PushConst::PROVIDER_MI:
  68. // 初始化小米推送
  69. $client = new MiPushCommon($package, $appSecret);
  70. // 循环推送
  71. $client->setRegArr($regIdList);
  72. $result = $client->sendMessage($title, $content, $url);
  73. break;
  74. // OPPO
  75. case PushConst::PROVIDER_OPPO:
  76. // 初始化oppo推送
  77. $client = new OPPOPushCommon($appKey, $masterSecret);
  78. $messageId = $client->getMessageId($title, $content, $url);
  79. // 循环推送
  80. $client->setRegArr($regIdList);
  81. $result = $client->broadCastRegIds($messageId);
  82. break;
  83. case PushConst::PROVIDER_VIVO:
  84. $client = new VPush($appId, $appKey, $appSecret);
  85. $result = $client->send($regId, $title, $content, $url);
  86. break;
  87. }
  88. } catch (Exception $e) {
  89. $message = $e->getMessage();
  90. myLog('push')->info('Exception', compact('result', 'message'));
  91. Utils::throwError(ErrorConst::PUSH_FAIELD);
  92. }
  93. return $result;
  94. }
  95. }