Search in sources :

Example 1 with Menu

use of org.usc.wechat.mp.sdk.vo.menu.Menu in project wechat-mp-sdk by usc.

the class MenuUtilTest method main.

public static void main(String[] args) throws URISyntaxException {
    List<SingleMenuInfo> subMenuInfos = new ArrayList<SingleMenuInfo>();
    subMenuInfos.add(MenuType.VIEW.buildSingleMenuInfo("搜索", "http://www.soso.com/"));
    subMenuInfos.add(MenuType.VIEW.buildSingleMenuInfo("视频", "http://v.qq.com/"));
    subMenuInfos.add(MenuType.CLICK.buildSingleMenuInfo("赞一下我们", "V1001_GOOD"));
    List<MenuInfo> menuInfos = new ArrayList<MenuInfo>();
    menuInfos.add(MenuType.CLICK.buildSingleMenuInfo("今日歌曲", "V1001_TODAY_MUSIC"));
    menuInfos.add(MenuType.CLICK.buildSingleMenuInfo("歌手简介", "V1001_TODAY_SINGER"));
    menuInfos.add(new MultiMenuInfo("菜单", subMenuInfos));
    Menu menu = new Menu(menuInfos);
    System.out.println(MenuUtil.createMenu(Constants.LICENSE, menu));
    System.out.println(MenuUtil.getMenu(Constants.LICENSE));
    System.out.println(MenuUtil.deleteMenu(Constants.LICENSE));
}
Also used : MultiMenuInfo(org.usc.wechat.mp.sdk.vo.menu.MultiMenuInfo) SingleMenuInfo(org.usc.wechat.mp.sdk.vo.menu.SingleMenuInfo) MultiMenuInfo(org.usc.wechat.mp.sdk.vo.menu.MultiMenuInfo) MenuInfo(org.usc.wechat.mp.sdk.vo.menu.MenuInfo) SingleMenuInfo(org.usc.wechat.mp.sdk.vo.menu.SingleMenuInfo) ArrayList(java.util.ArrayList) Menu(org.usc.wechat.mp.sdk.vo.menu.Menu)

Example 2 with Menu

use of org.usc.wechat.mp.sdk.vo.menu.Menu in project wechat-mp-sdk by usc.

the class MenuUtil method getMenu.

public static Menu getMenu(License license) {
    String accessToken = AccessTokenUtil.getAccessToken(license);
    String url = WechatRequest.GET_MENU.getUrl();
    try {
        URI uri = new URIBuilder(url).setParameter("access_token", accessToken).build();
        String json = Request.Get(uri).connectTimeout(HttpUtil.CONNECT_TIMEOUT).socketTimeout(HttpUtil.SOCKET_TIMEOUT).execute().handleResponse(HttpUtil.UTF8_CONTENT_HANDLER);
        Menu menu = buildMenu(json);
        log.info("get menu:\n url={},\n rtn={},{}", uri, json, menu);
        return menu;
    } catch (Exception e) {
        String msg = "get menu failed: url=" + url + "?access_token=" + accessToken;
        log.error(msg, e);
        return null;
    }
}
Also used : Menu(org.usc.wechat.mp.sdk.vo.menu.Menu) URI(java.net.URI) URIBuilder(org.apache.http.client.utils.URIBuilder)

Example 3 with Menu

use of org.usc.wechat.mp.sdk.vo.menu.Menu in project wechat-mp-sdk by usc.

the class MenuUtil method buildMenu.

private static Menu buildMenu(String json) {
    JSONObject parseObject = JSONObject.parseObject(json);
    if (json == null) {
        return null;
    }
    JSONObject menuObject = parseObject.getJSONObject("menu");
    if (menuObject == null) {
        return null;
    }
    JSONArray jsonArray = menuObject.getJSONArray("button");
    if (jsonArray == null) {
        return null;
    }
    List<MenuInfo> menuInfos = new ArrayList<MenuInfo>();
    for (int i = 0; i < jsonArray.size(); i++) {
        JSONObject subJsonObject = jsonArray.getJSONObject(i);
        String type = subJsonObject.getString("type");
        String jsonString = subJsonObject.toString();
        if (StringUtils.isNotEmpty(type)) {
            menuInfos.add(JSONObject.parseObject(jsonString, SingleMenuInfo.class));
        } else {
            menuInfos.add(JSONObject.parseObject(jsonString, MultiMenuInfo.class));
        }
    }
    return new Menu(menuInfos);
}
Also used : MultiMenuInfo(org.usc.wechat.mp.sdk.vo.menu.MultiMenuInfo) JSONObject(com.alibaba.fastjson.JSONObject) SingleMenuInfo(org.usc.wechat.mp.sdk.vo.menu.SingleMenuInfo) MultiMenuInfo(org.usc.wechat.mp.sdk.vo.menu.MultiMenuInfo) MenuInfo(org.usc.wechat.mp.sdk.vo.menu.MenuInfo) SingleMenuInfo(org.usc.wechat.mp.sdk.vo.menu.SingleMenuInfo) JSONArray(com.alibaba.fastjson.JSONArray) ArrayList(java.util.ArrayList) Menu(org.usc.wechat.mp.sdk.vo.menu.Menu)

Aggregations

Menu (org.usc.wechat.mp.sdk.vo.menu.Menu)3 ArrayList (java.util.ArrayList)2 MenuInfo (org.usc.wechat.mp.sdk.vo.menu.MenuInfo)2 MultiMenuInfo (org.usc.wechat.mp.sdk.vo.menu.MultiMenuInfo)2 SingleMenuInfo (org.usc.wechat.mp.sdk.vo.menu.SingleMenuInfo)2 JSONArray (com.alibaba.fastjson.JSONArray)1 JSONObject (com.alibaba.fastjson.JSONObject)1 URI (java.net.URI)1 URIBuilder (org.apache.http.client.utils.URIBuilder)1