From 217882984233d0541de9a9cd99b6a97eb22b2fe9 Mon Sep 17 00:00:00 2001 From: xiaoyong931011 <15274802129@163.com> Date: Sat, 15 Jul 2023 10:07:24 +0800 Subject: [PATCH] 服务号菜单 --- src/main/java/cc/mrbird/febs/pay/model/ComplexButton.java | 11 +++ src/main/java/cc/mrbird/febs/mall/quartz/WxxcxJob.java | 1 src/main/java/cc/mrbird/febs/pay/model/Menu.java | 11 +++ src/main/java/cc/mrbird/febs/pay/util/WechatMenuUtil.java | 122 ++++++++++++++++++++++++++++++++++++++++ src/main/java/cc/mrbird/febs/pay/model/CommonButton.java | 13 ++++ src/main/java/cc/mrbird/febs/pay/model/Button.java | 13 ++++ src/main/java/cc/mrbird/febs/pay/util/WechatConfigure.java | 2 7 files changed, 172 insertions(+), 1 deletions(-) diff --git a/src/main/java/cc/mrbird/febs/mall/quartz/WxxcxJob.java b/src/main/java/cc/mrbird/febs/mall/quartz/WxxcxJob.java index d731967..803c177 100644 --- a/src/main/java/cc/mrbird/febs/mall/quartz/WxxcxJob.java +++ b/src/main/java/cc/mrbird/febs/mall/quartz/WxxcxJob.java @@ -39,6 +39,7 @@ * 有效期两小时 */ @Scheduled(cron = "0 0 0/1 * * ? ") +// @Scheduled(cron = "0 0/1 * * * ? ") public void getAccessToken() throws IOException { String appId = xcxProperties.getXcxAppid(); String appSecret = xcxProperties.getXcxSecret(); diff --git a/src/main/java/cc/mrbird/febs/pay/model/Button.java b/src/main/java/cc/mrbird/febs/pay/model/Button.java new file mode 100644 index 0000000..e63cf15 --- /dev/null +++ b/src/main/java/cc/mrbird/febs/pay/model/Button.java @@ -0,0 +1,13 @@ +package cc.mrbird.febs.pay.model; + +import lombok.Data; + +/** + * 菜单项的基类,所有一级菜单、二级菜单都共有一个相同的属性,那就是name + */ +@Data +public class Button { + + private String name; + +} diff --git a/src/main/java/cc/mrbird/febs/pay/model/CommonButton.java b/src/main/java/cc/mrbird/febs/pay/model/CommonButton.java new file mode 100644 index 0000000..a119d59 --- /dev/null +++ b/src/main/java/cc/mrbird/febs/pay/model/CommonButton.java @@ -0,0 +1,13 @@ +package cc.mrbird.febs.pay.model; + +import lombok.Data; + +/** + * 子菜单项 :没有子菜单的菜单项,有可能是二级菜单项,也有可能是不含二级菜单的一级菜单 + */ +@Data +public class CommonButton extends Button { + private String type; + private String key; + private String url; +} diff --git a/src/main/java/cc/mrbird/febs/pay/model/ComplexButton.java b/src/main/java/cc/mrbird/febs/pay/model/ComplexButton.java new file mode 100644 index 0000000..7466dfb --- /dev/null +++ b/src/main/java/cc/mrbird/febs/pay/model/ComplexButton.java @@ -0,0 +1,11 @@ +package cc.mrbird.febs.pay.model; + +import lombok.Data; + +/** + * 父菜单项 :包含有二级菜单项的一级菜单。这类菜单项包含有二个属性:name和sub_button,而sub_button以是一个子菜单项数组 + */ +@Data +public class ComplexButton extends Button { + private Button[] sub_button; +} diff --git a/src/main/java/cc/mrbird/febs/pay/model/Menu.java b/src/main/java/cc/mrbird/febs/pay/model/Menu.java new file mode 100644 index 0000000..86da9ea --- /dev/null +++ b/src/main/java/cc/mrbird/febs/pay/model/Menu.java @@ -0,0 +1,11 @@ +package cc.mrbird.febs.pay.model; + +import lombok.Data; + +/** + * 整个菜单对象的封装,菜单对象包含多个菜单项(最多只能有3个),这些菜单项即可以是子菜单项(不含二级菜单的一级菜单),也可以是父菜单项(包含二级菜单的菜单项) + */ +@Data +public class Menu { + private Button[] button; +} diff --git a/src/main/java/cc/mrbird/febs/pay/util/WechatConfigure.java b/src/main/java/cc/mrbird/febs/pay/util/WechatConfigure.java index be0a26a..4c940c8 100644 --- a/src/main/java/cc/mrbird/febs/pay/util/WechatConfigure.java +++ b/src/main/java/cc/mrbird/febs/pay/util/WechatConfigure.java @@ -6,7 +6,7 @@ public static final String SEND_INFO_URL = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token="; - public static final String WX_ACCESS_TOKEN_REDIS_KEY = "ywgpz_access_token"; + public static final String WX_ACCESS_TOKEN_REDIS_KEY = "access_token"; public static final String BAIDU_ACCESS_TOKEN_REDIS_KEY = "bd_access_token"; public static final String UNIFIEDORDER = "https://api.mch.weixin.qq.com/pay/unifiedorder"; diff --git a/src/main/java/cc/mrbird/febs/pay/util/WechatMenuUtil.java b/src/main/java/cc/mrbird/febs/pay/util/WechatMenuUtil.java new file mode 100644 index 0000000..b44c818 --- /dev/null +++ b/src/main/java/cc/mrbird/febs/pay/util/WechatMenuUtil.java @@ -0,0 +1,122 @@ +package cc.mrbird.febs.pay.util; + +import cc.mrbird.febs.pay.model.Button; +import cc.mrbird.febs.pay.model.CommonButton; +import cc.mrbird.febs.pay.model.ComplexButton; +import cc.mrbird.febs.pay.model.Menu; +import cn.hutool.json.JSONUtil; +import lombok.extern.slf4j.Slf4j; +import net.sf.json.JSONObject; +import org.springframework.http.converter.StringHttpMessageConverter; +import org.springframework.web.client.RestTemplate; + +import java.nio.charset.Charset; + +/** + * 菜单初始化 + */ +@Slf4j +public class WechatMenuUtil { + + public static void main(String[] args) { + // 第三方用户唯一凭证 + String appId = "wx052716b1a388b13c"; + // 第三方用户唯一凭证密钥 + String appSecret = "e7cf355e7234f035d8fea6a3a19513b9"; + String accessToken = "70_VnGMZaIo0pkY7WJOfGVnRlqUGC8UQPu0eN4L8Hc9PearLQgIAtWOC2UCaErhctjI7ORBk_aeBg3QudsLWi5z5_lIPd_zwMCf7E4g8rHkqvArD-N8jyKOKJfzNGsGWJeAFAKAN"; + + // 调用接口创建菜单 + String result = createMenu(getMenu(), accessToken); + + // 判断菜单创建结果 + if ("0".equals(result)) + log.info("菜单创建成功!"); + else + log.info("菜单创建失败,错误码:" + result); + } + + // 菜单创建(POST) 限100(次/天) + public static String menu_create_url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN"; + + /** + * 创建菜单 + * + * @param menu 菜单实例 + * @param accessToken 有效的access_token + * @return 0表示成功,其他值表示失败 + */ + public static String createMenu(Menu menu, String accessToken) { + RestTemplate restTemplate = new RestTemplate(); + restTemplate.getMessageConverters().stream().filter(converter -> converter instanceof StringHttpMessageConverter).forEach(converter -> ((StringHttpMessageConverter) converter).setDefaultCharset(Charset.forName("UTF-8"))); + String result = "0"; + // 拼装创建菜单的url + String url = menu_create_url.replace("ACCESS_TOKEN", accessToken); + // 将菜单对象转换成json字符串 + String jsonMenu = JSONObject.fromObject(menu).toString(); + + System.out.println(result); + // 调用接口创建菜单 + try { + // 发送post请求,并打印结果,以String类型接收响应结果JSON字符串 + String jsonObject = restTemplate.postForObject(url, jsonMenu, String.class); + if (null != jsonObject) { + cn.hutool.json.JSONObject parseObj = JSONUtil.parseObj(jsonObject); + if (!"0".equals(parseObj.getStr("errcode"))) { + result = parseObj.getStr("errcode"); + log.error("创建菜单失败 errcode:{} errmsg:{}", result, parseObj.getStr("errmsg")); + } + } + } catch (Exception e) { + log.error("创建菜单失败",e); + } + return result; + } + + /** + * 组装菜单数据 + * + * @return + */ + private static Menu getMenu() { + CommonButton btn11 = new CommonButton(); + btn11.setName("商城"); + btn11.setType("view"); + btn11.setKey("11"); + btn11.setUrl("https://www.baidu.com/"); + + CommonButton btn24 = new CommonButton(); + btn24.setName("公司简介"); + btn24.setType("click"); + btn24.setKey("24"); + + CommonButton btn31 = new CommonButton(); + btn31.setName("我的分享码"); + btn31.setType("click"); + btn31.setKey("31"); + + /** + * 微信: mainBtn1,mainBtn2,mainBtn3底部的三个一级菜单。 + */ + + ComplexButton mainBtn1 = new ComplexButton(); + mainBtn1.setName("药王谷铺子"); + mainBtn1.setSub_button(new CommonButton[] { btn11}); + + ComplexButton mainBtn2 = new ComplexButton(); + mainBtn2.setName("公司简介"); + mainBtn2.setSub_button(new CommonButton[] { btn24 }); + + ComplexButton mainBtn3 = new ComplexButton(); + mainBtn3.setName("我的分享码"); + mainBtn3.setSub_button(new CommonButton[] { btn31 }); + + + /** + * 封装整个菜单 + */ + Menu menu = new Menu(); + menu.setButton(new Button[] { mainBtn1, mainBtn2, mainBtn3 }); + + return menu; + } +} -- Gitblit v1.9.1