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.KfAccount;
import com.foxinmy.weixin4j.mp.model.KfSession; import com.foxinmy.weixin4j.mp.model.KfSession;
import com.foxinmy.weixin4j.mp.model.Menu; 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.MenuSetting;
import com.foxinmy.weixin4j.mp.model.QRParameter; import com.foxinmy.weixin4j.mp.model.QRParameter;
import com.foxinmy.weixin4j.mp.model.QRResult; import com.foxinmy.weixin4j.mp.model.QRResult;
@ -1226,11 +1227,31 @@ public class WeixinProxy {
* "http://mp.weixin.qq.com/wiki/16/8ed41ba931e4845844ad6d1eeb8060c8.html"> * "http://mp.weixin.qq.com/wiki/16/8ed41ba931e4845844ad6d1eeb8060c8.html">
* 删除菜单</a> * 删除菜单</a>
* @see com.foxinmy.weixin4j.mp.api.MenuApi * @see com.foxinmy.weixin4j.mp.api.MenuApi
* @return 处理结果
*/ */
public JsonResult deleteMenu() throws WeixinException { public JsonResult deleteMenu() throws WeixinException {
return menuApi.deleteMenu(); 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 * @param userId
* 可以是粉丝的OpenID也可以是粉丝的微信号 * 可以是粉丝的OpenID也可以是粉丝的微信号
* @return 匹配到的菜单配置
* @see <a href= * @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"> * "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> * 测试个性化菜单</a>
* @see com.foxinmy.weixin4j.model.Button * @see com.foxinmy.weixin4j.model.Button
* @see com.foxinmy.weixin4j.mp.api.MenuApi * @see com.foxinmy.weixin4j.mp.api.MenuApi
* @throws WeixinException * @throws WeixinException
* @return 匹配到的菜单配置
*/ */
public List<Button> matchCustomMenu(String userId) throws WeixinException { public List<Button> matchCustomMenu(String userId) throws WeixinException {
return menuApi.matchCustomMenu(userId); return menuApi.matchCustomMenu(userId);

View File

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

View File

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