Push.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. namespace App\Master\Framework\Library\GeTui;
  3. use App\Master\Enum\RedisKeyEnum;
  4. use App\Master\Framework\Library\Library;
  5. use App\Utils\Common;
  6. use App\Utils\RedisUtil;
  7. class Push extends Library
  8. {
  9. protected string $base_uri = "https://restapi.getui.com/v2/";
  10. private string $appId;
  11. private string $appKey;
  12. private string $appSecret;
  13. private string $masterSecret;
  14. public function __construct()
  15. {
  16. $this->appId = site('gt_app_id');
  17. $this->appKey = site('gt_app_key');
  18. $this->appSecret = site('gt_app_secret');
  19. $this->masterSecret = site('gt_master_secret');
  20. }
  21. /**
  22. * 消息推送
  23. * @param string $cid 设备ID
  24. * @param string $title 消息标题
  25. * @param string $body 消息内容
  26. * @param int $type 1=通知,2=透传
  27. * @param string $platform 平台
  28. * @return bool
  29. */
  30. public function push(string $cid, string $title, string $body,string $ring_name, int $type = 1, string $platform = 'android')
  31. {
  32. if (!$access_token = $this->auth()) {
  33. return $this->error('ge tui token error');
  34. }
  35. // 通知
  36. $notification = [
  37. "notification" => [
  38. "title" => $title,
  39. "body" => $body,
  40. "ring_name" => str_replace('.mp3', '', $ring_name),
  41. "channel_level" => 4,
  42. "channel_id" => str_replace('.mp3', '', $ring_name),
  43. "channel_name" => str_replace('.mp3', '', $ring_name),
  44. "click_type" => "startapp",//startapp:打开应用首页,payload:自定义消息内容启动应用,
  45. "payload" => json_encode(['t' => time()])
  46. ],
  47. ];
  48. // 透传
  49. $transmission = [
  50. "transmission" => json_encode([
  51. "title" => $title,
  52. "body" => $body,
  53. "ring_name" => $ring_name,
  54. "t" => time(),
  55. ], JSON_UNESCAPED_UNICODE)
  56. ];
  57. switch ($type) {
  58. case 1:
  59. $push_message = $notification;
  60. break;
  61. case 2:
  62. $push_message = $transmission;
  63. break;
  64. default:
  65. $push_message = $notification;
  66. }
  67. if ($platform == 'ios') {
  68. $push_channel = [
  69. "ios" => [
  70. "type" => "notify",
  71. "payload" => json_encode([
  72. "title" => $title,
  73. "body" => $body,
  74. ], JSON_UNESCAPED_UNICODE),
  75. "aps" => [
  76. "alert" => [
  77. "title" => $title,
  78. "body" => $body,
  79. ],
  80. "content-available" => 0,
  81. "sound" => $ring_name,
  82. "category" => "ACTIONABLE",
  83. ],
  84. "auto_badge" => "+1",
  85. ]
  86. ];
  87. $push_message = $transmission;
  88. }
  89. return $this->send($cid, $push_message, $access_token, $push_channel ?? []);
  90. }
  91. /**
  92. * 发送
  93. * @param $cid
  94. * @param $push_message
  95. * @param $access_token
  96. * @return bool
  97. */
  98. private function send($cid, $push_message, $access_token, $push_channel)
  99. {
  100. $params = [
  101. 'request_id' => Common::createNo('GT', 20),
  102. 'settings' => [
  103. "ttl" => 7200000
  104. ],
  105. "audience" => [
  106. "cid" => [
  107. $cid
  108. ]
  109. ],
  110. "push_message" => $push_message
  111. ];
  112. if (!empty($push_channel)) {
  113. $params['push_channel'] = $push_channel;
  114. }
  115. $response = $this->post('/push/single/cid', $params, [
  116. 'token' => $access_token
  117. ]);
  118. if ($response->getStatusCode() != 200) {
  119. return $this->error($response->getReasonPhrase());
  120. }
  121. $json = $response->getBody()->getContents();
  122. $body = json_decode($json, true);
  123. return $this->success('操作成功', $body);
  124. }
  125. /**
  126. * 获取token
  127. * @return false|mixed
  128. */
  129. public function auth()
  130. {
  131. if ($access_token = RedisUtil::getInstance(RedisKeyEnum::UNI_PUSH_TOKEN)->get()) {
  132. return $access_token;
  133. }
  134. $timestamp = ms_time();
  135. $response = $this->post('/auth', [
  136. 'sign' => hash('sha256', "{$this->appKey}{$timestamp}{$this->masterSecret}"),
  137. 'timestamp' => $timestamp,
  138. 'appkey' => $this->appKey
  139. ]);
  140. if ($response->getStatusCode() != 200) {
  141. return false;
  142. }
  143. $json = $response->getBody()->getContents();
  144. $data = json_decode($json, true);
  145. RedisUtil::getInstance(RedisKeyEnum::UNI_PUSH_TOKEN)->setex($data['data']['token'], 7000);
  146. return $data['data']['token'];
  147. }
  148. private function post(string $uri, array $params = [], array $header = [])
  149. {
  150. return $this->postJson($this->appId.$uri,$params,$header);
  151. }
  152. }