2020 * */
2121class Client extends BaseClient
2222{
23- const URL = [
24- 'create ' => '/cgi-bin/menu/create ' ,
25- 'get_current_selfmenu_info ' => '/cgi-bin/get_current_selfmenu_info ' ,
26- 'delete ' => '/cgi-bin/menu/delete ' ,
27- 'addconditional ' => '/cgi-bin/menu/addconditional ' ,
28- 'delconditional ' => '/cgi-bin/menu/delconditional ' ,
29- 'trymatch ' => '/cgi-bin/menu/trymatch ' ,
30- 'get ' => '/cgi-bin/menu/get '
31- ];
3223
3324 /**
34- * 创建接口
35- * @param array $data 自定义标签需要的数据
36- * @return array
37- * @throws HttpException
38- */
39- function create (array $ data )
40- {
41- $ response = $ this ->getClient ()
42- ->setMethod ('POST ' )
43- ->setBody (
44- $ this ->jsonDataToStream ($ data )
45- )
46- ->send ($ this ->buildUrl (
47- self ::URL ['create ' ],
48- [
49- 'access_token ' => $ this ->getAccessToken ()
50- ]
51- ));
52-
53- $ this ->checkResponse ($ response , $ jsonData );
54- return $ jsonData ;
55- }
56-
57- /**
58- * 查询接口
59- * @return array
25+ * 获取自定义菜单配置
26+ * @return mixed
6027 * @throws HttpException
6128 */
62- public function queryConfig ()
29+ public function list ()
6330 {
6431 $ response = $ this ->getClient ()
6532 ->setMethod ('GET ' )
66- ->send ($ this ->buildUrl (
67- self ::URL ['get_current_selfmenu_info ' ],
68- [
69- 'access_token ' => $ this ->getAccessToken (),
70- ]
71- ));
33+ ->send ($ this ->buildUrl ('/cgi-bin/menu/get ' , [
34+ 'access_token ' => $ this ->app [ServiceProviders::AccessToken]->getToken ()
35+ ])
36+ );
7237
73- $ this ->checkResponse ($ response , $ jsonData );
74- return $ jsonData ;
38+ $ this ->checkResponse ($ response , $ parseData );
39+ return $ parseData ;
7540 }
7641
7742 /**
78- * 删除接口
79- * @return array
43+ * 获取当前菜单配置
44+ * @return mixed
8045 * @throws HttpException
8146 */
82- public function delete ()
47+ public function current ()
8348 {
8449 $ response = $ this ->getClient ()
8550 ->setMethod ('GET ' )
86- ->send ($ this ->buildUrl (
87- self ::URL ['delete ' ],
88- [
89- 'access_token ' => $ this ->getAccessToken (),
90- ]
91- ));
51+ ->send ($ this ->buildUrl ('/cgi-bin/get_current_selfmenu_info ' , [
52+ 'access_token ' => $ this ->app [ServiceProviders::AccessToken]->getToken ()
53+ ])
54+ );
9255
93- $ this ->checkResponse ($ response , $ jsonData );
94- return $ jsonData ;
56+ $ this ->checkResponse ($ response , $ parseData );
57+ return $ parseData ;
9558 }
9659
9760 /**
98- * 个性化菜单接口:创建个性化菜单
99- * @param array $data
100- * @return array
61+ * 创建菜单或个性化菜单
62+ * @param array $buttons
63+ * @param array $matchRule
64+ * @return bool
10165 * @throws HttpException
10266 */
103- function addconditional (array $ data )
67+ public function create (array $ buttons , array $ matchRule = [] )
10468 {
69+ if (!empty ($ matchRule )) {
70+ // 创建个性化菜单
71+ $ response = $ this ->getClient ()
72+ ->setMethod ('POST ' )
73+ ->setBody ($ this ->jsonDataToStream ([
74+ 'button ' => $ buttons ,
75+ 'matchrule ' => $ matchRule ,
76+ ]))
77+ ->send ($ this ->buildUrl ('/cgi-bin/menu/addconditional ' , [
78+ 'access_token ' => $ this ->app [ServiceProviders::AccessToken]->getToken ()
79+ ])
80+ );
81+ return $ this ->checkResponse ($ response , $ parseData );
82+ }
83+
84+ // 创建菜单
10585 $ response = $ this ->getClient ()
10686 ->setMethod ('POST ' )
107- ->setBody (
108- $ this ->jsonDataToStream ($ data )
109- )
110- ->send ($ this ->buildUrl (
111- self ::URL ['addconditional ' ],
112- [
113- 'access_token ' => $ this ->getAccessToken ()
114- ]
115- ));
116-
117- $ this ->checkResponse ($ response , $ jsonData );
118- return $ jsonData ;
87+ ->setBody ($ this ->jsonDataToStream (['button ' => $ buttons ]))
88+ ->send ($ this ->buildUrl ('/cgi-bin/menu/create ' , [
89+ 'access_token ' => $ this ->app [ServiceProviders::AccessToken]->getToken ()
90+ ])
91+ );
92+ return $ this ->checkResponse ($ response , $ parseData );
11993 }
12094
12195 /**
122- * 个性化菜单接口:删除个性化菜单
123- * @param string $menuId 菜单id,可以通过自定义菜单查询接口获取。
124- * @return array
96+ * 删除菜单或个性化菜单
97+ * @param int|null $menuId
98+ * @return bool
12599 * @throws HttpException
126100 */
127- function delconditional ( string $ menuId )
101+ public function delete ( int $ menuId = null )
128102 {
129- $ response = $ this ->getClient ()
130- ->setMethod ('POST ' )
131- ->setBody (
132- $ this ->jsonDataToStream ([
133- 'menuid ' => $ menuId ,
103+ if (is_null ($ menuId )) {
104+ // 删除所有菜单
105+ $ response = $ this ->getClient ()
106+ ->setMethod ('GET ' )
107+ ->send ($ this ->buildUrl ('/cgi-bin/menu/delete ' , [
108+ 'access_token ' => $ this ->app [ServiceProviders::AccessToken]->getToken ()
134109 ])
135- )
136- ->send ($ this ->buildUrl (
137- self ::URL ['delconditional ' ],
138- [
139- 'access_token ' => $ this ->getAccessToken ()
140- ]
141- ));
110+ );
111+ return $ this ->checkResponse ($ response , $ parseData );
112+ }
142113
143- $ this ->checkResponse ($ response , $ jsonData );
144- return $ jsonData ;
145- }
146-
147- /**
148- * 个性化菜单接口:测试个性化菜单匹配结果
149- * @param string $userId 可以是粉丝的OpenID,也可以是粉丝的微信号。
150- * @return array
151- * @throws HttpException
152- */
153- function match (string $ userId )
154- {
114+ // 删除个性化菜单
155115 $ response = $ this ->getClient ()
156116 ->setMethod ('POST ' )
157- ->setBody (
158- $ this ->jsonDataToStream ([
159- 'user_id ' => $ userId ,
160- ])
161- )
162- ->send ($ this ->buildUrl (
163- self ::URL ['trymatch ' ],
164- [
165- 'access_token ' => $ this ->getAccessToken ()
166- ]
167- ));
168-
169- $ this ->checkResponse ($ response , $ jsonData );
170- return $ jsonData ;
117+ ->setBody ($ this ->jsonDataToStream (['menuid ' => $ menuId ]))
118+ ->send ($ this ->buildUrl ('/cgi-bin/menu/delconditional ' , [
119+ 'access_token ' => $ this ->app [ServiceProviders::AccessToken]->getToken ()
120+ ])
121+ );
122+ return $ this ->checkResponse ($ response , $ parseData );
171123 }
172124
173125 /**
174- * 获取自定义菜单配置
175- * @return array
126+ * 测试个性化菜单匹配结果
127+ * @param string $userId
128+ * @return bool
176129 * @throws HttpException
177130 */
178- public function query ( )
131+ public function match ( string $ userId )
179132 {
180133 $ response = $ this ->getClient ()
181- ->setMethod ('GET ' )
182- ->send ($ this ->buildUrl (
183- self ::URL ['get ' ],
184- [
185- 'access_token ' => $ this ->getAccessToken (),
186- ]
187- ));
188-
189- $ this ->checkResponse ($ response , $ jsonData );
190- return $ jsonData ;
191- }
192-
193- protected function getAccessToken ()
194- {
195- return $ this ->app [ServiceProviders::AccessToken]->getToken ();
134+ ->setMethod ('POST ' )
135+ ->setBody ($ this ->jsonDataToStream (['user_id ' => $ userId ]))
136+ ->send ($ this ->buildUrl ('/cgi-bin/menu/trymatch ' , [
137+ 'access_token ' => $ this ->app [ServiceProviders::AccessToken]->getToken ()
138+ ])
139+ );
140+ $ this ->checkResponse ($ response , $ parseData );
141+ return $ parseData ;
196142 }
197143}
0 commit comments