Example #1
0
        //
        // GET: /Menu/

        public ActionResult Index()
        {
            GetMenuResult result = new GetMenuResult(new ButtonGroup());

            //初始化
            for (int i = 0; i < 3; i++)
            {
                var subButton = new SubButton();
                for (int j = 0; j < 5; j++)
                {
                    var singleButton = new SingleClickButton();
                    subButton.sub_button.Add(singleButton);
                }
            }

            return(View(result));
        }
Example #2
0
        public ActionResult GetMenuJson(Guid accountid)
        {
            OfficialAccount_BLL bll = new OfficialAccount_BLL();
            OfficialAccount     oa  = bll.Get(a => a.AccountID == accountid);

            string accessToken = AccessTokenContainer.TryGetAccessToken(oa.AppID, oa.AppSecret);

            GetMenuResult result = CommonApi.GetMenu(accessToken);

            foreach (var item in result.menu.button)
            {
                var a2a = item.GetType();
            }

            var aa = result;

            return(Json(result, JsonRequestBehavior.AllowGet));
        }
Example #3
0
        public async Task <GetMenuResult> GetMenuAsync()
        {
            string appId = await SettingManager.GetSettingValueForTenantAsync(WechatSettings.General.AppId, InfrastructureSession.TenantId.Value);

            string appSecret = await SettingManager.GetSettingValueForTenantAsync(WechatSettings.General.Secret, InfrastructureSession.TenantId.Value);

            string accessToken = AccessTokenContainer.TryGetAccessToken(appId, appSecret);

            try
            {
                GetMenuResult result = CommonApi.GetMenu(accessToken);
                return(result);
            }
            catch (WeixinMenuException exception)
            {
                throw new UserFriendlyException(exception.Message);
            }
        }
Example #4
0
        public IActionResult Menu()
        {
            Config cfg = Config.GetSystemConfig();

            if (string.IsNullOrEmpty(cfg.AppId) || string.IsNullOrEmpty(cfg.AppSecret))
            {
                return(EchoTipPage("请先到【系统设置】> 【基本设置】 设置公众号AppId和AppSecret!"));
            }
            string accessToken = "";

            try
            {
                accessToken = AccessTokenContainer.TryGetAccessToken(cfg.AppId, cfg.AppSecret);
            }
            catch (Exception ex)
            {
                return(EchoTipPage(ex.Message));
            }

            if (string.IsNullOrEmpty(accessToken))
            {
                return(EchoTipPage("AccessToken 获取错误,请确认微信配置没错。或者服务器没加入白名单!"));
            }
            //XTrace.WriteLine("当前AccessToken:" + accessToken);
            //获取菜单
            GetMenuResult result = new GetMenuResult(new ButtonGroup());

            try
            {
                result = Senparc.Weixin.MP.CommonAPIs.CommonApi.GetMenu(accessToken);
                if (result == null)
                {
                    result = new GetMenuResult(new ButtonGroup());
                }
            }
            catch (Exception ex)
            {
                result = new GetMenuResult(new ButtonGroup());
                XTrace.WriteLine("获取菜单出错:" + ex.Message);
            }
            Core.Admin.WriteLogActions("查看公众号菜单;");
            return(View(result));
        }
        // GET: WC/MenuSetting
        public ActionResult Index()
        {
            WC_OfficalAccountsModel model = account_BLL.GetCurrentAccount();

            ViewBag.CurrentOfficalAcount = model;
            GetMenuResult result = new GetMenuResult(new ButtonGroup());

            //初始化
            for (int i = 0; i < 3; i++)
            {
                var subButton = new SubButton();
                for (int j = 0; j < 5; j++)
                {
                    var singleButton = new SingleClickButton();
                    subButton.sub_button.Add(singleButton);
                }
            }

            return(View(result));
        }
        public ActionResult Index()
        {
            GetMenuResult result = new GetMenuResult(new ButtonGroup());

            //初始化
            for (int i = 0; i < 3; i++)
            {
                var subButton = new SubButton();
                for (int j = 0; j < 5; j++)
                {
                    var singleButton = new SingleClickButton();
                    subButton.sub_button.Add(singleButton);
                }
            }

            //获取服务器外网IP
            ViewData["IP"] = GetIP() ?? "使用CMD命令ping sdk.weixin.senparc.com";

            return(View(result));
        }
Example #7
0
 public ActionResult GetMenu(string token)
 {
     try
     {
         GetMenuResult menu = CommonApi.GetMenu(token);
         if (menu == null)
         {
             return(base.Json(new { error = "菜单不存在或验证失败!" }, JsonRequestBehavior.AllowGet));
         }
         return(base.Json(menu, JsonRequestBehavior.AllowGet));
     }
     catch (WeixinMenuException exception)
     {
         return(base.Json(new { error = "菜单不存在或验证失败:" + exception.Message }, JsonRequestBehavior.AllowGet));
     }
     catch (Exception exception2)
     {
         return(base.Json(new { error = "菜单不存在或验证失败:" + exception2.Message }, JsonRequestBehavior.AllowGet));
     }
 }
        /// <summary>
        /// 根据微信返回的Json数据得到可用的GetMenuResult结果
        /// </summary>
        /// <param name="resultFull"></param>
        /// <param name="buttonGroupBase">ButtonGroupBase的衍生类型,可以为ButtonGroup或ConditionalButtonGroup。返回的GetMenuResult中的menu属性即为此示例。</param>
        /// <returns></returns>
        public static GetMenuResult GetMenuFromJsonResult(GetMenuResultFull resultFull, ButtonGroupBase buttonGroupBase)
        {
            GetMenuResult result = null;

            if (buttonGroupBase == null)
            {
                throw new ArgumentNullException("buttonGroupBase不可以为空!");
            }

            try
            {
                //重新整理按钮信息
                ButtonGroupBase buttonGroup    = buttonGroupBase; // ?? new ButtonGroup();
                var             rootButtonList = resultFull.menu.button;

                GetButtonGroup(rootButtonList, buttonGroup);//设置默认菜单
                result = new GetMenuResult(buttonGroupBase)
                {
                    menu = buttonGroup,
                    //conditionalmenu = resultFull.conditionalmenu
                };

                //设置个性化菜单列表
                if (resultFull.conditionalmenu != null)
                {
                    var conditionalMenuList = new List <ConditionalButtonGroup>();
                    foreach (var conditionalMenu in resultFull.conditionalmenu)
                    {
                        var conditionalButtonGroup = new ConditionalButtonGroup();
                        GetButtonGroup(conditionalMenu.button, conditionalButtonGroup);//设置默认菜单
                        conditionalMenuList.Add(conditionalButtonGroup);
                    }
                    result.conditionalmenu = conditionalMenuList;
                }
            }
            catch (Exception ex)
            {
                throw new WeixinMenuException(ex.Message, ex);
            }
            return(result);
        }
Example #9
0
        // GET: Admin/Menu
        public ActionResult Index()
        {
            if (Session["CurrentAccountID"] == null)
            {
                return(RedirectToAction("Select", "OfficialAccount", new { Area = "Admin" }));
            }
            Guid accountid = Guid.Parse(Session["CurrentAccountID"].ToString());

            ViewBag.AccountID = accountid;
            OfficialAccount_BLL bll = new OfficialAccount_BLL();
            OfficialAccount     oa  = bll.Get(a => a.AccountID == accountid);

            ViewBag.AppID = oa.AppID;
            string accessToken = AccessTokenContainer.TryGetAccessToken(oa.AppID, oa.AppSecret);

            GetMenuResult result = CommonApi.GetMenu(accessToken);


            return(View(result));
            //return Json(result,JsonRequestBehavior.AllowGet);
        }
Example #10
0
        //
        // GET: /Menu/

        public ActionResult Index()
        {
            if (CheckSupervisorLogin() == false)
            {
                return(Redirect("~/Home/Login?returnUrl=" + System.Web.HttpUtility.UrlEncode("~/Home/Manager")));
            }

            GetMenuResult result = new GetMenuResult(new ButtonGroup());

            //初始化
            for (int i = 0; i < 3; i++)
            {
                var subButton = new SubButton();
                for (int j = 0; j < 5; j++)
                {
                    var singleButton = new SingleClickButton();
                    subButton.sub_button.Add(singleButton);
                }
            }

            return(View(result));
        }
Example #11
0
        /// <summary>
        /// 根据微信返回的Json数据得到可用的GetMenuResult结果
        /// </summary>
        /// <param name="resultFull"></param>
        /// <returns></returns>
        public static GetMenuResult GetMenuFromJsonResult(GetMenuResultFull resultFull)
        {
            GetMenuResult result = null;

            try
            {
                //重新整理按钮信息
                ButtonGroup bg = new ButtonGroup();
                foreach (var rootButton in resultFull.button)
                {
                    if (rootButton.name == null)
                    {
                        continue;                                                                             //没有设置一级菜单
                    }
                    var availableSubButton = rootButton.sub_button.Count(z => !string.IsNullOrEmpty(z.name)); //可用二级菜单按钮数量
                    if (availableSubButton == 0)
                    {
                        //底部单击按钮
                        if (rootButton.type == null ||
                            (rootButton.type.Equals("CLICK", StringComparison.OrdinalIgnoreCase) &&
                             string.IsNullOrEmpty(rootButton.key)))
                        {
                            throw new WeixinMenuException("单击按钮的key不能为空!");
                        }

                        if (rootButton.type.Equals("CLICK", StringComparison.OrdinalIgnoreCase))
                        {
                            //点击
                            bg.button.Add(new SingleClickButton()
                            {
                                name = rootButton.name,
                                key  = rootButton.key,
                                type = rootButton.type
                            });
                        }
                        else if (rootButton.type.Equals("VIEW", StringComparison.OrdinalIgnoreCase))
                        {
                            //URL
                            bg.button.Add(new SingleViewButton()
                            {
                                name = rootButton.name,
                                url  = rootButton.url,
                                type = rootButton.type
                            });
                        }
                        else if (rootButton.type.Equals("LOCATION_SELECT", StringComparison.OrdinalIgnoreCase))
                        {
                            //弹出地理位置选择器
                            bg.button.Add(new SingleLocationSelectButton()
                            {
                                name = rootButton.name,
                                key  = rootButton.key,
                                type = rootButton.type
                            });
                        }
                        else if (rootButton.type.Equals("PIC_PHOTO_OR_ALBUM", StringComparison.OrdinalIgnoreCase))
                        {
                            //弹出拍照或者相册发图
                            bg.button.Add(new SinglePicPhotoOrAlbumButton()
                            {
                                name = rootButton.name,
                                key  = rootButton.key,
                                type = rootButton.type
                            });
                        }
                        else if (rootButton.type.Equals("PIC_SYSPHOTO", StringComparison.OrdinalIgnoreCase))
                        {
                            //弹出系统拍照发图
                            bg.button.Add(new SinglePicSysphotoButton()
                            {
                                name = rootButton.name,
                                key  = rootButton.key,
                                type = rootButton.type
                            });
                        }
                        else if (rootButton.type.Equals("PIC_WEIXIN", StringComparison.OrdinalIgnoreCase))
                        {
                            //弹出微信相册发图器
                            bg.button.Add(new SinglePicWeixinButton()
                            {
                                name = rootButton.name,
                                key  = rootButton.key,
                                type = rootButton.type
                            });
                        }
                        else if (rootButton.type.Equals("SCANCODE_PUSH", StringComparison.OrdinalIgnoreCase))
                        {
                            //扫码推事件
                            bg.button.Add(new SingleScancodePushButton()
                            {
                                name = rootButton.name,
                                key  = rootButton.key,
                                type = rootButton.type
                            });
                        }
                        else
                        {
                            //扫码推事件且弹出“消息接收中”提示框
                            bg.button.Add(new SingleScancodeWaitmsgButton()
                            {
                                name = rootButton.name,
                                key  = rootButton.key,
                                type = rootButton.type
                            });
                        }
                    }
                    else
                    {
                        //底部二级菜单
                        var subButton = new SubButton(rootButton.name);
                        bg.button.Add(subButton);

                        foreach (var subSubButton in rootButton.sub_button)
                        {
                            if (subSubButton.name == null)
                            {
                                continue; //没有设置菜单
                            }

                            if (subSubButton.type.Equals("CLICK", StringComparison.OrdinalIgnoreCase) &&
                                string.IsNullOrEmpty(subSubButton.key))
                            {
                                throw new WeixinMenuException("单击按钮的key不能为空!");
                            }


                            if (subSubButton.type.Equals("CLICK", StringComparison.OrdinalIgnoreCase))
                            {
                                //点击
                                subButton.sub_button.Add(new SingleClickButton()
                                {
                                    name = subSubButton.name,
                                    key  = subSubButton.key,
                                    type = subSubButton.type
                                });
                            }
                            else if (subSubButton.type.Equals("VIEW", StringComparison.OrdinalIgnoreCase))
                            {
                                //URL
                                subButton.sub_button.Add(new SingleViewButton()
                                {
                                    name = subSubButton.name,
                                    url  = subSubButton.url,
                                    type = subSubButton.type
                                });
                            }
                            else if (subSubButton.type.Equals("LOCATION_SELECT", StringComparison.OrdinalIgnoreCase))
                            {
                                //弹出地理位置选择器
                                subButton.sub_button.Add(new SingleLocationSelectButton()
                                {
                                    name = subSubButton.name,
                                    key  = subSubButton.key,
                                    type = subSubButton.type
                                });
                            }
                            else if (subSubButton.type.Equals("PIC_PHOTO_OR_ALBUM", StringComparison.OrdinalIgnoreCase))
                            {
                                //弹出拍照或者相册发图
                                subButton.sub_button.Add(new SinglePicPhotoOrAlbumButton()
                                {
                                    name = subSubButton.name,
                                    key  = subSubButton.key,
                                    type = subSubButton.type
                                });
                            }
                            else if (subSubButton.type.Equals("PIC_SYSPHOTO", StringComparison.OrdinalIgnoreCase))
                            {
                                //弹出系统拍照发图
                                subButton.sub_button.Add(new SinglePicSysphotoButton()
                                {
                                    name = subSubButton.name,
                                    key  = subSubButton.key,
                                    type = subSubButton.type
                                });
                            }
                            else if (subSubButton.type.Equals("PIC_WEIXIN", StringComparison.OrdinalIgnoreCase))
                            {
                                //弹出微信相册发图器
                                subButton.sub_button.Add(new SinglePicWeixinButton()
                                {
                                    name = subSubButton.name,
                                    key  = subSubButton.key,
                                    type = subSubButton.type
                                });
                            }
                            else if (subSubButton.type.Equals("SCANCODE_PUSH", StringComparison.OrdinalIgnoreCase))
                            {
                                //扫码推事件
                                subButton.sub_button.Add(new SingleScancodePushButton()
                                {
                                    name = subSubButton.name,
                                    key  = subSubButton.key,
                                    type = subSubButton.type
                                });
                            }
                            else
                            {
                                //扫码推事件且弹出“消息接收中”提示框
                                subButton.sub_button.Add(new SingleScancodeWaitmsgButton()
                                {
                                    name = subSubButton.name,
                                    key  = subSubButton.key,
                                    type = subSubButton.type
                                });
                            }
                        }
                    }
                }

                result = new GetMenuResult()
                {
                    menu = bg
                };
            }
            catch (Exception ex)
            {
                throw new WeixinMenuException(ex.Message, ex);
            }
            return(result);
        }
Example #12
0
        /// <summary>
        /// 根据微信返回的Json数据得到可用的GetMenuResult结果
        /// </summary>
        /// <param name="resultFull"></param>
        /// <returns></returns>
        public static GetMenuResult GetMenuFromJsonResult(GetMenuResultFull resultFull)
        {
            GetMenuResult result = null;

            try
            {
                //重新整理按钮信息
                ButtonGroup bg = new ButtonGroup();
                foreach (var rootButton in resultFull.menu.button)
                {
                    if (rootButton.name == null)
                    {
                        continue;                                                                             //没有设置一级菜单
                    }
                    var availableSubButton = rootButton.sub_button.Count(z => !string.IsNullOrEmpty(z.name)); //可用二级菜单按钮数量
                    if (availableSubButton == 0)
                    {
                        //底部单击按钮
                        if (rootButton.type == null ||
                            (rootButton.type.Equals("CLICK", StringComparison.OrdinalIgnoreCase) &&
                             string.IsNullOrEmpty(rootButton.key)))
                        {
                            throw new WeixinMenuException("单击按钮的key不能为空!");
                        }

                        if (rootButton.type.Equals("CLICK", StringComparison.OrdinalIgnoreCase))
                        {
                            //点击
                            bg.button.Add(new SingleClickButton()
                            {
                                name = rootButton.name,
                                key  = rootButton.key,
                                type = rootButton.type
                            });
                        }
                        else if (rootButton.type.Equals("VIEW", StringComparison.OrdinalIgnoreCase))
                        {
                            //URL
                            bg.button.Add(new SingleViewButton()
                            {
                                name = rootButton.name,
                                url  = rootButton.url,
                                type = rootButton.type
                            });
                        }
                        else
                        {
                            //未处理情况
                        }
                    }
                    else
                    {
                        //底部二级菜单
                        var subButton = new SubButton(rootButton.name);
                        bg.button.Add(subButton);

                        foreach (var subSubButton in rootButton.sub_button)
                        {
                            if (subSubButton.name == null)
                            {
                                continue; //没有设置菜单
                            }

                            if (subSubButton.type.Equals("CLICK", StringComparison.OrdinalIgnoreCase) &&
                                string.IsNullOrEmpty(subSubButton.key))
                            {
                                throw new WeixinMenuException("单击按钮的key不能为空!");
                            }


                            if (subSubButton.type.Equals("CLICK", StringComparison.OrdinalIgnoreCase))
                            {
                                //点击
                                subButton.sub_button.Add(new SingleClickButton()
                                {
                                    name = subSubButton.name,
                                    key  = subSubButton.key,
                                    type = subSubButton.type
                                });
                            }
                            else if (subSubButton.type.Equals("VIEW", StringComparison.OrdinalIgnoreCase))
                            {
                                //URL
                                subButton.sub_button.Add(new SingleViewButton()
                                {
                                    name = subSubButton.name,
                                    url  = subSubButton.url,
                                    type = subSubButton.type
                                });
                            }
                            else
                            {
                                //未处理情况
                            }
                        }
                    }
                }

                result = new GetMenuResult()
                {
                    menu = bg
                };
            }
            catch (Exception ex)
            {
                throw new WeixinMenuException(ex.Message, ex);
            }
            return(result);
        }
Example #13
0
        private void GetMenu()
        {
            try
            {
                Model.wx_userweixin weixin = GetWeiXinCode();

                string error       = "";
                string accessToken = WeiXinCRMComm.getAccessToken(weixin.id, out error);

                if (error != "")
                {
                    JscriptMsg(error, "", "Error");
                    return;
                }
                GetMenuResult result = mMrg.GetMenu(accessToken);
                if (result == null)
                {
                    //JscriptMsg("未获得到菜单,请参考【使用规则】,自行排查问题!", "", "Error");
                    return;
                    //强制刷新
                    //accessToken = cpp.FlushAccessToken(weixin.id, out  error);
                    //result = CommonApi.GetMenu(accessToken);
                }
                var     topButtonList = result.menu.button;
                int     topNum        = topButtonList.Count;
                TextBox txtName       = new TextBox();
                TextBox txtKey        = new TextBox();
                TextBox txtUrl        = new TextBox();
                for (int i = 0; i < topNum; i++)
                {
                    var topButton = topButtonList[i];
                    if (topButton != null)
                    {
                        txtName      = this.FindControl("txtTop" + (i + 1)) as TextBox;
                        txtKey       = this.FindControl("txtTop" + (i + 1) + "Key") as TextBox;
                        txtUrl       = this.FindControl("txtTop" + (i + 1) + "Url") as TextBox;
                        txtName.Text = topButton.name;


                        if (topButton.GetType() != typeof(SubButton))
                        {     //下面无子菜单
                            if (topButton.GetType() == typeof(SingleViewButton))
                            { //view 页面跳转
                                txtUrl.Text = ((SingleViewButton)topButton).url;
                            }
                            else
                            {
                                txtKey.Text = ((SingleClickButton)topButton).key;
                            }
                        }
                        else
                        {   //下面有子菜单
                            IList <SingleButton> subButtonList = ((SubButton)topButton).sub_button;

                            if (subButtonList != null && subButtonList.Count > 0)
                            {
                                TextBox txtSubName = new TextBox();
                                TextBox txtSubKey  = new TextBox();
                                TextBox txtSubUrl  = new TextBox();
                                for (int j = 0; j < subButtonList.Count; j++)
                                {
                                    txtSubName = this.FindControl("txtMenu" + (i + 1) + (j + 1)) as TextBox;
                                    txtSubKey  = this.FindControl("txtMenu" + (i + 1) + (j + 1) + "Key") as TextBox;
                                    txtSubUrl  = this.FindControl("txtMenu" + (i + 1) + (j + 1) + "Url") as TextBox;

                                    if (subButtonList[j].GetType() == typeof(SingleViewButton))
                                    {
                                        SingleViewButton sub = (SingleViewButton)subButtonList[j];

                                        txtSubName.Text = sub.name;
                                        txtSubUrl.Text  = sub.url;
                                    }
                                    else
                                    {
                                        SingleClickButton sub = (SingleClickButton)subButtonList[j];
                                        txtSubName.Text = sub.name;
                                        txtSubKey.Text  = sub.key;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }
Example #14
0
        /// <summary>
        /// 根据微信返回的Json数据得到可用的GetMenuResult结果
        /// </summary>
        /// <param name="resultFull"></param>
        /// <returns></returns>
        public GetMenuResult GetMenuFromJsonResult(GetMenuResultFull resultFull)
        {
            GetMenuResult result = null;
            try
            {
                //重新整理按钮信息
                ButtonGroup bg = new ButtonGroup();
                foreach (var rootButton in resultFull.menu.button)
                {
                    if (rootButton.name == null)
                    {
                        continue;//没有设置一级菜单
                    }
                    var availableSubButton = rootButton.sub_button.Count(z => !string.IsNullOrEmpty(z.name));//可用二级菜单按钮数量
                    if (availableSubButton == 0)
                    {
                        //底部单击按钮
                        if (rootButton.type.Equals("CLICK", StringComparison.OrdinalIgnoreCase)
                            && string.IsNullOrEmpty(rootButton.key))
                        {
                            throw new WeChatMenuException("单击按钮的key不能为空!");
                        }

                        if (rootButton.type.Equals("CLICK", StringComparison.OrdinalIgnoreCase))
                        {
                            //点击
                            bg.button.Add(new SingleClickButton()
                            {
                                name = rootButton.name,
                                key = rootButton.key,
                                type = rootButton.type
                            });
                        }
                        else
                        {
                            //URL
                            bg.button.Add(new SingleViewButton()
                            {
                                name = rootButton.name,
                                url = rootButton.url,
                                type = rootButton.type
                            });
                        }

                    }
                    //else if (availableSubButton < 1)
                    //{
                    //    throw new WeChatMenuException("子菜单至少需要填写1个!");
                    //}
                    else
                    {
                        //底部二级菜单
                        var subButton = new SubButton(rootButton.name);
                        bg.button.Add(subButton);

                        foreach (var subSubButton in rootButton.sub_button)
                        {
                            if (subSubButton.name == null)
                            {
                                continue; //没有设置菜单
                            }

                            if (subSubButton.type.Equals("CLICK", StringComparison.OrdinalIgnoreCase)
                                && string.IsNullOrEmpty(subSubButton.key))
                            {
                                throw new WeChatMenuException("单击按钮的key不能为空!");
                            }


                            if (subSubButton.type.Equals("CLICK", StringComparison.OrdinalIgnoreCase))
                            {
                                //点击
                                subButton.sub_button.Add(new SingleClickButton()
                                {
                                    name = subSubButton.name,
                                    key = subSubButton.key,
                                    type = subSubButton.type
                                });
                            }
                            else
                            {
                                //URL
                                subButton.sub_button.Add(new SingleViewButton()
                                {
                                    name = subSubButton.name,
                                    url = subSubButton.url,
                                    type = subSubButton.type
                                });
                            }
                        }
                    }
                }

                if (bg.button.Count < 1)
                {
                    throw new WeChatMenuException("一级菜单按钮至少为1个!");
                }

                result = new GetMenuResult()
                {
                    menu = bg
                };
            }
            catch (Exception ex)
            {
                throw new WeChatMenuException(ex.Message, ex);
            }
            return result;
        }
Example #15
0
        public ActionResult GetMenu()
        {
            try
            {
                MenuManage         mMrg   = new MenuManage();
                WxBaseConfigEntity weixin = app.GetDefaultConfig();
                AccessTokenResult  token  = AccessTokenContainer.GetAccessTokenResult(weixin.AppId);
                if (token.errcode != Senparc.Weixin.ReturnCode.请求成功)
                {
                    return(Error(token.errmsg));
                }
                GetMenuResult result = mMrg.GetMenu(token.access_token);
                if (result == null)
                {
                    return(Error("未获得到菜单,请参考【使用规则】,自行排查问题!"));
                }

                var topButtonList = result.menu.button;
                int topNum        = topButtonList.Count;
                //List<string> menu = new List<string>();
                Dictionary <string, string> menu = new Dictionary <string, string>();
                for (int i = 0; i < topNum; i++)
                {
                    var topButton = topButtonList[i];
                    if (topButton != null)
                    {
                        menu.Add("Top" + (i + 1), topButton.name);
                        if (topButton.GetType() != typeof(SubButton))
                        {     //下面无子菜单
                            if (topButton.GetType() == typeof(SingleViewButton))
                            { //view 页面跳转
                                menu.Add("Top" + (i + 1) + "Key", ((SingleViewButton)topButton).url);
                            }
                            else
                            {
                                menu.Add("Top" + (i + 1) + "Url", ((SingleClickButton)topButton).key);
                            }
                        }
                        else
                        {   //下面有子菜单
                            IList <SingleButton> subButtonList = ((SubButton)topButton).sub_button;
                            if (subButtonList != null && subButtonList.Count > 0)
                            {
                                for (int j = 0; j < subButtonList.Count; j++)
                                {
                                    if (subButtonList[j].GetType() == typeof(SingleViewButton))
                                    {
                                        SingleViewButton sub = (SingleViewButton)subButtonList[j];
                                        menu.Add("Menu" + (i + 1) + (j + 1), sub.name);
                                        menu.Add("Menu" + (i + 1) + (j + 1) + "Url", sub.url);
                                    }
                                    else
                                    {
                                        SingleClickButton sub = (SingleClickButton)subButtonList[j];
                                        menu.Add("Menu" + (i + 1) + (j + 1), sub.name);
                                        menu.Add("Menu" + (i + 1) + (j + 1) + "Key", sub.key);
                                    }
                                }
                            }
                        }
                    }
                }
                return(Success("菜单获取成功", menu));
            }
            catch (Exception ex)
            {
                return(Error(ex.Message));
            }
        }
        public static GetMenuResult GetMenuFromJson(string jsonString)
        {
            var finalResult = new GetMenuResult(new ButtonGroup());

            try
            {
                //@"{""menu"":{""button"":[{""type"":""click"",""name"":""单击测试"",""key"":""OneClick"",""sub_button"":[]},{""name"":""二级菜单"",""sub_button"":[{""type"":""click"",""name"":""返回文本"",""key"":""SubClickRoot_Text"",""sub_button"":[]},{""type"":""click"",""name"":""返回图文"",""key"":""SubClickRoot_News"",""sub_button"":[]},{""type"":""click"",""name"":""返回音乐"",""key"":""SubClickRoot_Music"",""sub_button"":[]}]}]}}"
                object jsonResult = null;

#if NET35 || NET40 || NET45
                JavaScriptSerializer js = new JavaScriptSerializer();
                jsonResult = js.Deserialize <object>(jsonString);
#else
                jsonResult = Newtonsoft.Json.JsonConvert.DeserializeObject <object>(jsonString);
#endif

                var fullResult = jsonResult as Dictionary <string, object>;
                if (fullResult != null && fullResult.ContainsKey("menu"))
                {
                    //得到菜单
                    var menu    = fullResult["menu"];
                    var buttons = (menu as Dictionary <string, object>)["button"] as object[];

                    foreach (var rootButton in buttons)
                    {
                        var fullButton = rootButton as Dictionary <string, object>;
                        if (fullButton.ContainsKey("key") && !string.IsNullOrEmpty(fullButton["key"] as string))
                        {
                            //按钮,无下级菜单
                            finalResult.menu.button.Add(GetSingleButtonFromJsonObject(fullButton));
                        }
                        else
                        {
                            //二级菜单
                            var subButton = new SubButton(fullButton["name"] as string);
                            finalResult.menu.button.Add(subButton);
                            foreach (var sb in fullButton["sub_button"] as object[])
                            {
                                subButton.sub_button.Add(GetSingleButtonFromJsonObject(sb as Dictionary <string, object>));
                            }
                        }
                    }
                }
                else if (fullResult != null && fullResult.ContainsKey("errmsg"))
                {
                    //菜单不存在
                    throw new ErrorJsonResultException(fullResult["errmsg"] as string, null, null);
                }
            }
            catch (ErrorJsonResultException ex)
            {
                finalResult = null;

                //如果没有惨淡会返回错误代码:46003:menu no exist
            }
            catch (Exception ex)
            {
                //其他异常
                finalResult = null;
            }
            return(finalResult);
        }
        /// <summary>
        /// 根据微信返回的Json数据得到可用的GetMenuResult结果
        /// </summary>
        /// <param name="resultFull"></param>
        /// <returns></returns>
        public static GetMenuResult GetMenuFromJsonResult(GetMenuResultFull resultFull)
        {
            GetMenuResult result = null;

            try
            {
                //重新整理按钮信息
                ButtonGroup bg = new ButtonGroup();
                foreach (var rootButton in resultFull.menu.button)
                {
                    if (rootButton.name == null)
                    {
                        continue;                                                                             //没有设置一级菜单
                    }
                    var availableSubButton = rootButton.sub_button.Count(z => !string.IsNullOrEmpty(z.name)); //可用二级菜单按钮数量
                    if (availableSubButton == 0)
                    {
                        //底部单击按钮
                        if (string.IsNullOrEmpty(rootButton.key))
                        {
                            throw new WeixinMenuException("单击按钮的key不能为空!");
                        }

                        bg.button.Add(new SingleButton()
                        {
                            name = rootButton.name,
                            key  = rootButton.key,
                            type = rootButton.type//目前只有click
                        });
                    }
                    else if (availableSubButton < 2)
                    {
                        throw new WeixinMenuException("子菜单至少需要填写2个!");
                    }
                    else
                    {
                        //底部二级菜单
                        var subButton = new SubButton(rootButton.name);
                        bg.button.Add(subButton);

                        foreach (var subSubButton in rootButton.sub_button)
                        {
                            if (subSubButton.name == null)
                            {
                                continue; //没有设置菜单
                            }

                            if (string.IsNullOrEmpty(subSubButton.key))
                            {
                                throw new WeixinMenuException("单击按钮的key不能为空!");
                            }

                            subButton.sub_button.Add(new SingleButton()
                            {
                                name = subSubButton.name,
                                key  = subSubButton.key,
                                type = subSubButton.type
                                       //目前只有click
                            });
                        }
                    }
                }

                if (bg.button.Count < 2)
                {
                    throw new WeixinMenuException("一级菜单按钮至少为2个!");
                }

                result = new GetMenuResult()
                {
                    menu = bg
                };
            }
            catch (Exception ex)
            {
                throw new WeixinMenuException(ex.Message, ex);
            }
            return(result);
        }
Example #18
0
        /// <summary>
        /// 根据微信返回的Json数据得到可用的GetMenuResult结果
        /// 将Json结构自定义菜单转换为自定义菜单实体
        /// </summary>
        /// <param name="resultFull"></param>
        /// <returns></returns>
        private static GetMenuResult GetMenuFromJsonResult(GetMenuResultJson resultFull)
        {
            GetMenuResult result = null;

            try
            {
                ButtonGroup bg = new ButtonGroup();
                //循环遍历Json结构
                foreach (var rootButton in resultFull.menu.button)
                {
                    if (rootButton.name == null)
                    {
                        continue;//没有设置一级菜单
                    }
                    //可用二级菜单按钮数量
                    var availableSubButton = rootButton.sub_button.Count(z => !string.IsNullOrEmpty(z.name));
                    //一级菜单格式转换
                    if (availableSubButton == 0)
                    {
                        //按钮格式校验
                        if (rootButton.type == null ||
                            (rootButton.type.Equals("CLICK", StringComparison.OrdinalIgnoreCase) &&
                             string.IsNullOrEmpty(rootButton.key)))
                        {
                            throw new WeixinMenuException("单击按钮的key不能为空!");
                        }
                        if (rootButton.type.Equals("CLICK", StringComparison.OrdinalIgnoreCase))
                        {
                            //底部单击按钮
                            bg.button.Add(new SingleClickButton()
                            {
                                name = rootButton.name,
                                key  = rootButton.key,
                                type = rootButton.type
                            });
                        }
                        else
                        {
                            //底部URL按钮
                            bg.button.Add(new SingleViewButton()
                            {
                                name = rootButton.name,
                                url  = rootButton.url,
                                type = rootButton.type
                            });
                        }
                    }
                    else if (availableSubButton < 1)
                    {
                        throw new WeixinMenuException("子菜单至少需要填写1个!");
                    }
                    //二级菜单格式转换
                    else
                    {
                        //底部二级菜单
                        var subButton = new SubButton(rootButton.name);
                        bg.button.Add(subButton);
                        foreach (var subSubButton in rootButton.sub_button)
                        {
                            if (subSubButton.name == null)
                            {
                                continue; //没有设置菜单
                            }
                            //按钮格式校验
                            if (subSubButton.type.Equals("CLICK", StringComparison.OrdinalIgnoreCase) &&
                                string.IsNullOrEmpty(subSubButton.key))
                            {
                                throw new WeixinMenuException("单击按钮的key不能为空!");
                            }
                            if (subSubButton.type.Equals("CLICK", StringComparison.OrdinalIgnoreCase))
                            {
                                //底部单击按钮
                                subButton.sub_button.Add(new SingleClickButton()
                                {
                                    name = subSubButton.name,
                                    key  = subSubButton.key,
                                    type = subSubButton.type
                                });
                            }
                            else
                            {
                                //底部URL按钮
                                subButton.sub_button.Add(new SingleViewButton()
                                {
                                    name = subSubButton.name,
                                    url  = subSubButton.url,
                                    type = subSubButton.type
                                });
                            }
                        }
                    }
                }
                if (bg.button.Count < 1)
                {
                    throw new WeixinMenuException("一级菜单按钮至少为1个!");
                }
                //保存到自定义菜单实体
                result = new GetMenuResult()
                {
                    menu = bg
                };
            }
            catch (Exception ex)
            {
                throw new WeixinMenuException(ex.Message, ex);
            }
            return(result);
        }