OPPOPushCommon.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. <?php
  2. namespace App\Libs\Push\OPPOPush;
  3. use Exception;
  4. use GuzzleHttp\Client;
  5. use GuzzleHttp\Exception\GuzzleException;
  6. class OPPOPushCommon
  7. {
  8. // app key
  9. private $_appKey;
  10. // master secret
  11. private $_masterSecret;
  12. // token
  13. private $_authToken;
  14. private $_reg_arr;
  15. /**
  16. * OPPOPushCommon constructor.
  17. * @param $appKey
  18. * @param $masterSecret
  19. * @throws GuzzleException
  20. */
  21. public function __construct($appKey, $masterSecret)
  22. {
  23. $this->_appKey = $appKey;
  24. $this->_masterSecret = $masterSecret;
  25. $this->_authToken = $this->getAuthToken();
  26. }
  27. public function setRegArr($regArr)
  28. {
  29. $this->_reg_arr = $regArr;
  30. }
  31. /**
  32. * 全部推送
  33. * @param $messageId
  34. * @return mixed
  35. * @throws GuzzleException
  36. */
  37. public function broadCastAll($messageId)
  38. {
  39. return $this->broadCast('all', $messageId);
  40. }
  41. /**
  42. * 批量推送
  43. * @param $messageId
  44. * @return mixed
  45. * @throws GuzzleException
  46. */
  47. public function broadCastRegIds($messageId)
  48. {
  49. return $this->broadCast(2, $messageId);
  50. }
  51. /**
  52. * 保存通知栏消息内容体
  53. * @param $title
  54. * @param $content
  55. * @param $url
  56. * @param array $params
  57. * @return mixed|string
  58. * @throws GuzzleException
  59. */
  60. public function getMessageId($title, $content, $url, $params = [])
  61. {
  62. // 组装数据
  63. $pushMessage = new PushMessage();
  64. $pushMessage->style(1);
  65. $pushMessage->title($title);
  66. $pushMessage->content($content);
  67. $pushMessage->click_action_type(1);
  68. $pushMessage->click_action_activity('com.nearme.instant.action.PUSH');
  69. $pushMessage->action_parameters(json_encode($params));
  70. $pushMessage->channel_id('OPPO PUSH');
  71. $pushMessage->auth_token($this->_authToken);
  72. $fields = $pushMessage->getData();
  73. // 请求数据
  74. $data = [
  75. 'form_params' => $fields
  76. ];
  77. // 请求
  78. $client = new Client(['base_uri' => config('push.server.oppo.saveMessage'), 'timeout' => 10.0,]);
  79. $response = $client->request('POST', '', $data);
  80. $body = $response->getBody();
  81. $result = json_decode($body, true);
  82. $messageId = getProp($result['data'], 'message_id');
  83. if (!$messageId) {
  84. throw new Exception('保存通知栏消息内容体失败');
  85. }
  86. $this->logPush(__FUNCTION__, 'result', compact('data', 'messageId'));
  87. return $messageId;
  88. }
  89. /**
  90. * 添加标签
  91. * @param $name
  92. * @param $desc
  93. * @return mixed
  94. * @throws GuzzleException
  95. */
  96. public function addTags($name, $desc)
  97. {
  98. $data = [
  99. 'headers' => [
  100. 'auth_token' => $this->_authToken
  101. ],
  102. 'json' => [
  103. 'name' => $name,
  104. 'desc' => $desc,
  105. ]
  106. ];
  107. // 请求
  108. $client = new Client(['base_uri' => config('push.server.oppo.addTags'), 'timeout' => 10.0,]);
  109. $response = $client->request('POST', '', $data);
  110. $body = $response->getBody();
  111. $result = json_decode($body, true);
  112. $this->logPush(__FUNCTION__, 'result', compact('data', 'result'));
  113. return $result;
  114. }
  115. /**
  116. * 标签订阅接口
  117. * @param $regId
  118. * @param $tags
  119. * @return mixed
  120. * @throws GuzzleException
  121. */
  122. public function subscribeTags($regId, $tags)
  123. {
  124. $data = [
  125. 'headers' => [
  126. 'auth_token' => $this->_authToken
  127. ],
  128. 'json' => [
  129. 'registration_id' => $regId,
  130. 'tags' => $tags,
  131. ]
  132. ];
  133. // 请求
  134. $client = new Client(['base_uri' => config('push.server.oppo.subTags'), 'timeout' => 10.0,]);
  135. $response = $client->request('POST', '', $data);
  136. $body = $response->getBody();
  137. $result = json_decode($body, true);
  138. $this->logPush(__FUNCTION__, 'result', compact('data', 'result'));
  139. return $result;
  140. }
  141. /**
  142. * 取消标签订阅接口
  143. * @param $regId
  144. * @param $tags
  145. * @return mixed
  146. * @throws GuzzleException
  147. */
  148. public function unSubscribeTags($regId, $tags)
  149. {
  150. $data = [
  151. 'headers' => [
  152. 'auth_token' => $this->_authToken
  153. ],
  154. 'json' => [
  155. 'registration_id' => $regId,
  156. 'tags' => $tags,
  157. ]
  158. ];
  159. // 请求
  160. $client = new Client(['base_uri' => config('push.server.oppo.unSubTags'), 'timeout' => 10.0,]);
  161. $response = $client->request('POST', '', $data);
  162. $body = $response->getBody();
  163. $result = json_decode($body, true);
  164. $this->logPush(__FUNCTION__, 'result', compact('data', 'result'));
  165. return $result;
  166. }
  167. /**
  168. * 取消标签订阅接口
  169. * @param $regId
  170. * @return mixed
  171. * @throws GuzzleException
  172. */
  173. public function getAllTags($regId)
  174. {
  175. $data = [
  176. 'headers' => [
  177. 'auth_token' => $this->_authToken
  178. ],
  179. 'json' => [
  180. 'registration_id' => $regId,
  181. ]
  182. ];
  183. // 请求
  184. $client = new Client(['base_uri' => config('push.server.oppo.getAllTags'), 'timeout' => 10.0,]);
  185. $response = $client->request('POST', '', $data);
  186. $body = $response->getBody();
  187. $result = json_decode($body, true);
  188. $this->logPush(__FUNCTION__, 'result', compact('data', 'result'));
  189. return $result;
  190. }
  191. /**
  192. * 查询APP当天发送量的状态
  193. * @return mixed
  194. * @throws GuzzleException
  195. */
  196. public function fetchPushPermit()
  197. {
  198. $data = [
  199. 'headers' => [
  200. 'auth_token' => $this->_authToken
  201. ]
  202. ];
  203. // 请求
  204. $client = new Client(['base_uri' => config('push.server.oppo.fetchPushPermit'), 'timeout' => 10.0,]);
  205. $response = $client->request('GET', '', $data);
  206. $body = $response->getBody();
  207. $result = json_decode($body, true);
  208. $this->logPush(__FUNCTION__, 'result', compact('data', 'result'));
  209. return $result;
  210. }
  211. /**
  212. * 获得auth_token权限令牌
  213. * @return mixed|string
  214. * @throws GuzzleException
  215. */
  216. private function getAuthToken()
  217. {
  218. // 生成sign
  219. $timestamp = getMillisecond();
  220. $sign = hash('sha256', $this->_appKey . $timestamp . $this->_masterSecret);
  221. // 数据组装
  222. $data = [
  223. 'form_params' => [
  224. 'app_key' => $this->_appKey,
  225. 'sign' => $sign,
  226. 'timestamp' => $timestamp
  227. ]
  228. ];
  229. $client = new Client(['base_uri' => config('push.server.oppo.auth'), 'timeout' => 10.0,]);
  230. $response = $client->request('POST', '', $data);
  231. $body = $response->getBody();
  232. $result = json_decode($body, true);
  233. $authToken = getProp($result['data'], 'auth_token');
  234. if (!$authToken) {
  235. throw new Exception('获取auth token失败');
  236. }
  237. $this->logPush(__FUNCTION__, 'result', compact('authToken'));
  238. return $authToken;
  239. }
  240. /**
  241. * 广播推送-通知栏消息
  242. * @param $targetType
  243. * @param $messageId
  244. * @return mixed
  245. * @throws GuzzleException
  246. */
  247. private function broadCast($targetType, $messageId)
  248. {
  249. $param = [
  250. 'auth_token' => $this->_authToken,
  251. 'message_id' => $messageId,
  252. 'target_type' => $targetType,
  253. ];
  254. // reg_id 批量
  255. if ((int)$targetType === 2 && $this->_reg_arr) {
  256. $param['target_value'] = implode(',', $this->_reg_arr);
  257. }
  258. // 请求数据
  259. $data = [
  260. 'form_params' => $param
  261. ];
  262. // 请求
  263. $client = new Client(['base_uri' => config('push.server.oppo.broadcast'), 'timeout' => 10.0,]);
  264. $response = $client->request('POST', '', $data);
  265. $body = $response->getBody();
  266. $result = json_decode($body, true);
  267. $this->logPush(__FUNCTION__, 'result', compact('targetType', 'messageId', 'result'));
  268. return $result;
  269. }
  270. private function logPush($className, $message, $data = [])
  271. {
  272. var_dump('[' . $className . '] ' . $message, $data);
  273. myLog('push')->info('[OPPO] [' . $className . '] ' . $message, $data);
  274. }
  275. }