|
@@ -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());
|
|
|
}
|
|
|
}
|
|
|
}
|