From ec642e3f2e61daa7a4a395b43994c12b5b035905 Mon Sep 17 00:00:00 2001 From: ronger Date: Wed, 17 Mar 2021 09:59:24 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E9=80=9A=E8=BF=87=E8=AF=BB?= =?UTF-8?q?=E5=8F=96=E6=9C=AC=E5=9C=B0=20json=20=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=88=9B=E5=BB=BA=E5=85=AC=E4=BC=97=E5=8F=B7=E8=8F=9C=E5=8D=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../wx/mp/controller/WxMenuController.java | 54 +++---------------- .../forest/wx/mp/service/WxMenuService.java | 17 ++++++ .../wx/mp/service/impl/WxMenuServiceImpl.java | 46 ++++++++++++++++ src/main/resources/wxMpMenus.json | 21 ++++++++ 4 files changed, 92 insertions(+), 46 deletions(-) create mode 100644 src/main/java/com/rymcu/forest/wx/mp/service/WxMenuService.java create mode 100644 src/main/java/com/rymcu/forest/wx/mp/service/impl/WxMenuServiceImpl.java create mode 100644 src/main/resources/wxMpMenus.json diff --git a/src/main/java/com/rymcu/forest/wx/mp/controller/WxMenuController.java b/src/main/java/com/rymcu/forest/wx/mp/controller/WxMenuController.java index 09668aa..1a0e8ad 100644 --- a/src/main/java/com/rymcu/forest/wx/mp/controller/WxMenuController.java +++ b/src/main/java/com/rymcu/forest/wx/mp/controller/WxMenuController.java @@ -1,5 +1,6 @@ package com.rymcu.forest.wx.mp.controller; +import com.rymcu.forest.wx.mp.service.WxMenuService; import lombok.AllArgsConstructor; import me.chanjar.weixin.common.bean.menu.WxMenu; import me.chanjar.weixin.common.bean.menu.WxMenuButton; @@ -7,8 +8,11 @@ import me.chanjar.weixin.common.error.WxErrorException; import me.chanjar.weixin.mp.api.WxMpService; import me.chanjar.weixin.mp.bean.menu.WxMpGetSelfMenuInfoResult; import me.chanjar.weixin.mp.bean.menu.WxMpMenu; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; +import javax.annotation.Resource; +import java.io.IOException; import java.net.MalformedURLException; import static me.chanjar.weixin.common.api.WxConsts.MenuButtonType; @@ -21,7 +25,8 @@ import static me.chanjar.weixin.common.api.WxConsts.MenuButtonType; @RequestMapping("/wx/menu/{appId}") public class WxMenuController { private final WxMpService wxService; - + @Resource + private WxMenuService wxMenuService; /** *
      * 自定义菜单创建接口
@@ -38,52 +43,9 @@ public class WxMenuController {
     }
 
     @GetMapping("/create")
-    public String menuCreateSample(@PathVariable String appId) throws WxErrorException, MalformedURLException {
-        WxMenu menu = new WxMenu();
-        WxMenuButton button1 = new WxMenuButton();
-        button1.setType(MenuButtonType.VIEW);
-        button1.setName("官方网站");
-        button1.setUrl("https://rymcu.com");
-
-//        WxMenuButton button2 = new WxMenuButton();
-//        button2.setType(WxConsts.BUTTON_MINIPROGRAM);
-//        button2.setName("小程序");
-//        button2.setAppId("wx286b93c14bbf93aa");
-//        button2.setPagePath("pages/lunar/index.html");
-//        button2.setUrl("http://mp.weixin.qq.com");
-
-        WxMenuButton button3 = new WxMenuButton();
-        button3.setName("学习教程");
-
-        menu.getButtons().add(button1);
-//        menu.getButtons().add(button2);
-        menu.getButtons().add(button3);
-
-        WxMenuButton button31 = new WxMenuButton();
-        button31.setType(MenuButtonType.VIEW);
-        button31.setName("51单片机入门教程");
-        button31.setUrl("https://mp.weixin.qq.com/mp/homepage?__biz=MzA3NjMzMzM1Mw==&hid=1&sn=672df75323f9976d990f6be14355070b");
-
-//        WxMenuButton button34 = new WxMenuButton();
-//        button34.setType(MenuButtonType.VIEW);
-//        button34.setName("获取用户信息");
-//
-//        ServletRequestAttributes servletRequestAttributes =
-//            (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
-//        if (servletRequestAttributes != null) {
-//            HttpServletRequest request = servletRequestAttributes.getRequest();
-//            URL requestURL = new URL(request.getRequestURL().toString());
-//            String url = this.wxService.switchoverTo(appId).oauth2buildAuthorizationUrl(
-//                String.format("%s://%s/wx/redirect/%s/greet", requestURL.getProtocol(), requestURL.getHost(), appId),
-//                WxConsts.OAuth2Scope.SNSAPI_USERINFO, null);
-//            button34.setUrl(url);
-//        }
-
-        button3.getSubButtons().add(button31);
-//        button3.getSubButtons().add(button34);
-
+    public String menuCreateSample(@PathVariable String appId) throws WxErrorException, IOException {
         this.wxService.switchover(appId);
-        return this.wxService.getMenuService().menuCreate(menu);
+        return this.wxService.getMenuService().menuCreate(wxMenuService.getMenus());
     }
 
     /**
diff --git a/src/main/java/com/rymcu/forest/wx/mp/service/WxMenuService.java b/src/main/java/com/rymcu/forest/wx/mp/service/WxMenuService.java
new file mode 100644
index 0000000..b67745d
--- /dev/null
+++ b/src/main/java/com/rymcu/forest/wx/mp/service/WxMenuService.java
@@ -0,0 +1,17 @@
+package com.rymcu.forest.wx.mp.service;
+
+import me.chanjar.weixin.common.bean.menu.WxMenu;
+
+import java.io.IOException;
+
+/**
+ * @author ronger
+ */
+public interface WxMenuService {
+    /**
+     * 获取公众号菜单配置
+     * @return
+     * @throws IOException
+     */
+    WxMenu getMenus() throws IOException;
+}
diff --git a/src/main/java/com/rymcu/forest/wx/mp/service/impl/WxMenuServiceImpl.java b/src/main/java/com/rymcu/forest/wx/mp/service/impl/WxMenuServiceImpl.java
new file mode 100644
index 0000000..f70493c
--- /dev/null
+++ b/src/main/java/com/rymcu/forest/wx/mp/service/impl/WxMenuServiceImpl.java
@@ -0,0 +1,46 @@
+package com.rymcu.forest.wx.mp.service.impl;
+
+import com.rymcu.forest.wx.mp.service.WxMenuService;
+import me.chanjar.weixin.common.bean.menu.WxMenu;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.core.io.Resource;
+import org.springframework.stereotype.Service;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Scanner;
+
+/**
+ * @author ronger
+ */
+@Service
+public class WxMenuServiceImpl implements WxMenuService {
+    @Value("classpath:wxMpMenus.json")
+    private Resource menuResource;
+
+    @Override
+    public WxMenu getMenus() throws IOException {
+        File file = menuResource.getFile();
+        String menuJson = this.jsonRead(file);
+        WxMenu wxMenu = WxMenu.fromJson(menuJson);
+        return wxMenu;
+    }
+
+    private String jsonRead(File file) {
+        Scanner scanner = null;
+        StringBuilder buffer = new StringBuilder();
+        try {
+            scanner = new Scanner(file, "utf-8");
+            while (scanner.hasNextLine()) {
+                buffer.append(scanner.nextLine());
+            }
+        } catch (Exception e) {
+
+        } finally {
+            if (scanner != null) {
+                scanner.close();
+            }
+        }
+        return buffer.toString();
+    }
+}
diff --git a/src/main/resources/wxMpMenus.json b/src/main/resources/wxMpMenus.json
new file mode 100644
index 0000000..e2518f1
--- /dev/null
+++ b/src/main/resources/wxMpMenus.json
@@ -0,0 +1,21 @@
+{
+  "menu": {
+    "button": [
+      {
+        "type": "view",
+        "name": "官方网站",
+        "url": "https://rymcu.com"
+      },
+      {
+        "name": "学习教程",
+        "sub_button": [
+          {
+            "type": "view",
+            "name": "51单片机入门教程",
+            "url": "https://mp.weixin.qq.com/mp/homepage?__biz=MzA3NjMzMzM1Mw==&hid=1&sn=672df75323f9976d990f6be14355070b"
+          }
+        ]
+      }
+    ]
+  }
+}
\ No newline at end of file