Device.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. /*
  3. * This file is part of the overtrue/wechat.
  4. *
  5. * (c) overtrue <i@overtrue.me>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. /**
  11. * Device.php.
  12. *
  13. * @author allen05ren <allen05ren@outlook.com>
  14. * @copyright 2016 overtrue <i@overtrue.me>
  15. *
  16. * @see https://github.com/overtrue
  17. * @see http://overtrue.me
  18. */
  19. namespace EasyWeChat\ShakeAround;
  20. use EasyWeChat\Core\AbstractAPI;
  21. use EasyWeChat\Core\Exceptions\InvalidArgumentException;
  22. /**
  23. * Class Device.
  24. */
  25. class Device extends AbstractAPI
  26. {
  27. const API_DEVICE_APPLYID = 'https://api.weixin.qq.com/shakearound/device/applyid';
  28. const API_DEVICE_APPLYSTATUS = 'https://api.weixin.qq.com/shakearound/device/applystatus';
  29. const API_DEVICE_UPDATE = 'https://api.weixin.qq.com/shakearound/device/update';
  30. const API_DEVICE_BINDLOCATION = 'https://api.weixin.qq.com/shakearound/device/bindlocation';
  31. const API_DEVICE_SEARCH = 'https://api.weixin.qq.com/shakearound/device/search';
  32. /**
  33. * Apply device ids.
  34. *
  35. * @param int $quantity
  36. * @param string $reason
  37. * @param string $comment
  38. * @param int $poiId
  39. *
  40. * @return \EasyWeChat\Support\Collection
  41. */
  42. public function apply($quantity, $reason, $comment = '', $poiId = null)
  43. {
  44. $params = [
  45. 'quantity' => intval($quantity),
  46. 'apply_reason' => $reason,
  47. ];
  48. if (!empty($comment)) {
  49. $params['comment'] = $comment;
  50. }
  51. if (!is_null($poiId)) {
  52. $params['poi_id'] = intval($poiId);
  53. }
  54. return $this->parseJSON('json', [self::API_DEVICE_APPLYID, $params]);
  55. }
  56. /**
  57. * Get audit status.
  58. *
  59. * @param int $applyId
  60. *
  61. * @return \EasyWeChat\Support\Collection
  62. */
  63. public function getStatus($applyId)
  64. {
  65. $params = [
  66. 'apply_id' => intval($applyId),
  67. ];
  68. return $this->parseJSON('json', [self::API_DEVICE_APPLYSTATUS, $params]);
  69. }
  70. /**
  71. * Update a device comment.
  72. *
  73. * @param array $deviceIdentifier
  74. * @param string $comment
  75. *
  76. * @return \EasyWeChat\Support\Collection
  77. */
  78. public function update(array $deviceIdentifier, $comment)
  79. {
  80. $params = [
  81. 'device_identifier' => $deviceIdentifier,
  82. 'comment' => $comment,
  83. ];
  84. return $this->parseJSON('json', [self::API_DEVICE_UPDATE, $params]);
  85. }
  86. /**
  87. * Bind location for device.
  88. *
  89. * @param array $deviceIdentifier
  90. * @param int $poiId
  91. * @param int $type
  92. * @param string $poiAppid
  93. *
  94. * @return \EasyWeChat\Support\Collection
  95. *
  96. * @throws InvalidArgumentException
  97. */
  98. public function bindLocation(array $deviceIdentifier, $poiId, $type = 1, $poiAppid = null)
  99. {
  100. $params = [
  101. 'device_identifier' => $deviceIdentifier,
  102. 'poi_id' => intval($poiId),
  103. ];
  104. if (2 === $type) {
  105. if (is_null($poiAppid)) {
  106. throw new InvalidArgumentException('If value of argument #3 is 2, argument #4 is required.');
  107. }
  108. $params['type'] = 2;
  109. $params['poi_appid'] = $poiAppid;
  110. }
  111. return $this->parseJSON('json', [self::API_DEVICE_BINDLOCATION, $params]);
  112. }
  113. /**
  114. * Fetch batch of devices by deviceIds.
  115. *
  116. * @param array $deviceIdentifiers
  117. *
  118. * @return \EasyWeChat\Support\Collection
  119. */
  120. public function fetchByIds(array $deviceIdentifiers)
  121. {
  122. $params = [
  123. 'type' => 1,
  124. 'device_identifiers' => $deviceIdentifiers,
  125. ];
  126. return $this->fetch($params);
  127. }
  128. /**
  129. * Pagination to fetch batch of devices.
  130. *
  131. * @param int $lastSeen
  132. * @param int $count
  133. *
  134. * @return \EasyWeChat\Support\Collection
  135. */
  136. public function pagination($lastSeen, $count)
  137. {
  138. $params = [
  139. 'type' => 2,
  140. 'last_seen' => intval($lastSeen),
  141. 'count' => intval($count),
  142. ];
  143. return $this->fetch($params);
  144. }
  145. /**
  146. * Fetch batch of devices by applyId.
  147. *
  148. * @param int $applyId
  149. * @param int $lastSeen
  150. * @param int $count
  151. *
  152. * @return \EasyWeChat\Support\Collection
  153. */
  154. public function fetchByApplyId($applyId, $lastSeen, $count)
  155. {
  156. $params = [
  157. 'type' => 3,
  158. 'apply_id' => intval($applyId),
  159. 'last_seen' => intval($lastSeen),
  160. 'count' => intval($count),
  161. ];
  162. return $this->fetch($params);
  163. }
  164. /**
  165. * Fetch batch of devices.
  166. *
  167. * @param array $params
  168. *
  169. * @return \EasyWeChat\Support\Collection
  170. */
  171. private function fetch($params)
  172. {
  173. return $this->parseJSON('json', [self::API_DEVICE_SEARCH, $params]);
  174. }
  175. }