fixed menu create null type bug

This commit is contained in:
jinyu 2017-06-12 16:05:58 +08:00
parent df9ef56bad
commit e1beceb5aa
2 changed files with 99 additions and 99 deletions

View File

@ -18,6 +18,7 @@ import com.foxinmy.weixin4j.mp.model.Menu;
import com.foxinmy.weixin4j.mp.model.MenuMatchRule;
import com.foxinmy.weixin4j.token.TokenManager;
import com.foxinmy.weixin4j.type.ButtonType;
import com.foxinmy.weixin4j.util.StringUtil;
/**
* 菜单相关API
@ -59,16 +60,15 @@ public class MenuApi extends MpApi {
JSON.toJSONString(data, new NameFilter() {
@Override
public String process(Object object, String name, Object value) {
if (object instanceof Button && name.equals("content")) {
if (object instanceof Button && name.equals("content")
&& StringUtil.isNotBlank(((Button) object).getType())) {
ButtonType buttonType = ButtonType.valueOf(((Button) object).getType());
if (buttonType != null) {
if (ButtonType.view == buttonType || ButtonType.miniprogram == buttonType) {
return "url";
} else if (ButtonType.media_id == buttonType || ButtonType.view_limited == buttonType) {
return "media_id";
} else {
return "key";
}
if (ButtonType.view == buttonType || ButtonType.miniprogram == buttonType) {
return "url";
} else if (ButtonType.media_id == buttonType || ButtonType.view_limited == buttonType) {
return "media_id";
} else {
return "key";
}
}
return name;

View File

@ -16,6 +16,7 @@ import com.foxinmy.weixin4j.model.Button;
import com.foxinmy.weixin4j.model.Token;
import com.foxinmy.weixin4j.token.TokenManager;
import com.foxinmy.weixin4j.type.ButtonType;
import com.foxinmy.weixin4j.util.StringUtil;
/**
* 菜单相关API
@ -28,100 +29,99 @@ import com.foxinmy.weixin4j.type.ButtonType;
*/
public class MenuApi extends QyApi {
private final TokenManager tokenManager;
private final TokenManager tokenManager;
public MenuApi(TokenManager tokenManager) {
this.tokenManager = tokenManager;
}
public MenuApi(TokenManager tokenManager) {
this.tokenManager = tokenManager;
}
/**
* 自定义菜单(管理员须拥有应用的管理权限 并且应用必须设置在回调模式)
*
* @param agentid
* 应用ID
*
* @param buttons
* 菜单列表
* @throws WeixinException
* @see <a href=
* "http://qydev.weixin.qq.com/wiki/index.php?title=%E5%88%9B%E5%BB%BA%E5%BA%94%E7%94%A8%E8%8F%9C%E5%8D%95">
* 创建自定义菜单</a>
* @see com.foxinmy.weixin4j.model.Button
*/
public ApiResult createMenu(int agentid, List<Button> buttons) throws WeixinException {
String menu_create_uri = getRequestUri("menu_create_uri");
Token token = tokenManager.getCache();
JSONObject obj = new JSONObject();
obj.put("button", buttons);
WeixinResponse response = weixinExecutor.post(String.format(menu_create_uri, token.getAccessToken(), agentid),
JSON.toJSONString(obj, new NameFilter() {
@Override
public String process(Object object, String name, Object value) {
if (object instanceof Button && name.equals("content")) {
ButtonType buttonType = ButtonType.valueOf(((Button) object).getType());
if (buttonType != null) {
if (ButtonType.view == buttonType) {
return "url";
} else if (ButtonType.media_id == buttonType || ButtonType.view_limited == buttonType) {
return "media_id";
} else {
return "key";
}
}
}
return name;
}
}));
/**
* 自定义菜单(管理员须拥有应用的管理权限 并且应用必须设置在回调模式)
*
* @param agentid
* 应用ID
*
* @param buttons
* 菜单列表
* @throws WeixinException
* @see <a href=
* "http://qydev.weixin.qq.com/wiki/index.php?title=%E5%88%9B%E5%BB%BA%E5%BA%94%E7%94%A8%E8%8F%9C%E5%8D%95">
* 创建自定义菜单</a>
* @see com.foxinmy.weixin4j.model.Button
*/
public ApiResult createMenu(int agentid, List<Button> buttons) throws WeixinException {
String menu_create_uri = getRequestUri("menu_create_uri");
Token token = tokenManager.getCache();
JSONObject obj = new JSONObject();
obj.put("button", buttons);
WeixinResponse response = weixinExecutor.post(String.format(menu_create_uri, token.getAccessToken(), agentid),
JSON.toJSONString(obj, new NameFilter() {
@Override
public String process(Object object, String name, Object value) {
if (object instanceof Button && name.equals("content")
&& StringUtil.isNotBlank(((Button) object).getType())) {
ButtonType buttonType = ButtonType.valueOf(((Button) object).getType());
if (ButtonType.view == buttonType) {
return "url";
} else if (ButtonType.media_id == buttonType || ButtonType.view_limited == buttonType) {
return "media_id";
} else {
return "key";
}
}
return name;
}
}));
return response.getAsResult();
}
return response.getAsResult();
}
/**
* 查询菜单(管理员须拥有应用的管理权限 并且应用必须设置在回调模式)
*
* @param agentid
* 应用ID
* @return 菜单集合
* @throws WeixinException
* @see <a href=
* "http://qydev.weixin.qq.com/wiki/index.php?title=%E8%8E%B7%E5%8F%96%E8%8F%9C%E5%8D%95%E5%88%97%E8%A1%A8">
* 查询菜单</a>
* @see com.foxinmy.weixin4j.model.Button
*/
public List<Button> getMenu(int agentid) throws WeixinException {
String menu_get_uri = getRequestUri("menu_get_uri");
Token token = tokenManager.getCache();
WeixinResponse response = weixinExecutor.get(String.format(menu_get_uri, token.getAccessToken(), agentid));
JSONArray buttons = response.getAsJson().getJSONArray("button");
List<Button> buttonList = new ArrayList<Button>(buttons.size());
ParseProcess processor = new ExtraProcessor() {
@Override
public void processExtra(Object object, String key, Object value) {
((Button) object).setContent(String.valueOf(value));
}
};
for (int i = 0; i < buttons.size(); i++) {
buttonList.add(JSON.parseObject(buttons.getString(i), Button.class, processor));
}
return buttonList;
}
/**
* 查询菜单(管理员须拥有应用的管理权限 并且应用必须设置在回调模式)
*
* @param agentid
* 应用ID
* @return 菜单集合
* @throws WeixinException
* @see <a href=
* "http://qydev.weixin.qq.com/wiki/index.php?title=%E8%8E%B7%E5%8F%96%E8%8F%9C%E5%8D%95%E5%88%97%E8%A1%A8">
* 查询菜单</a>
* @see com.foxinmy.weixin4j.model.Button
*/
public List<Button> getMenu(int agentid) throws WeixinException {
String menu_get_uri = getRequestUri("menu_get_uri");
Token token = tokenManager.getCache();
WeixinResponse response = weixinExecutor.get(String.format(menu_get_uri, token.getAccessToken(), agentid));
JSONArray buttons = response.getAsJson().getJSONArray("button");
List<Button> buttonList = new ArrayList<Button>(buttons.size());
ParseProcess processor = new ExtraProcessor() {
@Override
public void processExtra(Object object, String key, Object value) {
((Button) object).setContent(String.valueOf(value));
}
};
for (int i = 0; i < buttons.size(); i++) {
buttonList.add(JSON.parseObject(buttons.getString(i), Button.class, processor));
}
return buttonList;
}
/**
* 删除菜单(管理员须拥有应用的管理权限 并且应用必须设置在回调模式)
*
* @param agentid
* 应用ID
* @throws WeixinException
* @see <a href=
* "http://qydev.weixin.qq.com/wiki/index.php?title=%E5%88%A0%E9%99%A4%E8%8F%9C%E5%8D%95">
* 删除菜单</a>
* @return 处理结果
*/
public ApiResult deleteMenu(int agentid) throws WeixinException {
String menu_delete_uri = getRequestUri("menu_delete_uri");
Token token = tokenManager.getCache();
WeixinResponse response = weixinExecutor.get(String.format(menu_delete_uri, token.getAccessToken(), agentid));
/**
* 删除菜单(管理员须拥有应用的管理权限 并且应用必须设置在回调模式)
*
* @param agentid
* 应用ID
* @throws WeixinException
* @see <a href=
* "http://qydev.weixin.qq.com/wiki/index.php?title=%E5%88%A0%E9%99%A4%E8%8F%9C%E5%8D%95">
* 删除菜单</a>
* @return 处理结果
*/
public ApiResult deleteMenu(int agentid) throws WeixinException {
String menu_delete_uri = getRequestUri("menu_delete_uri");
Token token = tokenManager.getCache();
WeixinResponse response = weixinExecutor.get(String.format(menu_delete_uri, token.getAccessToken(), agentid));
return response.getAsResult();
}
return response.getAsResult();
}
}