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

View File

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