OPPOPushCommon.php 8.4 KB

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