其他分享
首页 > 其他分享> > 微信服务号新手入门讲解、第一章(创建菜单)

微信服务号新手入门讲解、第一章(创建菜单)

作者:互联网

服务号最多有三个一级菜单,每个一级菜单可以最多配置5二级菜单,配置菜单可以登陆微信服务号后台进行配置,也可以使用代码调用微信接口服务来自定义配置,各有优缺点,今天主要分享下代码配置菜单的方式。

先了解下菜单的官方文档:https://developers.weixin.qq.com/doc/offiaccount/Custom_Menus/Creating_Custom-Defined_Menu.html

接口调用请求说明
http请求方式:POST(请使用https协议) https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN

由官方文档提供的接口调用请求说明可以知道,生成菜单需要ACCESS_TOKEN这个参数,而这个参数又是由APPID和APPSECRET来调用ACCESS_TOKEN的接口来获取的,所以我们主要步骤如下:

1 获取ACCESS_TOKEN
2 根据文档构建菜单数据格式
3 调用菜单接口生成菜单

上代码:

1 获取ACCESS_TOKEN(该参数在微信那边是有每天调用次数的上限的,需要将该参数缓存起来,过期后再重新调用获取)

/**
	 * 获取基础accesstoken地址,get方式
	 */
	public final static String token_Url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
	/**
	 * 获取公众号的唯一秘钥,Accesstoken
	 * @param appId  
	 * @param appsecred
	 * @return	accessToken 
	 */
	public static String getAccessToken(){
		//拼接请求地址
		String requestUrl = token_Url.replace("APPID", appId).replace("APPSECRET", secret);
		String accessToken = "";
		long token_time = Singleton.getInstance().getTokenTime();
		long nowTime = System.currentTimeMillis()/1000;
		
		//判断上次获取的accessToken是否有效,如果无效重新获取,并缓存
		if(0 == token_time || nowTime - token_time > 7000){
			//调用接口
			String jsonString = HttsRequest(requestUrl, "GET", null);
			try{
			//将json字符串转换成java对象
			JSONObject jsonObj = JSONObject.fromObject(jsonString);
			boolean err = jsonObj.containsKey("access_token");
	  		if(err){
	  			accessToken = jsonObj.getString("access_token");
	  			logger.info("获取access_token: "+accessToken);
	  		}else{
	  			accessToken = "";
	  			logger.info("获取access_token失败!"+jsonObj);
	  		}
			
			// 缓存accessToken
			Singleton.getInstance().setAccessToken(accessToken);
			Singleton.getInstance().setTokenTime(System.currentTimeMillis()/1000);
			
			}catch(Exception e){
				e.printStackTrace();
			}
		}else{
			accessToken = Singleton.getInstance().getAccessToken();
		}
		return accessToken;
	}

使用单例模式缓存


public class Singleton {
	
	private static Singleton singleton = null;
	
	private String accessToken;
	
	private long tokenTime;
	
	private String jsapiTicket;
	
	private long ticketTime;
	
	private Singleton() {

	}
	
	 // 静态工厂方法
	public static synchronized Singleton getInstance(){
		if(singleton == null){
			singleton = new Singleton();
		}
		return singleton;
	}

	public String getAccessToken() {
		return accessToken;
	}

	public void setAccessToken(String accessToken) {
		this.accessToken = accessToken;
	}

	public long getTokenTime() {
		return tokenTime;
	}

	public void setTokenTime(long tokenTime) {
		this.tokenTime = tokenTime;
	}

	public String getJsapiTicket() {
		return jsapiTicket;
	}

	public void setJsapiTicket(String jsapiTicket) {
		this.jsapiTicket = jsapiTicket;
	}

	public long getTicketTime() {
		return ticketTime;
	}

	public void setTicketTime(long ticketTime) {
		this.ticketTime = ticketTime;
	}

}

2 根据文档构建菜单数据格式

public String menuJSON(){ 
		
		Menu menu = new Menu();
		
		ViewButton btn11 = new ViewButton();
		ViewButton btn12 = new ViewButton();
		ViewButton btn13 = new ViewButton();
		ViewButton btn14 = new ViewButton();
		ViewButton btn15 = new ViewButton();
		btn11.setName("会员活动");
		btn11.setUrl("https://www.baidu.com");
		btn11.setType("view");
		
		btn12.setName("信息公开");
		btn12.setUrl("https://www.baidu.com");
		btn12.setType("view");
		
		btn13.setName("政策发布");
		btn13.setUrl("https://www.baidu.com");
		btn13.setType("view");
		
		btn14.setName("会员热点");
		btn14.setUrl("https://www.baidu.com");
		btn14.setType("view");
		
		btn15.setName("更多精彩");
		btn15.setUrl("https://www.baidu.com");
		btn15.setType("view");
		
		ViewButton btn21 = new ViewButton();
		ViewButton btn22 = new ViewButton();
		ViewButton btn23 = new ViewButton();
		
		btn21.setName("我的服务");
		btn21.setUrl("http://www.baidu.com");
		btn21.setType("view");
		
		btn22.setName("在线客服");
		btn22.setUrl("https://www.baidu.com");
		btn22.setType("view");
		
		btn23.setName("我的业务"); 
		btn23.setUrl("http://www.baidu.com"); 
		btn23.setType("view");
		
		ViewButton btn31 = new ViewButton();
		ViewButton btn32 = new ViewButton();
		ViewButton btn33 = new ViewButton();
		ViewButton btn34 = new ViewButton();
		ViewButton btn35 = new ViewButton();
		
		btn31.setName("公司介绍");
		btn31.setUrl("http://www.baidu.com");
		btn31.setType("view");
		
		btn32.setName("公司产品");
		btn32.setUrl("http://www.baidu.com");
		btn32.setType("view");
		
		btn33.setName("解决方案");
		btn33.setUrl("http://www.baidu.com");
		btn33.setType("view");
		
		btn34.setName("经典案例");
		btn34.setUrl("http://www.baidu.com");
		btn34.setType("view");
		
		btn35.setName("更多精彩");
		btn35.setUrl("http://www.baidu.com");
		btn35.setType("view");
		
		ComplexButton btn1 = new ComplexButton();
		ComplexButton btn2 = new ComplexButton();
		ComplexButton btn3 = new ComplexButton();
		btn1.setName("第一个一级菜单");
		btn2.setName("第二个一级菜单");
		btn3.setName("第三个一级菜单");
		
		//一级菜单中的子菜单
		btn1.setSub_button(new Button[]{btn15});
		btn2.setSub_button(new Button[]{btn21, btn23});
		btn3.setSub_button(new Button[]{btn31});
		
		//菜单对象
		menu.setButton(new Button[]{btn1,btn2,btn3});
		
		//将java对象转换成JSON字符串
		String menuJson = JSONObject.fromObject(menu).toString();
		return menuJson;
	}

3 调用菜单接口生成菜单

private final static String MENU_CREATE_URL = 
			" https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN";
	
	public boolean createMenu(String json,String accessToken){
		boolean result = false;
		//拼装请求地址
		String requestUrl = MENU_CREATE_URL.replace("ACCESS_TOKEN", accessToken);
		//调用接口
		String respjson = CommUtil.HttsRequest(requestUrl, "POST", json);
		//将JSONObject字符串转成对象
		JSONObject jsonobject = JSONObject.fromObject(respjson);
		if(jsonobject != null){
			int errCode = jsonobject.getInt("errcode");
			String errMsg = jsonobject.getString("errmsg");
			if(0 == errCode){
				result = true;
				System.out.println("创建菜单成功!");
			}else{
				result= false;
				System.out.println("创建菜单失败:"+errCode);
			}
		}
		
		return result;
	}

4 调用:


public class MenuTest {
		
	public static void main(String[] args) {
		//创建菜单
		MenuUtils mu = new MenuUtils();
		String menuJson = mu.menuJSON();
		//生成菜单前先确认域名是否对应正确
		boolean ceshihao = mu.createMenu(menuJson,CommUtil.getAccessToken());

	}

}

http请求工具包到我主要找,有现成的代码。
https://blog.csdn.net/lilin869626257/article/details/121426949

标签:菜单,String,accessToken,微信,ViewButton,新手入门,new,com
来源: https://blog.csdn.net/lilin869626257/article/details/121501292