zoukankan      html  css  js  c++  java
  • 微信自定义菜单

    自己写的构造自定义菜单的方法,虽然有些重复,但毕竟是自己写的感觉更清楚些!各位有什么好的意见指出来呀!

    两个类SubButton(子菜单),ParentButton(父级菜单)。

    public class ParentButton
    {
      public string name;
      public List<SubButton> list;
    }
    
    public class SubButton
    {
      public string name;
      public string type;
      public string url;
      public string key;
    }

     

    构造好的菜单传给GetCustomMenuJsonData得到正确的Json格式数据。

    public string GetCustomMenuJsonData(List<ParentButton> list)
    {
      string jsonStr = "";
      string str = "";
      if (list != null && list.Count > 0)
      {
        foreach (ParentButton parentButton in list)
        {
          if (parentButton != null)
          {
            string subStr = "";
            if (parentButton.list != null && parentButton.list.Count > 0)
            {
              foreach (SubButton button in parentButton.list)
              {
                if (button != null)
                {
                  if (subStr != "")
                  {
                    subStr += ",";
                  }
                  if (button.type == "click")
                  {
                    subStr += "{ "type":"click", "name":"" + button.name + "","key":"" + button.key + "" }";
                  }
                  else
                  {
                    subStr += "{ "type":"view", "name":"" + button.name + "","url":"" + button.url + "" }";
                  }
    
                  }
    
                }
    
              }
              if (str != "")
              {
                str += ",";
              }
              str += " {"name":"" + parentButton.name + "", "sub_button":[" + subStr + "]}";
    
            }
          }
          jsonStr = ("{ "button":[" + str + "]}");
        }
      return jsonStr;
      }
    View Code

    举例:

    Publish List<ParentButton> CreateCustomButton()
    
    {
    
    ParentButton button1 = new ParentButton();
    
    List<SubButton> subButtonList1 = new List<SubButton>();
    SubButton subButton = new SubButton();
    subButton.name = "百度";
    subButton.type = "view";
    subButton.url = "www.baidu.com";
    subButtonList1.Add(subButton);
    
    subButton = new SubButton();
    subButton.name = "谷歌";
    subButton.type = "view";
    subButton.url = "www.google.com";
    
    subButtonList1.Add(subButton);
    
    button1.name = "搜索"; 
    button1.list = subButtonList1;
    
    
    ParentButton button2 = new ParentButton();
    button2.name="其他";
    
    List<ParentButton> list = new List<ParentButton>();
    list.Add(button1);
    list.Add(button2);
    
    return list;
    
    }
    View Code

    得到json数据后通过

    https://api.wexin.qq.com/cgi-bin/token?grant_type=client_credential&appid=AppId&secret=AppSecret

    获取到accesstoken,然后通过

    https://api.weixin.qq.com/cgi-bin/menu/create?access_token=AccessToken

    提交自定义菜单的json数据,生成菜单!

      

    
    
  • 相关阅读:
    多测师肖老师_设计用例方法之场景法___(4.6)
    多测师肖老师_设计用例方法之正交表___(4.5)
    多测师肖老师_设计用例方法之因果图___(4.4)
    多测师肖老师_设计用例方法之边界值___(4.3)
    多测师肖老师_设计用例方法之状态迁移法___(4.7)
    多测师肖老师_设计用例方法之等价类___(4.2)
    多测师肖老师_设计用例方法之微信发红包xmind图___(5.1)
    Python+Appium自动化环境搭建
    QQ传文件测试要点
    Python算法(一)冒泡排序
  • 原文地址:https://www.cnblogs.com/flywing/p/4089567.html
Copyright © 2011-2022 走看看