public QyJsonResult Push(int appId)
        {
            var categories = CommonService.lstCategory.Where(x => x.AppId == appId && (x.CategoryCode == null || !x.CategoryCode.EndsWith(_endFix, true, CultureInfo.CurrentCulture)) && (x.IsDeleted == null || x.IsDeleted == false) && (x.IsVirtual == null || x.IsVirtual == false)).OrderBy(x => x.CategoryOrder).Select(x =>
            {
                var function = x.Function.IsNullOrEmpty() ? null : JsonHelper.FromJson <ButtonReturnType>(x.Function);
                return(new CategroyChild
                {
                    Id = x.Id,
                    key = x.CategoryCode,
                    name = function == null ? x.CategoryName : function.Button.name,
                    sub_button = new List <MenuFull_RootButton>(),
                    type = function == null ? null : function.Button.type,
                    ParentId = x.ParentCode.GetValueOrDefault(),
                    order = x.CategoryOrder.GetValueOrDefault(),
                    url = function == null ? null : function.Button.url,
                });
            }).ToList();

            var buttons = new List <MenuFull_RootButton>();

            categories.Where(x => x.ParentId == 0).ToList().ForEach(x => GenerateMenuHierarchy(categories, x, buttons));

            var menu = new GetMenuResultFull {
                menu = new MenuFull_ButtonGroup {
                    button = buttons
                }
            };
            var btnMenus = CommonApi.GetMenuFromJsonResult(menu).menu;
            var result   = CommonApi.CreateMenu(WeChatCommonService.GetWeiXinToken(appId), appId, btnMenus);

            return(result);
        }
Exemplo n.º 2
0
        public async Task <APIResultDto> CreateMenu(GetMenuResultFull fullJson)
        {
            try
            {
                //GetMenuResultFull resultFull = Newtonsoft.Json.JsonConvert.DeserializeObject<GetMenuResultFull>(fullJson);
                //重新整理按钮信息
                WxJsonResult     result      = null;
                IButtonGroupBase buttonGroup = null;

                buttonGroup = CommonApi.GetMenuFromJsonResult(fullJson, new ButtonGroup()).menu;
                var accessToken = await AccessTokenContainer.TryGetAccessTokenAsync(AppId, AppSecret);

                result = CommonApi.CreateMenu(accessToken, buttonGroup);

                return(new APIResultDto()
                {
                    Code = 0, Msg = "上传菜单成功", Data = result
                });
            }
            catch (Exception ex)
            {
                return(new APIResultDto()
                {
                    Code = 901, Msg = "更新菜单失败", Data = ex.Message
                });
            }
        }
Exemplo n.º 3
0
        public ActionResult CreateMenuFromJson(string token, string fullJson)
        {
            //TODO:根据"conditionalmenu"判断自定义菜单

            var apiName = "使用JSON更新";

            try
            {
                GetMenuResultFull resultFull = Newtonsoft.Json.JsonConvert.DeserializeObject <GetMenuResultFull>(fullJson);

                //重新整理按钮信息
                WxJsonResult     result      = null;
                IButtonGroupBase buttonGroup = null;

                buttonGroup = CommonAPIs.CommonApi.GetMenuFromJsonResult(resultFull, new ButtonGroup()).menu;
                result      = CommonAPIs.CommonApi.CreateMenu(token, buttonGroup);

                var json = new
                {
                    Success = result.errmsg == "ok",
                    Message = "菜单更新成功。" + apiName
                };
                return(Json(json));
            }
            catch (Exception ex)
            {
                var json = new { Success = false, Message = string.Format("更新失败:{0}。{1}", ex.Message, apiName) };
                return(Json(json));
            }
        }
Exemplo n.º 4
0
        public ActionResult CreateMenu(string token, GetMenuResultFull resultFull, MenuMatchRule menuMatchRule)
        {
            if (String.IsNullOrWhiteSpace(token))
            {
                token = GetToken();
            }

            var useAddCondidionalApi = menuMatchRule != null && !menuMatchRule.CheckAllNull();
            var apiName = string.Format("使用接口:{0}。", (useAddCondidionalApi ? "个性化菜单接口" : "普通自定义菜单接口"));

            try
            {
                if (token.IsNullOrEmpty())
                {
                    throw new WeixinException("Token不能为空!");
                }

                //重新整理按钮信息
                WxJsonResult     result      = null;
                IButtonGroupBase buttonGroup = null;
                if (useAddCondidionalApi)
                {
                    //个性化接口
                    buttonGroup = CommonApi.GetMenuFromJsonResult(resultFull, new ConditionalButtonGroup()).menu;

                    var addConditionalButtonGroup = buttonGroup as ConditionalButtonGroup;
                    addConditionalButtonGroup.matchrule = menuMatchRule;
                    result   = CommonApi.CreateMenuConditional(token, addConditionalButtonGroup);
                    apiName += string.Format("menuid:{0}。", (result as CreateMenuConditionalResult).menuid);
                }
                else
                {
                    //普通接口
                    buttonGroup = CommonApi.GetMenuFromJsonResult(resultFull, new ButtonGroup()).menu;
                    result      = CommonApi.CreateMenu(token, buttonGroup);
                }

                var json = new
                {
                    Success = result.errmsg == "ok",
                    Message = "菜单更新成功。" + apiName
                };
                return(Json(json, new JsonSerializerSettings()
                {
                    ContractResolver = new DefaultContractResolver()
                }));
            }
            catch (Exception ex)
            {
                var json = new { Success = false, Message = string.Format("更新失败:{0}。{1}", ex.Message, apiName) };
                return(Json(json, new JsonSerializerSettings()
                {
                    ContractResolver = new DefaultContractResolver()
                }));
            }
        }
Exemplo n.º 5
0
 /// <summary>
 /// 创建菜单
 /// </summary>
 /// <param name="token">微信AccessToken</param>
 /// <param name="resultFull">菜单结构</param>
 private void CreateMenu(string token, GetMenuResultFull resultFull)
 {
     try
     {
         //重新整理按钮信息
         var bg     = Senparc.Weixin.MP.CommonAPIs.CommonApi.GetMenuFromJsonResult(resultFull, new ButtonGroup()).menu;
         var result = Senparc.Weixin.MP.CommonAPIs.CommonApi.CreateMenu(token, bg);
     }
     catch (Exception ex)
     {
     }
 }
Exemplo n.º 6
0
        public ActionResult CreateMenu(GetMenuResultFull resultFull)
        {
            WxJsonResult result = null;

            try
            {
                var accessToken = AccessTokenContainer.TryGetToken(AppId, AppSecret);
                var bg          = CommonApi.GetMenuFromJsonResult(resultFull).menu;
                result = CommonApi.CreateMenu(accessToken, bg);
            }
            catch (Exception)
            {
                //TODO
            }
            return(Json(result, JsonRequestBehavior.AllowGet));
        }
Exemplo n.º 7
0
        /// <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()
                        ;
                        //fix bug 16030701  https://github.com/JeffreySu/WeiXinMPSDK/issues/169
                        conditionalButtonGroup.matchrule = conditionalMenu.matchrule;
                        conditionalButtonGroup.menuid    = conditionalMenu.menuid;
                        //fix bug 16030701 end

                        GetButtonGroup(conditionalMenu.button, conditionalButtonGroup);//设置默认菜单
                        conditionalMenuList.Add(conditionalButtonGroup);
                    }
                    result.conditionalmenu = conditionalMenuList;
                }
            }
            catch (Exception ex)
            {
                throw new WeixinMenuException(ex.Message, ex);
            }
            return(result);
        }
Exemplo n.º 8
0
        public ActionResult CreateMenu(MPMenuViewModel request)
        {
            string            token         = GetToken(); // request.token;
            GetMenuResultFull resultFull    = request.ResultFull;
            MenuMatchRule     menuMatchRule = request.MenuMatchRule;

            var useAddCondidionalApi = menuMatchRule != null && !menuMatchRule.CheckAllNull();
            var apiName = string.Format("使用接口:{0}。", (useAddCondidionalApi ? "个性化菜单接口" : "普通自定义菜单接口"));

            try
            {
                //重新整理按钮信息
                WxJsonResult     result      = null;
                IButtonGroupBase buttonGroup = null;
                if (useAddCondidionalApi)
                {
                    //个性化接口
                    buttonGroup = Senparc.Weixin.MP.CommonAPIs.CommonApi.GetMenuFromJsonResult(resultFull, new ConditionalButtonGroup()).menu;

                    var addConditionalButtonGroup = buttonGroup as ConditionalButtonGroup;
                    addConditionalButtonGroup.matchrule = menuMatchRule;
                    result   = Senparc.Weixin.MP.CommonAPIs.CommonApi.CreateMenuConditional(token, addConditionalButtonGroup);
                    apiName += string.Format("menuid:{0}。", (result as CreateMenuConditionalResult).menuid);
                }
                else
                {
                    //普通接口
                    buttonGroup = Senparc.Weixin.MP.CommonAPIs.CommonApi.GetMenuFromJsonResult(resultFull, new ButtonGroup()).menu;
                    result      = Senparc.Weixin.MP.CommonAPIs.CommonApi.CreateMenu(token, buttonGroup);
                }

                var json = new
                {
                    Success = result.errmsg == "ok",
                    Message = "菜单更新成功。" + apiName
                };
                return(Json(json));
            }
            catch (Exception ex)
            {
                var json = new { Success = false, Message = string.Format("更新失败:{0}。{1}", ex.Message, apiName) };
                return(Json(json));
            }
        }
Exemplo n.º 9
0
        public async Task PushMenuAsync(string accessTokenOrAppId, int siteId)
        {
            var resultFull = new GetMenuResultFull
            {
                menu = new MenuFull_ButtonGroup
                {
                    button = new List <MenuFull_RootButton>()
                }
            };

            var openMenus = await _wxMenuRepository.GetMenusAsync(siteId);

            foreach (var firstMenu in openMenus.Where(x => x.ParentId == 0))
            {
                var root = new MenuFull_RootButton
                {
                    name       = firstMenu.Text,
                    type       = firstMenu.MenuType.GetValue(),
                    url        = firstMenu.Url,
                    key        = firstMenu.Key,
                    sub_button = new List <MenuFull_RootButton>()
                };
                foreach (var child in openMenus.Where(x => x.ParentId == firstMenu.Id))
                {
                    root.sub_button.Add(new MenuFull_RootButton
                    {
                        name = child.Text,
                        type = child.MenuType.GetValue(),
                        url  = child.Url,
                        key  = child.Key
                    });
                }

                resultFull.menu.button.Add(root);
            }

            var buttonGroup = CommonApi.GetMenuFromJsonResult(resultFull, new ButtonGroup()).menu;
            var result      = CommonApi.CreateMenu(accessTokenOrAppId, buttonGroup);

            if (result.errmsg != "ok")
            {
                throw new Exception(result.errmsg);
            }
        }
Exemplo n.º 10
0
        /// <summary>
        /// 使用Json数据更新菜单
        /// </summary>
        /// <param name="fullJson">json数据</param>
        /// <returns></returns>
        public ActionResult CreateMenuFromJson(string fullJson)
        {
            try
            {
                GetMenuResultFull resultFull = Newtonsoft.Json.JsonConvert.DeserializeObject <GetMenuResultFull>(fullJson);

                //重新整理按钮信息
                WxJsonResult     result      = null;
                IButtonGroupBase buttonGroup = null;

                buttonGroup = CommonApi.GetMenuFromJsonResult(resultFull, new ButtonGroup()).menu;
                result      = CommonApi.CreateMenu(AccessTokenContainer.TryGetAccessToken(AppId, Secret), buttonGroup);
                return(Json(Model.SysEnum.成功, "菜单更新成功"));
            }
            catch (Exception ex)
            {
                return(Json(Model.SysEnum.失败, string.Format("更新失败:{0}", ex.Message)));
            }
        }
Exemplo n.º 11
0
 public ActionResult CreateMenu(string token, GetMenuResultFull resultFull)
 {
     try
     {
         //重新整理按钮信息
         var bg     = CommonApi.GetMenuFromJsonResult(resultFull).menu;
         var result = CommonApi.CreateMenu(token, bg);
         var json   = new
         {
             Success = result.errmsg == "ok",
             Message = result.errmsg
         };
         return(Json(json));
     }
     catch (Exception ex)
     {
         var json = new { Success = false, Message = ex.Message };
         return(Json(json));
     }
 }
Exemplo n.º 12
0
 public ActionResult CreateMenu(string token, GetMenuResultFull resultFull)
 {
     try
     {
         //重新整理按钮信息
         var bg = CommonAPIs.CommonApi.GetMenuFromJsonResult(resultFull).menu;
         var result = CommonAPIs.CommonApi.CreateMenu(token, bg);
         var json = new
         {
             Success = result.errmsg == "ok",
             Message = result.errmsg
         };
         return Json(json);
     }
     catch (Exception ex)
     {
         var json = new { Success = false, Message = ex.Message };
         return Json(json);
     }
 }
Exemplo n.º 13
0
        public ActionResult CreateMenu(GetMenuResultFull resultFull, MenuMatchRule menuMatchRule)
        {
            if (string.IsNullOrEmpty(_wxAccount.AppId) || string.IsNullOrEmpty(_wxAccount.Secret))
            {
                return(Json(new { error = "AppId或AppSecret为空" }, JsonRequestBehavior.AllowGet));
            }

            var token = AccessTokenContainer.TryGetAccessToken(_wxAccount.AppId, _wxAccount.Secret);

            var useAddCondidionalApi = menuMatchRule != null && !menuMatchRule.CheckAllNull();
            var apiName = string.Format("使用接口:{0}。", (useAddCondidionalApi ? "个性化菜单接口" : "普通自定义菜单接口"));

            try
            {
                //重新整理按钮信息
                WxJsonResult     result      = null;
                IButtonGroupBase buttonGroup = null;
                if (useAddCondidionalApi)
                {
                    //个性化接口
                    buttonGroup = CommonApi.GetMenuFromJsonResult(resultFull, new ConditionalButtonGroup()).menu;

                    var addConditionalButtonGroup = buttonGroup as ConditionalButtonGroup;
                    addConditionalButtonGroup.matchrule = menuMatchRule;
                    result   = CommonApi.CreateMenuConditional(token, addConditionalButtonGroup);
                    apiName += string.Format("menuid:{0}。", (result as CreateMenuConditionalResult).menuid);
                }
                else
                {
                    //普通接口
                    buttonGroup = CommonApi.GetMenuFromJsonResult(resultFull, new ButtonGroup()).menu;
                    result      = CommonApi.CreateMenu(token, buttonGroup);
                }

                return(Json(new { success = result.errmsg == "ok", message = "菜单更新成功。" + apiName }));
            }
            catch (Exception ex)
            {
                return(Json(new { success = false, message = string.Format("更新失败:{0}。{1}", ex.Message, apiName) }));
            }
        }
Exemplo n.º 14
0
        /// <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);
        }
        /// <summary>
        /// 更新自定义菜单(默认菜单)
        /// </summary>
        /// <param name="appId">微信appId</param>
        /// <param name="menuFull_Button">自定义菜单组</param>
        /// <returns></returns>
        private async Task UpdateDefaultMenu(string appId, MenuFull_ButtonGroup menuFull_Button)
        {
            await _wechatAppConfigManager.RegisterWechatApp(appId);

            try
            {
                var resultFull = new GetMenuResultFull()
                {
                    menu = menuFull_Button
                };

                // 数据校验
                var buttonGroup = CommonApi.GetMenuFromJsonResult(resultFull, new ButtonGroup()).menu;
                // 立即创建
                var result = CommonApi.CreateMenu(appId, buttonGroup);
            }
            catch (UserFriendlyException e)
            {
                throw e;
            }
        }
Exemplo n.º 16
0
        public ActionResult CreateMenu(string token, GetMenuResultFull resultFull, MenuMatchRule menuMatchRule)
        {
                var useAddCondidionalApi = menuMatchRule != null && !menuMatchRule.CheckAllNull();
            var apiName = string.Format("使用接口:{0}。" , (useAddCondidionalApi ? "个性化菜单接口" : "普通自定义菜单接口"));
            try
            {
                //重新整理按钮信息
                WxJsonResult result = null;
                IButtonGroupBase buttonGroup = null;
                if (useAddCondidionalApi)
                {
                    //个性化接口
                    buttonGroup = CommonAPIs.CommonApi.GetMenuFromJsonResult(resultFull, new ConditionalButtonGroup()).menu;

                    var addConditionalButtonGroup = buttonGroup as ConditionalButtonGroup;
                    addConditionalButtonGroup.matchrule = menuMatchRule;
                    result = CommonAPIs.CommonApi.CreateMenuConditional(token, addConditionalButtonGroup);
                    apiName += string.Format("menuid:{0}。", (result as CreateMenuConditionalResult).menuid);
                }
                else
                {
                    //普通接口
                    buttonGroup = CommonAPIs.CommonApi.GetMenuFromJsonResult(resultFull,new ButtonGroup()).menu;
                    result = CommonAPIs.CommonApi.CreateMenu(token, buttonGroup);
                }

                var json = new
                {
                    Success = result.errmsg == "ok",
                    Message = "菜单更新成功。"+ apiName
                };
                return Json(json);
            }
            catch (Exception ex)
            {
                var json = new { Success = false, Message =string.Format("更新失败:{0}。{1}",ex.Message, apiName) };
                return Json(json);
            }
        }
Exemplo n.º 17
0
        private bool SyncMenu(GetMenuResultFull resultFull, AccountInfo accountInfo, out string errorMessage)
        {
            var isSync = false;

            errorMessage = string.Empty;

            var bg          = CommonApi.GetMenuFromJsonResult(resultFull).menu;
            var accessToken = MPUtils.GetAccessToken(accountInfo);
            var result      = CommonApi.CreateMenu(accessToken, bg);

            if (result.errmsg == "ok")
            {
                isSync = true;
            }
            else
            {
                isSync       = false;
                errorMessage = result.errmsg;
            }

            return(isSync);
        }
        /// <summary>
        /// 创建个性化菜单
        /// </summary>
        /// <param name="appId">微信appId</param>
        /// <param name="menuFull_Button">菜单数据定义</param>
        /// <param name="menuMatchRule">个性化匹配规则</param>
        /// <returns></returns>
        private async Task CreateConditionalMenu(string appId, MenuFull_ButtonGroup menuFull_Button, MenuMatchRule menuMatchRule)
        {
            await _wechatAppConfigManager.RegisterWechatApp(appId);

            try
            {
                var resultFull = new GetMenuResultFull()
                {
                    menu = menuFull_Button
                };

                // 数据校验
                var buttonGroup = CommonApi.GetMenuFromJsonResult(resultFull, new ConditionalButtonGroup()).menu;
                // 附加规则
                var addConditionalButtonGroup = buttonGroup as ConditionalButtonGroup;
                addConditionalButtonGroup.matchrule = menuMatchRule;
                // 立即创建
                var result = CommonApi.CreateMenuConditional(appId, addConditionalButtonGroup);
            }
            catch (UserFriendlyException e)
            {
                throw e;
            }
        }
Exemplo n.º 19
0
        public ActionResult CreateMenu(string token, GetMenuResultFull resultFull, MenuMatchRule menuMatchRule)
        {
            bool   flag = (menuMatchRule != null) && !menuMatchRule.CheckAllNull();
            string str  = "使用接口:" + (flag ? "个性化菜单接口" : "普通自定义菜单接口") + "。";

            try
            {
                WxJsonResult     result     = null;
                IButtonGroupBase buttonData = null;
                if (flag)
                {
                    ConditionalButtonGroup menu = CommonApi.GetMenuFromJsonResult(resultFull, new ConditionalButtonGroup()).menu as ConditionalButtonGroup;
                    menu.matchrule = menuMatchRule;
                    result         = CommonApi.CreateMenuConditional(token, menu, 0x2710);
                    str            = str + "menuid:" + (result as CreateMenuConditionalResult).menuid + "。";
                }
                else
                {
                    buttonData = CommonApi.GetMenuFromJsonResult(resultFull, new ButtonGroup()).menu;
                    result     = CommonApi.CreateMenu(token, buttonData, 0x2710);
                }
                var data = new {
                    Success = result.errmsg == "ok",
                    Message = "菜单更新成功。" + str
                };
                return(base.Json(data));
            }
            catch (Exception exception)
            {
                var type2 = new {
                    Success = false,
                    Message = "更新失败:" + exception.Message + "。" + str
                };
                return(base.Json(type2));
            }
        }
Exemplo n.º 20
0
        /// <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;
        }
Exemplo n.º 21
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 (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);
        }
Exemplo n.º 22
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);
        }
Exemplo n.º 23
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);
        }
Exemplo n.º 24
0
        public void Sync_OnClick(object sender, EventArgs e)
        {
            if (Page.IsPostBack && Page.IsValid)
            {
                try
                {
                    var isSync       = false;
                    var errorMessage = string.Empty;

                    var accountInfo = WeiXinManager.GetAccountInfo(PublishmentSystemID);

                    if (WeiXinManager.IsBinding(accountInfo))
                    {
                        var resultFull = new GetMenuResultFull();
                        resultFull.menu        = new MenuFull_ButtonGroup();
                        resultFull.menu.button = new List <MenuFull_RootButton>();

                        var publishmentSystemUrl = PageUtils.AddProtocolToUrl(PageUtility.GetPublishmentSystemUrl(PublishmentSystemInfo, string.Empty));

                        var menuInfoList = DataProviderWX.MenuDAO.GetMenuInfoList(PublishmentSystemID, 0);
                        foreach (var menuInfo in menuInfoList)
                        {
                            var rootButton = new MenuFull_RootButton();
                            rootButton.name = menuInfo.MenuName;

                            rootButton.sub_button = new List <MenuFull_RootButton>();
                            var subMenuInfoList = DataProviderWX.MenuDAO.GetMenuInfoList(PublishmentSystemID, menuInfo.MenuID);

                            if (subMenuInfoList.Count > 0)
                            {
                                foreach (var subMenuInfo in subMenuInfoList)
                                {
                                    var subButton = new MenuFull_RootButton();

                                    var isExists = false;

                                    subButton.name = subMenuInfo.MenuName;
                                    if (subMenuInfo.MenuType == EMenuType.Site)
                                    {
                                        var pageUrl = string.Empty;
                                        if (subMenuInfo.ContentID > 0)
                                        {
                                            var tableStyle = NodeManager.GetTableStyle(PublishmentSystemInfo, subMenuInfo.ChannelID);
                                            var tableName  = NodeManager.GetTableName(PublishmentSystemInfo, subMenuInfo.ChannelID);

                                            var contentInfo = DataProvider.ContentDAO.GetContentInfoNotTrash(tableStyle, tableName, subMenuInfo.ContentID);
                                            if (contentInfo != null)
                                            {
                                                pageUrl = PageUtility.GetContentUrl(PublishmentSystemInfo, contentInfo, true);
                                            }
                                        }
                                        else
                                        {
                                            var nodeInfo = NodeManager.GetNodeInfo(PublishmentSystemID, subMenuInfo.ChannelID);
                                            if (nodeInfo != null)
                                            {
                                                pageUrl = PageUtility.GetChannelUrl(PublishmentSystemInfo, nodeInfo);
                                            }
                                        }

                                        if (!string.IsNullOrEmpty(pageUrl))
                                        {
                                            isExists       = true;
                                            subButton.type = "view";
                                            subButton.url  = PageUtils.AddProtocolToUrl(pageUrl);
                                        }
                                    }
                                    else if (subMenuInfo.MenuType == EMenuType.Keyword)
                                    {
                                        if (KeywordManager.IsExists(PublishmentSystemID, subMenuInfo.Keyword))
                                        {
                                            isExists       = true;
                                            subButton.type = "click";
                                            subButton.key  = subMenuInfo.Keyword;
                                        }
                                    }
                                    else if (subMenuInfo.MenuType == EMenuType.Url)
                                    {
                                        if (!string.IsNullOrEmpty(subMenuInfo.Url))
                                        {
                                            isExists       = true;
                                            subButton.type = "view";
                                            subButton.url  = subMenuInfo.Url;
                                        }
                                    }

                                    if (!isExists)
                                    {
                                        subButton.type = "view";
                                        subButton.url  = publishmentSystemUrl;
                                    }

                                    rootButton.sub_button.Add(subButton);
                                }
                            }
                            else
                            {
                                var isExists = false;

                                if (menuInfo.MenuType == EMenuType.Site)
                                {
                                    var pageUrl = string.Empty;
                                    if (menuInfo.ContentID > 0)
                                    {
                                        var tableStyle = NodeManager.GetTableStyle(PublishmentSystemInfo, menuInfo.ChannelID);
                                        var tableName  = NodeManager.GetTableName(PublishmentSystemInfo, menuInfo.ChannelID);

                                        var contentInfo = DataProvider.ContentDAO.GetContentInfoNotTrash(tableStyle, tableName, menuInfo.ContentID);
                                        if (contentInfo != null)
                                        {
                                            pageUrl = PageUtility.GetContentUrl(PublishmentSystemInfo, contentInfo, true);
                                        }
                                    }
                                    else
                                    {
                                        var nodeInfo = NodeManager.GetNodeInfo(PublishmentSystemID, menuInfo.ChannelID);
                                        if (nodeInfo != null)
                                        {
                                            pageUrl = PageUtility.GetChannelUrl(PublishmentSystemInfo, nodeInfo);
                                        }
                                    }

                                    if (!string.IsNullOrEmpty(pageUrl))
                                    {
                                        isExists        = true;
                                        rootButton.type = "view";
                                        rootButton.url  = PageUtils.AddProtocolToUrl(pageUrl);
                                    }
                                }
                                else if (menuInfo.MenuType == EMenuType.Keyword)
                                {
                                    if (KeywordManager.IsExists(PublishmentSystemID, menuInfo.Keyword))
                                    {
                                        isExists        = true;
                                        rootButton.type = "click";
                                        rootButton.key  = menuInfo.Keyword;
                                    }
                                }
                                else if (menuInfo.MenuType == EMenuType.Url)
                                {
                                    if (!string.IsNullOrEmpty(menuInfo.Url))
                                    {
                                        isExists        = true;
                                        rootButton.type = "view";
                                        rootButton.url  = menuInfo.Url;
                                    }
                                }

                                if (!isExists)
                                {
                                    rootButton.type = "view";
                                    rootButton.url  = publishmentSystemUrl;
                                }
                            }

                            resultFull.menu.button.Add(rootButton);
                        }

                        isSync = SyncMenu(resultFull, accountInfo, out errorMessage);
                    }
                    else
                    {
                        errorMessage = "您的微信公众号未绑定,请先绑定之后同步菜单";
                    }

                    if (isSync)
                    {
                        SuccessMessage("菜单同步成功,取消关注公众账号后再次关注,可以立即看到创建后的效果");
                    }
                    else
                    {
                        FailMessage($"菜单同步失败:{errorMessage}");
                        var logInfo = new ErrorLogInfo(0, DateTime.Now, errorMessage, string.Empty, "微信同步菜单错误");
                        LogUtils.AddErrorLog(logInfo);
                    }
                }
                catch (Exception ex)
                {
                    FailMessage($"菜单同步失败:{ex.Message}");

                    var logInfo = new ErrorLogInfo(0, DateTime.Now, ex.Message, ex.StackTrace, "微信同步菜单错误");
                    LogUtils.AddErrorLog(logInfo);
                }
            }
        }
Exemplo n.º 25
0
        /// <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();

                        //fix bug 16030701  https://github.com/JeffreySu/WeiXinMPSDK/issues/169
                        conditionalButtonGroup.matchrule = conditionalMenu.matchrule;
                        conditionalButtonGroup.menuid = conditionalMenu.menuid;
                        //fix bug 16030701 end

                        GetButtonGroup(conditionalMenu.button, conditionalButtonGroup);//设置默认菜单
                        conditionalMenuList.Add(conditionalButtonGroup);
                    }
                    result.conditionalmenu = conditionalMenuList;
                }
            }
            catch (Exception ex)
            {
                throw new WeixinMenuException(ex.Message, ex);
            }
            return result;
        }
        public QyJsonResult Push(int appId)
        {
            var categories = CommonService.lstCategory.Where(x => x.AppId == appId && (x.CategoryCode == null || !x.CategoryCode.EndsWith(_endFix, true, CultureInfo.CurrentCulture)) && (x.IsDeleted == null || x.IsDeleted == false)).OrderBy(x => x.CategoryOrder).Select(x =>
            {
                var function = x.Function.IsNullOrEmpty() ? null : JsonHelper.FromJson <ButtonReturnType>(x.Function);
                return(new CategroyChild
                {
                    Id = x.Id,
                    key = x.CategoryCode,
                    name = function == null ? x.CategoryName : function.Button.name,
                    sub_button = new List <MenuFull_RootButton>(),
                    //跳转到新闻列表本身也是view, 因此在同步时将其type改为view.
                    type = function == null ? null : "view-news-list".Equals(function.Button.type) ? "view" : function.Button.type,
                    ParentId = x.ParentCode.GetValueOrDefault(),
                    order = x.CategoryOrder.GetValueOrDefault(),
                    url = function == null ? null : function.Button.url,
                });
            }).ToList();

            foreach (var category in categories)
            {
                if ("click".Equals(category.type))
                {
                    var key   = category.key.Split(':')[0];
                    int keyId = default(int);
                    if (int.TryParse(key, out keyId))
                    {
                        var autoReply = _autoReplyService.GetDetail(keyId);
                        if (autoReply == null)
                        {
                            throw new System.Exception(string.Format("{0} 的口令不存在, 请重新选择.", category.name));
                        }
                        if ((int)AutoReplyKeywordEnum.MENU == autoReply.KeywordType &&
                            null != autoReply.Keywords && autoReply.Keywords.Count > 0)
                        {
                            if ((int)AutoReplyMenuEnum.SCAN_PUSH_EVENT == autoReply.Keywords[0].SecondaryType)
                            {
                                category.type = "scancode_push";
                            }
                            else if ((int)AutoReplyMenuEnum.SCAN_WITH_PROMPT == autoReply.Keywords[0].SecondaryType)
                            {
                                category.type = "scancode_waitmsg";
                            }
                        }
                    }
                    else
                    {
                        throw new System.Exception(string.Format("请重新选择 {0} 的口令.", category.key));
                    }
                }
            }

            var buttons = new List <MenuFull_RootButton>();

            categories.Where(x => x.ParentId == 0).ToList().ForEach(x => GenerateMenuHierarchy(categories, x, buttons));

            var config = WeChatCommonService.GetWeChatConfigByID(appId);

            //修改
            if (categories != null && categories.Count > 0)
            {
                var menu = new GetMenuResultFull {
                    menu = new MenuFull_ButtonGroup {
                        button = buttons
                    }
                };
                var btnMenus = CommonApi.GetMenuFromJsonResult(menu).menu;
                if (config.IsCorp.HasValue && !config.IsCorp.Value)
                {
                    var result = Innocellence.Weixin.MP.CommonAPIs.CommonApi.CreateMenu(config.WeixinCorpId, config.WeixinCorpSecret, btnMenus);
                    return(new QyJsonResult()
                    {
                        errcode = (ReturnCode_QY)result.errcode, errmsg = result.errmsg
                    });
                }
                else
                {
                    var result = CommonApi.CreateMenu(WeChatCommonService.GetWeiXinToken(appId), int.Parse(config.WeixinAppId), btnMenus);
                    return(result);
                }
            }
            //删除
            else
            {
                if (config.IsCorp.HasValue && !config.IsCorp.Value)
                {
                    var result = Innocellence.Weixin.MP.CommonAPIs.CommonApi.DeleteMenu(config.WeixinCorpId, config.WeixinCorpSecret);
                    return(new QyJsonResult()
                    {
                        errcode = (ReturnCode_QY)result.errcode, errmsg = result.errmsg
                    });
                }
                else
                {
                    var result = CommonApi.DeleteMenu(WeChatCommonService.GetWeiXinToken(appId), int.Parse(config.WeixinAppId));
                    return(result);
                }
            }
        }
Exemplo n.º 27
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
                        {
                            //URL
                            bg.button.Add(new SingleViewButton()
                            {
                                name = rootButton.name,
                                url = rootButton.url,
                                type = rootButton.type
                            });
                        }
                    }
                    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 (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 < 2)
                {
                    throw new WeixinMenuException("一级菜单按钮至少为2个!");
                }

                result = new GetMenuResult()
                             {
                                 menu = bg
                             };
            }
            catch (Exception ex)
            {
                throw new WeixinMenuException(ex.Message, ex);
            }
            return result;
        }
Exemplo n.º 28
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 (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;
        }
Exemplo n.º 29
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 (string.IsNullOrEmpty(rootButton.name))
                    {
                        continue;//没有设置一级菜单
                    }
                    var availableSubButton = rootButton.sub_button == null ? 0 : 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 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 (string.IsNullOrEmpty(subSubButton.name))
                            {
                                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;
        }
Exemplo n.º 30
0
        private void LoadFromServer()
        {
            AccessTokenResult token             = CommonApi.GetToken(SinGooCMS.Weixin.Config.AppID, SinGooCMS.Weixin.Config.AppSecret, "client_credential");
            string            strUrl            = string.Format("https://api.weixin.qq.com/cgi-bin/menu/get?access_token={0}", token.access_token);
            string            strValue          = NetWorkUtils.HttpGet(strUrl);
            GetMenuResultFull getMenuResultFull = JsonUtils.JsonToObject <GetMenuResultFull>(strValue);

            if (getMenuResultFull != null && getMenuResultFull.menu != null && getMenuResultFull.menu.button.Count > 0)
            {
                WxMenu.EmptyLocal();
                int num = 1;
                foreach (MenuFull_RootButton current in getMenuResultFull.menu.button)
                {
                    WxMenuInfo wxMenuInfo = new WxMenuInfo
                    {
                        RootID        = 0,
                        ParentID      = 0,
                        Type          = (current.type ?? string.Empty),
                        Name          = current.name,
                        EventKey      = (current.key ?? string.Empty),
                        Url           = (current.url ?? string.Empty),
                        ChildCount    = 0,
                        ChildIDs      = string.Empty,
                        Sort          = num,
                        AutoTimeStamp = System.DateTime.Now
                    };
                    int num2 = WxMenu.Add(wxMenuInfo);
                    if (num2 > 0)
                    {
                        wxMenuInfo.AutoID = num2;
                        wxMenuInfo.RootID = num2;
                        num++;
                        if (current.sub_button != null && current.sub_button.Count > 0)
                        {
                            wxMenuInfo.ChildCount = current.sub_button.Count;
                            int num3 = 1;
                            foreach (MenuFull_RootButton current2 in current.sub_button)
                            {
                                WxMenuInfo entity = new WxMenuInfo
                                {
                                    RootID        = num2,
                                    ParentID      = num2,
                                    Type          = (current2.type ?? string.Empty),
                                    Name          = current2.name,
                                    EventKey      = (current2.key ?? string.Empty),
                                    Url           = (current2.url ?? string.Empty),
                                    ChildCount    = 0,
                                    ChildIDs      = string.Empty,
                                    Sort          = num3,
                                    AutoTimeStamp = System.DateTime.Now
                                };
                                int num4 = WxMenu.Add(entity);
                                if (num4 > 0)
                                {
                                    WxMenuInfo expr_27E = wxMenuInfo;
                                    expr_27E.ChildIDs = expr_27E.ChildIDs + num4 + ",";
                                    num3++;
                                }
                            }
                        }
                        if (wxMenuInfo.ChildIDs.EndsWith(","))
                        {
                            wxMenuInfo.ChildIDs = wxMenuInfo.ChildIDs.TrimEnd(new char[]
                            {
                                ','
                            });
                        }
                        WxMenu.Update(wxMenuInfo);
                    }
                }
            }
            this.BindData();
        }