Ejemplo n.º 1
0
 /// <summary>
 /// 创建菜单
 /// </summary>
 /// <param name="accessToken"></param>
 /// <param name="buttonData"></param>
 /// <returns></returns>
 public static WxJsonResult CreateMenu(string accessToken, ButtonGroup buttonData)
 {
     var urlFormat = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token={0}";
     return CommonJsonSend.Send(accessToken, urlFormat, buttonData);
 }
Ejemplo n.º 2
0
        public JsonResult syncMenuOnline(List<List<_poseMenu>> menus)
        {
            WxJsonResult result = new WxJsonResult();
            ButtonGroup bg = new ButtonGroup();
            IList<BaseButton> topList = new List<BaseButton>();
            IList<SingleButton> subList = new List<SingleButton>();
            for (int i = 0; i < menus.Count; i++)
            {
                List<_poseMenu> list = menus[i].OrderBy(m => m.sort).ToList();
                //过滤掉不符合条件的

                for (int x = list.Count - 1; x >= 1; x--)
                {
                    if (string.IsNullOrEmpty(list[x].title))
                        list.RemoveAt(x);
                    if (string.IsNullOrEmpty(list[x].key) && string.IsNullOrEmpty(list[x].url))
                        list.RemoveAt(x);
                }
                if (!string.IsNullOrEmpty(list[0].title))
                {
                    if (list.Count() == 1) //没有子菜单
                    {
                        if (!string.IsNullOrEmpty(list.First().key))//click
                        {
                            SingleClickButton topSingleButton = new SingleClickButton();
                            topSingleButton.name = list.First().title;
                            topSingleButton.key = list.First().key;
                            topList.Add(topSingleButton);
                        }
                        else if (!string.IsNullOrEmpty(list[0].url))//view
                        {
                            SingleViewButton topSingleButton = new SingleViewButton();
                            topSingleButton.name = list.First().title;
                            topSingleButton.url = list.First().url;
                            topList.Add(topSingleButton);
                        }
                    }
                    else//有子菜单
                    {
                        subList = new List<SingleButton>();
                        SubButton topButton = new SubButton(list.First().title);
                        list.Remove(list.First());
                        for (int j = 0; j < list.Count(); j++)
                        {
                            if (!string.IsNullOrEmpty(list[j].url))//view
                            {
                                SingleViewButton sub = new SingleViewButton();
                                sub.name = list[j].title;
                                sub.url = list[j].url;
                                subList.Add(sub);
                            }
                            else if (!string.IsNullOrEmpty(list[j].key))//click
                            {
                                SingleClickButton sub = new SingleClickButton();
                                sub.name = list[j].title;
                                sub.key = list[j].key;
                                subList.Add(sub);
                            }
                        }
                        if (subList.Count > 0)
                        {
                            topButton.sub_button.AddRange(subList);
                            topList.Add(topButton);
                        }
                    }
                }
            }
            bg.button.AddRange(topList);
            if (bg.button.Count() > 0)
                result = WeChartAPI.CreateMenu(GetToken(), bg);
            return Json(result, JsonRequestBehavior.AllowGet);
        }
Ejemplo n.º 3
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 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 (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;
        }
Ejemplo n.º 4
0
 /// <summary>
 /// 创建菜单
 /// </summary>
 /// <param name="accessToken"></param>
 /// <param name="buttonData"></param>
 /// <returns></returns>
 public static WxJsonResult CreateMenu(string accessToken, ButtonGroup buttonData)
 {
     return CommonApi.CreateMenu(accessToken, buttonData);
 }