/// <summary> /// 根据“text”生成Xml数据包 /// </summary> /// <param name="text"></param> public void SetUpAddTextMenuXmlPage(string text) { string XmlData; string Date; //生成数据包时间戳 TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1); int time = (int)ts.TotalSeconds; string Time = time.ToString(); Date = DateTime.Now.ToLocalTime().ToString(); XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; settings.NewLineOnAttributes = true; XmlWriter wxml; StringBuilder sbxml = new StringBuilder(); wxml = XmlWriter.Create(sbxml, settings); wxml.WriteStartDocument(); wxml.WriteStartElement("xml"); wxml.WriteStartElement("ToUserName"); wxml.WriteCData("mycreate"); wxml.WriteEndElement(); wxml.WriteStartElement("FromUserName"); wxml.WriteCData("wx4fbb38c93e921603"); wxml.WriteEndElement(); wxml.WriteStartElement("CreateTime"); wxml.WriteCData(Time); wxml.WriteEndElement(); wxml.WriteStartElement("MsgType"); wxml.WriteCData("text"); wxml.WriteEndElement(); wxml.WriteStartElement("Content"); wxml.WriteCData(text); wxml.WriteEndElement(); wxml.WriteStartElement("MsgId"); wxml.WriteString("121"); wxml.WriteEndElement(); wxml.WriteStartElement("AgentId"); wxml.WriteString("128"); wxml.WriteEndElement(); wxml.Close(); XmlData = sbxml.ToString(); DB db = new DB(); db.AddextMenuXmlPage("7", XmlData,Time,Date); }
/// <summary> /// 根据参数获取数据库中相应的Xml数据包 /// /// </summary> /// <param name="user"></param> /// <param name="number"></param> /// <returns></returns> public string GetFunctionXmlPage(string user, string number) { string XmlData = null; DB db = new DB(); switch (number) { case "1": XmlData = db.GetAppointFunctionXmlPage(user, "TextMenuFunction1"); break; case "2": XmlData = db.GetAppointFunctionXmlPage(user, "TextMenuFunction2"); break; case "3": XmlData = db.GetAppointFunctionXmlPage(user, "TextMenuFunction3"); break; case "4": XmlData = db.GetAppointFunctionXmlPage(user, "TextMenuFunction4"); break; case "5": XmlData = db.GetAppointFunctionXmlPage(user, "TextMenuFunction5"); break; case "6": XmlData = db.GetAppointFunctionXmlPage(user, "TextMenuFunction6"); break; case "7": XmlData = db.GetAppointFunctionXmlPage(user, "TextMenuFunction7"); break; } return XmlData; }
/// <summary> /// 根据前台获取的数据生成Function的Xml数据包按照用户(User)存入数据库 /// </summary> /// <param name="Data"></param> public void SetUpMenuTextFunctionXmlPage(string NumberData,string user, string Data) { string XmlData; string Date; //生成数据包时间戳 TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1); int time = (int)ts.TotalSeconds; string Time = time.ToString(); Date = DateTime.Now.ToLocalTime().ToString(); XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; settings.NewLineOnAttributes = true; XmlWriter wxml; StringBuilder sbxml = new StringBuilder(); wxml = XmlWriter.Create(sbxml, settings); wxml.WriteStartDocument(); wxml.WriteStartElement("xml"); wxml.WriteStartElement("ToUserName"); wxml.WriteCData("mycreate"); wxml.WriteEndElement(); wxml.WriteStartElement("FromUserName"); wxml.WriteCData("wx4fbb38c93e921603"); wxml.WriteEndElement(); wxml.WriteStartElement("CreateTime"); wxml.WriteCData(Time); wxml.WriteEndElement(); wxml.WriteStartElement("MsgType"); wxml.WriteCData("text"); wxml.WriteEndElement(); wxml.WriteStartElement("Content"); wxml.WriteCData(Data); wxml.WriteEndElement(); wxml.WriteStartElement("MsgId"); wxml.WriteString("121"); wxml.WriteEndElement(); wxml.WriteStartElement("AgentId"); wxml.WriteString("128"); wxml.WriteEndElement(); wxml.Close(); XmlData = sbxml.ToString(); DB db = new DB(); switch (NumberData) { case "功能1": db.AddMenuTextFunctionXmlPage("TextMenuFunction1", XmlData, user, Time, Date); break; case "功能2": db.AddMenuTextFunctionXmlPage("TextMenuFunction2", XmlData, user, Time, Date); break; case "功能3": db.AddMenuTextFunctionXmlPage("TextMenuFunction3", XmlData, user, Time, Date); break; case "功能4": db.AddMenuTextFunctionXmlPage("TextMenuFunction4", XmlData, user, Time, Date); break; case "功能5": db.AddMenuTextFunctionXmlPage("TextMenuFunction5", XmlData, user, Time, Date); break; case "功能6": db.AddMenuTextFunctionXmlPage("TextMenuFunction6", XmlData, user, Time, Date); break; case "功能7": db.AddMenuTextFunctionXmlPage("TextMenuFunction7", XmlData, user, Time, Date); break; } }
/// <summary> /// 测试函数 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Button_Click_DB(object sender, EventArgs e) { DB db = new DB(); db.ConnectDB(); }
/// <summary> /// 触发后向数据库提交新添加用户的数据 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Buttton_Click_AddUser(object sender, EventArgs e) { string a; string b; System.Collections.Specialized.NameValueCollection nc = new System.Collections.Specialized.NameValueCollection(Request.Form); Response.Write(nc.GetValues("AddUser")[0].ToString()); Response.Write(nc.GetValues("User")[0].ToString()); a = nc.GetValues("AddUser")[0].ToString(); b = nc.GetValues("User")[0].ToString(); DB db = new DB(); db.AddUser(a, b); }
protected void Button_Click_Up(object sender, EventArgs e) { DB db = new DB(); //db.ChangeData(); db.DelectData(); }
/// <summary> /// 网页前端通过调用此函数,可以上传多媒体获得“media_id” /// </summary> /// <param name="filepath"></param> /// <param name="access_token"></param> /// <param name="type"></param> public void Uploadfile(string filepath, string access_token, string type) { using (WebClient client = new WebClient()) { // byte[] b = client.UploadFile(string.Format("http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token={0}&type={1}", access_token, type), filepath); byte[] b = client.UploadFile(string.Format("https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token={0}&type={1}", access_token, type), filepath); string repData = Encoding.Default.GetString(b); //*********************************************************************** //调用Json类,解析微信服务器回送的Json数据包 //json_data h = JsonConvert.DeserializeObject<json_data>(repData); // string jsonStr = JsonConvert.SerializeObject(h); //string 转Json字符串 //************************************************************************ //将获取的媒体media_id存入指定的文件中 if (repData.Contains("media_id")) { if (repData.Contains("image")) { string a_media; json_data Data = JsonConvert.DeserializeObject<json_data>(repData); a_media = Data.media_id; DB a = new DB(); a.AddMediaID("ImageMediaID", a_media, "User"); StreamWriter sw = new StreamWriter(@"E:\程序库\repData_image.txt"); sw.WriteLine(a_media); sw.Flush(); sw.Close(); } if (repData.Contains("video")) { string a_media; json_data Data = JsonConvert.DeserializeObject<json_data>(repData); a_media = Data.media_id; DB a = new DB(); a.AddMediaID("VideoMediaID", a_media, "User"); StreamWriter sw = new StreamWriter(@"E:\程序库\repData_video.txt"); sw.WriteLine(a_media); sw.Flush(); sw.Close(); } if (repData.Contains("voice")) { string a_media; json_data Data = JsonConvert.DeserializeObject<json_data>(repData); a_media = Data.media_id; DB a = new DB(); a.AddMediaID("VoiceMediaID", a_media, "User"); StreamWriter sw = new StreamWriter(@"E:\程序库\repData_voice.txt"); sw.WriteLine(a_media); sw.Flush(); sw.Close(); } } } }
/// <summary> /// 通过判断事件类型进行消息的回复 /// </summary> /// <param name="ReqTime"></param> /// <param name="ReqNonce"></param> /// <param name="Msg"></param> /// <param name="choose"></param> public void Reply_Msg(string ReqTime, string ReqNonce, string Msg, string choose) { string sToken = "D2op9SR8HV4U5vVL7p1bKFm2fjLdXV8"; string sCorpID = "wx4fbb38c93e921603"; string sEncodingAESKey = "vQrIN6MevZ2RJekY79C0RC19Fe4KNcvI6UM4IQvGeWq"; Tencent.WXBizMsgCrypt wxcpt = new Tencent.WXBizMsgCrypt(sToken, sEncodingAESKey, sCorpID); string sReqTimeStamp = ReqTime; string sReqNonce = ReqNonce; string respdata = ""; if (Choose_MsgType == "event")//事件处理 { if (Choose_Event == "click")//菜单点击事件的回复 { if (choose == "Soft_01") { respdata = "<xml><ToUserName><![CDATA[mycreate]]></ToUserName>" + "<FromUserName><![CDATA[!]]></FromUserName>" + "<CreateTime>1348831860</CreateTime>" + "<MsgType><![CDATA[text]]></MsgType>" + "<Content><![CDATA[软件工程]]></Content>" + "<MsgId>1234567890123456</MsgId>" + "<AgentID>128</AgentID></xml>"; } if (choose == "Math_01") { /* ServiceApi a = new ServiceApi(); a.Text_Service(); string text; StreamReader sr = new StreamReader(@"E:\程序库\Text_Service.txt"); text = sr.ReadToEnd(); sr.Close(); */ DB db = new DB(); respdata = db.GetTextMenuXmlPage(); } if (choose == "Electrical_01") { if (Choose_FromUserName == "BB") { DB db = new DB(); respdata = db.GetAppointUserXmlPage("BB"); } if (Choose_FromUserName == "Keybord") { DB db = new DB(); respdata = db.GetAppointUserXmlPage("Keybord"); } if (Choose_FromUserName == "Panda") { DB db = new DB(); respdata = db.GetAppointUserXmlPage("Panda"); } } if (choose == "China_01") { respdata = "<xml><ToUserName><![CDATA[mycreate]]></ToUserName>" + "<FromUserName><![CDATA[wx582396d3bd56c7]]></FromUserName>" + "<CreateTime>1348831860</CreateTime>" + "<MsgType><![CDATA[text]]></MsgType>" + "<Content><![CDATA[汉语言文学]]></Content>" + "<MsgId>1234567890123456</MsgId>" + "<AgentID>128</AgentID></xml>"; } } } if (Choose_MsgType == "text")//文本消息的回复 { /* StreamReader sr = new StreamReader(@"E:\程序库\repData_video.txt"); RepData = sr.ReadLine(); sr.Close(); respdata = "<xml><ToUserName><![CDATA[mycreate]]></ToUserName>"+ "<FromUserName><![CDATA[wx582396d3bd56c7]]></FromUserName>"+ "<CreateTime>1357290913</CreateTime><MsgType><![CDATA[video]]></MsgType>"+ "<Video><MediaId>"+ "<![CDATA["+RepData+"]]>"+ "</MediaId>"+ "<Title><![CDATA[细节]]></Title><Description><![CDATA[习大大]]></Description></Video></xml>"; */ if (Choose_FromUserName == "BB") { ServiceApi sap = new ServiceApi(); respdata = sap.GetFunctionXmlPage("BB", Choose_Text); } if (Choose_FromUserName == "Keybord") { ServiceApi sap = new ServiceApi(); respdata = sap.GetFunctionXmlPage("Keybord", Choose_Text); } if (Choose_FromUserName == "Panda") { ServiceApi sap = new ServiceApi(); respdata = sap.GetFunctionXmlPage("Panda", Choose_Text); } } if (Choose_MsgType == "image")//图片消息的回复 { /* StreamReader sr = new StreamReader(@"E:\程序库\repData_image.txt"); RepData = sr.ReadLine(); sr.Close(); */ DB db = new DB(); RepData = db.GetImageMedia("BB"); respdata = "<xml><ToUserName><![CDATA[wx4fbb38c93e921603]]></ToUserName>" + "<FromUserName><![CDATA[Keybord]]></FromUserName>" + "<CreateTime>1348831860</CreateTime><MsgType><![CDATA[image]]></MsgType>" + "<Image><MediaId>" + "<![CDATA[" + RepData + "]]>" + "</MediaId></Image></xml>"; } if (Choose_MsgType == "voice")//语音消息的回复 { /* StreamReader sr = new StreamReader(@"E:\程序库\repData_voice.txt"); RepData = sr.ReadLine(); sr.Close(); */ DB db = new DB(); RepData = db.GetVoideMedia("User");//调用函数获得音频MediaID respdata = "<xml><ToUserName><![CDATA[wx4fbb38c93e921603]]></ToUserName>" + "<FromUserName><![CDATA[Keybord]]></FromUserName>" + "<CreateTime>1357290913</CreateTime><MsgType><![CDATA[voice]]></MsgType>" + "<Voice><MediaId>" + "<![CDATA[" + RepData + "]]>" + "</MediaId></Voice></xml>"; } string sEncryptMsg = ""; //xml格式的密文 int ret = 0; ret = wxcpt.EncryptMsg(respdata, sReqTimeStamp, sReqNonce, ref sEncryptMsg); if (ret != 0) { System.Console.WriteLine("ERR: EncryptMsg Fail, ret: " + ret); return; } // TODO: // 加密成功,企业需要将加密之后的sEncryptMsg返回 //HttpUtils.SetResponse(sEncryptMsg); return; }
//前端调用该方法来获得Access_token; /// <summary> /// 网页前端,一件获取Access_token /// </summary> public void Get_Access_token() { createMenu Get_access = new createMenu(); Access_tok = Get_access.Get_Access_token("wx4fbb38c93e921603", "O1Ftul_DfvuGJwzZOmUKfGCGJBollEvDk-RqFjxuthumfN3HDbpnareA8mK7LShA"); DB a = new DB(); a.AddAccessToken(Access_tok); }