微信公众号之自定义菜单

本文依照官方文档所得 https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141013

Utils.class

package com.wechat.utils;

import java.net.URLEncoder;
import java.util.Arrays;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSONObject;
import com.wechat.common.Constants;

/**
 * 工具类
 * @author Administrator
 *
 */
public class Utils {
	
	public static Log logger = LogFactory.getLog(Utils.class);
	
	
	/**
	 * 获取access_token
	 * @return
	 */
	public static String getAccessToken(){
		logger.info("start get access_token...");
		JSONObject result = HttpClientUtil.get(Constants.URL_TOKEN_GET);
		logger.info("getAccessToken result: " + result.toJSONString());
		if(!StringUtils.isEmpty(result.getString("access_token"))){
			return result.getString("access_token");
		} else {
			return Constants.ERR_00001;
		}
	}
	
	/**
     * 获取授权菜单url
     * @param redirectUri
     * @param scope
     * @return
     */
	@SuppressWarnings("deprecation")
	public static String getAuth2CodeUrl(String redirectUri, String scope){
    	StringBuffer url = new StringBuffer();
    	url.append(Constants.URL_GET_AUTH_CODE);
    	url.append(Constants.APP_ID);
    	url.append("&redirect_uri=");
    	url.append(URLEncoder.encode(redirectUri));
    	url.append("&response_type=code&scope=");
    	url.append(scope);
    	url.append("&state=");
    	url.append(randomState(6));
    	url.append("#wechat_redirect");
    	System.out.println("getAuth2CodeUrl: " + url.toString());
    	return url.toString();
    }
	
	/**
     * 随机state
     * @param stateLength
     * @return
     */
    public static String randomState(int stateLength){
    	String stateRange = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    	Random random = new Random();
    	//默认6位
    	if(0 == stateLength) stateLength = Constants.WX_AUTH2_STATE_LENGTH; 
    	StringBuffer randomState = new StringBuffer();
    	for (int i = 0; i < stateLength; i++) {
    		randomState.append(stateRange.charAt(random.nextInt(stateRange.length())));
			
		}
    	return randomState.toString();
    }
	

}

MenuUtil.class

package com.wechat.utils;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.alibaba.fastjson.JSONObject;
import com.wechat.common.Constants;
import com.wechat.vo.WechatButton;

/**
 * 自定义菜单
 * @author Administrator
 *
 */
public class MenuUtil {
	
	public static Log logger = LogFactory.getLog(Utils.class);
	
	public static final String URL_CREATE_MENU = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=";
	public static final String URL_SELECT_MENU = "https://api.weixin.qq.com/cgi-bin/menu/get?access_token=";
	public static final String URL_DEL_MENU = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=";
	
	/**
	 * 创建菜单
	 * @param menu 菜单
	 * @param charset 编码
	 * @param accessToken
	 */
	public static void createMenu(String menu, String charset, String accessToken){
		logger.info("start createMenu...");
		JSONObject result = HttpClientUtil.post(URL_CREATE_MENU + accessToken, menu, charset);
		logger.info("createMenu result: " +  result.toJSONString());
	}
	
	/**
	 * 查询菜单
	 * @param accessToken
	 * @return
	 */
	public static String selectMenu(String accessToken){
		logger.info("start selectMenu...");
		String result = HttpClientUtil.get_String(URL_SELECT_MENU + accessToken);
		logger.info("selectMenu result: " +  result);
		return result;
	}
	
	/**
	 * 删除菜单
	 * @param accessToken
	 * @return
	 */
	public static String delMenu(String accessToken){
		logger.info("start delMenu...");
		String result = HttpClientUtil.get_String(URL_DEL_MENU + accessToken);
		logger.info("delMenu result: " +  result);
		return result;
	}
	
	public static void main(String[] args) {
		String token = Utils.getAccessToken();
		delMenu(token);
		
		WechatButton button_11 = new WechatButton();
		button_11.setName("百度");
		button_11.setType(WechatButton.TYPE_VIEW);
		button_11.setUrl(Utils.getAuth2CodeUrl(
				"http://www.baidu.com", Constants.AUTH_SCOPE_BASE));
		
		WechatButton button_12 = new WechatButton();
		button_12.setName("注册");
		button_12.setType(WechatButton.TYPE_VIEW);
		button_12.setUrl(Utils.getAuth2CodeUrl(
				"https://passport.csdn.net/account/login?action=mobileRegister", Constants.AUTH_SCOPE_BASE));
		//放入button list
		List<WechatButton> buttonList_1 = new ArrayList<>();
		buttonList_1.add(button_11);
		buttonList_1.add(button_12);
		JSONObject button1 = new JSONObject();
		button1.put("name", "个人");
		button1.put("sub_button", buttonList_1);
		
		
		WechatButton button_20 = new WechatButton();
		button_20.setName("博客");
		button_20.setType(WechatButton.TYPE_VIEW);
		button_20.setUrl("https://blog.csdn.net/weixin_42803662");
		//放入button list
		List<WechatButton> buttonList_2 = new ArrayList<>();
		buttonList_2.add(button_20);
		JSONObject button2 = new JSONObject();
		button2.put("name", "产品介绍");
		button2.put("sub_button", buttonList_2);

		
		WechatButton button_30 = new WechatButton();
		button_30.setName("公司介绍");
		button_30.setType(WechatButton.TYPE_VIEW);
		button_30.setUrl("https://blog.csdn.net");
		WechatButton button_31 = new WechatButton();
		button_31.setName("联系我们");
		button_31.setType(WechatButton.TYPE_VIEW);
		button_31.setUrl("https://blog.csdn.net/weixin_42803662");
		//放入button list
		List<WechatButton> buttonList_3 = new ArrayList<>();
		buttonList_3.add(button_30);
		buttonList_3.add(button_31);
		JSONObject button3 = new JSONObject();
		button3.put("name", "关于我们");
		button3.put("sub_button", buttonList_3);
		
		List<JSONObject> buttonList = new ArrayList<>();
		buttonList.add(button1);
		buttonList.add(button2);
		buttonList.add(button3);
		JSONObject button = new JSONObject();
		button.put("button", buttonList);
		
		createMenu(button.toJSONString(), "utf-8", token);
	}

}

WechatButton.class

package com.wechat.vo;

/**
 * 菜单按钮
 * @author Administrator
 *
 */
public class WechatButton {
	
	public static final String TYPE_CLICK = "click";
	public static final String TYPE_VIEW = "view";
	
	private String name;
	
	private String type;
	
	private String key;
	
	private String mediaId;
	
	private String url;
	
	//此处省略getter()  setter()

}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值