using LYFZ.Weixin.SDK; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms; namespace LYFZ.Software.MainBusiness.OAShopManagement { public class frmCustommenuManager:LYFZ.Software.UI.OAShopManagement.frmCustommenuManager { public int ID = 0; public string MenuData = ""; BLL.BLL_ErpMicroInterface bllInterface = new BLL.BLL_ErpMicroInterface(); public frmCustommenuManager(){ this.Load += frmCustommenuManager_Load; this.btnxiazaiMenu.Click += btnxiazaiMenu_Click; this.btnUpdateMenu.Click += btnUpdateMenu_Click; this.btnReset.Click += btnReset_Click; this.linkLabelURL.Click += linkLabelURL_Click; this.btnSaveData.Click += btnSaveData_Click; } void btnSaveData_Click(object sender, EventArgs e) { if (CustommenuConfigureModel != null) { try { UpdateCurrentMenuList(); if (SaveData()) { MessageBoxCustom.Show("当前菜单配置已保存成功"); } else { MessageBoxCustom.Show("当前菜单配置保存失败"); } } catch (Exception ex) { MessageBoxCustom.Show("当前菜单配置保存出错:" + ex.Message); } } } /// /// 保存菜单配置 /// /// bool SaveData() { if (CustommenuConfigureModel != null) { CustommenuConfigureModel.Sconfig_Value = CustomMenuAPI.GetMenuDataXML(CurrentMenuList); if (ID > 0) { LYFZ.Model.Model_ErpMicroInterface modFace = bllInterface.GetModel(ID); modFace.MenuData = CustommenuConfigureModel.Sconfig_Value; bllInterface.Update(modFace); } MenuData = CustommenuConfigureModel.Sconfig_Value; return bllMIF.UpdateMicroInterfaceCustommenuModel(CustommenuConfigureModel); } else { return false; } } void linkLabelURL_Click(object sender, EventArgs e) { try { System.Diagnostics.Process.Start(BLL.BLL_MicroInterface.GetGlobalReturnCodeURL()); } catch { } } void btnReset_Click(object sender, EventArgs e) { for (int i = 1; i <= 3; i++) { ComponentLibrary.ComboBoxEx cbxType = (LYFZ.ComponentLibrary.ComboBoxEx)this.plMenuManager.Controls["cbxmenutype" + i.ToString()]; cbxType.SelectedIndex=-1; ComponentLibrary.TextBoxEx txtevalue = (LYFZ.ComponentLibrary.TextBoxEx)this.plMenuManager.Controls[cbxType.Name.Replace("cbxmenutype", "txtevalue")]; txtevalue.Text = ""; ComponentLibrary.TextBoxEx txtmname = (LYFZ.ComponentLibrary.TextBoxEx)this.plMenuManager.Controls[cbxType.Name.Replace("cbxmenutype", "txtmname")]; txtmname.Text = ""; for (int j = 1; j <= 5; j++) { ComponentLibrary.ComboBoxEx sub_cbxType = (LYFZ.ComponentLibrary.ComboBoxEx)this.plMenuManager.Controls["cbxmenutype" + i.ToString() + "_" + j.ToString()]; sub_cbxType.SelectedIndex = -1; ComponentLibrary.TextBoxEx sub_txtevalue = (LYFZ.ComponentLibrary.TextBoxEx)this.plMenuManager.Controls[sub_cbxType.Name.Replace("cbxmenutype", "txtevalue")]; sub_txtevalue.Text = ""; ComponentLibrary.TextBoxEx sub_txtmname = (LYFZ.ComponentLibrary.TextBoxEx)this.plMenuManager.Controls[sub_cbxType.Name.Replace("cbxmenutype", "txtmname")]; sub_txtmname.Text = ""; } } } /// /// 当前菜单集合 /// List CurrentMenuList = new List(); void UpdateCurrentMenuList() { CurrentMenuList.Clear(); for (int i = 1; i <= 3; i++) { MenuModel mMode = new MenuModel(); ComponentLibrary.ComboBoxEx cbxType = (LYFZ.ComponentLibrary.ComboBoxEx)this.plMenuManager.Controls["cbxmenutype" + i.ToString()]; ComponentLibrary.TextBoxEx txtevalue = (LYFZ.ComponentLibrary.TextBoxEx)this.plMenuManager.Controls[cbxType.Name.Replace("cbxmenutype", "txtevalue")]; ComponentLibrary.TextBoxEx txtmname = (LYFZ.ComponentLibrary.TextBoxEx)this.plMenuManager.Controls[cbxType.Name.Replace("cbxmenutype", "txtmname")]; if (txtmname.Text.Trim().Length > 0) { mMode.Name = txtmname.Text.Trim(); ItemValue typeItem = (ItemValue)cbxType.SelectedItem; CustomWXMenuType menuType = (CustomWXMenuType)typeItem.Value; if (SetMenuModel(mMode, menuType, txtevalue.Text.Trim())) { for (int j = 1; j <= 5; j++) { MenuModel sub_mMode = new MenuModel(); ComponentLibrary.ComboBoxEx sub_cbxType = (LYFZ.ComponentLibrary.ComboBoxEx)this.plMenuManager.Controls["cbxmenutype" + i.ToString() + "_" + j.ToString()]; ComponentLibrary.TextBoxEx sub_txtevalue = (LYFZ.ComponentLibrary.TextBoxEx)this.plMenuManager.Controls[sub_cbxType.Name.Replace("cbxmenutype", "txtevalue")]; ComponentLibrary.TextBoxEx sub_txtmname = (LYFZ.ComponentLibrary.TextBoxEx)this.plMenuManager.Controls[sub_cbxType.Name.Replace("cbxmenutype", "txtmname")]; if (sub_txtmname.Text.Trim().Length > 0) { sub_mMode.Name = sub_txtmname.Text.Trim(); ItemValue sub_typeItem = (ItemValue)sub_cbxType.SelectedItem; CustomWXMenuType sub_menuType = (CustomWXMenuType)sub_typeItem.Value; if (SetMenuModel(sub_mMode, sub_menuType, sub_txtevalue.Text.Trim())) { mMode.Sub_buttons.Add(sub_mMode); } else { return; } } else { continue; } } CurrentMenuList.Add(mMode); } else { return; } } else { continue; } } } bool SetMenuModel(MenuModel _Model,CustomWXMenuType cMtype,string value) { if (value.Trim().Length <= 0 && cMtype!= CustomWXMenuType.Sub_button) { MessageBoxCustom.Show("请设置菜单事件值"); return false; } string[] menuTypeDescriptions = LYFZ.WinAPI.CustomPublicMethod.ConvertEnumToDescription(cMtype).Split('|'); _Model.Type = (WXMenuType)Enum.Parse(typeof(WXMenuType), menuTypeDescriptions[1]); switch (_Model.Type) { case WXMenuType.click: case WXMenuType.location_select: _Model.Key = value; break; case WXMenuType.view: _Model.Url =value; //value; break; case WXMenuType.miniprogram: string[] tempValues = value.Split('|'); if (tempValues.Length == 3) { _Model.Appid = tempValues[0]; _Model.Pagepath = tempValues[1]; _Model.Url = tempValues[2]; //value; } else { MessageBoxCustom.Show("链接小程序菜单事件值格试错误"); return false; } break; case WXMenuType.view_limited: case WXMenuType.media_id: _Model.Media_id = value; break; case WXMenuType.scancode_waitmsg: case WXMenuType.scancode_push: case WXMenuType.pic_sysphoto: case WXMenuType.pic_photo_or_album: case WXMenuType.pic_weixin: _Model.Key = value; break; case WXMenuType.sub_button: break; default: goto case WXMenuType.click; // break; } return true; } void BindMenuType() { for (int i = 1; i <= 3; i++) { ComponentLibrary.ComboBoxEx cbxType = (LYFZ.ComponentLibrary.ComboBoxEx)this.plMenuManager.Controls["cbxmenutype" + i.ToString()]; BindMenuType(cbxType); for (int j = 1; j <= 5; j++) { ComponentLibrary.ComboBoxEx sub_cbxType = (LYFZ.ComponentLibrary.ComboBoxEx)this.plMenuManager.Controls["cbxmenutype" + i.ToString()+"_"+j.ToString()]; BindMenuType(sub_cbxType); } } } void BindMenuType(ComponentLibrary.ComboBoxEx cbxType) { cbxType.Items.Clear(); foreach (int value in Enum.GetValues(typeof(LYFZ.Weixin.SDK.CustomWXMenuType))) { try { CustomWXMenuType menuType = (CustomWXMenuType)value; if (cbxType.Name.Contains("_") && menuType == CustomWXMenuType.Sub_button) { continue; } else { string[] menuTypeDescriptions = LYFZ.WinAPI.CustomPublicMethod.ConvertEnumToDescription(menuType).Split('|'); cbxType.Items.Add(new ItemValue(menuType, menuTypeDescriptions[0])); } } catch { } } cbxType.SelectedIndexChanged += cbxType_SelectedIndexChanged; cbxType.KeyUp += cbxType_KeyUp; } void cbxType_KeyUp(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Delete) { ComponentLibrary.ComboBoxEx cbxType = (LYFZ.ComponentLibrary.ComboBoxEx)sender; cbxType.SelectedIndex = -1; } } void cbxType_SelectedIndexChanged(object sender, EventArgs e) { ComponentLibrary.ComboBoxEx cbxType = (LYFZ.ComponentLibrary.ComboBoxEx)sender; ComponentLibrary.TextBoxEx txtevalue = (LYFZ.ComponentLibrary.TextBoxEx)this.plMenuManager.Controls[cbxType.Name.Replace("cbxmenutype", "txtevalue")]; if (cbxType.SelectedItem != null) { ItemValue titem = (ItemValue)cbxType.SelectedItem; CustomWXMenuType menuType = (CustomWXMenuType)titem.Value; switch (menuType) { case CustomWXMenuType.Sub_button: txtevalue.Text = ""; txtevalue.ReadOnly = true; break; case CustomWXMenuType.Click: txtevalue.ReadOnly = false; break; case CustomWXMenuType.View: txtevalue.ReadOnly = false; // txtevalue.Text = ""; break; case CustomWXMenuType.miniprogram: txtevalue.ReadOnly = false; break; /* case CustomWXMenuType.LYFZ_OrderQuery: case CustomWXMenuType.LYFZ_MobileAPP: case CustomWXMenuType.LYFZ_MemberQuery: case CustomWXMenuType.LYFZ_JCBDZH: case CustomWXMenuType.LYFZ_AccountBonding: txtevalue.ReadOnly = true; txtevalue.Text = menuType.ToString(); if (menuType == CustomWXMenuType.LYFZ_MobileAPP) { txtevalue.Text = LYFZ.BLL.BLL_MicroInterface.GetWebAPPGoToUrl(LYFZ.Software.MainBusiness.CommonLogical.SuccessfulLogin.SoftwareInfo.SDomainName.ToString()); } break;*/ default: txtevalue.ReadOnly = false; break; } } else { txtevalue.ReadOnly = false; } } LYFZ.BLL.BLL_MicroInterface bllMIF = new BLL.BLL_MicroInterface(); //LYFZ.Model.Model_MicroInterface modelMIF = new Model.Model_MicroInterface(); LYFZ.Model.Model_ErpMicroInterface modelMIF = new LYFZ.Model.Model_ErpMicroInterface(); void btnUpdateMenu_Click(object sender, EventArgs e) { if (MessageBoxCustom.Show("确定要上传自定义菜单到服务器并替换吗?", msgBoxButton: MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes) { string updateMenuMsg = ""; foreach (MenuModel mMdel in CurrentMenuList) { if (mMdel.Type == WXMenuType.sub_button && mMdel.Sub_buttons.Count <= 0) { MessageBoxCustom.Show(mMdel.Name + " 菜单类型为“含有子菜单”,但实际含有子菜单为0个,请设置完整后重试。"); return; } } UpdateCurrentMenuList(); LYFZ.ComponentLibrary.FrmLoadHandling.LoadDoWorkMethod(delegate(object obj, System.ComponentModel.BackgroundWorker backgroundWorker) { try { backgroundWorker.ReportProgress(0, "正在保存菜单配置..."); if (!SaveData()) { MessageBoxCustom.Show("当前菜单配置保存失败,请重试"); return; } backgroundWorker.ReportProgress(0, "正在上传菜单..."); string menudata =CustomMenuAPI.GetMenuDataString(CurrentMenuList); if (CurrentMenuList.Count <= 0) { if (MessageBoxCustom.Show("当前没有任何自定义菜单项,如果继续上传将删除服务器上的菜单?", msgBoxButton: MessageBoxButtons.YesNo, backgroundWorker: backgroundWorker) == System.Windows.Forms.DialogResult.Yes) { updateMenuMsg = LYFZ.WinAPI.CustomPublicMethod.HttpWebRequestGET("http://wx.lyfz.net/WeiXinAPP/WXServiceHandler.aspx?type=delmenu&appid=" + modelMIF.AppID.Trim(), Encoding.UTF8); } else { updateMenuMsg = ""; } } else { updateMenuMsg = LYFZ.WinAPI.CustomPublicMethod.HttpWebRequestPOST("http://wx.lyfz.net/WeiXinAPP/WXServiceHandler.aspx?type=addmenu&appid=" + modelMIF.AppID.Trim(), menudata, Encoding.UTF8); } } catch (Exception ex) { updateMenuMsg = "上传菜单时错误:" + ex.Message; } }); if (updateMenuMsg.Contains("\"errmsg\":\"ok\"")) { MessageBoxCustom.Show("菜单上传成功"); } else if (updateMenuMsg.Trim().Length>0) { if (updateMenuMsg.Contains("40001")) { LYFZ.Weixin.SDK.Helpers.TokenHelper.ClearAccessToken(); } MessageBoxCustom.Show(updateMenuMsg); } } } void btnxiazaiMenu_Click(object sender, EventArgs e) { string getMenuMsg = "";//CustomMenuAPI.GetTempXmlString(); LYFZ.ComponentLibrary.FrmLoadHandling.LoadDoWorkMethod(delegate(object obj, System.ComponentModel.BackgroundWorker backgroundWorker) { try { backgroundWorker.ReportProgress(0, "正在下载菜单..."); //modelMIF = bllMIF.Model; /* modelMIF = bllMIF.Model; mConfig = new LYFZ.BLL.BLL_Config().GetModel_SysConfig(); int PortNumber = LYFZ.Software.MainBusiness.CommonLogical.SuccessfulLogin.SoftwareInfo.WebServerPort; if (!LYFZ.WinAPI.CustomPublicMethod.CheckInternaNetworkAddress(mConfig.ServerHostOrIP) && LYFZ.Software.MainBusiness.CommonLogical.SuccessfulLogin.SoftwareInfo.YS_EnableMapped) { PortNumber = LYFZ.Software.MainBusiness.CommonLogical.SuccessfulLogin.SoftwareInfo.YS_tAppport; }*/ getMenuMsg = LYFZ.WinAPI.CustomPublicMethod.HttpWebRequestGET("http://wx.lyfz.net/WeiXinAPP/WXServiceHandler.aspx?type=getmenu&appid=" + modelMIF.AppID.Trim(), Encoding.UTF8); } catch (Exception ex) { getMenuMsg = "获取菜单时错误:"+ex.Message; } }); string menuXmlString = ""; try { System.Xml.XmlDocument xml = Newtonsoft.Json.JsonConvert.DeserializeXmlNode(getMenuMsg); menuXmlString = xml.InnerXml; } catch(Exception ex) { if (getMenuMsg.ToLower().IndexOf("{\"menu") == 0) { getMenuMsg = String.Format("获取菜单时错误:不支持的个性化菜单,{0}", ex.Message); } else { getMenuMsg = String.Format("获取菜单时错误:{0} {1}", ex.Message, getMenuMsg); } } if (getMenuMsg.Contains("获取菜单时错误:")) { if (getMenuMsg.Contains("40001")) { // LYFZ.Weixin.SDK.Helpers.TokenHelper.ClearAccessToken(); MessageBoxCustom.Show("获取access_token时AppSecret错误,或者access_token无效"); } else if (getMenuMsg.Contains("46003")) { MessageBoxCustom.Show("代码:“46003”,当前公众号还没有创建自定义菜单项,请添加自定义菜单后点击“上传菜单”即可。"); } else if (getMenuMsg.Contains("48001")) { MessageBoxCustom.Show("代码:“48001”,当前公众号 API 功能未授权,请确认公众号已获得该接口,可以在公众平台官网-开发者中心页中查看接口权限"); } else if (getMenuMsg.Contains("40155")) { MessageBoxCustom.Show("代码:“40155”,请勿添加其他公众号的主页链接,请对 “http://mp.weixin.qq.com/mp/”这样的公众号页面网址做网址缩短处理"); } else { MessageBoxCustom.Show(getMenuMsg); } } else { if (menuXmlString.Trim().Length > 0) { bindMenuTrrViewList(menuXmlString); MessageBoxCustom.Show("自定义菜单下载成功"); } } } void bindMenuTrrViewList(string menuXmlStr) { if (menuXmlStr.Trim().Length > 0) { System.Xml.XmlDocument xml = new System.Xml.XmlDocument(); try { xml.LoadXml(menuXmlStr); CurrentMenuList = CustomMenuAPI.GetMenuModelList(xml); bindMenuTrrViewList(); } catch (Exception ex) { MessageBoxCustom.Show("解析菜单时出错:" + ex.Message); } } } /// /// 帮定菜单树结构 /// void bindMenuTrrViewList() { for (int i = 1; i <= 3; i++) { ComponentLibrary.ComboBoxEx cbxType = (LYFZ.ComponentLibrary.ComboBoxEx)this.plMenuManager.Controls["cbxmenutype" + i.ToString()]; cbxType.SelectedIndex = -1; ComponentLibrary.TextBoxEx txtevalue = (LYFZ.ComponentLibrary.TextBoxEx)this.plMenuManager.Controls[cbxType.Name.Replace("cbxmenutype", "txtevalue")]; txtevalue.Text = ""; ComponentLibrary.TextBoxEx txtmname = (LYFZ.ComponentLibrary.TextBoxEx)this.plMenuManager.Controls[cbxType.Name.Replace("cbxmenutype", "txtmname")]; txtmname.Text = ""; for (int j = 1; j <= 5; j++) { ComponentLibrary.ComboBoxEx sub_cbxType = (LYFZ.ComponentLibrary.ComboBoxEx)this.plMenuManager.Controls["cbxmenutype" + i.ToString() + "_" + j.ToString()]; sub_cbxType.SelectedIndex = -1; ComponentLibrary.TextBoxEx sub_txtevalue = (LYFZ.ComponentLibrary.TextBoxEx)this.plMenuManager.Controls[sub_cbxType.Name.Replace("cbxmenutype", "txtevalue")]; sub_txtevalue.Text = ""; ComponentLibrary.TextBoxEx sub_txtmname = (LYFZ.ComponentLibrary.TextBoxEx)this.plMenuManager.Controls[sub_cbxType.Name.Replace("cbxmenutype", "txtmname")]; sub_txtmname.Text = ""; } } for (int i = 0; i < CurrentMenuList.Count; i++) { MenuModel mMode = CurrentMenuList[i]; bindMenuTrrView(mMode, 1, i + 1,0); } } void bindMenuTrrView(MenuModel mMode, int level,int index,int pindex) { string cName = "cbxmenutype" + index.ToString(); if (level == 1) { cName = "cbxmenutype" + index.ToString(); } else { cName = "cbxmenutype" + pindex + "_" + index.ToString(); } ComponentLibrary.ComboBoxEx cbxType = (LYFZ.ComponentLibrary.ComboBoxEx)this.plMenuManager.Controls[cName]; ComponentLibrary.TextBoxEx txtevalue = (LYFZ.ComponentLibrary.TextBoxEx)this.plMenuManager.Controls[cbxType.Name.Replace("cbxmenutype", "txtevalue")]; ComponentLibrary.TextBoxEx txtmname = (LYFZ.ComponentLibrary.TextBoxEx)this.plMenuManager.Controls[cbxType.Name.Replace("cbxmenutype", "txtmname")]; setCbxType(cbxType, mMode); setEvAlue(txtevalue, mMode); txtmname.Text = mMode.Name; txtmname.Tag = mMode; if (mMode.Sub_buttons.Count > 0) { for (int i = 0; i < mMode.Sub_buttons.Count; i++) { bindMenuTrrView(mMode.Sub_buttons[i], 2, i + 1,index); } } } void setCbxType(ComponentLibrary.ComboBoxEx cbxType, MenuModel mMode) { // CustomWXMenuType menuType = (CustomWXMenuType)Enum.Parse(typeof(CustomWXMenuType), mMode.Type.ToString(), true); if (mMode.Sub_buttons.Count > 0) { cbxType.SelectedIndex = 0; } else { switch (mMode.Type) { case WXMenuType.sub_button: cbxType.SelectedIndex = 0; break; case WXMenuType.click: if (cbxType.Items.Count == 4) { cbxType.SelectedIndex = 1; } else { cbxType.SelectedIndex = 0; } break; case WXMenuType.view: if (cbxType.Items.Count == 4) { cbxType.SelectedIndex = 2; } else { cbxType.SelectedIndex = 1; } break; case WXMenuType.miniprogram: if (cbxType.Items.Count == 4) { cbxType.SelectedIndex = 3; } else { cbxType.SelectedIndex = 1; } break; default: if (cbxType.Items.Count == 4) { cbxType.SelectedIndex = 1; } else { cbxType.SelectedIndex = 0; } break; } } } string getEvalue(MenuModel mMode) { switch (mMode.Type) { case WXMenuType.click: case WXMenuType.location_select: return mMode.Key; //break; case WXMenuType.view: return mMode.Url; case WXMenuType.miniprogram: return mMode.Appid + "|" + mMode.Pagepath + "|" + mMode.Url; // break; case WXMenuType.view_limited: case WXMenuType.media_id: return mMode.Media_id; // break; case WXMenuType.scancode_waitmsg: case WXMenuType.scancode_push: case WXMenuType.pic_sysphoto: case WXMenuType.pic_photo_or_album: case WXMenuType.pic_weixin: return mMode.Key; // break; case WXMenuType.sub_button: return ""; // break; default: goto case WXMenuType.click; // break; } //return mMode.Key; } void setEvAlue(ComponentLibrary.TextBoxEx txtevalue, MenuModel mMode) { switch (mMode.Type) { case WXMenuType.click: case WXMenuType.location_select: txtevalue.Text = mMode.Key; break; case WXMenuType.view: txtevalue.Text = mMode.Url; break; case WXMenuType.miniprogram: txtevalue.Text = mMode.Appid + "|" + mMode.Pagepath + "|" + mMode.Url; break; case WXMenuType.view_limited: case WXMenuType.media_id: txtevalue.Text = mMode.Media_id; break; case WXMenuType.scancode_waitmsg: case WXMenuType.scancode_push: case WXMenuType.pic_sysphoto: case WXMenuType.pic_photo_or_album: case WXMenuType.pic_weixin: txtevalue.Text = mMode.Key; break; case WXMenuType.sub_button: txtevalue.Text = ""; break; default: goto case WXMenuType.click; // break; } } /// /// 当前配置的菜单数据 /// LYFZ.Model.Model_ErpSystemConfigure CustommenuConfigureModel = null; void frmCustommenuManager_Load(object sender, EventArgs e) { BindMenuType(); CustommenuConfigureModel = bllMIF.GetCustommenuModel(); string menuDataXmlString = CustommenuConfigureModel.Sconfig_Value; if (ID > 0) { modelMIF = bllInterface.GetModel(ID); menuDataXmlString = modelMIF.MenuData; MenuData = modelMIF.MenuData; } else if (ID == 0) { menuDataXmlString = ""; } if (menuDataXmlString.Trim().Length > 0) { bindMenuTrrViewList(menuDataXmlString); } } } }