| 1 | package QQ::weixin::work::menu; | ||
| 2 | |||
| 3 | =encoding utf8 | ||
| 4 | |||
| 5 | =head1 Name | ||
| 6 | |||
| 7 | QQ::weixin::work::menu | ||
| 8 | |||
| 9 | =head1 DESCRIPTION | ||
| 10 | |||
| 11 | 应用管理-自定义菜单 | ||
| 12 | |||
| 13 | =cut | ||
| 14 | |||
| 15 | use strict; | ||
| 16 | use base qw(QQ::weixin::work); | ||
| 17 | use Encode; | ||
| 18 | use LWP::UserAgent; | ||
| 19 | use JSON; | ||
| 20 | use utf8; | ||
| 21 | |||
| 22 | our $VERSION = '0.10'; | ||
| 23 | our @EXPORT = qw/ create get delete /; | ||
| 24 | |||
| 25 | =head1 FUNCTION | ||
| 26 | |||
| 27 | =head2 create(access_token, agentid, hash); | ||
| 28 | |||
| 29 | 创建菜单 | ||
| 30 | 最后更新:2021/03/18 | ||
| 31 | |||
| 32 | =head2 SYNOPSIS | ||
| 33 | |||
| 34 | L<https://developer.work.weixin.qq.com/document/path/90231> | ||
| 35 | |||
| 36 | =head3 请求说明: | ||
| 37 | |||
| 38 | 自定义菜单接口可实现多种类型按钮,如下: | ||
| 39 | |||
| 40 | 字段值 功能名称 说明 | ||
| 41 | click 点击推事件 成员点击click类型按钮后,企业微信服务器会通过消息接口推送消息类型为event 的结构给开发者(参考消息接口指南),并且带上按钮中开发者填写的key值,开发者可以通过自定义的key值与成员进行交互; | ||
| 42 | view 跳转URL 成员点击view类型按钮后,企业微信客户端将会打开开发者在按钮中填写的网页URL,可与网页授权获取成员基本信息接口结合,获得成员基本信息。 | ||
| 43 | scancode_push 扫码推事件 成员点击按钮后,企业微信客户端将调起扫一扫工具,完成扫码操作后显示扫描结果(如果是URL,将进入URL),且会将扫码的结果传给开发者,开发者可用于下发消息。 | ||
| 44 | scancode_waitmsg 扫码推事件 且弹出“消息接收中”提示框 成员点击按钮后,企业微信客户端将调起扫一扫工具,完成扫码操作后,将扫码的结果传给开发者,同时收起扫一扫工具,然后弹出“消息接收中”提示框,随后可能会收到开发者下发的消息。 | ||
| 45 | pic_sysphoto 弹出系统拍照发图 弹出系统拍照发图 成员点击按钮后,企业微信客户端将调起系统相机,完成拍照操作后,会将拍摄的相片发送给开发者,并推送事件给开发者,同时收起系统相机,随后可能会收到开发者下发的消息。 | ||
| 46 | pic_photo_or_album 弹出拍照或者相册发图 成员点击按钮后,企业微信客户端将弹出选择器供成员选择“拍照”或者“从手机相册选择”。成员选择后即走其他两种流程。 | ||
| 47 | pic_weixin 弹出企业微信相册发图器 成员点击按钮后,企业微信客户端将调起企业微信相册,完成选择操作后,将选择的相片发送给开发者的服务器,并推送事件给开发者,同时收起相册,随后可能会收到开发者下发的消息。 | ||
| 48 | location_select 弹出地理位置选择器 成员点击按钮后,企业微信客户端将调起地理位置选择工具,完成选择操作后,将选择的地理位置发送给开发者的服务器,同时收起位置选择工具,随后可能会收到开发者下发的消息。 | ||
| 49 | view_miniprogram 跳转到小程序 成员点击按钮后,企业微信客户端将会打开开发者在按钮中配置的小程序 | ||
| 50 | |||
| 51 | =head4 请求包结构体为: | ||
| 52 | |||
| 53 | 示例:构造click和view类型的请求包如下 | ||
| 54 | |||
| 55 | { | ||
| 56 | "button":[ | ||
| 57 | { | ||
| 58 | "type":"click", | ||
| 59 | "name":"今日歌曲", | ||
| 60 | "key":"V1001_TODAY_MUSIC" | ||
| 61 | }, | ||
| 62 | { | ||
| 63 | "name":"菜单", | ||
| 64 | "sub_button":[ | ||
| 65 | { | ||
| 66 | "type":"view", | ||
| 67 | "name":"搜索", | ||
| 68 | "url":"http://www.soso.com/" | ||
| 69 | }, | ||
| 70 | { | ||
| 71 | "type":"click", | ||
| 72 | "name":"赞一下我们", | ||
| 73 | "key":"V1001_GOOD" | ||
| 74 | } | ||
| 75 | ] | ||
| 76 | } | ||
| 77 | ] | ||
| 78 | } | ||
| 79 | 示例:其他新增按钮类型的请求 | ||
| 80 | |||
| 81 | { | ||
| 82 | "button": [ | ||
| 83 | { | ||
| 84 | "name": "扫码", | ||
| 85 | "sub_button": [ | ||
| 86 | { | ||
| 87 | "type": "scancode_waitmsg", | ||
| 88 | "name": "扫码带提示", | ||
| 89 | "key": "rselfmenu_0_0", | ||
| 90 | "sub_button": [ ] | ||
| 91 | }, | ||
| 92 | { | ||
| 93 | "type": "scancode_push", | ||
| 94 | "name": "扫码推事件", | ||
| 95 | "key": "rselfmenu_0_1", | ||
| 96 | "sub_button": [ ] | ||
| 97 | }, | ||
| 98 | { | ||
| 99 | "type":"view_miniprogram", | ||
| 100 | "name":"小程序", | ||
| 101 | "pagepath":"pages/lunar/index", | ||
| 102 | "appid":"wx4389ji4kAAA" | ||
| 103 | } | ||
| 104 | ] | ||
| 105 | }, | ||
| 106 | { | ||
| 107 | "name": "发图", | ||
| 108 | "sub_button": [ | ||
| 109 | { | ||
| 110 | "type": "pic_sysphoto", | ||
| 111 | "name": "系统拍照发图", | ||
| 112 | "key": "rselfmenu_1_0", | ||
| 113 | "sub_button": [ ] | ||
| 114 | }, | ||
| 115 | { | ||
| 116 | "type": "pic_photo_or_album", | ||
| 117 | "name": "拍照或者相册发图", | ||
| 118 | "key": "rselfmenu_1_1", | ||
| 119 | "sub_button": [ ] | ||
| 120 | }, | ||
| 121 | { | ||
| 122 | "type": "pic_weixin", | ||
| 123 | "name": "微信相册发图", | ||
| 124 | "key": "rselfmenu_1_2", | ||
| 125 | "sub_button": [ ] | ||
| 126 | } | ||
| 127 | ] | ||
| 128 | }, | ||
| 129 | { | ||
| 130 | "name": "发送位置", | ||
| 131 | "type": "location_select", | ||
| 132 | "key": "rselfmenu_2_0" | ||
| 133 | } | ||
| 134 | ] | ||
| 135 | } | ||
| 136 | |||
| 137 | =head4 参数说明: | ||
| 138 | |||
| 139 | 参数 必须 说明 | ||
| 140 | access_token 是 调用接口凭证 | ||
| 141 | agentid 是 企业应用的id | ||
| 142 | button 是 一级菜单数组,个数应为1~3个 | ||
| 143 | sub_button 否 二级菜单数组,个数应为1~5个 | ||
| 144 | type 是 菜单的响应动作类型 | ||
| 145 | name 是 菜单的名字。不能为空,主菜单不能超过16字节,子菜单不能超过40字节。 | ||
| 146 | key click等点击类型必须 菜单KEY值,用于消息接口推送,不超过128字节 | ||
| 147 | url view类型必须 网页链接,成员点击菜单可打开链接,不超过1024字节。为了提高安全性,建议使用https的url | ||
| 148 | pagepath view_miniprogram类型必须 小程序的页面路径 | ||
| 149 | appid view_miniprogram类型必须 小程序的appid(仅与企业绑定的小程序可配置) | ||
| 150 | |||
| 151 | =head3 权限说明 | ||
| 152 | |||
| 153 | 仅企业可调用;第三方不可调用。 | ||
| 154 | |||
| 155 | =head3 RETURN 返回结果 | ||
| 156 | |||
| 157 | { | ||
| 158 | "errcode": 0, | ||
| 159 | "errmsg": "ok" | ||
| 160 | } | ||
| 161 | |||
| 162 | =head4 RETURN 参数说明 | ||
| 163 | |||
| 164 | 参数 说明 | ||
| 165 | errcode 返回码 | ||
| 166 | errmsg 对返回码的文本描述内容 | ||
| 167 | |||
| 168 | =cut | ||
| 169 | |||
| 170 | sub create { | ||
| 171 | if ( @_ && $_[0] && $_[1] && ref $_[2] eq 'HASH' ) { | ||
| 172 | my $access_token = $_[0]; | ||
| 173 | my $agentid = $_[1]; | ||
| 174 | my $json = $_[2]; | ||
| 175 | my $ua = LWP::UserAgent->new; | ||
| 176 | $ua->timeout(30); | ||
| 177 | $ua->env_proxy; | ||
| 178 | |||
| 179 | my $response = $ua->post("https://qyapi.weixin.qq.com/cgi-bin/menu/create?access_token=$access_token&agentid=$agentid",Content => to_json($json,{allow_nonref=>1}),Content_type =>'application/json'); | ||
| 180 | if ($response->is_success) { | ||
| 181 | return from_json($response->decoded_content,{utf8 => 1, allow_nonref => 1}); | ||
| 182 | } | ||
| 183 | |||
| 184 | } | ||
| 185 | return 0; | ||
| 186 | } | ||
| 187 | |||
| 188 | =head2 get(access_token,agentid); | ||
| 189 | |||
| 190 | 获取菜单 | ||
| 191 | 最后更新:2019/02/28 | ||
| 192 | |||
| 193 | =head2 SYNOPSIS | ||
| 194 | |||
| 195 | L<https://developer.work.weixin.qq.com/document/path/90232> | ||
| 196 | |||
| 197 | =head3 请求说明: | ||
| 198 | |||
| 199 | =head4 参数说明: | ||
| 200 | |||
| 201 | 参数 必须 说明 | ||
| 202 | access_token 是 调用接口凭证 | ||
| 203 | agentid 是 应用id | ||
| 204 | |||
| 205 | =head4 权限说明: | ||
| 206 | |||
| 207 | 仅企业可调用;第三方不可调用。 | ||
| 208 | |||
| 209 | =head3 RETURN 返回结果: | ||
| 210 | |||
| 211 | 返回结果与请参考菜单创建接口 | ||
| 212 | |||
| 213 | =cut | ||
| 214 | |||
| 215 | sub get { | ||
| 216 | if ( @_ && $_[0] && $_[1] ) { | ||
| 217 | my $access_token = $_[0]; | ||
| 218 | my $agentid = $_[1]; | ||
| 219 | my $ua = LWP::UserAgent->new; | ||
| 220 | $ua->timeout(30); | ||
| 221 | $ua->env_proxy; | ||
| 222 | |||
| 223 | my $response = $ua->get("https://qyapi.weixin.qq.com/cgi-bin/menu/get?access_token=$access_token&agentid=$agentid"); | ||
| 224 | if ($response->is_success) { | ||
| 225 | return from_json($response->decoded_content,{utf8 => 1, allow_nonref => 1}); | ||
| 226 | } | ||
| 227 | |||
| 228 | } | ||
| 229 | return 0; | ||
| 230 | } | ||
| 231 | |||
| 232 | =head2 delete(access_token,agentid); | ||
| 233 | |||
| 234 | 删除菜单 | ||
| 235 | 最后更新:2019/02/28 | ||
| 236 | |||
| 237 | =head2 SYNOPSIS | ||
| 238 | |||
| 239 | L<https://work.weixin.qq.com/api/doc/90000/90135/90233> | ||
| 240 | |||
| 241 | =head3 请求说明: | ||
| 242 | |||
| 243 | =head4 参数说明: | ||
| 244 | |||
| 245 | 参数 必须 说明 | ||
| 246 | access_token 是 调用接口凭证 | ||
| 247 | agentid 是 应用id | ||
| 248 | |||
| 249 | =head4 权限说明: | ||
| 250 | |||
| 251 | 仅企业可调用;第三方不可调用。 | ||
| 252 | |||
| 253 | =head3 RETURN 返回结果: | ||
| 254 | |||
| 255 | { | ||
| 256 | "errcode":0, | ||
| 257 | "errmsg":"ok" | ||
| 258 | } | ||
| 259 | |||
| 260 | =cut | ||
| 261 | |||
| 262 | sub delete { | ||
| 263 | if ( @_ && $_[0] && $_[1] ) { | ||
| 264 | my $access_token = $_[0]; | ||
| 265 | my $agentid = $_[1]; | ||
| 266 | my $ua = LWP::UserAgent->new; | ||
| 267 | $ua->timeout(30); | ||
| 268 | $ua->env_proxy; | ||
| 269 | |||
| 270 | my $response = $ua->get("https://qyapi.weixin.qq.com/cgi-bin/menu/delete?access_token=$access_token&agentid=$agentid"); | ||
| 271 | if ($response->is_success) { | ||
| 272 | return from_json($response->decoded_content,{utf8 => 1, allow_nonref => 1}); | ||
| 273 | } | ||
| 274 | |||
| 275 | } | ||
| 276 | return 0; | ||
| 277 | } | ||
| 278 | |||
| 279 | |||
| 280 | 1; | ||
| 281 | __END__ |