|
3 | 3 | import me.chanjar.weixin.common.bean.menu.WxMenu; |
4 | 4 | import me.chanjar.weixin.common.bean.menu.WxMenuButton; |
5 | 5 | import me.chanjar.weixin.common.error.WxErrorException; |
6 | | -import me.chanjar.weixin.mp.api.WxMpConfigStorage; |
7 | | -import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage; |
8 | 6 | import me.chanjar.weixin.mp.api.WxMpService; |
9 | 7 | import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl; |
| 8 | +import me.chanjar.weixin.mp.config.WxMpConfigStorage; |
| 9 | +import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl; |
10 | 10 |
|
11 | 11 | import static me.chanjar.weixin.common.api.WxConsts.MenuButtonType; |
12 | 12 |
|
|
16 | 16 | */ |
17 | 17 | public class MenuConfig { |
18 | 18 |
|
19 | | -/** |
20 | | - * 定义菜单结构 |
21 | | - * |
22 | | - * @return |
23 | | - */ |
24 | | -protected static WxMenu getMenu() { |
25 | | - |
26 | | -MainConfig mainConfig = new MainConfig("appid", "appsecret", "token", "aesKey"); |
27 | | -WxMpService wxMpService = mainConfig.wxMpService(); |
28 | | - |
29 | | -WxMenu menu = new WxMenu(); |
30 | | -WxMenuButton button1 = new WxMenuButton(); |
31 | | -button1.setType(MenuButtonType.VIEW); |
32 | | -button1.setName("买家订单"); |
33 | | -button1.setUrl(wxMpService.oauth2buildAuthorizationUrl("", "snsapi_base", "")); |
34 | | - |
35 | | -WxMenuButton button2 = new WxMenuButton(); |
36 | | -button2.setName("我是卖家"); |
37 | | - |
38 | | -WxMenuButton button21 = new WxMenuButton(); |
39 | | -button21.setType(MenuButtonType.VIEW); |
40 | | -button21.setName("我的订单"); |
41 | | -button21.setUrl(wxMpService.oauth2buildAuthorizationUrl("", "snsapi_base", "")); |
42 | | - |
43 | | -WxMenuButton button22 = new WxMenuButton(); |
44 | | -button22.setType(MenuButtonType.VIEW); |
45 | | -button22.setName("收入统计"); |
46 | | -button22.setUrl(wxMpService.oauth2buildAuthorizationUrl("", "snsapi_base", "")); |
47 | | - |
48 | | -WxMenuButton button23 = new WxMenuButton(); |
49 | | -button23.setType(MenuButtonType.VIEW); |
50 | | -button23.setName("发布商品"); |
51 | | -button23.setUrl(wxMpService.oauth2buildAuthorizationUrl("", "snsapi_base", "")); |
52 | | - |
53 | | -WxMenuButton button24 = new WxMenuButton(); |
54 | | -button24.setType(MenuButtonType.VIEW); |
55 | | -button24.setName("商品管理"); |
56 | | -button24.setUrl(wxMpService.oauth2buildAuthorizationUrl("", "snsapi_base", "")); |
57 | | - |
58 | | -button2.getSubButtons().add(button21); |
59 | | -button2.getSubButtons().add(button22); |
60 | | -button2.getSubButtons().add(button23); |
61 | | -button2.getSubButtons().add(button24); |
62 | | - |
63 | | -WxMenuButton button3 = new WxMenuButton(); |
64 | | -button3.setType(MenuButtonType.CLICK); |
65 | | -button3.setName("使用帮助"); |
66 | | -button3.setKey("help"); |
67 | | - |
68 | | -menu.getButtons().add(button1); |
69 | | -menu.getButtons().add(button2); |
70 | | -menu.getButtons().add(button3); |
71 | | - |
72 | | -return menu; |
73 | | -} |
74 | | - |
75 | | - |
76 | | -public static class MainConfig { |
77 | | - |
78 | | -private String appId; |
79 | | -private String appSecret; |
80 | | -private String token; |
81 | | -private String aesKey; |
82 | | - |
83 | | -/** |
84 | | - * 为了生成自定义菜单使用的构造函数 |
85 | | - * |
86 | | - * @param appId |
87 | | - * @param appSecret |
88 | | - * @param token |
89 | | - * @param aesKey |
90 | | - */ |
91 | | -protected MainConfig(String appId, String appSecret, String token, String aesKey) { |
92 | | -this.appId = appId; |
93 | | -this.appSecret = appSecret; |
94 | | -this.token = token; |
95 | | -this.aesKey = aesKey; |
96 | | -} |
97 | | - |
98 | | -public WxMpConfigStorage wxMpConfigStorage() { |
99 | | -WxMpInMemoryConfigStorage configStorage = new WxMpInMemoryConfigStorage(); |
100 | | -configStorage.setAppId(this.appId); |
101 | | -configStorage.setSecret(this.appSecret); |
102 | | -configStorage.setToken(this.token); |
103 | | -configStorage.setAesKey(this.aesKey); |
104 | | -return configStorage; |
105 | | -} |
106 | | - |
107 | | -public WxMpService wxMpService() { |
108 | | -WxMpService wxMpService = new WxMpServiceImpl(); |
109 | | -wxMpService.setWxMpConfigStorage(wxMpConfigStorage()); |
110 | | -return wxMpService; |
111 | | -} |
112 | | - |
113 | | -} |
114 | | - |
115 | | -/** |
116 | | - * 运行此main函数即可生成公众号自定义菜单,注意要修改MainConfig构造方法行代码的对应四个参数为实际值 |
117 | | - * |
118 | | - * @param args |
119 | | - */ |
120 | | -public static void main(String[] args) { |
121 | | -MainConfig mainConfig = new MainConfig("appid", "appsecret", "token", "aesKey"); |
122 | | -WxMpService wxMpService = mainConfig.wxMpService(); |
123 | | -try { |
124 | | -wxMpService.getMenuService().menuCreate(getMenu()); |
125 | | -} catch (WxErrorException e) { |
126 | | -e.printStackTrace(); |
127 | | -} |
128 | | -} |
| 19 | + /** |
| 20 | + * 定义菜单结构 |
| 21 | + * |
| 22 | + * @return |
| 23 | + */ |
| 24 | + protected static WxMenu getMenu() { |
| 25 | + |
| 26 | + MainConfig mainConfig = new MainConfig("appid", "appsecret", "token", "aesKey"); |
| 27 | + WxMpService wxMpService = mainConfig.wxMpService(); |
| 28 | + |
| 29 | + WxMenu menu = new WxMenu(); |
| 30 | + WxMenuButton button1 = new WxMenuButton(); |
| 31 | + button1.setType(MenuButtonType.VIEW); |
| 32 | + button1.setName("买家订单"); |
| 33 | + button1.setUrl(wxMpService.oauth2buildAuthorizationUrl("", "snsapi_base", "")); |
| 34 | + |
| 35 | + WxMenuButton button2 = new WxMenuButton(); |
| 36 | + button2.setName("我是卖家"); |
| 37 | + |
| 38 | + WxMenuButton button21 = new WxMenuButton(); |
| 39 | + button21.setType(MenuButtonType.VIEW); |
| 40 | + button21.setName("我的订单"); |
| 41 | + button21.setUrl(wxMpService.oauth2buildAuthorizationUrl("", "snsapi_base", "")); |
| 42 | + |
| 43 | + WxMenuButton button22 = new WxMenuButton(); |
| 44 | + button22.setType(MenuButtonType.VIEW); |
| 45 | + button22.setName("收入统计"); |
| 46 | + button22.setUrl(wxMpService.oauth2buildAuthorizationUrl("", "snsapi_base", "")); |
| 47 | + |
| 48 | + WxMenuButton button23 = new WxMenuButton(); |
| 49 | + button23.setType(MenuButtonType.VIEW); |
| 50 | + button23.setName("发布商品"); |
| 51 | + button23.setUrl(wxMpService.oauth2buildAuthorizationUrl("", "snsapi_base", "")); |
| 52 | + |
| 53 | + WxMenuButton button24 = new WxMenuButton(); |
| 54 | + button24.setType(MenuButtonType.VIEW); |
| 55 | + button24.setName("商品管理"); |
| 56 | + button24.setUrl(wxMpService.oauth2buildAuthorizationUrl("", "snsapi_base", "")); |
| 57 | + |
| 58 | + button2.getSubButtons().add(button21); |
| 59 | + button2.getSubButtons().add(button22); |
| 60 | + button2.getSubButtons().add(button23); |
| 61 | + button2.getSubButtons().add(button24); |
| 62 | + |
| 63 | + WxMenuButton button3 = new WxMenuButton(); |
| 64 | + button3.setType(MenuButtonType.CLICK); |
| 65 | + button3.setName("使用帮助"); |
| 66 | + button3.setKey("help"); |
| 67 | + |
| 68 | + menu.getButtons().add(button1); |
| 69 | + menu.getButtons().add(button2); |
| 70 | + menu.getButtons().add(button3); |
| 71 | + |
| 72 | + return menu; |
| 73 | + } |
| 74 | + |
| 75 | + |
| 76 | + public static class MainConfig { |
| 77 | + |
| 78 | + private String appId; |
| 79 | + private String appSecret; |
| 80 | + private String token; |
| 81 | + private String aesKey; |
| 82 | + |
| 83 | + /** |
| 84 | + * 为了生成自定义菜单使用的构造函数 |
| 85 | + * |
| 86 | + * @param appId |
| 87 | + * @param appSecret |
| 88 | + * @param token |
| 89 | + * @param aesKey |
| 90 | + */ |
| 91 | + protected MainConfig(String appId, String appSecret, String token, String aesKey) { |
| 92 | + this.appId = appId; |
| 93 | + this.appSecret = appSecret; |
| 94 | + this.token = token; |
| 95 | + this.aesKey = aesKey; |
| 96 | + } |
| 97 | + |
| 98 | + public WxMpConfigStorage wxMpConfigStorage() { |
| 99 | + WxMpDefaultConfigImpl configStorage = new WxMpDefaultConfigImpl(); |
| 100 | + configStorage.setAppId(this.appId); |
| 101 | + configStorage.setSecret(this.appSecret); |
| 102 | + configStorage.setToken(this.token); |
| 103 | + configStorage.setAesKey(this.aesKey); |
| 104 | + return configStorage; |
| 105 | + } |
| 106 | + |
| 107 | + public WxMpService wxMpService() { |
| 108 | + WxMpService wxMpService = new WxMpServiceImpl(); |
| 109 | + wxMpService.setWxMpConfigStorage(wxMpConfigStorage()); |
| 110 | + return wxMpService; |
| 111 | + } |
| 112 | + |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * 运行此main函数即可生成公众号自定义菜单,注意要修改MainConfig构造方法行代码的对应四个参数为实际值 |
| 117 | + * |
| 118 | + * @param args |
| 119 | + */ |
| 120 | + public static void main(String[] args) { |
| 121 | + MainConfig mainConfig = new MainConfig("appid", "appsecret", "token", "aesKey"); |
| 122 | + WxMpService wxMpService = mainConfig.wxMpService(); |
| 123 | + try { |
| 124 | + wxMpService.getMenuService().menuCreate(getMenu()); |
| 125 | + } catch (WxErrorException e) { |
| 126 | + e.printStackTrace(); |
| 127 | + } |
| 128 | + } |
129 | 129 |
|
130 | 130 | } |
0 commit comments