微信公衆番号カスタムメニュー設定管理
4880 ワード
微信開発時にインタフェースデバッグツールに従ってカスタムメニューの設定を行うのは面倒でエラーが発生しやすい。特にテスト番号を使ってテストを行う場合、テスト後にカスタムメニューの設定機能がないため、自分でカスタムメニューをカプセル化する方法でカスタムメニューの設定を行うことができる。
親測定で使用可能なコード
しゅほうしき
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;
}
}