AppPushService.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace services\extend\push;
  3. use Yii;
  4. use common\enums\AppPushEnum;
  5. use common\enums\AccessTokenGroupEnum;
  6. use common\models\member\Auth;
  7. use common\queues\AppPushJob;
  8. /**
  9. * app 推送
  10. *
  11. * Class AppPushService
  12. * @package services\extend
  13. */
  14. class AppPushService
  15. {
  16. /**
  17. * @var bool
  18. */
  19. public $queueSwitch = false;
  20. /**
  21. * 推送类型
  22. *
  23. * @var string
  24. */
  25. protected $client;
  26. /**
  27. * @param $type
  28. */
  29. public function client($client)
  30. {
  31. $this->client = $client;
  32. return $this;
  33. }
  34. /**
  35. * @param $title
  36. * @param $content
  37. * @param Auth $auth
  38. * @param array $transmissionContent
  39. * @return string|void|null
  40. * @throws \yii\base\InvalidConfigException
  41. */
  42. public function push($title, $content, Auth $auth, $transmissionContent = [])
  43. {
  44. if (empty($type = $this->client)) {
  45. $type = Yii::$app->services->config->backendConfig('plus_plus');
  46. }
  47. // 是否进入队列
  48. if ($this->queueSwitch == true) {
  49. $messageId = Yii::$app->queue->push(new AppPushJob([
  50. 'title' => $title,
  51. 'content' => $content,
  52. 'type' => $type,
  53. 'auth' => $auth,
  54. 'transmissionContent' => $transmissionContent,
  55. ]));
  56. return $messageId;
  57. } else {
  58. $this->realPlus($type, $title, $content, $auth, $transmissionContent);
  59. }
  60. }
  61. /**
  62. *
  63. * @param $type
  64. * @param $title
  65. * @param $content
  66. * @param Auth $auth
  67. * @param array $transmissionContent
  68. * @throws \yii\base\InvalidConfigException
  69. */
  70. public function realPlus($type, $title, $content, Auth $auth, $transmissionContent = [])
  71. {
  72. switch ($type) {
  73. case AppPushEnum::GE_TUI :
  74. // 个推
  75. if ($auth->oauth_client == AccessTokenGroupEnum::IOS) {
  76. Yii::$app->services->extendGeTui->client()->ios($title, $content, $auth->oauth_client_user_id, $transmissionContent);
  77. } else {
  78. Yii::$app->services->extendGeTui->client()->android($title, $content, $auth->oauth_client_user_id, $transmissionContent);
  79. }
  80. break;
  81. case AppPushEnum::J_PUSH :
  82. // 极光推送
  83. Yii::$app->services->extendJPush->client()->send($title, $content, $auth->oauth_client_user_id, $transmissionContent);
  84. break;
  85. }
  86. }
  87. }
粤ICP备19079148号