123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477 |
- using LYFZ.Weixin.SDK;
- using LYFZ.WXLibrary;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Web;
- namespace LYFZ.WeixinServers.WeiXinAPP.Api
- {
-
-
-
- public class WeiXinService : IHttpHandler
- {
- public void ProcessRequest(HttpContext context)
- {
- bool bSignature = CheckSignature(context);
- string temp = "";
-
- if(LYFZ.WXLibrary.CommonHandleClass.MicroLetterEventList.Count==0)
- {
- Global.InitializationMicroLetterEventList();
- }
- foreach (string key in LYFZ.WXLibrary.CommonHandleClass.MicroLetterEventList)
- {
- if (!string.IsNullOrEmpty(temp))
- {
- temp += "\r\n\t";
- }
- temp += key;
- }
- WriteLog(context, temp, "EventList" + DateTime.Now.ToString("yyyyMMddHHmmssffff"));
-
- if (!string.IsNullOrEmpty(context.Request["echostr"]))
- {
- if (bSignature)
- {
- Print(context, context.Request["echostr"]);
- }
- else
- {
- Print(context, "签名检查出现异常!");
- }
- }
- else
- {
- string xml = GetHttpRequestData(context);
- WriteLog(context, "获取XML:" + xml, DateTime.Now.ToString("ddHHmmss"));
- var safeMode = context.Request.QueryString.Get("encrypt_type") == "aes";
- WeixinMessage wxMessage = MessageSignature(context, safeMode, xml);
- WriteLog(context, "进入数据操作功能", wxMessage.Body.CreateTime.Value.ToString());
- string openId = wxMessage.Body.FromUserName.Value.ToString();
- string myUserName = wxMessage.Body.ToUserName.Value.ToString();
- if (bSignature)
- {
-
-
- string appid =string.IsNullOrEmpty( context.Request["appid"])?"": context.Request["appid"].ToString().Trim().Trim('/');
- WriteLog(context, "进入关键字获取", wxMessage.Body.CreateTime.Value.ToString());
- SendMessageDataToWeiXin(wxMessage, appid, context);
- }
- else
- {
-
- Print(context, WxMessageControl.TextMessageData(openId, myUserName, "签名检查出现异常!"), "application/x-www-form-urlencoded");
- }
-
- }
- }
- public bool GetWeiXinNoticEvent(HttpContext context)
- {
- string xml = GetHttpRequestData(context);
- var safeMode = context.Request.QueryString.Get("encrypt_type") == "aes";
- WeixinMessage wxMessage = MessageSignature(context, safeMode, xml);
- string appid = context.Request["appid"].ToString().Trim().Trim('/');
-
- SendMessageDataToWeiXin(wxMessage, appid, context);
-
- return true;
- }
- void SendMessageDataToWeiXin(WeixinMessage message, string appid, HttpContext context)
- {
- string openId = message.Body.FromUserName.Value.ToString();
- string myUserName = message.Body.ToUserName.Value.ToString();
- string executeData = new LYFZ.WXLibrary.WeixinExecutor().ServiceExecute(message).ToLower().Trim();
- WriteLog(context, "获取数据操作关键字数据:" + executeData, message.Body.CreateTime.Value.ToString());
- LYFZ.WeixinServiceDate.Model.Model_CustomerInterfaces cusModel;
- if (!string.IsNullOrEmpty(appid))
- {
- cusModel = Global.GetCustomerModelByWxAppId(appid);
- }
- else
- {
- cusModel = Global.GetCustomerModelByWxNumber(myUserName);
- }
-
- string cShellDomainName = "";
- string requestUrl = "";
- if (cusModel != null && cusModel.ID > 0)
- {
- WriteLog(context, "数据集合:" +Newtonsoft.Json.JsonConvert.SerializeObject(cusModel), message.Body.CreateTime.Value.ToString());
- if (cusModel.ShellDomainName.Trim().Length > 1)
- {
- cShellDomainName = cusModel.ShellDomainName.Trim();
- requestUrl = (LYFZ.WXLibrary.CommonHandleClass.GetNetUrl(cShellDomainName, cusModel.PortNumber) + "/WeiXinAPP/Api/WeiXinService.ashx");
-
- }
- string replayMsg = "";
- if (LYFZ.WXLibrary.CommonHandleClass.MicroLetterEventList.Contains(executeData))
- {
- try
- {
- System.Data.DataRow[] dataRows = Global.KeywordDaTable.Select("Keyword='" + executeData + "'");
- if (dataRows.Length > 0)
- {
- executeData = dataRows[0]["FunctionCode"].ToString();
- }
- WriteLog(context, "进入关键字获功能" + executeData, message.Body.CreateTime.Value.ToString());
- WeiXinMessageData(message, executeData, appid, cusModel, context);
- }
- catch(Exception e)
- {
- WriteLog(context, "数据异常" + e.Message, message.Body.CreateTime.Value.ToString());
- Print(context, WxMessageControl.TextMessageData(openId, myUserName, "数据异常"), "application/x-www-form-urlencoded");
- }
- }
- else if (OpenPlatformConfig.IsTransmitService(executeData))
- {
- replayMsg = ReplayPassiveMessageAPI.TransmitService(openId, myUserName);
-
- }
- else
- {
- replayMsg = "";
-
- Print(context, WxMessageControl.TextMessageData(openId, myUserName, "非法指令请求 公众号AppID:" + appid + "对应的加密锁域名:" + cusModel.JMGDomainName + "收到的信息:" + executeData), "application/x-www-form-urlencoded");
- }
- }
- else
- {
- Print(context, WxMessageControl.TextMessageData(openId, myUserName, "无法查询到相应的数据绑定!"), "application/x-www-form-urlencoded");
- }
- }
- public void WeiXinMessageData(WeixinMessage message, string executeData, string appid, LYFZ.WeixinServiceDate.Model.Model_CustomerInterfaces cusModel, HttpContext context)
- {
- string openId = message.Body.FromUserName.Value.ToString();
-
-
-
- cusModel.ShellDomainName = "http://" + cusModel.ShellDomainName;
- switch (executeData)
- {
- case "AccountBinding":
- WriteLog(context, "进入绑定账号功能" + executeData, message.Body.CreateTime.Value.ToString());
- CheckAccountBinding(openId, cusModel, context);
- return;
-
- case "UserBinding":
- WriteLog(context, "进入绑定账号功能" + executeData, message.Body.CreateTime.Value.ToString());
- CheckUserBinding(openId, cusModel, context);
- return;
-
- case "AccountUnbind":
- WriteLog(context, "进入解除绑定账号功能" + executeData, message.Body.CreateTime.Value.ToString());
- CustomerAccountUnbind(openId, cusModel, context);
- return;
-
- case "OrderQuery":
- WriteLog(context, "进入订单查询功能" + executeData, message.Body.CreateTime.Value.ToString());
- CustomerOrderQuery(openId, cusModel, context);
- return;
-
- case "MemberQuery":
- WriteLog(context, "进入会员查询功能" + executeData, message.Body.CreateTime.Value.ToString());
- CustomerMemberQuery(openId, cusModel, context);
- return;
-
- case "FinancialBriefing":
- WriteLog(context, "进入今日财务功能" + executeData, message.Body.CreateTime.Value.ToString());
- SearchFinanceToday(openId, cusModel, context);
- return;
-
- }
-
- string myUserName = message.Body.ToUserName.Value.ToString();
- WriteLog(context, "进入绑定账号功能" + executeData, message.Body.CreateTime.Value.ToString());
-
- Print(context, "");
- }
- private void CheckUserBinding(string openId, WeixinServiceDate.Model.Model_CustomerInterfaces cusModel, HttpContext context)
- {
-
- WriteLog(context, "数据URL" + cusModel.ShellDomainName + "/api/WXApi.ashx/CheckWXCustomerAccount?openid=" + openId, DateTime.Now.ToString("ddHHmmss"));
- string getText = LYFZ.WinAPI.HttpClientHelper.GetResponse(cusModel.ShellDomainName + "/api/WXApi.ashx/CheckWXUserAccount?openid=" + openId);
-
- Newtonsoft.Json.Linq.JObject jobject = (Newtonsoft.Json.Linq.JObject)Newtonsoft.Json.JsonConvert.DeserializeObject(getText);
- string code = jobject.GetValue("code").ToString();
- string content="";
- if(code.Equals("200"))
- {
- content = "账号已绑定,无需重复绑定!";
- }
- else
- {
- content = "账号未绑定,需要绑定对账号进行数据绑定!\n<a href=\"http://kf.lyfz.net/WeiXinAPP/BondingUser.aspx?openid=" + openId + "&cusid=" + cusModel.ID + "\">绑定账号</a>";
- }
- Print(context, WxMessageControl.TextMessageData(openId, cusModel.AppSecret,content), "application/x-www-form-urlencoded");
- }
-
-
-
-
-
-
- private void SearchFinanceToday(string openId, WeixinServiceDate.Model.Model_CustomerInterfaces cusModel, HttpContext context)
- {
- string getText = LYFZ.WinAPI.HttpClientHelper.GetResponse(cusModel.ShellDomainName + "/api/WXApi.ashx/SearchFinanceToday?openid=" + openId);
- WriteLog(context, "数据URL" + cusModel.ShellDomainName + "/api/WXApi.ashx/SearchFinanceToday?openid=" + openId, DateTime.Now.ToString("ddHHmmss"));
- Newtonsoft.Json.Linq.JObject jobject = (Newtonsoft.Json.Linq.JObject)Newtonsoft.Json.JsonConvert.DeserializeObject(getText);
- string code = jobject.GetValue("code").ToString();
- string content = "";
- if (code.Equals("200"))
- {
- content = jobject.GetValue("data").ToString();
- }
- else
- {
- content = "账号绑定解除失败,失败原因:" + jobject.GetValue("data").ToString() + "";
- }
- Print(context, WxMessageControl.TextMessageData(openId, cusModel.AppSecret, content), "application/x-www-form-urlencoded");
- }
- private void CustomerMemberQuery(string openId, WeixinServiceDate.Model.Model_CustomerInterfaces cusModel, HttpContext context)
- {
- string getText = LYFZ.WinAPI.HttpClientHelper.GetResponse(cusModel.ShellDomainName + "/api/WXApi.ashx/SearchCustomerData?openid=" + openId);
- WriteLog(context, "数据URL" + cusModel.ShellDomainName + "/api/WXApi.ashx/SearchCustomerData?openid=" + openId, DateTime.Now.ToString("ddHHmmss"));
- Newtonsoft.Json.Linq.JObject jobject = (Newtonsoft.Json.Linq.JObject)Newtonsoft.Json.JsonConvert.DeserializeObject(getText);
- string code = jobject.GetValue("code").ToString();
- string content = "";
- if (code.Equals("200"))
- {
- Newtonsoft.Json.Linq.JArray array = (Newtonsoft.Json.Linq.JArray)jobject.GetValue("data");
-
- if(array.Count>0)
- {
- Newtonsoft.Json.Linq.JObject cusObject = (Newtonsoft.Json.Linq.JObject)array[0];
- content += "姓名:"+cusObject.GetValue("持卡人")+"\n";
- content += "电话:" + cusObject.GetValue("电话") + "\n";
- content += "性别:" + (cusObject.GetValue("性别").ToString() == "0" ? "男" : "女") + "\n";
- content += "会员卡号:" + (string.IsNullOrEmpty((string)cusObject.GetValue("会员卡号")) ? "未办理" : (string)cusObject.GetValue("会员卡号")) + "\n";
- content += "积分:" + cusObject.GetValue("积分") + "\n";
- content += "欠款:" + cusObject.GetValue("欠款") + "\n";
- content += "金额:" + cusObject.GetValue("金额") + "\n";
- content += "折扣:" + cusObject.GetValue("折扣") + "\n";
- }
- else
- {
- content = "当前客户没有数据";
- }
-
- }
- else
- {
- content = "账号绑定解除失败,失败原因:" + jobject.GetValue("data").ToString() + "";
- }
- Print(context, WxMessageControl.TextMessageData(openId, cusModel.AppSecret, content), "application/x-www-form-urlencoded");
- }
- void CustomerOrderQuery(string openId, LYFZ.WeixinServiceDate.Model.Model_CustomerInterfaces cusModel, HttpContext context)
- {
- string getText = LYFZ.WinAPI.HttpClientHelper.GetResponse(cusModel.ShellDomainName + "/api/WXApi.ashx/SearchCustomerOrder?openid=" + openId);
- WriteLog(context, "数据URL" + cusModel.ShellDomainName + "/api/WXApi.ashx/SearchCustomerOrder?openid=" + openId, DateTime.Now.ToString("ddHHmmss"));
- Newtonsoft.Json.Linq.JObject jobject = (Newtonsoft.Json.Linq.JObject)Newtonsoft.Json.JsonConvert.DeserializeObject(getText);
- string code = jobject.GetValue("code").ToString();
- string content = "";
- if (code.Equals("200"))
- {
- WriteLog(context, "数据" + getText, DateTime.Now.ToString("ddHHmmss"));
- content = jobject.GetValue("data").ToString();
-
-
- }
- else
- {
- content = "账号绑定解除失败,失败原因:" + jobject.GetValue("data").ToString() + "";
- }
- Print(context, WxMessageControl.TextMessageData(openId, cusModel.AppSecret, content), "application/x-www-form-urlencoded");
- }
- void CheckAccountBinding(string openId, LYFZ.WeixinServiceDate.Model.Model_CustomerInterfaces cusModel, HttpContext context)
- {
- WriteLog(context, "数据URL" + cusModel.ShellDomainName + "/api/WXApi.ashx/CheckWXCustomerAccount?openid=" + openId, DateTime.Now.ToString("ddHHmmss"));
- string getText = LYFZ.WinAPI.HttpClientHelper.GetResponse(cusModel.ShellDomainName + "/api/WXApi.ashx/CheckWXCustomerAccount?openid=" + openId);
-
- Newtonsoft.Json.Linq.JObject jobject = (Newtonsoft.Json.Linq.JObject)Newtonsoft.Json.JsonConvert.DeserializeObject(getText);
- string code = jobject.GetValue("code").ToString();
- string content="";
- if(code.Equals("200"))
- {
- content = "账号已绑定,无需重复绑定!";
- }
- else
- {
- content = "账号未绑定,需要绑定对账号进行数据绑定!\n<a href=\"http://kf.lyfz.net/WeiXinAPP/BondingAccount.aspx?openid=" + openId + "&cusid=" + cusModel.ID + "\">绑定账号</a>";
- }
- Print(context, WxMessageControl.TextMessageData(openId, cusModel.AppSecret,content), "application/x-www-form-urlencoded");
- }
- void CustomerAccountUnbind(string openId, LYFZ.WeixinServiceDate.Model.Model_CustomerInterfaces cusModel, HttpContext context)
- {
- string getText = LYFZ.WinAPI.HttpClientHelper.GetResponse(cusModel.ShellDomainName + "/api/WXApi.ashx/UnBindWXCustomerAccount?openid=" + openId+"&type=customer");
- WriteLog(context, "数据URL" + cusModel.ShellDomainName + "/api/WXApi.ashx/UnBindWXCustomerAccount?openid=" + openId, DateTime.Now.ToString("ddHHmmss"));
- Newtonsoft.Json.Linq.JObject jobject = (Newtonsoft.Json.Linq.JObject)Newtonsoft.Json.JsonConvert.DeserializeObject(getText);
- string code = jobject.GetValue("code").ToString();
- string content = "";
- if (code.Equals("200"))
- {
- content = "账号绑定已解除";
- }
- else
- {
- content = "账号绑定解除失败,失败原因:" + jobject.GetValue("data").ToString() + "";
- }
- Print(context, WxMessageControl.TextMessageData(openId, cusModel.AppSecret, content), "application/x-www-form-urlencoded");
- }
- public WeixinMessage MessageSignature(HttpContext context, bool safeMode, string xml)
- {
- WeixinMessage message = null;
- string signature = context.Request["signature"];
-
- string timestamp = context.Request["timestamp"];
- string nonce = context.Request["nonce"];
- string decryptMsg = "";
- if (safeMode)
- {
- var msg_signature = context.Request.QueryString.Get("msg_signature");
- var wxBizMsgCrypt = new Tencent.WXBizMsgCrypt(OpenPlatformConfig.OpenToken, OpenPlatformConfig.OpenEncodingAESKey, OpenPlatformConfig.OpenAppID);
- var ret = wxBizMsgCrypt.DecryptMsg(msg_signature, timestamp, nonce, xml, ref decryptMsg);
- if (ret != 0)
- {
-
- LYFZ.WXLibrary.CommonHandleClass.WriteLog(string.Format("新版开放平台接口解密失败,解密消息返回{0},密文为:{1}", ret, xml));
- }
- }
- else
- {
- decryptMsg = xml;
- }
- try
- {
- message = AcceptMessageAPI.Parse(decryptMsg);
-
- }
- catch {
-
- }
- return message;
- }
- public string GetHttpRequestData(HttpContext context)
- {
- HttpRequest request = context.Request;
- Stream stream = request.InputStream;
- string xml = string.Empty;
- string responseJson = string.Empty;
- if (stream.Length != 0)
- {
- StreamReader streamReader = new StreamReader(stream);
- xml = streamReader.ReadToEnd();
- }
- return xml;
- }
-
-
-
-
-
- public bool CheckSignature(HttpContext context )
- {
- try
- {
- string signature = context.Request["signature"];
- string timestamp = context.Request["timestamp"];
- string nonce = context.Request["nonce"];
- string ent = "";
- bool b = LYFZ.Weixin.SDK.BasicAPI.CheckSignature(signature, timestamp, nonce, OpenPlatformConfig.OpenToken, out ent);
- WriteLog(context, ent, "Ent" + DateTime.Now.ToString("yyyyMMddHHmmssffff"));
- return b;
- }
- catch { }
- finally
- {
-
- }
- return false;
- }
- public void WriteLog(HttpContext context,string content,string filename="" )
- {
- LYFZ.WXLibrary.CommonHandleClass.WriteLog(content, "-2", Global.LogsDongleDomainList);
-
- }
- public void Print(HttpContext context, string content, string ContentType = "text/plain")
- {
- context.Response.ContentType = ContentType;
- context.Response.Write(content);
- }
-
- public bool IsReusable
- {
- get
- {
- return false;
- }
- }
- }
- }
|