You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

225 lines
7.4 KiB

  1. <?php
  2. namespace JPush;
  3. use InvalidArgumentException;
  4. class DevicePayload {
  5. const DEVICE_URL = 'https://device.jpush.cn/v3/devices/';
  6. const DEVICE_STATUS_URL = 'https://device.jpush.cn/v3/devices/status/';
  7. const TAG_URL = 'https://device.jpush.cn/v3/tags/';
  8. const IS_IN_TAG_URL = 'https://device.jpush.cn/v3/tags/{tag}/registration_ids/{registration_id}';
  9. const ALIAS_URL = 'https://device.jpush.cn/v3/aliases/';
  10. private $client;
  11. /**
  12. * DevicePayload constructor.
  13. * @param $client JPush
  14. */
  15. public function __construct($client)
  16. {
  17. $this->client = $client;
  18. }
  19. public function getDevices($registrationId) {
  20. $url = DevicePayload::DEVICE_URL . $registrationId;
  21. return Http::get($this->client, $url);
  22. }
  23. public function updateAlias($registration_id, $alias) {
  24. return $this->updateDevice($registration_id, $alias);
  25. }
  26. public function addTags($registration_id, $tags) {
  27. $tags = is_array($tags) ? $tags : array($tags);
  28. return $this->updateDevice($registration_id, null, null, $tags);
  29. }
  30. public function removeTags($registration_id, $tags) {
  31. $tags = is_array($tags) ? $tags : array($tags);
  32. return $this->updateDevice($registration_id, null, null, null, $tags);
  33. }
  34. public function updateMoblie($registration_id, $mobile) {
  35. return $this->updateDevice($registration_id, null, $mobile);
  36. }
  37. public function updateDevice($registrationId, $alias = null, $mobile = null, $addTags = null, $removeTags = null) {
  38. $payload = array();
  39. if (!is_string($registrationId)) {
  40. throw new InvalidArgumentException('Invalid registration_id');
  41. }
  42. $aliasIsNull = is_null($alias);
  43. $mobileIsNull = is_null($mobile);
  44. $addTagsIsNull = is_null($addTags);
  45. $removeTagsIsNull = is_null($removeTags);
  46. if ($aliasIsNull && $addTagsIsNull && $removeTagsIsNull && $mobileIsNull) {
  47. throw new InvalidArgumentException("alias, addTags, removeTags not all null");
  48. }
  49. if (!$aliasIsNull) {
  50. if (is_string($alias)) {
  51. $payload['alias'] = $alias;
  52. } else {
  53. throw new InvalidArgumentException("Invalid alias string");
  54. }
  55. }
  56. if (!$mobileIsNull) {
  57. if (is_string($mobile)) {
  58. $payload['mobile'] = $mobile;
  59. } else {
  60. throw new InvalidArgumentException("Invalid mobile string");
  61. }
  62. }
  63. $tags = array();
  64. if (!$addTagsIsNull) {
  65. if (is_array($addTags)) {
  66. $tags['add'] = $addTags;
  67. } else {
  68. throw new InvalidArgumentException("Invalid addTags array");
  69. }
  70. }
  71. if (!$removeTagsIsNull) {
  72. if (is_array($removeTags)) {
  73. $tags['remove'] = $removeTags;
  74. } else {
  75. throw new InvalidArgumentException("Invalid removeTags array");
  76. }
  77. }
  78. if (count($tags) > 0) {
  79. $payload['tags'] = $tags;
  80. }
  81. $url = DevicePayload::DEVICE_URL . $registrationId;
  82. return Http::post($this->client, $url, json_encode($payload));
  83. }
  84. public function getTags() {
  85. $url = DevicePayload::TAG_URL;
  86. return Http::get($this->client, $url);
  87. }
  88. public function isDeviceInTag($registrationId, $tag) {
  89. if (!is_string($registrationId)) {
  90. throw new InvalidArgumentException("Invalid registration_id");
  91. }
  92. if (!is_string($tag)) {
  93. throw new InvalidArgumentException("Invalid tag");
  94. }
  95. $url = str_replace('{tag}', $tag, self::IS_IN_TAG_URL);
  96. $url = str_replace('{registration_id}', $registrationId, $url);
  97. return Http::get($this->client, $url);
  98. }
  99. public function addDevicesToTag($tag, $addDevices) {
  100. $device = is_array($addDevices) ? $addDevices : array($addDevices);
  101. return $this->updateTag($tag, $device, null);
  102. }
  103. public function removeDevicesFromTag($tag, $removeDevices) {
  104. $device = is_array($removeDevices) ? $removeDevices : array($removeDevices);
  105. return $this->updateTag($tag, null, $device);
  106. }
  107. public function updateTag($tag, $addDevices = null, $removeDevices = null) {
  108. if (!is_string($tag)) {
  109. throw new InvalidArgumentException("Invalid tag");
  110. }
  111. $addDevicesIsNull = is_null($addDevices);
  112. $removeDevicesIsNull = is_null($removeDevices);
  113. if ($addDevicesIsNull && $removeDevicesIsNull) {
  114. throw new InvalidArgumentException("Either or both addDevices and removeDevices must be set.");
  115. }
  116. $registrationId = array();
  117. if (!$addDevicesIsNull) {
  118. if (is_array($addDevices)) {
  119. $registrationId['add'] = $addDevices;
  120. } else {
  121. throw new InvalidArgumentException("Invalid addDevices");
  122. }
  123. }
  124. if (!$removeDevicesIsNull) {
  125. if (is_array($removeDevices)) {
  126. $registrationId['remove'] = $removeDevices;
  127. } else {
  128. throw new InvalidArgumentException("Invalid removeDevices");
  129. }
  130. }
  131. $url = DevicePayload::TAG_URL . $tag;
  132. $payload = array('registration_ids'=>$registrationId);
  133. return Http::post($this->client, $url, json_encode($payload));
  134. }
  135. public function deleteTag($tag) {
  136. if (!is_string($tag)) {
  137. throw new InvalidArgumentException("Invalid tag");
  138. }
  139. $url = DevicePayload::TAG_URL . $tag;
  140. return Http::delete($this->client, $url);
  141. }
  142. public function getAliasDevices($alias, $platform = null) {
  143. if (!is_string($alias)) {
  144. throw new InvalidArgumentException("Invalid alias");
  145. }
  146. $url = self::ALIAS_URL . $alias;
  147. if (!is_null($platform)) {
  148. if (is_array($platform)) {
  149. $isFirst = true;
  150. foreach($platform as $item) {
  151. if ($isFirst) {
  152. $url = $url . '?platform=' . $item;
  153. $isFirst = false;
  154. } else {
  155. $url = $url . ',' . $item;
  156. }
  157. }
  158. } else if (is_string($platform)) {
  159. $url = $url . '?platform=' . $platform;
  160. } else {
  161. throw new InvalidArgumentException("Invalid platform");
  162. }
  163. }
  164. return Http::get($this->client, $url);
  165. }
  166. public function deleteAlias($alias) {
  167. if (!is_string($alias)) {
  168. throw new InvalidArgumentException("Invalid alias");
  169. }
  170. $url = self::ALIAS_URL . $alias;
  171. return Http::delete($this->client, $url);
  172. }
  173. public function getDevicesStatus($registrationId) {
  174. if (!is_array($registrationId) && !is_string($registrationId)) {
  175. throw new InvalidArgumentException('Invalid registration_id');
  176. }
  177. if (is_string($registrationId)) {
  178. $registrationId = explode(',', $registrationId);
  179. }
  180. $payload = array();
  181. if (count($registrationId) <= 0) {
  182. throw new InvalidArgumentException('Invalid registration_id');
  183. }
  184. $payload['registration_ids'] = $registrationId;
  185. $url = DevicePayload::DEVICE_STATUS_URL;
  186. return Http::post($this->client, $url, json_encode($payload));
  187. }
  188. }