コード例 #1
0
        //保存
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                string error       = string.Empty;
                string accessToken = cpp.GetAccessToken(this.id, out error);
                Model.weixin_account accountModel = new BLL.weixin_account().GetModel(this.id);

                if (!string.IsNullOrEmpty(error))
                {
                    JscriptMsg(error, string.Empty);
                    return;
                }

                //重新整理按钮信息
                ButtonGroup          bg      = new ButtonGroup();
                TextBox              txtName = new TextBox();
                TextBox              txtKey  = new TextBox();
                TextBox              txtUrl  = new TextBox();
                IList <BaseButton>   topList = new List <BaseButton>();
                IList <SingleButton> subList = new List <SingleButton>();
                //菜单设置
                for (int i = 0; i < 3; i++)
                {
                    txtName = this.FindControl("txtTop" + (i + 1) + "Name") as TextBox;
                    txtKey  = this.FindControl("txtTop" + (i + 1) + "Key") as TextBox;
                    txtUrl  = this.FindControl("txtTop" + (i + 1) + "Url") as TextBox;
                    if (txtName.Text.Trim() == "")
                    {
                        // 如果名称为空,则忽略该菜单,以及下面的子菜单
                        continue;
                    }

                    subList = new List <SingleButton>();
                    TextBox txtSubName = new TextBox();
                    TextBox txtSubKey  = new TextBox();
                    TextBox txtSubUrl  = new TextBox();
                    for (int j = 0; j < 5; j++)
                    {
                        //子菜单的设置
                        txtSubName = this.FindControl("txtMenu" + (i + 1) + (j + 1) + "Name") as TextBox;
                        txtSubKey  = this.FindControl("txtMenu" + (i + 1) + (j + 1) + "Key") as TextBox;
                        txtSubUrl  = this.FindControl("txtMenu" + (i + 1) + (j + 1) + "Url") as TextBox;
                        if (txtSubName.Text.Trim() == "")
                        {
                            continue;
                        }

                        if (txtSubUrl.Text.Trim() != "")
                        {
                            SingleViewButton sub = new SingleViewButton();
                            sub.name = txtSubName.Text.Trim();
                            //2015-6-8 链接跳转加入OAuth授权
                            //删除OAuth授权
                            //sub.url = txtSubUrl.Text.Trim().Contains("https://open.weixin.qq.com/") ? txtSubUrl.Text.Trim() :
                            //    OAuth.GetAuthorizeUrl(accountModel.appid, txtSubUrl.Text.Trim(), "1", OAuthScope.snsapi_base);
                            sub.url = txtSubUrl.Text.Trim();
                            subList.Add(sub);
                        }
                        else if (txtSubKey.Text.Trim() != "")
                        {
                            SingleClickButton sub = new SingleClickButton();
                            sub.name = txtSubName.Text.Trim();
                            sub.key  = txtSubKey.Text.Trim();
                            subList.Add(sub);
                        }
                        else
                        {
                            //报错 :子菜单必须有key和name
                            JscriptMsg("二级菜单的名称和key或者url必填!", string.Empty);
                            return;
                        }
                    }

                    if (subList != null && subList.Count > 0)
                    {
                        //有子菜单, 该一级菜单是SubButton
                        if (subList.Count < 1)
                        {
                            JscriptMsg("子菜单个数必须为2至5个!", string.Empty);
                            return;
                        }
                        SubButton topButton = new SubButton(Utils.CutString(txtName.Text.Trim(), 16));
                        topButton.sub_button.AddRange(subList);
                        topList.Add(topButton);
                    }
                    else
                    {
                        // 无子菜单
                        if (txtKey.Text.Trim() == "" && txtUrl.Text.Trim() == "")
                        {
                            JscriptMsg("如无子菜单,必须填写Key或者URL值!", string.Empty);
                            return;
                        }

                        if (txtUrl.Text.Trim() != "")
                        {  //view 页面跳转
                            SingleViewButton topSingleButton = new SingleViewButton();
                            topSingleButton.name = txtName.Text.Trim();
                            topSingleButton.url  = txtUrl.Text.Trim();
                            topList.Add(topSingleButton);
                        }
                        else if (txtKey.Text.Trim() != "")
                        {
                            SingleClickButton topSingleButton = new SingleClickButton();
                            topSingleButton.name = txtName.Text.Trim();
                            topSingleButton.key  = txtKey.Text.Trim();
                            topList.Add(topSingleButton);
                        }
                    }
                }

                bg.button.AddRange(topList);
                var result = mMrg.CreateMenu(accessToken, bg);
                JscriptMsg("自定义菜单保存成功!", "menu_list.aspx");
            }
            catch (Exception ex)
            {
                JscriptMsg("出错了:" + ex.Message, "menu_list.aspx");
            }
        }
コード例 #2
0
        /// <summary>
        /// 更新菜单
        /// </summary>
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                Model.wx_userweixin weixin = GetWeiXinCode();

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

                if (error != "")
                {
                    JscriptMsg(error, "", "Error");
                    return;
                }


                //重新整理按钮信息
                ButtonGroup          bg      = new ButtonGroup();
                TextBox              txtName = new TextBox();
                TextBox              txtKey  = new TextBox();
                TextBox              txtUrl  = new TextBox();
                IList <BaseButton>   topList = new List <BaseButton>();
                IList <SingleButton> subList = new List <SingleButton>();
                #region 菜单设置
                for (int i = 0; i < 3; i++)
                {
                    txtName = this.FindControl("txtTop" + (i + 1)) as TextBox;
                    txtKey  = this.FindControl("txtTop" + (i + 1) + "Key") as TextBox;
                    txtUrl  = this.FindControl("txtTop" + (i + 1) + "Url") as TextBox;
                    if (txtName.Text.Trim() == "")
                    {
                        // 如果名称为空,则忽略该菜单,以及下面的子菜单
                        continue;
                    }


                    subList = new List <SingleButton>();
                    TextBox txtSubName = new TextBox();
                    TextBox txtSubKey  = new TextBox();
                    TextBox txtSubUrl  = new TextBox();
                    for (int j = 0; j < 5; j++)
                    {
                        #region 子菜单的设置
                        txtSubName = this.FindControl("txtMenu" + (i + 1) + (j + 1)) as TextBox;
                        txtSubKey  = this.FindControl("txtMenu" + (i + 1) + (j + 1) + "Key") as TextBox;
                        txtSubUrl  = this.FindControl("txtMenu" + (i + 1) + (j + 1) + "Url") as TextBox;
                        if (txtSubName.Text.Trim() == "")
                        {
                            continue;
                        }

                        if (txtSubUrl.Text.Trim() != "")
                        {
                            SingleViewButton sub = new SingleViewButton();
                            sub.name = txtSubName.Text.Trim();
                            sub.url  = txtSubUrl.Text.Trim();
                            subList.Add(sub);
                        }
                        else if (txtSubKey.Text.Trim() != "")
                        {
                            SingleClickButton sub = new SingleClickButton();
                            sub.name = txtSubName.Text.Trim();
                            sub.key  = txtSubKey.Text.Trim();
                            subList.Add(sub);
                        }
                        else
                        {
                            //报错 :子菜单必须有key和name
                            JscriptMsg("二级菜单的名称和key或者url必填!", "", "Error");
                            return;
                        }
                        #endregion
                    } //子菜单循环结束


                    if (subList != null && subList.Count > 0)
                    {
                        //有子菜单, 该一级菜单是SubButton
                        if (subList.Count < 1)
                        {
                            JscriptMsg("子菜单个数为2~5个!", "", "Error");
                            return;
                        }
                        SubButton topButton = new SubButton(Utils.CutString(txtName.Text.Trim(), 16));
                        topButton.sub_button.AddRange(subList);
                        topList.Add(topButton);
                    }
                    else
                    {
                        // 无子菜单
                        if (txtKey.Text.Trim() == "" && txtUrl.Text.Trim() == "")
                        {
                            JscriptMsg("若无子菜单,必须填写key或者url值!", "", "Error");
                            return;
                        }

                        if (txtUrl.Text.Trim() != "")
                        {  //view 页面跳转
                            SingleViewButton topSingleButton = new SingleViewButton();
                            topSingleButton.name = txtName.Text.Trim();
                            topSingleButton.url  = txtUrl.Text.Trim();
                            topList.Add(topSingleButton);
                        }
                        else if (txtKey.Text.Trim() != "")
                        {
                            SingleClickButton topSingleButton = new SingleClickButton();
                            topSingleButton.name = txtName.Text.Trim();
                            topSingleButton.key  = txtKey.Text.Trim();
                            topList.Add(topSingleButton);
                        }
                    }
                }
                #endregion

                bg.button.AddRange(topList);

                var result = mMrg.CreateMenu(accessToken, bg);
                JscriptMsg("菜单提交成功!", "wxMenu.aspx", "Success");
            }
            catch (Exception ex)
            {
                JscriptMsg("报错:" + ex.Message, "", "Error");
            }
        }