Relation.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. * Relation.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. /**
  22. * Class Relation.
  23. */
  24. class Relation extends AbstractAPI
  25. {
  26. const API_DEVICE_BINDPAGE = 'https://api.weixin.qq.com/shakearound/device/bindpage';
  27. const API_RELATION_SEARCH = 'https://api.weixin.qq.com/shakearound/relation/search';
  28. /**
  29. * Bind pages for device.
  30. *
  31. * @param array $deviceIdentifier
  32. * @param array $pageIds
  33. *
  34. * @return \EasyWeChat\Support\Collection
  35. */
  36. public function bindPage(array $deviceIdentifier, array $pageIds)
  37. {
  38. $params = [
  39. 'device_identifier' => $deviceIdentifier,
  40. 'page_ids' => $pageIds,
  41. ];
  42. return $this->parseJSON('json', [self::API_DEVICE_BINDPAGE, $params]);
  43. }
  44. /**
  45. * Get pageIds by deviceId.
  46. *
  47. * @param array $deviceIdentifier
  48. * @param bool $raw
  49. *
  50. * @return array|\EasyWeChat\Support\Collection
  51. */
  52. public function getPageByDeviceId(array $deviceIdentifier, $raw = false)
  53. {
  54. $params = [
  55. 'type' => 1,
  56. 'device_identifier' => $deviceIdentifier,
  57. ];
  58. $result = $this->parseJSON('json', [self::API_RELATION_SEARCH, $params]);
  59. if (true === $raw) {
  60. return $result;
  61. }
  62. $page_ids = [];
  63. if (!empty($result->data['relations'])) {
  64. foreach ($result->data['relations'] as $item) {
  65. $page_ids[] = $item['page_id'];
  66. }
  67. }
  68. return $page_ids;
  69. }
  70. /**
  71. * Get devices by pageId.
  72. *
  73. * @param int $pageId
  74. * @param int $begin
  75. * @param int $count
  76. *
  77. * @return \EasyWeChat\Support\Collection
  78. */
  79. public function getDeviceByPageId($pageId, $begin, $count)
  80. {
  81. $params = [
  82. 'type' => 2,
  83. 'page_id' => intval($pageId),
  84. 'begin' => intval($begin),
  85. 'count' => intval($count),
  86. ];
  87. return $this->parseJSON('json', [self::API_RELATION_SEARCH, $params]);
  88. }
  89. }