Menu.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. * Menu.php.
  12. *
  13. * @author overtrue <i@overtrue.me>
  14. * @copyright 2015 overtrue <i@overtrue.me>
  15. *
  16. * @see https://github.com/overtrue
  17. * @see http://overtrue.me
  18. */
  19. namespace EasyWeChat\Menu;
  20. use EasyWeChat\Core\AbstractAPI;
  21. /**
  22. * Class Menu.
  23. */
  24. class Menu extends AbstractAPI
  25. {
  26. const API_CREATE = 'https://api.weixin.qq.com/cgi-bin/menu/create';
  27. const API_GET = 'https://api.weixin.qq.com/cgi-bin/menu/get';
  28. const API_DELETE = 'https://api.weixin.qq.com/cgi-bin/menu/delete';
  29. const API_QUERY = 'https://api.weixin.qq.com/cgi-bin/get_current_selfmenu_info';
  30. const API_CONDITIONAL_CREATE = 'https://api.weixin.qq.com/cgi-bin/menu/addconditional';
  31. const API_CONDITIONAL_DELETE = 'https://api.weixin.qq.com/cgi-bin/menu/delconditional';
  32. const API_CONDITIONAL_TEST = 'https://api.weixin.qq.com/cgi-bin/menu/trymatch';
  33. /**
  34. * Get all menus.
  35. *
  36. * @return \EasyWeChat\Support\Collection
  37. */
  38. public function all()
  39. {
  40. return $this->parseJSON('get', [self::API_GET]);
  41. }
  42. /**
  43. * Get current menus.
  44. *
  45. * @return \EasyWeChat\Support\Collection
  46. */
  47. public function current()
  48. {
  49. return $this->parseJSON('get', [self::API_QUERY]);
  50. }
  51. /**
  52. * Add menu.
  53. *
  54. * @param array $buttons
  55. * @param array $matchRule
  56. *
  57. * @return \EasyWeChat\Support\Collection
  58. */
  59. public function add(array $buttons, array $matchRule = [])
  60. {
  61. if (!empty($matchRule)) {
  62. return $this->parseJSON('json', [self::API_CONDITIONAL_CREATE, [
  63. 'button' => $buttons,
  64. 'matchrule' => $matchRule,
  65. ]]);
  66. }
  67. return $this->parseJSON('json', [self::API_CREATE, ['button' => $buttons]]);
  68. }
  69. /**
  70. * Destroy menu.
  71. *
  72. * @param int $menuId
  73. *
  74. * @return \EasyWeChat\Support\Collection
  75. */
  76. public function destroy($menuId = null)
  77. {
  78. if (null !== $menuId) {
  79. return $this->parseJSON('json', [self::API_CONDITIONAL_DELETE, ['menuid' => $menuId]]);
  80. }
  81. return $this->parseJSON('get', [self::API_DELETE]);
  82. }
  83. /**
  84. * Test conditional menu.
  85. *
  86. * @param string $userId
  87. *
  88. * @return \EasyWeChat\Support\Collection
  89. */
  90. public function test($userId)
  91. {
  92. return $this->parseJSON('json', [self::API_CONDITIONAL_TEST, ['user_id' => $userId]]);
  93. }
  94. }