我是靠谱客的博主 单薄豌豆,这篇文章主要介绍【微信公众号】如何设置微信公众号测试号自定义菜单(java),现在分享给大家,希望可以做个参考。

由于测试号不能像公众号那样直接设置自定义菜单,这个时候就需要自己编写代码了。

整个项目是使用springboot编写了,运行后需要访问localhost/menu查看结果。项目下载git地址如下:

github: https://github.com/smile-yan/weixin-menu-setting/

首先需要导入WxJava的jar包,

复制代码
1
2
3
4
5
6
<!-- https://mvnrepository.com/artifact/com.github.binarywang/weixin-java-mp --> <dependency> <groupId>com.github.binarywang</groupId> <artifactId>weixin-java-mp</artifactId> <version>3.3.0</version> </dependency>

但是借助WxJava SDK,非常容易实现。代码如下:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package cn.smileyan.weixin.controller; import me.chanjar.weixin.common.api.WxConsts; import me.chanjar.weixin.common.bean.menu.WxMenu; import me.chanjar.weixin.common.bean.menu.WxMenuButton; import me.chanjar.weixin.common.error.WxErrorException; import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage; import me.chanjar.weixin.mp.api.WxMpService; import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * @author smileyan * 其中appid与appsecret是读取application.yml中的数据 */ @RestController public class MenuController { @Value("${wechat.appid}") private String appid; @Value("${wechat.appsecret}") private String appsecret; @RequestMapping("/menu") private String setMenu() { // 1.根据appid和appsecret和回调地址配置微信授权 WxMpInMemoryConfigStorage wxMpConfigStorage = new WxMpInMemoryConfigStorage(); wxMpConfigStorage.setAppId(appid); wxMpConfigStorage.setSecret(appsecret); WxMpServiceImpl wxMpService = new WxMpServiceImpl(); wxMpService.setWxMpConfigStorage(wxMpConfigStorage); /** * 2. 设置按钮 * menu对象是总的按钮,button是具体的按钮 */ WxMenu menu = new WxMenu(); WxMenuButton button1 = new WxMenuButton(); button1.setType(WxConsts.MenuButtonType.VIEW); button1.setName("泛舟网络"); button1.setUrl("https://www.smileyan.cn/movie/login/login"); WxMenuButton button2 = new WxMenuButton(); button2.setType(WxConsts.MenuButtonType.VIEW); button2.setName("影院"); button2.setUrl("https://www.smileyan.cn/movie/login/welcome"); // 3. 添加到menu menu.getButtons().add(button1); menu.getButtons().add(button2); String result = menu.toJson().toString(); System.out.println(result); // 根据运行结果返回相应的字符串 try { wxMpService.getMenuService().menuCreate(result); return "SUCCESS"+" "+result; } catch (WxErrorException e) { e.printStackTrace(); } return "FAILURE"+result; } }

接着我们需要打开浏览器访问localhost/menu,如果返回值是SUCCESS...说明成功了就可以看到效果了,但是需要注意有时候微信公众号不会更新这么快,有时候需要我们取消关注,然后再次关注,才能看到自定义菜单。

如果返回值有FAILURE,说明失败了,需要检查是否哪个地方出错了。特别注意:如果是url的话,不能含有空格,并且对应的setType需要时view。

感谢Wechat-Group为我们提供这么好用的工具!

最后

以上就是单薄豌豆最近收集整理的关于【微信公众号】如何设置微信公众号测试号自定义菜单(java)的全部内容,更多相关【微信公众号】如何设置微信公众号测试号自定义菜单(java)内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(76)

评论列表共有 0 条评论

立即
投稿
返回
顶部