mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-10-26 02:39:18 +08:00
菜单接口的支持
This commit is contained in:
@@ -110,7 +110,7 @@ public interface WxCpService {
|
||||
* @param menu
|
||||
* @throws WxErrorException
|
||||
*/
|
||||
public void menuCreate(WxMenu menu) throws WxErrorException;
|
||||
public void menuCreate(WxCpMenu menu) throws WxErrorException;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
@@ -129,7 +129,7 @@ public interface WxCpService {
|
||||
* @return
|
||||
* @throws WxErrorException
|
||||
*/
|
||||
public WxMenu menuGet() throws WxErrorException;
|
||||
public WxCpMenu menuGet() throws WxErrorException;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
|
||||
@@ -106,7 +106,7 @@ public class WxCpServiceImpl implements WxCpService {
|
||||
execute(new SimplePostRequestExecutor(), url, message.toJson());
|
||||
}
|
||||
|
||||
public void menuCreate(WxMenu menu) throws WxErrorException {
|
||||
public void menuCreate(WxCpMenu menu) throws WxErrorException {
|
||||
String url = "https://api.weixin.qq.com/cgi-bin/menu/create";
|
||||
execute(new SimplePostRequestExecutor(), url, menu.toJson());
|
||||
}
|
||||
@@ -116,11 +116,11 @@ public class WxCpServiceImpl implements WxCpService {
|
||||
execute(new SimpleGetRequestExecutor(), url, null);
|
||||
}
|
||||
|
||||
public WxMenu menuGet() throws WxErrorException {
|
||||
public WxCpMenu menuGet() throws WxErrorException {
|
||||
String url = "https://api.weixin.qq.com/cgi-bin/menu/get";
|
||||
try {
|
||||
String resultContent = execute(new SimpleGetRequestExecutor(), url, null);
|
||||
return WxMenu.fromJson(resultContent);
|
||||
return WxCpMenu.fromJson(resultContent);
|
||||
} catch (WxErrorException e) {
|
||||
// 46003 不存在的菜单数据
|
||||
if (e.getError().getErrorCode() == 46003) {
|
||||
|
||||
@@ -12,7 +12,7 @@ import me.chanjar.weixin.enterprise.util.json.WxCpGsonBuilder;
|
||||
* @author Daniel Qian
|
||||
*
|
||||
*/
|
||||
public class WxMenu {
|
||||
public class WxCpMenu {
|
||||
|
||||
private List<WxMenuButton> buttons = new ArrayList<WxMenuButton>();
|
||||
|
||||
@@ -28,12 +28,12 @@ public class WxMenu {
|
||||
return WxCpGsonBuilder.create().toJson(this);
|
||||
}
|
||||
|
||||
public static WxMenu fromJson(String json) {
|
||||
return WxCpGsonBuilder.create().fromJson(json, WxMenu.class);
|
||||
public static WxCpMenu fromJson(String json) {
|
||||
return WxCpGsonBuilder.create().fromJson(json, WxCpMenu.class);
|
||||
}
|
||||
|
||||
public static WxMenu fromJson(InputStream is) {
|
||||
return WxCpGsonBuilder.create().fromJson(new InputStreamReader(is), WxMenu.class);
|
||||
public static WxCpMenu fromJson(InputStream is) {
|
||||
return WxCpGsonBuilder.create().fromJson(new InputStreamReader(is), WxCpMenu.class);
|
||||
}
|
||||
|
||||
public static class WxMenuButton {
|
||||
@@ -13,7 +13,7 @@ public class WxCpGsonBuilder {
|
||||
static {
|
||||
INSTANCE.disableHtmlEscaping();
|
||||
INSTANCE.registerTypeAdapter(WxCpMessage.class, new WxCpMessageGsonAdapter());
|
||||
INSTANCE.registerTypeAdapter(WxMenu.class, new WxCpMenuGsonAdapter());
|
||||
INSTANCE.registerTypeAdapter(WxCpMenu.class, new WxCpMenuGsonAdapter());
|
||||
INSTANCE.registerTypeAdapter(WxCpDepart.class, new WxCpDepartGsonAdapter());
|
||||
INSTANCE.registerTypeAdapter(WxUser.class, new WxCpUserGsonAdapter());
|
||||
INSTANCE.registerTypeAdapter(WxUserList.class, new WxCpUserListGsonAdapter());
|
||||
|
||||
@@ -11,7 +11,7 @@ package me.chanjar.weixin.enterprise.util.json;
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
import me.chanjar.weixin.common.GsonHelper;
|
||||
import me.chanjar.weixin.enterprise.bean.WxMenu;
|
||||
import me.chanjar.weixin.enterprise.bean.WxCpMenu;
|
||||
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonDeserializationContext;
|
||||
@@ -27,13 +27,13 @@ import com.google.gson.JsonSerializer;
|
||||
* @author Daniel Qian
|
||||
*
|
||||
*/
|
||||
public class WxCpMenuGsonAdapter implements JsonSerializer<WxMenu>, JsonDeserializer<WxMenu> {
|
||||
public class WxCpMenuGsonAdapter implements JsonSerializer<WxCpMenu>, JsonDeserializer<WxCpMenu> {
|
||||
|
||||
public JsonElement serialize(WxMenu menu, Type typeOfSrc, JsonSerializationContext context) {
|
||||
public JsonElement serialize(WxCpMenu menu, Type typeOfSrc, JsonSerializationContext context) {
|
||||
JsonObject json = new JsonObject();
|
||||
|
||||
JsonArray buttonArray = new JsonArray();
|
||||
for (WxMenu.WxMenuButton button : menu.getButtons()) {
|
||||
for (WxCpMenu.WxMenuButton button : menu.getButtons()) {
|
||||
JsonObject buttonJson = convertToJson(button);
|
||||
buttonArray.add(buttonJson);
|
||||
}
|
||||
@@ -42,7 +42,7 @@ public class WxCpMenuGsonAdapter implements JsonSerializer<WxMenu>, JsonDeserial
|
||||
return json;
|
||||
}
|
||||
|
||||
protected JsonObject convertToJson(WxMenu.WxMenuButton button) {
|
||||
protected JsonObject convertToJson(WxCpMenu.WxMenuButton button) {
|
||||
JsonObject buttonJson = new JsonObject();
|
||||
buttonJson.addProperty("type", button.getType());
|
||||
buttonJson.addProperty("name", button.getName());
|
||||
@@ -50,7 +50,7 @@ public class WxCpMenuGsonAdapter implements JsonSerializer<WxMenu>, JsonDeserial
|
||||
buttonJson.addProperty("url", button.getUrl());
|
||||
if (button.getSubButtons() != null && button.getSubButtons().size() > 0) {
|
||||
JsonArray buttonArray = new JsonArray();
|
||||
for (WxMenu.WxMenuButton sub_button : button.getSubButtons()) {
|
||||
for (WxCpMenu.WxMenuButton sub_button : button.getSubButtons()) {
|
||||
buttonArray.add(convertToJson(sub_button));
|
||||
}
|
||||
buttonJson.add("sub_button", buttonArray);
|
||||
@@ -58,18 +58,17 @@ public class WxCpMenuGsonAdapter implements JsonSerializer<WxMenu>, JsonDeserial
|
||||
return buttonJson;
|
||||
}
|
||||
|
||||
public WxMenu deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
|
||||
public WxCpMenu deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
|
||||
/*
|
||||
* 操蛋的微信
|
||||
* 创建菜单时是 { button : ... }
|
||||
* 查询菜单时是 { menu : { button : ... } }
|
||||
*/
|
||||
WxMenu menu = new WxMenu();
|
||||
JsonObject menuJson = json.getAsJsonObject().get("menu").getAsJsonObject();
|
||||
JsonArray buttonsJson = menuJson.get("button").getAsJsonArray();
|
||||
WxCpMenu menu = new WxCpMenu();
|
||||
JsonArray buttonsJson = json.getAsJsonObject().get("button").getAsJsonArray();
|
||||
for (int i = 0; i < buttonsJson.size(); i++) {
|
||||
JsonObject buttonJson = buttonsJson.get(i).getAsJsonObject();
|
||||
WxMenu.WxMenuButton button = convertFromJson(buttonJson);
|
||||
WxCpMenu.WxMenuButton button = convertFromJson(buttonJson);
|
||||
menu.getButtons().add(button);
|
||||
if (buttonJson.get("sub_button") == null || buttonJson.get("sub_button").isJsonNull()) {
|
||||
continue;
|
||||
@@ -83,8 +82,8 @@ public class WxCpMenuGsonAdapter implements JsonSerializer<WxMenu>, JsonDeserial
|
||||
return menu;
|
||||
}
|
||||
|
||||
protected WxMenu.WxMenuButton convertFromJson(JsonObject json) {
|
||||
WxMenu.WxMenuButton button = new WxMenu.WxMenuButton();
|
||||
protected WxCpMenu.WxMenuButton convertFromJson(JsonObject json) {
|
||||
WxCpMenu.WxMenuButton button = new WxCpMenu.WxMenuButton();
|
||||
button.setName(GsonHelper.getString(json, "name"));
|
||||
button.setKey(GsonHelper.getString(json, "key"));
|
||||
button.setUrl(GsonHelper.getString(json, "url"));
|
||||
|
||||
@@ -2,6 +2,7 @@ package me.chanjar.weixin.enterprise.api;
|
||||
|
||||
import javax.xml.bind.JAXBException;
|
||||
|
||||
import me.chanjar.weixin.enterprise.bean.WxCpMenu;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.DataProvider;
|
||||
import org.testng.annotations.Guice;
|
||||
@@ -9,8 +10,7 @@ import org.testng.annotations.Test;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
|
||||
import me.chanjar.weixin.enterprise.bean.WxMenu;
|
||||
import me.chanjar.weixin.enterprise.bean.WxMenu.WxMenuButton;
|
||||
import me.chanjar.weixin.enterprise.bean.WxCpMenu.WxMenuButton;
|
||||
import me.chanjar.weixin.enterprise.exception.WxErrorException;
|
||||
|
||||
/**
|
||||
@@ -20,14 +20,14 @@ import me.chanjar.weixin.enterprise.exception.WxErrorException;
|
||||
*/
|
||||
@Test(groups="menuAPI", dependsOnGroups="baseAPI")
|
||||
@Guice(modules = ApiTestModule.class)
|
||||
public class WxMenuAPITest {
|
||||
public class WxCpMenuAPITest {
|
||||
|
||||
@Inject
|
||||
protected WxCpServiceImpl wxService;
|
||||
|
||||
@Test(dataProvider = "menu")
|
||||
public void testCreateMenu(WxMenu wxMenu) throws WxErrorException {
|
||||
wxService.menuCreate(wxMenu);
|
||||
public void testCreateMenu(WxCpMenu wxCpMenu) throws WxErrorException {
|
||||
wxService.menuCreate(wxCpMenu);
|
||||
}
|
||||
|
||||
@Test(dependsOnMethods = { "testCreateMenu"})
|
||||
@@ -42,7 +42,7 @@ public class WxMenuAPITest {
|
||||
|
||||
@DataProvider(name="menu")
|
||||
public Object[][] getMenu() throws JAXBException {
|
||||
WxMenu menu = new WxMenu();
|
||||
WxCpMenu menu = new WxCpMenu();
|
||||
WxMenuButton button1 = new WxMenuButton();
|
||||
button1.setType(WxCpConsts.BUTTON_CLICK);
|
||||
button1.setName("今日歌曲");
|
||||
@@ -1,24 +1,23 @@
|
||||
package me.chanjar.weixin.enterprise.bean;
|
||||
|
||||
import me.chanjar.weixin.enterprise.bean.WxMenu;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.DataProvider;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import me.chanjar.weixin.enterprise.bean.WxMenu.WxMenuButton;
|
||||
import me.chanjar.weixin.enterprise.bean.WxCpMenu.WxMenuButton;
|
||||
|
||||
@Test
|
||||
public class WxMenuTest {
|
||||
public class WxCpMenuTest {
|
||||
|
||||
@Test(dataProvider="wxReturnMenu")
|
||||
public void testFromJson(String json) {
|
||||
WxMenu menu = WxMenu.fromJson(json);
|
||||
WxCpMenu menu = WxCpMenu.fromJson(json);
|
||||
Assert.assertEquals(menu.getButtons().size(), 3);
|
||||
}
|
||||
|
||||
@Test(dataProvider="wxPushMenu")
|
||||
public void testToJson(String json) {
|
||||
WxMenu menu = new WxMenu();
|
||||
WxCpMenu menu = new WxCpMenu();
|
||||
WxMenuButton button1 = new WxMenuButton();
|
||||
button1.setType("click");
|
||||
button1.setName("今日歌曲");
|
||||
Reference in New Issue
Block a user