From 3900848bcd0f7f1703e9c7f9fa96b4ce3c3f9563 Mon Sep 17 00:00:00 2001 From: xiezefan Date: Tue, 5 Jan 2016 00:15:42 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=8E=B7=E5=8F=96limit?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=A4=B1=E8=B4=A5=E6=8A=A5=E9=94=99=E7=9A=84?= =?UTF-8?q?BUG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/JPush/core/DevicePayload.php | 27 +++++++++++++++++++++------ src/JPush/core/PushPayload.php | 15 ++++++++++----- src/JPush/core/ReportPayload.php | 15 ++++++++++----- src/JPush/core/SchedulePayload.php | 14 ++++++++++---- 4 files changed, 51 insertions(+), 20 deletions(-) diff --git a/src/JPush/core/DevicePayload.php b/src/JPush/core/DevicePayload.php index c78ccae..5156105 100644 --- a/src/JPush/core/DevicePayload.php +++ b/src/JPush/core/DevicePayload.php @@ -1,6 +1,7 @@ 'rateLimitLimit', 'X-Rate-Limit-Remaining'=>'rateLimitRemaining', 'X-Rate-Limit-Reset'=>'rateLimitReset'); const DEVICE_URL = 'https://device.jpush.cn/v3/devices/'; const DEVICE_STATUS_URL = 'https://device.jpush.cn/v3/devices/status/'; @@ -27,17 +28,19 @@ public function getDevices($registrationId) { return $this->__processResp($response); } - public function updateDevice($registrationId, $alias = null, $addTags = null, $removeTags = null) { + public function updateDevice($registrationId, $alias = null, $mobile=null, $addTags = null, $removeTags = null) { $payload = array(); if (!is_string($registrationId)) { throw new InvalidArgumentException('Invalid registration_id'); } $aliasIsNull = is_null($alias); + $mobileIsNull = is_null($mobile); $addTagsIsNull = is_null($addTags); $removeTagsIsNull = is_null($removeTags); - if ($aliasIsNull && $addTagsIsNull && $removeTagsIsNull) { + + if ($aliasIsNull && $addTagsIsNull && $removeTagsIsNull && $mobileIsNull) { throw new InvalidArgumentException("alias, addTags, removeTags not all null"); } @@ -49,6 +52,14 @@ public function updateDevice($registrationId, $alias = null, $addTags = null, $r } } + if (!$mobileIsNull) { + if (is_string($mobile)) { + $payload['mobile'] = $mobile; + } else { + throw new InvalidArgumentException("Invalid mobile string"); + } + } + $tags = array(); if (!$addTagsIsNull) { @@ -227,10 +238,14 @@ private function __processResp($response) { $headers = $response['headers']; if (is_array($headers)) { $limit = array(); - $limit['rateLimitLimit'] = $headers['X-Rate-Limit-Limit']; - $limit['rateLimitRemaining'] = $headers['X-Rate-Limit-Remaining']; - $limit['rateLimitReset'] = $headers['X-Rate-Limit-Reset']; - $body['limit'] = (object)$limit; + foreach (self::$LIMIT_KEYS as $key => $value) { + if (array_key_exists($key, $headers)) { + $limit[$value] = $headers[$key]; + } + } + if (count($limit) > 0) { + $body['limit'] = (object)$limit; + } return (object)$body; } return $body; diff --git a/src/JPush/core/PushPayload.php b/src/JPush/core/PushPayload.php index d0b4677..06d8353 100644 --- a/src/JPush/core/PushPayload.php +++ b/src/JPush/core/PushPayload.php @@ -2,6 +2,7 @@ class PushPayload { private static $EFFECTIVE_DEVICE_TYPES = array('ios', 'android', 'winphone'); + private static $LIMIT_KEYS = array('X-Rate-Limit-Limit'=>'rateLimitLimit', 'X-Rate-Limit-Remaining'=>'rateLimitRemaining', 'X-Rate-Limit-Reset'=>'rateLimitReset'); const PUSH_URL = 'https://api.jpush.cn/v3/push'; const PUSH_VALIDATE_URL = ' https://api.jpush.cn/v3/push/validate'; private $client; @@ -559,13 +560,17 @@ private function __processResp($response) { $headers = $response['headers']; if (is_array($headers)) { $limit = array(); - $limit['rateLimitLimit'] = $headers['X-Rate-Limit-Limit']; - $limit['rateLimitRemaining'] = $headers['X-Rate-Limit-Remaining']; - $limit['rateLimitReset'] = $headers['X-Rate-Limit-Reset']; - $body['limit'] = (object)$limit; + foreach (self::$LIMIT_KEYS as $key => $value) { + if (array_key_exists($key, $headers)) { + $limit[$value] = $headers[$key]; + } + } + if (count($limit) > 0) { + $body['limit'] = (object)$limit; + } return (object)$body; } - return (object)$body; + return $body; } else { throw new APIRequestException($response); } diff --git a/src/JPush/core/ReportPayload.php b/src/JPush/core/ReportPayload.php index b09d926..b0014a8 100644 --- a/src/JPush/core/ReportPayload.php +++ b/src/JPush/core/ReportPayload.php @@ -2,7 +2,7 @@ class ReportPayload { private static $EFFECTIVE_TIME_UNIT = array('HOUR', 'DAY', 'MONTH'); - + private static $LIMIT_KEYS = array('X-Rate-Limit-Limit'=>'rateLimitLimit', 'X-Rate-Limit-Remaining'=>'rateLimitRemaining', 'X-Rate-Limit-Reset'=>'rateLimitReset'); const REPORT_URL = 'https://report.jpush.cn/v3/received'; const MESSAGES_URL = 'https://report.jpush.cn/v3/messages'; const USERS_URL = 'https://report.jpush.cn/v3/users'; @@ -81,12 +81,17 @@ private function __request($url) { $body = array(); $body['data'] = (array)json_decode($response['body']); $headers = $response['headers']; + if (is_array($headers)) { $limit = array(); - $limit['rateLimitLimit'] = $headers['X-Rate-Limit-Limit']; - $limit['rateLimitRemaining'] = $headers['X-Rate-Limit-Remaining']; - $limit['rateLimitReset'] = $headers['X-Rate-Limit-Reset']; - $body['limit'] = (object)$limit; + foreach (self::$LIMIT_KEYS as $key => $value) { + if (array_key_exists($key, $headers)) { + $limit[$value] = $headers[$key]; + } + } + if (count($limit) > 0) { + $body['limit'] = (object)$limit; + } return (object)$body; } return $body; diff --git a/src/JPush/core/SchedulePayload.php b/src/JPush/core/SchedulePayload.php index d46f8e6..aceba58 100644 --- a/src/JPush/core/SchedulePayload.php +++ b/src/JPush/core/SchedulePayload.php @@ -1,6 +1,8 @@ 'rateLimitLimit', 'X-Rate-Limit-Remaining'=>'rateLimitRemaining', 'X-Rate-Limit-Reset'=>'rateLimitReset'); + const SCHEDULES_URL = 'https://api.jpush.cn/v3/schedules'; private $client; @@ -171,10 +173,14 @@ private function __processResp($response) { $headers = $response['headers']; if (is_array($headers)) { $limit = array(); - $limit['rateLimitLimit'] = $headers['X-Rate-Limit-Limit']; - $limit['rateLimitRemaining'] = $headers['X-Rate-Limit-Remaining']; - $limit['rateLimitReset'] = $headers['X-Rate-Limit-Reset']; - $body['limit'] = (object)$limit; + foreach (self::$LIMIT_KEYS as $key => $value) { + if (array_key_exists($key, $headers)) { + $limit[$value] = $headers[$key]; + } + } + if (count($limit) > 0) { + $body['limit'] = (object)$limit; + } return (object)$body; } return $body;