微信公众号开发--自定义菜单栏

自己先看开发者文档

建五个实体类,用来存放按钮

/**
 * 微信公众号获得Token 的实体类
 * @author Administrator
 *
 */
public class AccessToken {
     //获取到的凭证
    private String accessToken;
    //凭证有效时间,单位:秒
    private int expiresin;

    public String getAccessToken() {
        return accessToken;
    }

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

    public int getExpiresin() {
        return expiresin;
    }

    public void setExpiresin(int expiresin) {
        this.expiresin = expiresin;
    }

    public AccessToken() {
        super();
        // TODO Auto-generated constructor stub
    }

    public AccessToken(String accessToken, int expiresin) {
        super();
        this.accessToken = accessToken;
        this.expiresin = expiresin;
    }
    
}
/**
 * 按钮基类
 * @author Administrator
 *
 */
public class BaseButton {
        private String type;
        private String name;
        private String key;
        private String url;
        private String media_id;
        public String getType() {
            return type;
        }
        public void setType(String type) {
            this.type = type;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getKey() {
            return key;
        }
        public void setKey(String key) {
            this.key = key;
        }
        public String getUrl() {
            return url;
        }
        public void setUrl(String url) {
            this.url = url;
        }
        public String getMedia_id() {
            return media_id;
        }
        public void setMedia_id(String media_id) {
            this.media_id = media_id;
        }
        
}
import java.util.List;

import com.google.gson.annotations.SerializedName;
/**
 * 自定义菜单栏 --父按钮
 * @author Administrator
 *
 */
public class FatherButton extends BaseButton {
private String button;//可能直接一个父按钮做响应
@SerializedName("sub_button")//为了保证Gson解析后子按钮的名字是“sub_button”,具体用法请搜索
private List<SonButton> sonButtons;//可能有多个子按钮
public String getButton() {
    return button;
}
public void setButton(String button) {
    this.button = button;
}
public List<SonButton> getSonButtons() {
    return sonButtons;
}
public void setSonButtons(List<SonButton> sonButtons) {
    this.sonButtons = sonButtons;
}

}
import java.util.List;

import com.google.gson.annotations.SerializedName;

public class Menu {
    @SerializedName("button")
    private List<FatherButton> fatherButtons;

    public List<FatherButton> getFatherButtons() {
        return fatherButtons;
    }

    public void setFatherButtons(List<FatherButton> fatherButtons) {
        this.fatherButtons = fatherButtons;
    }
    
}
/**
 * 自定义菜单栏 --子按钮
 * @author Administrator
 *
 */
public class SonButton extends BaseButton {
    private String sub_button;

    public String getSub_button() {
        return sub_button;
    }

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

记得开发者配置那边把回调地址授权一下,不然报错,还有java里写回调要编码一下才行

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

import com.fuyin.mp.entity.FatherButton;
import com.fuyin.mp.entity.Menu;
import com.fuyin.mp.entity.SonButton;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;


public class GetMenuJson {
 public String getMenuJson(){
      //Gson gson = new Gson();// json处理工具
         Gson gson = new GsonBuilder().disableHtmlEscaping().create();     
         Menu menu = new Menu();// 菜单类
      List<FatherButton> fatherButtons = new ArrayList<FatherButton>();// 菜单中的父按钮集合
      // -----------
      // 父按钮1
      FatherButton fb1 = new FatherButton();
      fb1.setName("1");
      List<SonButton> sonButtons1 = new ArrayList<SonButton>();// 子按钮的集合
      // 子按钮1-1 跳页面
      SonButton sb11 = new SonButton();
      sb11.setName("11");
      sb11.setUrl(WxaApi.WebUrl+"/demo/departmentIntroduction.html");
      sb11.setType("view");
      // 子按钮1-2 跳页面
      SonButton sb12 = new SonButton();
      sb12.setName("12");
      sb12.setUrl(WxaApi.WebUrl+"/demo/doctorIntroduction.html");
      sb12.setType("view");
      // 子按钮1-3 跳页面
      SonButton sb13 = new SonButton();
      sb13.setName("13");
      sb13.setUrl(redirect_uri());
      sb13.setType("view");
      sonButtons1.add(sb11);sonButtons1.add(sb12);sonButtons1.add(sb13);
      fb1.setSonButtons(sonButtons1);
      // -------------
      // 父按钮2
      FatherButton fb2 = new FatherButton();
      fb2.setName("2");
      List<SonButton> sonButtons2 = new ArrayList<SonButton>();// 子按钮的集合

      // 子按钮2-1  
      SonButton sb21 = new SonButton();
      sb21.setName("21");
      sb21.setUrl(WxaApi.WebUrl+"/demo/appointment.html");
      sb21.setType("view");
      // 子按钮2-3  
      SonButton sb22 = new SonButton();
      sb22.setName("22");
      sb22.setUrl(WxaApi.WebUrl+"/demo/pay.html");
      sb22.setType("view");
      // 子按钮2-4  
      SonButton sb23 = new SonButton();
      sb23.setName("23");
      sb23.setUrl(WxaApi.WebUrl+"/demo/report.html");
      sb23.setType("view");

      // 添加子按钮到子按钮集合
      sonButtons2.add(sb21);
      sonButtons2.add(sb22);
      sonButtons2.add(sb23);

      // 将子按钮放到2-0父按钮集合
      fb2.setSonButtons(sonButtons2);

      // ------------------
      // 父按钮3
      FatherButton fb3 = new FatherButton();
      fb3.setName("3");
      List<SonButton> sonButtons3 = new ArrayList<SonButton>();

      // 子按钮3-1  
      SonButton sb31 = new SonButton();
      sb31.setName("31");
      sb31.setUrl(WxaApi.WebUrl+"/demo/registrationRecord.html");
      sb31.setType("view");

      // 子按钮3-2  
      SonButton sb32 = new SonButton();
      sb32.setName("32");
      sb32.setUrl(WxaApi.WebUrl+"/demo/waitTreatRecord.html");
      sb32.setType("view");

      // 子按钮3-3  
      SonButton sb33 = new SonButton();
      sb33.setName("33");
      sb33.setUrl(WxaApi.WebUrl+"/demo/payRecord.html");
      sb33.setType("view");
      // 子按钮3-4 
      SonButton sb34 = new SonButton();
      sb34.setName("34");
      sb34.setUrl(WxaApi.WebUrl+"/demo/medicalCard.html");
      sb34.setType("view");
      // 添加子按钮到子按钮队列
      sonButtons3.add(sb31);
      sonButtons3.add(sb32);
      sonButtons3.add(sb33);
      sonButtons3.add(sb34);
      // 将子按钮放到3-0父按钮队列
      fb3.setSonButtons(sonButtons3);
      // ---------------------

      // 将父按钮加入到父按钮集合
      fatherButtons.add(fb1);
      fatherButtons.add(fb2);
      fatherButtons.add(fb3);

      // 将父按钮队列加入到菜单栏
      menu.setFatherButtons(fatherButtons);
      String json = gson.toJson(menu);
      System.out.println(json);// 测试输出
      return json;
 }
 public String redirect_uri(){
        //https://open.weixin.qq.com/connect/oauth2/authorize?
        //appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect
        String Url=WxaApi.getoauth2.replace("APPID", WxaApi.appID).replace("REDIRECT_URI", WxaApi.indexredirect_uri).replace("SCOPE", "snsapi_userinfo");
        return Url;
    }
}

猜你喜欢

转载自www.cnblogs.com/chaoswu/p/10174456.html
今日推荐