/// <summary> /// 根据消息类型创建消息控制器 /// </summary> /// <param name="request">微信请求上下文</param> /// <returns>实现IWeiXinHandler微信消息处理控制器</returns> public IWeiXinHandler CreateWeiXinHandler(WeiXinRequest request) { IWeiXinHandler weiXinHandler = null; try { foreach (Type pluginType in PluginTypes) { WeiXinHandlerType weiXinHandlerType = Activator.CreateInstance(pluginType) as WeiXinHandlerType; if (weiXinHandlerType == null) continue; if (request.WeiXinMsgType != WeiXinMsgType.Event) { if (weiXinHandlerType.WeiXinMsgType == request.WeiXinMsgType) { weiXinHandler = weiXinHandlerType; break; } } else { if (weiXinHandlerType.WeiXinEventType == request.WeiXinEventType) { weiXinHandler = weiXinHandlerType; break; } } } } catch (Exception ex) { throw ex; } if(weiXinHandler==null) throw new WeiXinHandlerNotFoundException { ExceptionMessage = new WeiXinTextMessageEntity { ToUserName = request.FromUserName, Content = "控制器未找到异常:此消息类型控制器可能未实现!", MsgType = WeiXinMsgType.Text.ToString().ToLower() }.GetXElement().ToString() }; return weiXinHandler; }
/// <summary> /// 构造函数,把请求消息封装到微信请求/响应上下文属性 /// </summary> /// <param name="requestXml">请求消息xml</param> public WeiXinContext(XElement requestXml) { Request=new WeiXinRequest(requestXml); Response=new WeiXinResponse(); }
string corpsecret = ConfigurationManager.AppSettings["corpsecret"]; //新建管理组id protected void Page_Load(object sender, EventArgs e) { string sVerifyMsgSig = HttpContext.Current.Request.QueryString["msg_signature"];//企业号的 msg_signature string sVerifyTimeStamp = HttpContext.Current.Request.QueryString["timestamp"]; string sVerifyNonce = HttpContext.Current.Request.QueryString["nonce"]; string sVerifyEchoStr = HttpContext.Current.Request.QueryString["echoStr"]; WXBizMsgCrypts wxcpt = new WXBizMsgCrypts(token, encodingAESKey, corpId); int ret = 0; string postString = string.Empty; #region if (HttpContext.Current.Request.HttpMethod.ToUpper() == "POST") { string msg = ""; using (Stream stream = HttpContext.Current.Request.InputStream) { Byte[] postBytes = new Byte[stream.Length]; stream.Read(postBytes, 0, (Int32)stream.Length); postString = Encoding.UTF8.GetString(postBytes); } if (!string.IsNullOrEmpty(postString)) { ret = wxcpt.DecryptMsg(sVerifyMsgSig, sVerifyTimeStamp, sVerifyNonce, postString, ref msg); if (ret != 0) { HttpContext.Current.Response.Write("ERR: VerifyURL fail, ret: " + ret); return; } WeiXinRequest requestXML = new WeiXinRequest(); XmlDocument doc = new XmlDocument(); doc.LoadXml(msg); XmlNode root = doc.FirstChild; requestXML.FromUserName = root["FromUserName"].InnerText; requestXML.MsgType = root["MsgType"].InnerText; if (requestXML.MsgType == "text") { requestXML.Content = root["Content"].InnerText; } if (requestXML.MsgType == "event") { requestXML.Wxevent = root["Event"].InnerText; requestXML.EventKey = root["EventKey"].InnerText; } #region string database = ""; string sql = ""; if (requestXML.MsgType == "text") { string orderid = requestXML.Content.Trim(); //订单号 string UserName = requestXML.FromUserName.Trim(); //微信号 Order order = new Order(); DataSet ds = new DataSet(); DataSet dss = select.DatabaseName(UserName);//当前数据库 if (dss != null && dss.Tables[0].Rows.Count > 0) { for (int a = 0; a < dss.Tables[0].Rows.Count; a++) { database = dss.Tables[0].Rows[a]["DataBaseName"].ToString(); if (database != "") { database = database + "."; #region ds = select.getOrder(orderid, UserName, database); //质保卡 if (ds != null) { order = select.fillorder(ds.Tables[0].Rows); sql += "产品的质保卡信息\n"; sql += "订单编号:" + order.orderId + "\n"; sql += "医院名称:" + order.hospital + "\n"; sql += "患者姓名:" + order.patient + "\n"; sql += "医生姓名:" + order.doctor + "\n"; string indte = ""; if (order.indate != null) { indte = Convert.ToDateTime(order.indate).ToString("yyyy-MM-dd"); } sql += "生产日期:" + indte + "\n"; for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { sql += "产品名称:" + ds.Tables[0].Rows[i]["itemname"].ToString() + "\n"; sql += "保修日期:" + ds.Tables[0].Rows[i]["Valid"].ToString() + "\n"; sql += "牙位如下" + "\n"; sql += "上右位:" + ds.Tables[0].Rows[i]["a_teeth"].ToString() + "\n"; sql += "上左位:" + ds.Tables[0].Rows[i]["b_teeth"].ToString() + "\n"; sql += "下右位:" + ds.Tables[0].Rows[i]["c_teeth"].ToString() + "\n"; sql += "下左位:" + ds.Tables[0].Rows[i]["d_teeth"].ToString() + "\n\n"; } } #endregion #region ds = select.getOrdered(orderid, UserName, database); //材料成分 if (ds != null) { order = select.fillorder(ds.Tables[0].Rows); sql += "产品的材料成分等可追溯信息\n"; sql += "订单编号:" + order.orderId + "\n"; sql += "医院名称:" + order.hospital + "\n"; sql += "患者姓名:" + order.patient + "\n"; sql += "医生姓名:" + order.doctor + "\n"; string indte = ""; if (order.indate != null) { indte = Convert.ToDateTime(order.indate).ToString("yyyy-MM-dd"); } sql += "生产日期:" + indte + "\n"; for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { sql += "产品名称:" + ds.Tables[0].Rows[i]["itemname"].ToString() + "\n"; sql += "材料名称:" + ds.Tables[0].Rows[i]["Name"].ToString() + "\n"; sql += "生产厂家及材料注册证号:" + ds.Tables[0].Rows[i]["Maker"].ToString() + "\n"; sql += "材料批号:" + ds.Tables[0].Rows[i]["batchNo"].ToString() + "\n\n"; } } #endregion } } } else { sql += "欢迎关注\n吉星义齿--义齿加工行业首选管理软件。\n\n没有所需的订单信息!!\n"; } if (sql == "") { sql += "欢迎关注\n吉星义齿--义齿加工行业首选管理软件。\n\n请正确输入订单号:\n根据输入订单号可以查询\n1、产品的质保卡信息\n2、产品的材料成分信息\n"; } } #endregion #region else if (requestXML.MsgType == "event") { if (requestXML.Wxevent == "unsubscribe") { //取消关注 } else { if (requestXML.Wxevent != null) { //菜单单击事件 if (requestXML.Wxevent.Equals("click")) { if (requestXML.EventKey.Equals("subkey1")) { #region string orderid = ""; int num = 0; sql = ""; DataSet ds = new DataSet(); DataSet dss = select.DatabaseName(requestXML.FromUserName); //当前数据库 if (dss != null && dss.Tables[0].Rows.Count > 0) { for (int a = 0; a < dss.Tables[0].Rows.Count; a++) { database = dss.Tables[0].Rows[a]["DataBaseName"].ToString(); if (database != "") { database = database + "."; ds = select.getOrdering(requestXML.FromUserName, database);//查询产品的加工进程 if (ds != null) { sql += "产品的加工进程\n"; for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { if (orderid != ds.Tables[0].Rows[i]["order_id"].ToString()) { num = num + 1; sql += "-----------------\n"; sql += "(" + num + ")订单编号:" + ds.Tables[0].Rows[i]["order_id"].ToString() + "\n"; sql += "医院名称:" + ds.Tables[0].Rows[i]["hospital"].ToString() + "\n"; sql += "医生姓名:" + ds.Tables[0].Rows[i]["doctor"].ToString() + "\n"; string indte = ""; if (ds.Tables[0].Rows[i]["inDate"].ToString() != "") { indte = Convert.ToDateTime(ds.Tables[0].Rows[i]["inDate"].ToString()).ToString("yyyy-MM-dd"); } sql += "到厂日期:" + indte + "\n"; string preoutdate = ""; if (ds.Tables[0].Rows[i]["preoutdate"].ToString() != "") { preoutdate = Convert.ToDateTime(ds.Tables[0].Rows[i]["preoutdate"].ToString()).ToString("yyyy-MM-dd"); } sql += "出厂日期:" + preoutdate + "\n"; sql += "患者姓名:" + ds.Tables[0].Rows[i]["patient"].ToString() + "\n"; orderid = ds.Tables[0].Rows[i]["order_id"].ToString(); } sql += "产品名称:" + ds.Tables[0].Rows[i]["itemname"].ToString() + "\n"; sql += "工序:" + ds.Tables[0].Rows[i]["process"].ToString() + "\n"; } } } } } else { sql += "当前没有您的产品的加工进程信息!!\n"; } if (sql == "") { sql += "当前没有您的产品的加工进程信息!"; } #endregion } else if (requestXML.EventKey.Equals("subkey3")) { sql += "结算单汇总查询统计(分析情况报告)"; } else if (requestXML.EventKey.Equals("key3")) { sql += "欢迎关注\n吉星义齿--义齿加工行业首选管理软件。\n\n请正确输入订单号:\n根据输入订单号可以查询\n1、产品的质保卡信息\n2、产品的材料成分信息\n"; } } } } } #endregion //发送消息,把收到的内容回复给发送者 string responeJsonStr = ""; if (requestXML.Wxevent != null && requestXML.Wxevent.Equals("click") && requestXML.EventKey.Equals("subkey3")) { responeJsonStr = "{"; responeJsonStr += "\"touser\": \"" + requestXML.FromUserName + "\","; responeJsonStr += "\"msgtype\": \"news\","; responeJsonStr += "\"agentid\": \"0\","; responeJsonStr += "\"news\": {"; responeJsonStr += "\"articles\":[{"; responeJsonStr += "\"title\": \"结算单查询\","; responeJsonStr += "\"description\": \"" + sql + "\","; responeJsonStr += "\"url\": \"http://www.chaya8.com/WebSearchs.aspx?UserName="******"\","; responeJsonStr += "\"picurl\": \"http://down.cnshu.cn/2011/cnshu_images/huanglu/1106241144.jpg\""; responeJsonStr += "}]"; responeJsonStr += "}"; responeJsonStr += "}"; } else { responeJsonStr = "{"; responeJsonStr += "\"touser\": \"" + requestXML.FromUserName + "\","; responeJsonStr += "\"msgtype\": \"text\","; responeJsonStr += "\"agentid\": \"0\","; responeJsonStr += "\"text\": {"; responeJsonStr += "\"content\": \"" + sql + "\""; responeJsonStr += "},"; responeJsonStr += "\"safe\":\"0\""; responeJsonStr += "}"; } weixinsendmessage wxsend = new weixinsendmessage(); wxsend.SendQYMessage(corpId, corpsecret, responeJsonStr, Encoding.UTF8); } } #endregion else //回调验证 { string sEchoStr = ""; ret = wxcpt.VerifyURL(sVerifyMsgSig, sVerifyTimeStamp, sVerifyNonce, sVerifyEchoStr, ref sEchoStr); if (ret != 0) { HttpContext.Current.Response.Write("ERR: VerifyURL fail, ret: " + ret); return; } HttpContext.Current.Response.Write(sEchoStr); HttpContext.Current.Response.End(); } }