微信公众号自定义菜单设置管理

微信开发时候如果按照接口调试工具进行自定义菜单的设置非常麻烦而且容易出错,尤其是使用测试号进行测试的时候,由于测试后没有自定义菜单的设置功能,我们可以自己封装一个自定义菜单的方法对其进行自定义菜单的设置。

亲测可用的代码

主方法
import entity.menu.Button;
import entity.menu.ClickButton;
import entity.menu.PhotoOrAlbumButton;
import entity.menu.SubButton;
import entity.menu.ViewButton;
import service.LlService;
/**
 * 	创建自定义菜单
 * @author LILUO
 *
 */
public class CreateMenu {
	public static void main(String[] args) {
		//创建button对象
			Button btn = new Button();
			btn.getButton().add(new ClickButton("一级菜单", "1"));//第一个一级菜单
			btn.getButton().add(new ViewButton("手机商城", "跳转地址"));//第二个一级菜单
			//含二级菜单
			SubButton sub = new SubButton("一级菜单");
			//二级菜单
			sub.getSub_button().add(new PhotoOrAlbumButton("图片上传", "31"));
			sub.getSub_button().add(new ClickButton("点击", "32"));
			sub.getSub_button().add(new ViewButton("跳转", "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxaa741fe1928b80fb&redirect_uri=回调地址&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect"));
			//加入到btn中
			btn.getButton().add(sub);
			
			//将对象转成json对象
			net.sf.json.JSONObject jsonObject = net.sf.json.JSONObject.fromObject(btn);
			//url
			//String url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token="+LlService.getAccessToken();
			String url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=20_pki8CuAWXbNOpq8qNKVJOAFUP3SPWertvTGDycHSSZ9REpUY4ZLNrzK1QtZ53yB0MAi8GguI2Y5xziuXymnA4b1jx4nTsSeEXEhBLIGD731UgmybONoG0PDA-70CM13ie8w6NcJapKsqOllOLDNgAEAQIS";
			//19_XHnjT1lkOO5841x2RRo7vFUORtfVINKSa8Z8pzVXf6jYkBuuhAyOuQUDtqCNxU_Oc7pbXjgccZq-uc_zUQwNPupDdlAhZIC09zcBWe6fEEurFV5jS_3SBvJg1eQLOsB6IFm7gIjlJEYIT4DkELKiAEARYX
		//执行修改
		String result = getAndPost.post(url, jsonObject.toString());
		System.out.println(result);
}

}

实体类

	package entity.menu;
	
	import java.util.ArrayList;
	import java.util.List;
	
	/**
	 *	自定义菜单,用来存放所有的自定义菜单
	 * @author LILUO
	 *
	 */
	public class Button {
		/**
		 * 	定义一个abstractButton用来存放多有的菜单内容
		 */
		private List Button = new ArrayList();
	
		public List getButton() {
			return Button;
		}
	
		public void setButton(List Button) {
			this.Button = Button;
		}
		
	}


package entity.menu;

public	abstract class AbstractButton {
	private String name;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	//通过构造函数创建对象的时候执行赋值
	public AbstractButton(String name) {
		super();
		this.name = name;
	}
	
	
}

package entity.menu;

public class ClickButton extends AbstractButton {
	
	
	private String type = "click";
	private String key;
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public String getKey() {
		return key;
	}
	public void setKey(String key) {
		this.key = key;
	}
	public ClickButton(String name, String key) {
		super(name);
		this.key = key;
	}
	
	
}
package entity.menu;

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

public class PhotoOrAlbumButton extends AbstractButton {
	private String type = "pic_photo_or_album";
	private String key;
	private List sub_button = new ArrayList();
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public String getKey() {
		return key;
	}
	public void setKey(String key) {
		this.key = key;
	}
	public List getSub_button() {
		return sub_button;
	}
	public void setSub_button(List sub_button) {
		this.sub_button = sub_button;
	}
	public PhotoOrAlbumButton(String name, String key) {
		super(name);
		this.key = key;
	}
	
	
}
package entity.menu;

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

public class SubButton extends AbstractButton {
	private List sub_button = new ArrayList();

	public List getSub_button() {
		return sub_button;
	}

	public void setSub_button(List sub_button) {
		this.sub_button = sub_button;
	}

	public SubButton(String name) {
		super(name);
	}
	
	
	
}
package entity.menu;

public class ViewButton extends AbstractButton {
	private String type = "view";
	private String url ;
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public ViewButton(String name, String url) {
		super(name);
		this.url = url;
	}
	
}

你可能感兴趣的:(微信公众号)