123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329 |
- <?php
- namespace App\Libs\Push\HuaWei\Admin;
- class Application
- {
- private $appid;
- private $appsecret;
- private $token_expiredtime;
- private $access_token;
- private $validate_only;
- private $token_url;
- private $send_url;
- private $token_query_url;
- private $topic_sub_url;
- private $topic_unsub_url;
- private $topic_list_url;
- private $fields;
- public function __construct($appid, $appsecret)
- {
- $this->appid = $appid;
- $this->appsecret = $appsecret;
- $this->token_url = config('push.server.huawei.authToken');
- $this->send_url = config('push.server.huawei.messageSend');
- $this->token_query_url = config('push.server.huawei.tokenQuery');
- $this->topic_sub_url = config('push.server.huawei.topicSub');
- $this->topic_unsub_url = config('push.server.huawei.topicUnSub');
- $this->topic_list_url = config('push.server.huawei.topicList');
- $this->token_expiredtime = null;
- $this->accesstoken = null;
- $this->validate_only = false;
- }
- public function appid($value)
- {
- $this->appid = $value;
- }
- public function appsecret($value)
- {
- $this->appsecret = $value;
- }
- public function validate_only($value)
- {
- $this->validate_only = $value;
- }
- public function getApplicationFields()
- {
- $keys = [
- 'appid',
- 'appsecret',
- 'token_url',
- 'send_url',
- 'token_query_url',
- 'topic_sub_url',
- 'topic_unsub_url',
- 'topic_list_url',
- 'validate_only',
- 'accesstoken',
- 'token_expiredtime'
- ];
- foreach ($keys as $key) {
- if (isset($this->$key)) {
- $this->fields[$key] = $this->$key;
- }
- }
- return $this->fields;
- }
- private function logPush($className, $message, $data = [])
- {
- var_dump('[' . $className . '] ' . $message, $data);
- myLog('push')->info('[HuaWei] [' . $className . '] ' . $message, compact('data'));
- }
- private function is_token_expired()
- {
- if (empty($this->accesstoken)) {
- return true;
- }
- if (time() > $this->token_expiredtime) {
- return true;
- }
- return false;
- }
- private function refresh_token()
- {
- // 请求地址
- $result = $this->curl_https_post($this->token_url, http_build_query([
- 'grant_type' => 'client_credentials',
- 'client_secret' => $this->appsecret,
- 'client_id' => $this->appid
- ]), ['Content-Type: application/x-www-form-urlencoded;charset=utf-8']);
- $result = json_decode($result, true);
- // 判断
- if ($result === null || !array_key_exists('access_token', $result)) {
- $this->logPush(__FUNCTION__, 'refresh_token result error!');
- return null;
- }
- $this->logPush(__FUNCTION__, 'refresh_token result:', $result);
- $this->accesstoken = getProp($result, 'access_token');
- $this->token_expiredtime = time() + getProp($result, 'expires_in');
- return $this->access_token;
- }
- /**
- * push_send_msg for push msg
- */
- public function push_send_msg($msg)
- {
- $body = [
- 'validate_only' => $this->validate_only,
- 'message' => $msg
- ];
- $this->logPush(__FUNCTION__, 'push_send_msg body:', $body);
- if ($this->is_token_expired()) {
- $this->refresh_token();
- }
- if (empty($this->accesstoken)) {
- $this->logPush(__FUNCTION__, 'accesstoken is empty!');
- return null;
- }
- $url = str_replace('{appid}', $this->appid, $this->send_url);
- $header = ['Content-Type: application/json', "Authorization: Bearer {$this->accesstoken}"];
- $result = $this->curl_https_post($url, json_encode($body), $header);
- $result = json_decode($result, true);
- $this->logPush(__FUNCTION__, 'push_send_msg result:', $result);
- return $result;
- }
- /**
- * common_send_msg for topic msg/other
- */
- public function common_send_msg($msg)
- {
- $this->logPush(__FUNCTION__, 'common_send_msg msg:', compact('msg'));
- if ($this->is_token_expired()) {
- $this->refresh_token();
- }
- if (empty($this->accesstoken)) {
- $this->logPush(__FUNCTION__, 'accesstoken is empty!');
- return null;
- }
- $url = str_replace('{appid}', $this->appid, $this->send_url);
- $header = ['Content-Type: application/json', "Authorization: Bearer {$this->accesstoken}"];
- $result = $this->curl_https_post($url, json_encode($msg), $header);
- $result = json_decode($result, true);
- $this->logPush(__FUNCTION__, 'common_send_msg result:', $result);
- return $result;
- }
- /**
- * 数据控制者数据查询
- * @param $pushToken
- * @return mixed|null
- */
- public function query_push_token($pushToken)
- {
- $body = ['token' => $pushToken];
- $this->logPush(__FUNCTION__, 'query_push_token body:', $body);
- if ($this->is_token_expired()) {
- $this->refresh_token();
- }
- if (empty($this->accesstoken)) {
- $this->logPush(__FUNCTION__, 'accesstoken is empty!');
- return null;
- }
- $url = str_replace('{appid}', $this->appid, $this->token_query_url);
- $header = ['Content-Type: application/json', "Authorization: Bearer {$this->accesstoken}"];
- $result = $this->curl_https_post($url, json_encode($body), $header);
- $result = json_decode($result, true);
- $this->logPush(__FUNCTION__, 'common_send_msg result:', $result);
- return $result;
- }
- /**
- * 主题订阅
- * @param $topic
- * @param array $tokenArray
- * @return bool|mixed|string|null
- */
- public function subscribe_topic($topic, array $tokenArray)
- {
- $body = ['topic' => $topic, 'tokenArray' => $tokenArray];
- $this->logPush(__FUNCTION__, 'subscribe_topic body:', $body);
- if ($this->is_token_expired()) {
- $this->refresh_token();
- }
- if (empty($this->accesstoken)) {
- $this->logPush(__FUNCTION__, 'accesstoken is empty!');
- return null;
- }
- $url = str_replace('{appid}', $this->appid, $this->topic_sub_url);
- $header = ['Content-Type: application/json', "Authorization: Bearer {$this->accesstoken}"];
- $result = $this->curl_https_post($url, json_encode($body), $header);
- $result = json_decode($result, true);
- $this->logPush(__FUNCTION__, 'subscribe_topic result:', $result);
- return $result;
- }
- /**
- * 主题退订
- * @param $topic
- * @param array $tokenArray
- * @return bool|mixed|string|null
- */
- public function unsubscribe_topic($topic, array $tokenArray)
- {
- $body = ['topic' => $topic, 'tokenArray' => $tokenArray];
- $this->logPush(__FUNCTION__, 'unsubscribe_topic body:', $body);
- if ($this->is_token_expired()) {
- $this->refresh_token();
- }
- if (empty($this->accesstoken)) {
- $this->logPush(__FUNCTION__, 'accesstoken is empty!');
- return null;
- }
- $url = str_replace('{appid}', $this->appid, $this->topic_unsub_url);
- $header = ['Content-Type: application/json', "Authorization: Bearer {$this->accesstoken}"];
- $result = $this->curl_https_post($url, json_encode($body), $header);
- $result = json_decode($result, true);
- $this->logPush(__FUNCTION__, 'unsubscribe_topic result:', $result);
- return $result;
- }
- /**
- * 主题列表
- * @param $token
- * @return bool|mixed|string|null
- */
- public function topic_list($token)
- {
- $body = ['token' => $token];
- $this->logPush(__FUNCTION__, 'topic_list body:', $body);
- if ($this->is_token_expired()) {
- $this->refresh_token();
- }
- if (empty($this->accesstoken)) {
- $this->logPush(__FUNCTION__, 'accesstoken is empty!');
- return null;
- }
- $url = str_replace('{appid}', $this->appid, $this->topic_list_url);
- $header = ['Content-Type: application/json', "Authorization: Bearer {$this->accesstoken}"];
- $result = $this->curl_https_post($url, json_encode($body), $header);
- $result = json_decode($result, true);
- $this->logPush(__FUNCTION__, 'topic_list result:', $result);
- return $result;
- }
- private function curl_https_post($url, $data = [], $header = [])
- {
- $this->logPush(__FUNCTION__, 'curl_https_post:', compact('url', 'data'));
- $ch = curl_init($url);
- curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
- curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
- // resolve SSL: no alternative certificate subject name matches target host name
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // check verify
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
- curl_setopt($ch, CURLOPT_TIMEOUT, 5);
- curl_setopt($ch, CURLOPT_POST, 1); // regular post request
- curl_setopt($ch, CURLOPT_POSTFIELDS, $data); // Post submit data
- $ret = @curl_exec($ch);
- if ($ret === false) {
- return null;
- }
- $info = curl_getinfo($ch);
- curl_close($ch);
- return $ret;
- }
- }
|