OPPOPushCommon.php 8.6 KB

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