增加公众号菜单管理

This commit is contained in:
TinyAnts 2022-09-13 19:02:15 +08:00
parent f4eca5bc13
commit 3cd11784e4
3 changed files with 126 additions and 11 deletions

View File

@ -2,36 +2,56 @@ package com.mdd.admin.controller.channel;
import com.mdd.admin.service.channel.IChannelOaMenuService; import com.mdd.admin.service.channel.IChannelOaMenuService;
import com.mdd.common.core.AjaxResult; import com.mdd.common.core.AjaxResult;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.List;
/** /**
* 公众号菜单管理 * 公众号菜单管理
*/ */
@RestController @RestController
@RequestMapping("api/channel/oa/menu") @RequestMapping("api/channel/oaMenu")
public class OaMenuController { public class OaMenuController {
@Resource @Resource
IChannelOaMenuService iChannelOaMenuService; IChannelOaMenuService iChannelOaMenuService;
@GetMapping("/list") /**
public Object list() { * 菜单详情
*
* @author fzr
* @return Object
*/
@GetMapping("/detail")
public Object detail() {
iChannelOaMenuService.list(); iChannelOaMenuService.list();
return AjaxResult.success(); return AjaxResult.success();
} }
@PostMapping("/add") /**
public Object add() { * 仅是保存菜单
*
* @author fzr
* @param params 参数
* @return Object
*/
@PostMapping("/save")
public Object save(@RequestBody List<Object> params) {
iChannelOaMenuService.save(params, false);
return AjaxResult.success(); return AjaxResult.success();
} }
@PostMapping("/del") /**
public Object del() { * 保存并发布菜单
*
* @author fzr
* @param params 参数
* @return Object
*/
@PostMapping("/publish")
public Object publish(@RequestBody List<Object> params) {
iChannelOaMenuService.save(params, true);
return AjaxResult.success(); return AjaxResult.success();
} }

View File

@ -1,5 +1,8 @@
package com.mdd.admin.service.channel; package com.mdd.admin.service.channel;
import java.util.List;
import java.util.Map;
/** /**
* 公众号菜单服务接口类 * 公众号菜单服务接口类
*/ */
@ -7,4 +10,13 @@ public interface IChannelOaMenuService {
Object list(); Object list();
/**
* 保存菜单
*
* @author fzr
* @param params 参数
* @param isPublish 是否发布
*/
void save(List<Object> params, Boolean isPublish);
} }

View File

@ -1,9 +1,17 @@
package com.mdd.admin.service.channel.impl; package com.mdd.admin.service.channel.impl;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl; import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import com.mdd.admin.service.channel.IChannelOaMenuService; import com.mdd.admin.service.channel.IChannelOaMenuService;
import com.mdd.common.exception.OperateException;
import com.mdd.common.utils.ArrayUtil;
import com.mdd.common.utils.ConfigUtil;
import com.mdd.common.utils.StringUtil;
import com.mdd.common.utils.ToolsUtil;
import me.chanjar.weixin.common.bean.menu.WxMenu; import me.chanjar.weixin.common.bean.menu.WxMenu;
import me.chanjar.weixin.common.bean.menu.WxMenuButton;
import me.chanjar.weixin.common.error.WxErrorException; import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.service.WxService; import me.chanjar.weixin.common.service.WxService;
import me.chanjar.weixin.mp.api.WxMpMenuService; import me.chanjar.weixin.mp.api.WxMpMenuService;
@ -15,6 +23,8 @@ import me.chanjar.weixin.mp.config.WxMpConfigStorage;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl; import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.LinkedList;
import java.util.List;
import java.util.Map; import java.util.Map;
@Service @Service
@ -45,4 +55,77 @@ public class ChannelOaMenuServiceImpl implements IChannelOaMenuService {
return null; return null;
} }
@Override
public void save(List<Object> objs, Boolean isPublish) {
if (objs.size() > 3) {
throw new OperateException("一级菜单超出限制(最多3个)");
}
List<Map<String, String>> params = new LinkedList<>();
for (Object o : objs) {
params.add(ToolsUtil.objectToMap(o));
}
List<WxMenuButton> menuButtons = new LinkedList<>();
for (Map<String, String> item : params) {
// 一级菜单
Assert.notNull(item.get("name"), "一级菜单名称不能为空");
WxMenuButton wxMenuButton = new WxMenuButton();
if (Integer.parseInt(item.get("menuType")) == 1) {
Assert.notNull(item.get("visitType"), "一级菜单visitType数缺失");
if (item.get("visitType").equals("miniprogram")) {
Assert.notNull(item.get("appId"), "一级菜单appId参数缺失");
Assert.notNull(item.get("url"), "一级菜单url数缺失");
Assert.notNull(item.get("pagePath"), "一级菜单pagePath数缺失");
wxMenuButton.setType(item.get("visitType"));
wxMenuButton.setAppId(item.get("appId"));
wxMenuButton.setUrl(item.get("url"));
wxMenuButton.setPagePath(item.get("pagePath"));
} else {
Assert.notNull(item.get("url"), "一级菜单url数缺失");
wxMenuButton.setType(item.get("visitType"));
wxMenuButton.setUrl(item.get("url"));
}
menuButtons.add(wxMenuButton);
}
// 子级菜单
if (Integer.parseInt(item.get("menuType")) == 2) {
Assert.notNull(item.get("subButtons"), "子级菜单不能为空");
List<Map<String, String>> subButtons = ArrayUtil.stringToListAsMapStr(item.get("subButtons"));
if (subButtons.size() > 5) {
throw new OperateException("子级菜单超出限制(最多5个)");
}
for (Map<String, String> subItem : subButtons) {
WxMenuButton subMenuButton = new WxMenuButton();
if (Integer.parseInt(subItem.get("menuType")) == 1) {
Assert.notNull(subItem.get("visitType"), "子级菜单visitType参数缺失!");
if (subItem.get("visitType").equals("miniprogram")) {
Assert.notNull(subItem.get("appId"), "子级菜单appId参数缺失!");
Assert.notNull(subItem.get("url"), "子级菜单url数缺失!");
Assert.notNull(subItem.get("pagePath"), "子级菜单pagePath数缺失!");
wxMenuButton.setType(subItem.get("visitType"));
wxMenuButton.setAppId(subItem.get("appId"));
wxMenuButton.setUrl(subItem.get("url"));
wxMenuButton.setPagePath(subItem.get("pagePath"));
} else {
Assert.notNull(subItem.get("url"), "子级菜单url数缺失");
wxMenuButton.setType(subItem.get("visitType"));
wxMenuButton.setUrl(subItem.get("url"));
}
menuButtons.add(subMenuButton);
}
}
}
}
ConfigUtil.set("oa_channel", "menus", JSON.toJSONString(menuButtons));
if (isPublish) {
WxMenu wxMenu = new WxMenu();
wxMenu.setButtons(menuButtons);
}
}
} }