OaMenuApi.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. /**
  3. * OaMenuApi.php UTF-8
  4. * 公众号菜单接口
  5. *
  6. * @date : 2018/9/26 18:27
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@huosdk.com>
  10. * @version : HuoMp 1.0
  11. */
  12. namespace huoMpMsg\controller;
  13. use huo\controller\common\Base;
  14. use huoMpMsg\status\OfficialAccountStatus;
  15. class OaMenuApi extends Base {
  16. /**
  17. * 自定义菜单创建接口
  18. * https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141013
  19. *
  20. * @param string $wx_app_id 第三方用户唯一凭证
  21. * @param string $wx_app_secret 第三方用户唯一凭证密钥,即appsecret
  22. * @param array $menu_data 菜单数据
  23. *
  24. * @return array
  25. */
  26. public function createMenu($wx_app_id, $wx_app_secret, $menu_data = []) {
  27. $_url = 'https://api.weixin.qq.com/cgi-bin/menu/create';
  28. $_access_token = Common::getAccessToken($wx_app_id, $wx_app_secret);
  29. $_url = $_url.'?access_token='.$_access_token;
  30. $_param = json_encode($menu_data, JSON_UNESCAPED_UNICODE);
  31. $_header = ['Content-Type: application/octet-stream; charset=utf-8'];
  32. $_ret = Common::curl($_url, $_param, 'POST', $_header);
  33. $_rdata = [];
  34. if (Common::isJson($_ret)) {
  35. $_rdata = json_decode($_ret, true);
  36. }
  37. if (isset($_rdata['errcode']) && '0' != $_rdata['errcode']) {
  38. $_code = $_rdata['errcode'];
  39. return $this->huoError($_code, $_rdata['errmsg']);
  40. }
  41. $_code = OfficialAccountStatus::NO_ERROR;
  42. return $this->huoSuccess($_code, OfficialAccountStatus::getMsg($_code), $_rdata);
  43. }
  44. /**
  45. * 自定义菜单查询接口
  46. * https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141014
  47. *
  48. * @param string $wx_app_id 第三方用户唯一凭证
  49. * @param string $wx_app_secret 第三方用户唯一凭证密钥,即appsecret
  50. *
  51. * @return array
  52. */
  53. public function getMenu($wx_app_id, $wx_app_secret) {
  54. $_url = 'https://api.weixin.qq.com/cgi-bin/menu/get';
  55. $_access_token = Common::getAccessToken($wx_app_id, $wx_app_secret);
  56. $_url = $_url.'?access_token='.$_access_token;
  57. $_ret = Common::curl($_url, '');
  58. $_rdata = [];
  59. if (Common::isJson($_ret)) {
  60. $_rdata = json_decode($_ret, true);
  61. }
  62. if (isset($_rdata['errcode']) && '0' != $_rdata['errcode']) {
  63. $_code = $_rdata['errcode'];
  64. return $this->huoError($_code, $_rdata['errmsg']);
  65. }
  66. $_code = OfficialAccountStatus::NO_ERROR;
  67. return $this->huoSuccess($_code, OfficialAccountStatus::getMsg($_code), $_rdata);
  68. }
  69. /**
  70. * 自定义菜单删除接口
  71. * https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141015
  72. *
  73. * 使用接口创建自定义菜单后,开发者还可使用接口删除当前使用的自定义菜单。另请注意,在个性化菜单时,调用此接口会删除默认菜单及全部个性化菜单。
  74. *
  75. * @param string $wx_app_id 第三方用户唯一凭证
  76. * @param string $wx_app_secret 第三方用户唯一凭证密钥,即appsecret
  77. *
  78. * @return array
  79. */
  80. public function deleteAllMenu($wx_app_id, $wx_app_secret) {
  81. $_url = 'https://api.weixin.qq.com/cgi-bin/menu/delete';
  82. $_access_token = Common::getAccessToken($wx_app_id, $wx_app_secret);
  83. $_url = $_url.'?access_token='.$_access_token;
  84. $_ret = Common::curl($_url, '');
  85. $_rdata = [];
  86. if (Common::isJson($_ret)) {
  87. $_rdata = json_decode($_ret, true);
  88. }
  89. if (isset($_rdata['errcode']) && '0' != $_rdata['errcode']) {
  90. $_code = $_rdata['errcode'];
  91. return $this->huoError($_code, $_rdata['errmsg']);
  92. }
  93. $_code = OfficialAccountStatus::NO_ERROR;
  94. return $this->huoSuccess($_code, OfficialAccountStatus::getMsg($_code), $_rdata);
  95. }
  96. /**
  97. * 个性化菜单接口
  98. * https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1455782296
  99. * 为了帮助公众号实现灵活的业务运营,微信公众平台新增了个性化菜单接口,开发者可以通过该接口,让公众号的不同用户群体看到不一样的自定义菜单。该接口开放给已认证订阅号和已认证服务号。
  100. *
  101. *
  102. * 1、用户标签(开发者的业务需求可以借助用户标签来完成)
  103. * 2、性别
  104. * 3、手机操作系统
  105. * 4、地区(用户在微信客户端设置的地区)
  106. * 5、语言(用户在微信客户端设置的语言)
  107. *
  108. * @param string $wx_app_id 第三方用户唯一凭证
  109. * @param string $wx_app_secret 第三方用户唯一凭证密钥,即appsecret
  110. * @param int $menuid 菜单ID
  111. *
  112. * @return array
  113. */
  114. public function addConditionalMenu($wx_app_id, $wx_app_secret, $menuid = 0) {
  115. $_url = 'https://api.weixin.qq.com/cgi-bin/menu/addconditional';
  116. $_access_token = Common::getAccessToken($wx_app_id, $wx_app_secret);
  117. $_url = $_url.'?access_token='.$_access_token;
  118. $_param['menuid'] = $menuid;
  119. $_param = json_encode($_param);
  120. $_header = ['Content-Type: application/octet-stream; charset=utf-8'];
  121. $_ret = Common::curl($_url, $_param, 'POST', $_header);
  122. $_rdata = [];
  123. if (Common::isJson($_ret)) {
  124. $_rdata = json_decode($_ret, true);
  125. }
  126. if (isset($_rdata['errcode']) && '0' != $_rdata['errcode']) {
  127. $_code = $_rdata['errcode'];
  128. return $this->huoError($_code, $_rdata['errmsg']);
  129. }
  130. $_code = OfficialAccountStatus::NO_ERROR;
  131. return $this->huoSuccess($_code, OfficialAccountStatus::getMsg($_code), $_rdata);
  132. }
  133. /**
  134. * 测试个性化菜单匹配结果
  135. * https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1455782296
  136. *
  137. * @param string $wx_app_id 第三方用户唯一凭证
  138. * @param string $wx_app_secret 第三方用户唯一凭证密钥,即appsecret
  139. * @param string $open_id 玩家openid
  140. *
  141. * @return array
  142. */
  143. public function tryMatchConditionalMenu($wx_app_id, $wx_app_secret, $open_id) {
  144. $_url = 'https://api.weixin.qq.com/cgi-bin/menu/trymatch';
  145. $_access_token = Common::getAccessToken($wx_app_id, $wx_app_secret);
  146. $_url = $_url.'?access_token='.$_access_token;
  147. $_param['user_id'] = $open_id;
  148. $_param = json_encode($_param);
  149. $_header = ['Content-Type: application/octet-stream; charset=utf-8'];
  150. $_ret = Common::curl($_url, $_param, 'POST', $_header);
  151. $_rdata = [];
  152. if (Common::isJson($_ret)) {
  153. $_rdata = json_decode($_ret, true);
  154. }
  155. if (isset($_rdata['errcode']) && '0' != $_rdata['errcode']) {
  156. $_code = $_rdata['errcode'];
  157. return $this->huoError($_code, $_rdata['errmsg']);
  158. }
  159. $_code = OfficialAccountStatus::NO_ERROR;
  160. return $this->huoSuccess($_code, OfficialAccountStatus::getMsg($_code), $_rdata);
  161. }
  162. }