OPPOPushCommon.php 9.5 KB

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