Exemplo n.º 1
0
        /// <summary>
        /// 创建自定义菜单
        /// 返回Hashtable对象:{"errcode":错误代码,"errmsg":错误消息}
        /// state:success,error分别代表成功,失败
        /// </summary>
        /// <param name="accessToken"></param>
        /// <param name="btnList"></param>
        /// <returns></returns>
        public Hashtable MenuCreate(string accessToken, List <MenuButton> btnList)
        {
            string wxurl = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + accessToken;

            List <object> leverOneBtnList = new List <object>();  //  一级按钮

            #region 构造按钮结构
            foreach (var item in btnList)
            {
                if (item.SubButtonList == null || item.SubButtonList.Count == 0)
                {
                    if (item.ButtonType == "view")
                    {
                        var btn = new { type = item.ButtonType.ToString(), name = item.Name, url = item.Url };
                        leverOneBtnList.Add(btn);
                    }
                    else if (item.ButtonType == "media_id")
                    {
                        var btn = new { type = item.ButtonType.ToString(), name = item.Name, media_id = item.MediaId };
                        leverOneBtnList.Add(btn);
                    }
                    else
                    {
                        var btn = new { type = item.ButtonType.ToString(), name = item.Name, key = item.Key };
                        leverOneBtnList.Add(btn);
                    }
                }
                else
                {
                    List <object> leverTwoBtnList = new List <object>();  // 二级按钮
                    foreach (var sitem in item.SubButtonList)
                    {
                        if (sitem.ButtonType == "view")
                        {
                            var btn = new { type = sitem.ButtonType.ToString(), name = sitem.Name, url = sitem.Url };
                            leverTwoBtnList.Add(btn);
                        }
                        else if (item.ButtonType == "media_id")
                        {
                            var btn = new { type = item.ButtonType.ToString(), name = item.Name, media_id = item.MediaId };
                            leverTwoBtnList.Add(btn);
                        }
                        else
                        {
                            var btn = new { type = sitem.ButtonType.ToString(), name = sitem.Name, key = sitem.Key };
                            leverTwoBtnList.Add(btn);
                        }
                    }

                    leverOneBtnList.Add(new { name = item.Name, sub_button = leverTwoBtnList });
                }
            }
            #endregion

            Hashtable btnHash = new Hashtable();
            btnHash.Add("button", leverOneBtnList);

            string    result  = HttpHelper.ServerPostRequest(wxurl, btnHash.ToJson());
            Hashtable resHash = result.DeserializeJson <Hashtable>();
            resHash["errmsg"] = ErrorCode.GetErrmsg(resHash["errcode"].ToString());
            return(resHash);
        }