Application.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. <?php
  2. namespace App\Libs\Push\HuaWei\Admin;
  3. class Application
  4. {
  5. private $appid;
  6. private $appsecret;
  7. private $token_expiredtime;
  8. private $access_token;
  9. private $validate_only;
  10. private $token_url;
  11. private $send_url;
  12. private $token_query_url;
  13. private $topic_sub_url;
  14. private $topic_unsub_url;
  15. private $topic_list_url;
  16. private $fields;
  17. public function __construct($appid, $appsecret)
  18. {
  19. $this->appid = $appid;
  20. $this->appsecret = $appsecret;
  21. $this->token_url = config('push.server.huawei.authToken');
  22. $this->send_url = config('push.server.huawei.messageSend');
  23. $this->token_query_url = config('push.server.huawei.tokenQuery');
  24. $this->topic_sub_url = config('push.server.huawei.topicSub');
  25. $this->topic_unsub_url = config('push.server.huawei.topicUnSub');
  26. $this->topic_list_url = config('push.server.huawei.topicList');
  27. $this->token_expiredtime = null;
  28. $this->accesstoken = null;
  29. $this->validate_only = false;
  30. }
  31. public function appid($value)
  32. {
  33. $this->appid = $value;
  34. }
  35. public function appsecret($value)
  36. {
  37. $this->appsecret = $value;
  38. }
  39. public function validate_only($value)
  40. {
  41. $this->validate_only = $value;
  42. }
  43. public function getApplicationFields()
  44. {
  45. $keys = [
  46. 'appid',
  47. 'appsecret',
  48. 'token_url',
  49. 'send_url',
  50. 'token_query_url',
  51. 'topic_sub_url',
  52. 'topic_unsub_url',
  53. 'topic_list_url',
  54. 'validate_only',
  55. 'accesstoken',
  56. 'token_expiredtime'
  57. ];
  58. foreach ($keys as $key) {
  59. if (isset($this->$key)) {
  60. $this->fields[$key] = $this->$key;
  61. }
  62. }
  63. return $this->fields;
  64. }
  65. private function logPush($className, $message, $data = [])
  66. {
  67. var_dump('[' . $className . '] ' . $message, $data);
  68. myLog('push')->info('[HuaWei] [' . $className . '] ' . $message, compact('data'));
  69. }
  70. private function is_token_expired()
  71. {
  72. if (empty($this->accesstoken)) {
  73. return true;
  74. }
  75. if (time() > $this->token_expiredtime) {
  76. return true;
  77. }
  78. return false;
  79. }
  80. private function refresh_token()
  81. {
  82. // 请求地址
  83. $result = $this->curl_https_post($this->token_url, http_build_query([
  84. 'grant_type' => 'client_credentials',
  85. 'client_secret' => $this->appsecret,
  86. 'client_id' => $this->appid
  87. ]), ['Content-Type: application/x-www-form-urlencoded;charset=utf-8']);
  88. $result = json_decode($result, true);
  89. // 判断
  90. if ($result === null || !array_key_exists('access_token', $result)) {
  91. $this->logPush(__FUNCTION__, 'refresh_token result error!');
  92. return null;
  93. }
  94. $this->logPush(__FUNCTION__, 'refresh_token result:', $result);
  95. $this->accesstoken = getProp($result, 'access_token');
  96. $this->token_expiredtime = time() + getProp($result, 'expires_in');
  97. return $this->access_token;
  98. }
  99. /**
  100. * push_send_msg for push msg
  101. */
  102. public function push_send_msg($msg)
  103. {
  104. $body = [
  105. 'validate_only' => $this->validate_only,
  106. 'message' => $msg
  107. ];
  108. $this->logPush(__FUNCTION__, 'push_send_msg body:', $body);
  109. if ($this->is_token_expired()) {
  110. $this->refresh_token();
  111. }
  112. if (empty($this->accesstoken)) {
  113. $this->logPush(__FUNCTION__, 'accesstoken is empty!');
  114. return null;
  115. }
  116. $url = str_replace('{appid}', $this->appid, $this->send_url);
  117. $header = ['Content-Type: application/json', "Authorization: Bearer {$this->accesstoken}"];
  118. $result = $this->curl_https_post($url, json_encode($body), $header);
  119. $result = json_decode($result, true);
  120. $this->logPush(__FUNCTION__, 'push_send_msg result:', $result);
  121. return $result;
  122. }
  123. /**
  124. * common_send_msg for topic msg/other
  125. */
  126. public function common_send_msg($msg)
  127. {
  128. $this->logPush(__FUNCTION__, 'common_send_msg msg:', compact('msg'));
  129. if ($this->is_token_expired()) {
  130. $this->refresh_token();
  131. }
  132. if (empty($this->accesstoken)) {
  133. $this->logPush(__FUNCTION__, 'accesstoken is empty!');
  134. return null;
  135. }
  136. $url = str_replace('{appid}', $this->appid, $this->send_url);
  137. $header = ['Content-Type: application/json', "Authorization: Bearer {$this->accesstoken}"];
  138. $result = $this->curl_https_post($url, json_encode($msg), $header);
  139. $result = json_decode($result, true);
  140. $this->logPush(__FUNCTION__, 'common_send_msg result:', $result);
  141. return $result;
  142. }
  143. /**
  144. * 数据控制者数据查询
  145. * @param $pushToken
  146. * @return mixed|null
  147. */
  148. public function query_push_token($pushToken)
  149. {
  150. $body = ['token' => $pushToken];
  151. $this->logPush(__FUNCTION__, 'query_push_token body:', $body);
  152. if ($this->is_token_expired()) {
  153. $this->refresh_token();
  154. }
  155. if (empty($this->accesstoken)) {
  156. $this->logPush(__FUNCTION__, 'accesstoken is empty!');
  157. return null;
  158. }
  159. $url = str_replace('{appid}', $this->appid, $this->token_query_url);
  160. $header = ['Content-Type: application/json', "Authorization: Bearer {$this->accesstoken}"];
  161. $result = $this->curl_https_post($url, json_encode($body), $header);
  162. $result = json_decode($result, true);
  163. $this->logPush(__FUNCTION__, 'common_send_msg result:', $result);
  164. return $result;
  165. }
  166. /**
  167. * 主题订阅
  168. * @param $topic
  169. * @param array $tokenArray
  170. * @return bool|mixed|string|null
  171. */
  172. public function subscribe_topic($topic, array $tokenArray)
  173. {
  174. $body = ['topic' => $topic, 'tokenArray' => $tokenArray];
  175. $this->logPush(__FUNCTION__, 'subscribe_topic body:', $body);
  176. if ($this->is_token_expired()) {
  177. $this->refresh_token();
  178. }
  179. if (empty($this->accesstoken)) {
  180. $this->logPush(__FUNCTION__, 'accesstoken is empty!');
  181. return null;
  182. }
  183. $url = str_replace('{appid}', $this->appid, $this->topic_sub_url);
  184. $header = ['Content-Type: application/json', "Authorization: Bearer {$this->accesstoken}"];
  185. $result = $this->curl_https_post($url, json_encode($body), $header);
  186. $result = json_decode($result, true);
  187. $this->logPush(__FUNCTION__, 'subscribe_topic result:', $result);
  188. return $result;
  189. }
  190. /**
  191. * 主题退订
  192. * @param $topic
  193. * @param array $tokenArray
  194. * @return bool|mixed|string|null
  195. */
  196. public function unsubscribe_topic($topic, array $tokenArray)
  197. {
  198. $body = ['topic' => $topic, 'tokenArray' => $tokenArray];
  199. $this->logPush(__FUNCTION__, 'unsubscribe_topic body:', $body);
  200. if ($this->is_token_expired()) {
  201. $this->refresh_token();
  202. }
  203. if (empty($this->accesstoken)) {
  204. $this->logPush(__FUNCTION__, 'accesstoken is empty!');
  205. return null;
  206. }
  207. $url = str_replace('{appid}', $this->appid, $this->topic_unsub_url);
  208. $header = ['Content-Type: application/json', "Authorization: Bearer {$this->accesstoken}"];
  209. $result = $this->curl_https_post($url, json_encode($body), $header);
  210. $result = json_decode($result, true);
  211. $this->logPush(__FUNCTION__, 'unsubscribe_topic result:', $result);
  212. return $result;
  213. }
  214. /**
  215. * 主题列表
  216. * @param $token
  217. * @return bool|mixed|string|null
  218. */
  219. public function topic_list($token)
  220. {
  221. $body = ['token' => $token];
  222. $this->logPush(__FUNCTION__, 'topic_list body:', $body);
  223. if ($this->is_token_expired()) {
  224. $this->refresh_token();
  225. }
  226. if (empty($this->accesstoken)) {
  227. $this->logPush(__FUNCTION__, 'accesstoken is empty!');
  228. return null;
  229. }
  230. $url = str_replace('{appid}', $this->appid, $this->topic_list_url);
  231. $header = ['Content-Type: application/json', "Authorization: Bearer {$this->accesstoken}"];
  232. $result = $this->curl_https_post($url, json_encode($body), $header);
  233. $result = json_decode($result, true);
  234. $this->logPush(__FUNCTION__, 'topic_list result:', $result);
  235. return $result;
  236. }
  237. private function curl_https_post($url, $data = [], $header = [])
  238. {
  239. $this->logPush(__FUNCTION__, 'curl_https_post:', compact('url', 'data'));
  240. $ch = curl_init($url);
  241. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
  242. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  243. // resolve SSL: no alternative certificate subject name matches target host name
  244. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // check verify
  245. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  246. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  247. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
  248. curl_setopt($ch, CURLOPT_TIMEOUT, 5);
  249. curl_setopt($ch, CURLOPT_POST, 1); // regular post request
  250. curl_setopt($ch, CURLOPT_POSTFIELDS, $data); // Post submit data
  251. $ret = @curl_exec($ch);
  252. if ($ret === false) {
  253. return null;
  254. }
  255. $info = curl_getinfo($ch);
  256. curl_close($ch);
  257. return $ret;
  258. }
  259. }