Browse Source

微信公众号-菜单bugfix

chao 3 năm trước cách đây
mục cha
commit
ea60eed4f4

+ 33 - 37
src/main/java/com/caimei365/manager/service/wechat/impl/WechatMenuServiceImpl.java

@@ -1,6 +1,7 @@
 package com.caimei365.manager.service.wechat.impl;
 
 import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 import com.caimei365.manager.dao.SystemDao;
 import com.caimei365.manager.dao.WeChatDao;
@@ -15,9 +16,7 @@ import org.springframework.util.StringUtils;
 
 import javax.annotation.Resource;
 import java.util.ArrayList;
-import java.util.HashMap;
 import java.util.List;
-import java.util.Map;
 
 /**
  * Description
@@ -213,43 +212,39 @@ public class WechatMenuServiceImpl implements WechatMenuService {
             return ResponseJson.error("菜单类型不正确!", null);
         }
         // 一级菜单
-        List<WechatMenu> buttons = weChatDao.getWechatMenuList(type, wxType);
-        if (buttons.size() >= 3) {
+        List<WechatMenu> rootMenus = weChatDao.getWechatMenuList(type, wxType);
+        if (rootMenus.size() > 3) {
             return ResponseJson.error("最多包括3个一级菜单!", null);
         }
         // 组装请求数据
-        List<Map<String,Object>> buttonMapList = new ArrayList<>();
-        for (WechatMenu button : buttons) {
-            Map<String,Object> buttonMap = new HashMap();
-            buttonMap.put("name", button.getName());
-            if (StringUtils.hasLength(button.getType())) {
+        JSONArray jsonArr = new JSONArray();
+        for (WechatMenu menu : rootMenus) {
+            JSONObject button = new JSONObject();
+            button.put("name", menu.getName());
+            if (StringUtils.hasLength(menu.getType())) {
                 // type不为空,则只有一级菜单
-                buttonMap.put("type", button.getType());
-                setTypeValue(button, buttonMap);
+                setMenuJsonValue(button, menu);
             } else {
-                // 二级菜单
-                List<WechatMenu> subList = weChatDao.getWechatMenuList(button.getId(), wxType);
-                if (subList.size() >= 5) {
+                // type 为空,则有二级菜单
+                List<WechatMenu> subList = weChatDao.getWechatMenuList(menu.getId(), wxType);
+                if (subList.size() > 5) {
                     return ResponseJson.error("每个一级菜单最多包含5个二级菜单!", null);
                 }
-                List<Map<String,Object>> menuMapList = new ArrayList<>();
-                for (WechatMenu menu : subList) {
-                    Map<String,Object> menuMap = new HashMap();
-                    menuMap.put("name", menu.getName());
-                    menuMap.put("type", menu.getType());
-                    setTypeValue(menu, menuMap);
-                    menuMapList.add(menuMap);
+                JSONArray subArr = new JSONArray();
+                for (WechatMenu sub : subList) {
+                    JSONObject subBtn = new JSONObject();
+                    subBtn.put("name", sub.getName());
+                    setMenuJsonValue(subBtn, sub);
+                    subArr.add(subBtn);
                 }
-                // 把二级菜单放入上级菜单的集合
-                buttonMap.put("sub_button", menuMapList);
+                // 把二级菜单放入上级菜单中
+                button.put("sub_button", subArr);
             }
-            buttonMapList.add(buttonMap);
+            jsonArr.add(button);
         }
-        Map<String,Object> map = new HashMap();
-        map.put("button", buttonMapList);
         // json 字符串
-        String jsonString = JSON.toJSONString(map);
-        log.info("》》》微信公众号菜单发布,发布数据:" + jsonString);
+        String jsonString = jsonArr.toJSONString();
+        log.info(">>>>>>>>微信公众号菜单发布,发布数据:" + jsonString);
         // 组装请求链接
         String url = wechatApiUrl + "/cgi-bin/menu/create?access_token=";
         if (1 == type) {
@@ -283,22 +278,23 @@ public class WechatMenuServiceImpl implements WechatMenuService {
     /**
      * 组装菜单数据
      */
-    private void setTypeValue(WechatMenu menu, Map<String, Object> map) {
+    private void setMenuJsonValue(JSONObject json, WechatMenu menu) {
+        json.put("type", menu.getType());
         if ("view".equals(menu.getType())) {
-            map.put("url", menu.getUrl());
+            json.put("url", menu.getUrl());
         } else if ("miniprogram".equals(menu.getType())) {
             // 小程序跳转
-            map.put("url", publicUrl);
-            map.put("appid", menu.getAppid());
-            map.put("pagepath", menu.getPagePath());
+            json.put("url", publicUrl);
+            json.put("appid", menu.getAppid());
+            json.put("pagepath", menu.getPagePath());
         } else if ("article_id".equals(menu.getType()) || "article_view_limited".equals(menu.getType())) {
-            map.put("article_id", menu.getArticleId());
+            json.put("article_id", menu.getArticleId());
         } else if ("media_id".equals(menu.getType()) || "view_limited".equals(menu.getType())) {
-            map.put("media_id", menu.getMediaId());
+            json.put("media_id", menu.getMediaId());
         } else {
-            map.put("key", menu.getKey());
+            json.put("key", menu.getKey());
             if(!"click".equals(menu.getType())) {
-                map.put("sub_button", new ArrayList());
+                json.put("sub_button", new ArrayList());
             }
         }
     }

+ 2 - 2
src/main/resources/mapper/WeChatDao.xml

@@ -2,7 +2,7 @@
 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 <mapper namespace="com.caimei365.manager.dao.WeChatDao">
     <select id="getWechatMenuList" resultType="com.caimei365.manager.entity.wechat.WechatMenu">
-        SELECT a.id, a.parent_id AS parentId, a.parent_ids AS parentIds, a.`name`, a.sort, a.type, a.`key`,
+        SELECT a.id, a.parent_id AS parentId, a.parent_ids AS parentIds, a.`name`, a.sort, TRIM(a.type) AS type, a.`key`,
                a.url, a.media_id AS mediaId, a.appid, a.page_path AS pagePath, a.wx_type AS wxType
         FROM wechat_menu a
         WHERE a.wx_type = #{wxType} AND a.parent_id = #{parentId}
@@ -41,7 +41,7 @@
                 AND a.response_type = #{responseType}
             </if>
             <if test="msgType != null and msgType != ''">
-                AND a.wx_type = #{msgType}
+                AND a.msg_type = #{msgType}
             </if>
             <if test="title != null and title != ''">
                 AND a.title LIKE concat('%',#{title},'%')