mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2026-02-16 13:26:23 +08:00
修复完善菜单特别是个性化菜单的创建和删除相关代码
This commit is contained in:
@@ -14,12 +14,24 @@ public interface WxMpMenuService {
|
||||
/**
|
||||
* <pre>
|
||||
* 自定义菜单创建接口
|
||||
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=自定义菜单创建接口
|
||||
* 详情请见:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141013&token=&lang=zh_CN
|
||||
* 如果要创建个性化菜单,请设置matchrule属性
|
||||
* 详情请见:http://mp.weixin.qq.com/wiki/0/c48ccd12b69ae023159b4bfaa7c39c20.html
|
||||
* 详情请见:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1455782296&token=&lang=zh_CN
|
||||
* </pre>
|
||||
* @return 如果是个性化菜单,则返回menuid,否则返回null
|
||||
*/
|
||||
void menuCreate(WxMenu menu) throws WxErrorException;
|
||||
String menuCreate(WxMenu menu) throws WxErrorException;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 自定义菜单创建接口
|
||||
* 详情请见: https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141013&token=&lang=zh_CN
|
||||
* 如果要创建个性化菜单,请设置matchrule属性
|
||||
* 详情请见:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1455782296&token=&lang=zh_CN
|
||||
* </pre>
|
||||
* @return 如果是个性化菜单,则返回menuid,否则返回null
|
||||
*/
|
||||
String menuCreate(String json) throws WxErrorException;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package me.chanjar.weixin.mp.api.impl;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import me.chanjar.weixin.common.bean.menu.WxMenu;
|
||||
import me.chanjar.weixin.common.exception.WxErrorException;
|
||||
import me.chanjar.weixin.mp.api.WxMpMenuService;
|
||||
@@ -24,7 +25,7 @@ public class WxMpMenuServiceImpl implements WxMpMenuService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void menuCreate(WxMenu menu) throws WxErrorException {
|
||||
public String menuCreate(WxMenu menu) throws WxErrorException {
|
||||
String menuJson = menu.toJson();
|
||||
String url = API_URL_PREFIX + "/create";
|
||||
if (menu.getMatchRule() != null) {
|
||||
@@ -35,6 +36,29 @@ public class WxMpMenuServiceImpl implements WxMpMenuService {
|
||||
|
||||
String result = this.wxMpService.post(url, menuJson);
|
||||
log.debug("创建菜单:{},结果:{}", menuJson, result);
|
||||
|
||||
if (menu.getMatchRule() != null) {
|
||||
return new JsonParser().parse(result).getAsJsonObject().get("menuid").getAsString();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String menuCreate(String json) throws WxErrorException {
|
||||
JsonParser jsonParser = new JsonParser();
|
||||
JsonObject jsonObject = jsonParser.parse(json).getAsJsonObject();
|
||||
String url = API_URL_PREFIX + "/create";
|
||||
if (jsonObject.get("matchrule") != null) {
|
||||
url = API_URL_PREFIX + "/addconditional";
|
||||
}
|
||||
|
||||
String result = this.wxMpService.post(url, json);
|
||||
if (jsonObject.get("matchrule") != null) {
|
||||
return jsonParser.parse(result).getAsJsonObject().get("menuid").getAsString();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -50,7 +74,7 @@ public class WxMpMenuServiceImpl implements WxMpMenuService {
|
||||
JsonObject jsonObject = new JsonObject();
|
||||
jsonObject.addProperty("menuid", menuId);
|
||||
String result = this.wxMpService.post(url, jsonObject.toString());
|
||||
log.debug("根据MeunId({})删除菜单结果:{}", menuId, result);
|
||||
log.debug("根据MeunId({})删除个性化菜单结果:{}", menuId, result);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -77,7 +101,7 @@ public class WxMpMenuServiceImpl implements WxMpMenuService {
|
||||
String resultContent = this.wxMpService.post(url, jsonObject.toString());
|
||||
return WxMenu.fromJson(resultContent);
|
||||
} catch (WxErrorException e) {
|
||||
// 46003 不存在的菜单数据 46002 不存在的菜单版本
|
||||
// 46003 不存在的菜单数据;46002 不存在的菜单版本
|
||||
if (e.getError().getErrorCode() == 46003
|
||||
|| e.getError().getErrorCode() == 46002) {
|
||||
return null;
|
||||
|
||||
Reference in New Issue
Block a user