概述
具体的辅助类,上一篇文章已经给出
/**
* 基础的菜单创建类
*
* 使用方法如:BaseMenuManaer.java
*
* @author SQ
*
*/
public class BaseMenuManaer {
private static Log log = LogFactory.getLog(BaseMenuManaer.class);
private final String appid="wx3e0757f2daabcd22",appSecret="b04c2f10398688c741c545abe0260ac8",domainName="";
// public static final String basePath = "http://windlienr.6655.la/";
// public static final String djy_xcc_url =
// "http://wxpt.192171.org/weixin/djy.html";
// 第三方用户唯一凭证,自己的公众号申请的.现在是订阅号
// public static String appId = "wx8db4f67cbf7be9f5";
// public static String appSecret = "96fa8dcd3b377c6ebc6d9d92a6e7647b";
public void newMenu() {
// 调用接口获取access_token
AccessToken at = WeixinUtil.getAccessToken(appid,
appSecret);
if (null != at) {
// 调用接口创建菜单
int result = WeixinUtil.createMenu(getMenu(), at.getToken());
// 判断菜单创建结果
if (0 == result)
log.info("菜单创建成功!");
else
log.info("菜单创建失败,错误码:" + result);
}
}
public static void main(String[] args) {
System.out.println(new BaseMenuManaer().newMenu2());
}
public int newMenu2() {
// 调用接口获取access_token
AccessToken at = WeixinUtil.getAccessToken(appid,
appSecret);
int result = -1;
if (null != at) {
// 调用接口创建菜单
result = WeixinUtil.createMenu(getMenu(), at.getToken());
// 判断菜单创建结果
if (0 == result)
log.info("菜单创建成功!");
else
log.info("菜单创建失败,错误码:" + result);
} else {
log.info("菜单创建失败,获取AccessToken 失败!");
}
return result;
}
/**
* 组装菜单数据
*
* button key值说明 U_T_bindUser U_S_notices
*
* 第一位: U表示User 即需要绑定验证 A表示所有关注者
*
* 第二位: T表示返回文本消息 S 表示需要java service处理
*
* 第三位: 表示菜单key值
*
* @return
*/
public Menu getMenu() {
String[][] info = {
{ "0", "绑定账号", "click", "U_T_bindUser"},
{ "0", "我的信息", "click", "U_S_myInfo" },
{ "0", "消息提醒", "click", "U_S_notices" },
// ,
{
"0",
"信息查询",
"view",
"https://open.weixin.qq.com/connect/oauth2/authorize?appid="
+ appid
+ "&redirect_uri="
+ appSecret
+ "/servlet/BwdyMenuRespServlet&response_type=code&scope=snsapi_base&state=http://baidu.com#wechat_redirect" },
{ "0", "公司信息", "click", "U_S_zzgxzj" },
{
"1",
"我的计划",
"view",
"https://open.weixin.qq.com/connect/oauth2/authorize?appid="
+ appid
+ "&redirect_uri="
+ domainName
+ "/servlet/BwdyMenuRespServlet&response_type=code&scope=snsapi_base&state=http://baidul.com/mobile/index_01.html#wechat_redirect" },
{ "1", "公司概况", "click", "U_T_myOrg" },
{ "1", "公司新闻", "click", "U_S_zzNews" },
{ "1", "三会一课", "view", "https://open.weixin.qq.com/connect/oauth2/authorize?appid="
+ appid
+ "&redirect_uri="
+ domainName
+ "/servlet/BwdyMenuRespServlet&response_type=code&scope=snsapi_base&state=http://baidul.com/page/list.html#wechat_redirect" },
{ "1", "公司活动", "click", "U_S_zzhd" },
//{ "1", "建议投稿", "click", "U_S_zzhd" },
{
"2",
"民族精神",
"view",
"https://open.weixin.qq.com/connect/oauth2/authorize?appid="
+ appid
+ "&redirect_uri="
+ domainName
+ "/servlet/BwdyMenuRespServlet&response_type=code&scope=snsapi_base&state=http://baidu.com/qm/party19th/sign/on#wechat_redirect" },
{
"2",
"两学一做",
"view",
"https://open.weixin.qq.com/connect/oauth2/authorize?appid="
+ appid
+ "&redirect_uri="
+ domainName
+ "/servlet/BwdyMenuRespServlet&response_type=code&scope=snsapi_base&state=http://baidu/qm/party/sign/on#wechat_redirect" },
{ "2", "学习", "click", "U_S_zzk" },
{ "2", "联系我们", "click", "A_T_lxwm" },
};
ComplexButton mainBtn1 = new ComplexButton();
mainBtn1.setName("我");
ComplexButton mainBtn2 = new ComplexButton();
mainBtn2.setName("我的信息");
ComplexButton mainBtn3 = new ComplexButton();
mainBtn3.setName("我的服务");
List<Button> btn1 = new ArrayList<Button>();
List<Button> btn2 = new ArrayList<Button>();
List<Button> btn3 = new ArrayList<Button>();
List<List<Button>> btns = new ArrayList<List<Button>>();
btns.add(btn1);
btns.add(btn2);
btns.add(btn3);
for (String[] arr : info) {
List<Button> b = getBtn(arr[0], btns);
if (arr[2].equals("click")) {
CommonButton arrBtn = new CommonButton();
arrBtn.setName(arr[1]);
((CommonButton) arrBtn).setType(arr[2]);
((CommonButton) arrBtn).setKey(arr[3]);
b.add(arrBtn);
} else {
ViewButton arrBtn = new ViewButton();
arrBtn.setName(arr[1]);
((ViewButton) arrBtn).setType(arr[2]);
((ViewButton) arrBtn).setUrl(arr[3]);
b.add(arrBtn);
}
}
mainBtn1.setSub_button(toArray(btn1));
mainBtn2.setSub_button(toArray(btn2));
mainBtn3.setSub_button(toArray(btn3));
Menu menu = new Menu();
menu.setButton(new Button[] { mainBtn1, mainBtn2, mainBtn3 });
// menu.setButton(new Button[] { mainBtn1, mainBtn2 });
return menu;
}
private static Button[] toArray(List<Button> btns) {
Button[] btn = new Button[btns.size()];
for (int i = 0, len = btns.size(); i < len; i++) {
btn[i] = btns.get(i);
}
return btn;
}
private static List<Button> getBtn(String num, List<List<Button>> btns) {
return btns.get(Integer.parseInt(num));
}
}
最后
以上就是眼睛大丝袜为你收集整理的java创建微信菜单第二种的全部内容,希望文章能够帮你解决java创建微信菜单第二种所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复