This commit is contained in:
jinyu 2016-03-23 17:02:33 +08:00
parent 0fb11904ec
commit 4ef35151de
3 changed files with 67 additions and 49 deletions

View File

@ -35,6 +35,7 @@ import com.foxinmy.weixin4j.mp.model.Group;
import com.foxinmy.weixin4j.mp.model.KfAccount;
import com.foxinmy.weixin4j.mp.model.KfSession;
import com.foxinmy.weixin4j.mp.model.Menu;
import com.foxinmy.weixin4j.mp.model.MenuMatchRule;
import com.foxinmy.weixin4j.mp.model.MenuSetting;
import com.foxinmy.weixin4j.mp.model.QRParameter;
import com.foxinmy.weixin4j.mp.model.QRResult;
@ -1226,11 +1227,31 @@ public class WeixinProxy {
* "http://mp.weixin.qq.com/wiki/16/8ed41ba931e4845844ad6d1eeb8060c8.html">
* 删除菜单</a>
* @see com.foxinmy.weixin4j.mp.api.MenuApi
* @return 处理结果
*/
public JsonResult deleteMenu() throws WeixinException {
return menuApi.deleteMenu();
}
/**
* 创建个性化菜单
*
* @param buttons
* 菜单列表
* @param matchRule
* 匹配规则 至少要有一个匹配信息是不为空
* @return 菜单ID
* @throws WeixinException
* @see <a href=
* "http://mp.weixin.qq.com/wiki/0/c48ccd12b69ae023159b4bfaa7c39c20.html#.E5.88.9B.E5.BB.BA.E4.B8.AA.E6.80.A7.E5.8C.96.E8.8F.9C.E5.8D.95">
* 创建个性化菜单</a>
* @see com.foxinmy.weixin4j.mp.api.MenuApi
* @see com.foxinmy.weixin4j.model.Button
*/
public String createCustomMenu(List<Button> buttons, MenuMatchRule matchRule) throws WeixinException {
return menuApi.createCustomMenu(buttons, matchRule);
}
/**
* 删除个性化菜单
*
@ -1250,13 +1271,13 @@ public class WeixinProxy {
*
* @param userId
* 可以是粉丝的OpenID也可以是粉丝的微信号
* @return 匹配到的菜单配置
* @see <a href=
* "http://mp.weixin.qq.com/wiki/0/c48ccd12b69ae023159b4bfaa7c39c20.html#.E6.B5.8B.E8.AF.95.E4.B8.AA.E6.80.A7.E5.8C.96.E8.8F.9C.E5.8D.95.E5.8C.B9.E9.85.8D.E7.BB.93.E6.9E.9C">
* 测试个性化菜单</a>
* @see com.foxinmy.weixin4j.model.Button
* @see com.foxinmy.weixin4j.mp.api.MenuApi
* @throws WeixinException
* @return 匹配到的菜单配置
*/
public List<Button> matchCustomMenu(String userId) throws WeixinException {
return menuApi.matchCustomMenu(userId);

View File

@ -44,32 +44,30 @@ public class MenuApi extends MpApi {
* @param buttons
* 菜单列表
* @throws WeixinException
* @see <a
* href="http://mp.weixin.qq.com/wiki/13/43de8269be54a0a6f64413e4dfa94f39.html">创建自定义菜单</a>
* @see <a href=
* "http://mp.weixin.qq.com/wiki/13/43de8269be54a0a6f64413e4dfa94f39.html">
* 创建自定义菜单</a>
* @see com.foxinmy.weixin4j.model.Button
* @return 处理结果
*/
public JsonResult createMenu(List<Button> buttons) throws WeixinException {
String menu_create_uri = getRequestUri("menu_create_uri");
JSONObject obj = new JSONObject();
obj.put("button", buttons);
return createMenu0(menu_create_uri, obj);
return createMenu0(menu_create_uri, obj).getAsJsonResult();
}
private JsonResult createMenu0(String url, JSONObject data)
throws WeixinException {
WeixinResponse response = weixinExecutor.post(
String.format(url, tokenHolder.getAccessToken()),
private WeixinResponse createMenu0(String url, JSONObject data) throws WeixinException {
return weixinExecutor.post(String.format(url, tokenHolder.getAccessToken()),
JSON.toJSONString(data, new NameFilter() {
@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")) {
ButtonType buttonType = ((Button) object).getType();
if (buttonType != null) {
if (ButtonType.view == buttonType) {
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";
} else {
return "key";
@ -80,17 +78,17 @@ public class MenuApi extends MpApi {
}
}));
return response.getAsJsonResult();
}
/**
* 查询菜单
*
* @return 菜单集合
* @throws WeixinException
* @see <a
* href="http://mp.weixin.qq.com/wiki/16/ff9b7b85220e1396ffa16794a9d95adc.html">查询菜单</a>
* @see <a href=
* "http://mp.weixin.qq.com/wiki/16/ff9b7b85220e1396ffa16794a9d95adc.html">
* 查询菜单</a>
* @see com.foxinmy.weixin4j.model.Button
* @return 菜单集合
*/
public List<Button> getMenu() throws WeixinException {
return buttonsConvertor(getMenu0().getJSONObject("menu"));
@ -99,38 +97,37 @@ public class MenuApi extends MpApi {
private JSONObject getMenu0() throws WeixinException {
String menu_get_uri = getRequestUri("menu_get_uri");
Token token = tokenHolder.getToken();
WeixinResponse response = weixinExecutor.get(String.format(
menu_get_uri, token.getAccessToken()));
WeixinResponse response = weixinExecutor.get(String.format(menu_get_uri, token.getAccessToken()));
return response.getAsJson();
}
/**
* 查询全部菜单(包含个性化菜单)
*
* @return 菜单集合
* @throws WeixinException
* @see <a
* href="http://mp.weixin.qq.com/wiki/16/ff9b7b85220e1396ffa16794a9d95adc.html">查询菜单</a>
* @see <a
* href="http://mp.weixin.qq.com/wiki/0/c48ccd12b69ae023159b4bfaa7c39c20.html">个性化菜单</a>
* @see <a href=
* "http://mp.weixin.qq.com/wiki/16/ff9b7b85220e1396ffa16794a9d95adc.html">
* 查询菜单</a>
* @see <a href=
* "http://mp.weixin.qq.com/wiki/0/c48ccd12b69ae023159b4bfaa7c39c20.html">
* 个性化菜单</a>
* @see com.foxinmy.weixin4j.model.Button
* @see com.foxinmy.weixin4j.mp.model.Menu
* @return 菜单集合
*/
public List<Menu> getAllMenu() throws WeixinException {
JSONObject response = getMenu0();
List<Menu> menus = new ArrayList<Menu>();
// 普通菜单
JSONObject menuObj = response.getJSONObject("menu");
menus.add(new Menu(menuObj.getString("menuid"),
buttonsConvertor(menuObj), null));
menus.add(new Menu(menuObj.getString("menuid"), buttonsConvertor(menuObj), null));
// 个性化菜单
JSONArray menuObjs = response.getJSONArray("conditionalmenu");
if (menuObjs != null && !menuObjs.isEmpty()) {
for (int i = 0; i < menuObjs.size(); i++) {
menuObj = menuObjs.getJSONObject(i);
menus.add(new Menu(menuObj.getString("menuid"),
buttonsConvertor(menuObj), menuObj.getObject(
"matchrule", MenuMatchRule.class)));
menus.add(new Menu(menuObj.getString("menuid"), buttonsConvertor(menuObj),
menuObj.getObject("matchrule", MenuMatchRule.class)));
}
}
return menus;
@ -139,16 +136,17 @@ public class MenuApi extends MpApi {
/**
* 删除菜单
*
* @return 处理结果
* @throws WeixinException
* @see <a
* href="http://mp.weixin.qq.com/wiki/16/8ed41ba931e4845844ad6d1eeb8060c8.html">删除菜单</a>
* @see <a href=
* "http://mp.weixin.qq.com/wiki/16/8ed41ba931e4845844ad6d1eeb8060c8.html">
* 删除菜单</a>
* @return 处理结果
*/
public JsonResult deleteMenu() throws WeixinException {
String menu_delete_uri = getRequestUri("menu_delete_uri");
Token token = tokenHolder.getToken();
WeixinResponse response = weixinExecutor.get(String.format(
menu_delete_uri, token.getAccessToken()));
WeixinResponse response = weixinExecutor.get(String.format(menu_delete_uri, token.getAccessToken()));
return response.getAsJsonResult();
}
@ -161,25 +159,27 @@ public class MenuApi extends MpApi {
* @param matchRule
* 匹配规则 至少要有一个匹配信息是不为空
* @throws WeixinException
* @see <a
* href="http://mp.weixin.qq.com/wiki/0/c48ccd12b69ae023159b4bfaa7c39c20.html#.E5.88.9B.E5.BB.BA.E4.B8.AA.E6.80.A7.E5.8C.96.E8.8F.9C.E5.8D.95">创建个性化菜单</a>
* @see <a href=
* "http://mp.weixin.qq.com/wiki/0/c48ccd12b69ae023159b4bfaa7c39c20.html#.E5.88.9B.E5.BB.BA.E4.B8.AA.E6.80.A7.E5.8C.96.E8.8F.9C.E5.8D.95">
* 创建个性化菜单</a>
* @see com.foxinmy.weixin4j.model.Button
* @return 菜单ID
*/
public JsonResult createCustomMenu(List<Button> buttons,
MenuMatchRule matchRule) throws WeixinException {
public String createCustomMenu(List<Button> buttons, MenuMatchRule matchRule) throws WeixinException {
String menu_create_uri = getRequestUri("menu_custom_create_uri");
JSONObject obj = new JSONObject();
obj.put("button", buttons);
obj.put("matchrule", matchRule.getRule());
return createMenu0(menu_create_uri, obj);
return createMenu0(menu_create_uri, obj).getAsJson().getString("menuid");
}
/**
* 删除个性化菜单
*
* @throws WeixinException
* @see <a
* href="http://mp.weixin.qq.com/wiki/0/c48ccd12b69ae023159b4bfaa7c39c20.html#.E5.88.A0.E9.99.A4.E4.B8.AA.E6.80.A7.E5.8C.96.E8.8F.9C.E5.8D.95">删除个性化菜单</a>
* @see <a href=
* "http://mp.weixin.qq.com/wiki/0/c48ccd12b69ae023159b4bfaa7c39c20.html#.E5.88.A0.E9.99.A4.E4.B8.AA.E6.80.A7.E5.8C.96.E8.8F.9C.E5.8D.95">
* 删除个性化菜单</a>
* @return 处理结果
*/
public JsonResult deleteCustomMenu(String menuId) throws WeixinException {
@ -187,8 +187,7 @@ public class MenuApi extends MpApi {
Token token = tokenHolder.getToken();
JSONObject obj = new JSONObject();
obj.put("menuid", menuId);
WeixinResponse response = weixinExecutor.post(
String.format(menu_delete_uri, token.getAccessToken()),
WeixinResponse response = weixinExecutor.post(String.format(menu_delete_uri, token.getAccessToken()),
obj.toJSONString());
return response.getAsJsonResult();
@ -200,8 +199,9 @@ public class MenuApi extends MpApi {
* @param userId
* 可以是粉丝的OpenID也可以是粉丝的微信号
* @return 匹配到的菜单配置
* @see <a
* href="http://mp.weixin.qq.com/wiki/0/c48ccd12b69ae023159b4bfaa7c39c20.html#.E6.B5.8B.E8.AF.95.E4.B8.AA.E6.80.A7.E5.8C.96.E8.8F.9C.E5.8D.95.E5.8C.B9.E9.85.8D.E7.BB.93.E6.9E.9C">测试个性化菜单</a>
* @see <a href=
* "http://mp.weixin.qq.com/wiki/0/c48ccd12b69ae023159b4bfaa7c39c20.html#.E6.B5.8B.E8.AF.95.E4.B8.AA.E6.80.A7.E5.8C.96.E8.8F.9C.E5.8D.95.E5.8C.B9.E9.85.8D.E7.BB.93.E6.9E.9C">
* 测试个性化菜单</a>
* @see com.foxinmy.weixin4j.model.Button
* @throws WeixinException
*/
@ -210,8 +210,7 @@ public class MenuApi extends MpApi {
Token token = tokenHolder.getToken();
JSONObject obj = new JSONObject();
obj.put("user_id", userId);
WeixinResponse response = weixinExecutor.post(
String.format(menu_trymatch_uri, token.getAccessToken()),
WeixinResponse response = weixinExecutor.post(String.format(menu_trymatch_uri, token.getAccessToken()),
obj.toJSONString());
return buttonsConvertor(response.getAsJson().getJSONObject("menu"));
@ -228,8 +227,7 @@ public class MenuApi extends MpApi {
JSONArray buttons = menu.getJSONArray("button");
List<Button> buttonList = new ArrayList<Button>(buttons.size());
for (int i = 0; i < buttons.size(); i++) {
buttonList.add(JSON.parseObject(buttons.getString(i), Button.class,
buttonProcess));
buttonList.add(JSON.parseObject(buttons.getString(i), Button.class, buttonProcess));
}
return buttonList;
}

View File

@ -102,8 +102,7 @@ public class MenuTest extends TokenTest {
buttons.add(new Button("only for iphone", "iphone", ButtonType.click));
MenuMatchRule matchRule = new MenuMatchRule();
matchRule.platform(ClientPlatformType.IOS);
JsonResult result = menuApi.createCustomMenu(buttons, matchRule);
Assert.assertEquals(0, result.getCode());
Assert.assertNotNull(menuApi.createCustomMenu(buttons, matchRule));
}
@Test