Esempio n. 1
0
        /// <summary>
        /// 根据微信返回的Json数据得到可用的GetMenuResult结果
        /// </summary>
        /// <param name="resultFull"></param>
        /// <returns></returns>
        public static MainButton ToMainButton(this FullButtonGroupMenu resultFull)
        {
            //重新整理按钮信息
            var bg = new MainButton();

            foreach (var rootButton in resultFull.Menu.Button)
            {
                if (rootButton.Name == null)
                {
                    continue;                                                                            //没有设置一级菜单
                }
                var availableSubButton = rootButton.SubButton.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 Exception("单击按钮的key不能为空!");
                    }
                    bg.Button.Add(rootButton.ToRealSingleButton());
                }
                else if (availableSubButton < 2)
                {
                    throw new Exception("子菜单至少需要填写2个!");
                }
                else
                {
                    //底部二级菜单
                    var subButton = new SubButton(rootButton.Name);
                    bg.Button.Add(subButton);

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

                        if (subSubButton.Type.Equals("CLICK", StringComparison.OrdinalIgnoreCase) &&
                            string.IsNullOrEmpty(subSubButton.Key))
                        {
                            throw new Exception("单击按钮的key不能为空!");
                        }
                        subButton.SubButtons.Add(subSubButton.ToRealSingleButton());
                    }
                }
            }

            if (bg.Button.Count < 2)
            {
                throw new Exception("一级菜单按钮至少为2个!");
            }
            return(bg);
        }
Esempio n. 2
0
        /// <summary>
        /// 创建菜单
        /// </summary>
        /// <param name="accessToken"></param>
        /// <param name="mainButton"></param>
        /// <returns></returns>
        public static bool CreateMenu(string companyId, string accessToken, MainButton mainButton)
        {
            ResError result = WxHttp.Post(WxUrl.CreateMenu.ToFormat(accessToken), mainButton);

            if (result.errcode == ResCode.请求成功)
            {
                return(true);
            }
            if (result.errcode == ResCode.获取accessToken时AppSecret错误或者accessToken无效)
            {
                WX_ApiConfig config         = WXApiConfigServices.QueryWXApiConfig(companyId);
                var          newAccessToken = AccessTokenContainer.TryGetToken(config.AppId, config.AppSecret, true);
                ResError     newResult      = WxHttp.Post(WxUrl.CreateMenu.ToFormat(newAccessToken), mainButton);
                if (newResult.errcode == ResCode.请求成功)
                {
                    return(true);
                }
            }
            TxtLogServices.WriteTxtLogEx("WeiXinBase", "微信创建菜单错误!错误代码:{0},说明:{1}", (int)result.errcode, result.errmsg);
            return(false);
        }