微信创建菜单
package com.wanhua.weixin.model;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.json.JSONObject;
import org.junit.Test;
import com.alibaba.fastjson.JSON;
import com.google.gson.JsonArray;
import com.sun.corba.se.impl.oa.poa.AOMEntry;
import com.wanhua.weixin.util.WXConst;
import com.wanhua.weixin.util.WXHttpUtil;
import com.wanhua.weixin.util.WXMsgManager;
/**
*
* weixin菜单
*
* @author w_xfpenga
*
* 2014-11-27
*
*/
public class Menu {
// 菜单标题,不超过16个字节,子菜单不超过40个字节
public String name;
// 菜单的响应动作类型
public String type;
// type非view时作为button的唯一标识
public String key;
// type为view时用上
public String url;
// 只能有二级菜单,不为空时只需要有name,而type/key/url都为空
public List<Menu> sub_button;
/**
* 创建Menu
*
* @Title: createMenu
* @Description: 创建Menu
* @param @return
* @param @throws IOException 设定文件
* @return string 返回类型
* @throws
*/
public static String createMenu() {
String returnMsg = "";
if (queryMenu().contains("errcode")) {
returnMsg = inintMenu();
}
return returnMsg;
}
/**
* 菜单创建的初始化
*
* @author w_xfpenga
*
*
* 2014-12-27
*/
public static String inintMenu() {
// 菜单一:待办列表
Menu DoingMenu = new Menu();
DoingMenu.name = "待办Todo";
DoingMenu.type = "click";
DoingMenu.key="01_DOING";
// DoingMenu.url = "http://10.10.103.206:9080/WHMobile/apps/services/www/POCMobileOA/mobilewebapp/default/index.html";
// DoingMenu.url ="http://10.13.18.152:10080/WHMobile/apps/services/preview/POCMobileOA/common/0/default/index.html";
// 菜单二:已办列表
Menu doneMenu = new Menu();
doneMenu.name = "已办Done";
doneMenu.type = "view";
doneMenu.url = "http://3a011670.ngrok.com/WHMobile/apps/services/preview/POCMobileOA/common/0/default/index.html";
// 菜单三:更多
Menu moreMenu = new Menu();
moreMenu.name = "更多More";
Menu loginMenu = new Menu();
loginMenu.name = "身份验证Login";
loginMenu.type = "click";
loginMenu.key = "01_LOGIN";
Menu logoutMenu = new Menu();
logoutMenu.name = "身份解绑Logout";
logoutMenu.type = "click";
logoutMenu.key = "02_LOGOUT";
Menu settingMenu = new Menu();
settingMenu.name = "设置Setting";
settingMenu.type = "click";
settingMenu.key = "03_SETTING";
Menu helperMenu = new Menu();
helperMenu.name = "帮助Help";
helperMenu.type = "click";
helperMenu.key = "04_HELPER";
List<Menu> moreList = new ArrayList<Menu>();
moreList.add(loginMenu);
moreList.add(logoutMenu);
moreList.add(settingMenu);
moreList.add(helperMenu);
moreMenu.sub_button = moreList;
List<Menu> menuList = new ArrayList<Menu>();
menuList.add(DoingMenu);
menuList.add(doneMenu);
menuList.add(moreMenu);
Map menuMap = new HashMap<String, Object>();
menuMap.put("button", menuList);
String menu = JSON.toJSONString(menuMap);
String action = WXConst.CREATE_MENU;
try {
WXHttpUtil.MsgHttpsRequest(action, "POST", menu);
return "createMenu 返回信息成功!!!";
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
return "createMenu 失败!!!";
}
}
/**
* 删除当前Menu
*
* @Title: deleteMenu
* @Description: 删除当前Menu
* @param @return 设定文件
* @return String 返回类型
* @throws
*/
public static String deleteMenu() {
String access_token = getAccess_token();
String action = WXConst.DELETE_MENU + access_token;
String returnMsg = "deleteMenu";
try {
return WXHttpUtil.getMenuUtils(action, returnMsg);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return "菜单查询失败!!!";
}
}
/**
* 查询当前Menu
*
* @Title: deleteMenu
* @Description: 查询当前Menu
* @param @return 设定文件
* @return String 返回类型
* @throws
*/
public static String queryMenu() {
String access_token = getAccess_token();
String action = WXConst.QUERY_MENU + access_token;
String returnMsg = "queryMenu";
try {
return WXHttpUtil.getMenuUtils(action, returnMsg);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return "菜单查询失败!!!";
}
}
/**
* 获取accessToken
* @return
*/
public static String getAccess_token() {
String url = WXConst.URL_GET_TOKEN;
String accessToken = null;
try {
URL urlGet = new URL(url);
HttpURLConnection http = (HttpURLConnection) urlGet
.openConnection();
http.setRequestMethod("GET"); // 必须是get方式请求
http.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
http.setDoOutput(true);
http.setDoInput(true);
System.setProperty("sun.net.client.defaultConnectTimeout", "30000");// 连接超时30秒
System.setProperty("sun.net.client.defaultReadTimeout", "30000"); // 读取超时30秒
http.connect();
InputStream is = http.getInputStream();
int size = is.available();
byte[] jsonBytes = new byte[size];
is.read(jsonBytes);
String message = new String(jsonBytes, "UTF-8");
JSONObject demoJson = new JSONObject(message);
accessToken = demoJson.getString("access_token");
System.out.println(message);
} catch (Exception e) {
e.printStackTrace();
}
return accessToken;
}
@Test
public void testManu(){
createMenu();
//deleteMenu();
}
}
posted on 2014-12-18 11:31 w_xunfangA 阅读(359) 评论(0) 编辑 收藏 举报