using Cloud.Public; using Cloud.Public.Entity; using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Threading; using System.Web; namespace Cloud.WeiXin { /// /// weixin 的摘要说明 /// public class weixin : IHttpHandler { public void ProcessRequest(HttpContext context) { HttpRequest request = context.Request; if (context.Request.HttpMethod != "POST") { context.Response.ContentType = "text/plain"; context.Response.Write("请用POST请求"); context.Response.End(); } Stopwatch api_sw = new Stopwatch(); api_sw.Start(); var api_log = new DbObject(); bool by_system_log = true; //post请求方法二的数据流 Dictionary post = PostInput(context); string usersign = null; #region post请求一 //获取客户端签名 if (request.Form["usersign"] != null) { usersign = request.Form["usersign"].ToString(); } else if (post["usersign"] != null) { usersign = post["usersign"].ToString(); } #endregion if (string.IsNullOrWhiteSpace(usersign)) { context.Response.Write("OK4"); context.Response.End(); } string jsondata = null; #region post请求一 //获取客户端签名 if (request.Form["data"] != null) { jsondata = request.Form["data"].ToString(); } else if (post["data"] != null) { jsondata = post["data"].ToString(); } #endregion if (string.IsNullOrWhiteSpace(jsondata)) { context.Response.Write("OK3"); context.Response.End(); } //判断是否继续往下执行 bool check = false; if (usersign == "weixin_system") //账号登陆 { check = true; } else { CloudParamEntity userparam = jsondata.JsonToModel(); //客户端签名校验 userparam.UserSign = usersign; check = true; } //接口调用 CloudDataEntity result = new CloudDataEntity(); if (check) { //调用接口定义的方法 result = this.Invoke(jsondata, usersign); } else { //登陆校验失败,退出 result.Success = false; result.MessageInfo = CloudHelper.GetKeyMsg(); } //输出结果 //CloudDataEntity2 result2 = new CloudDataEntity2(); DbObject result2 = new DbObject(); result2.Add("Success", result.Success); result2.Add("MessageInfo", result.MessageInfo.Replace("调用的目标发生了异常。", "")); result2.Add("Fields", result.Fields.ModelToJson()); result2.Add("Data", result.Data); result2.Add("RecordCount", result.RecordCount); result2.Add("Tag", result.Tag); //记录日志 api_sw.Stop(); if (by_system_log) { api_log.Add("invoke_success", result.Success); api_log.Add("invoke_message", result.MessageInfo); api_log.Add("run_time", api_sw.ElapsedMilliseconds); api_log.Add("cr_date", DateTime.Now); } //输出结果 OuputJson(context.Response, result2); } public bool IsReusable { get { return false; } } /// /// 输出返回接口到前端 /// /// /// private void OuputJson(HttpResponse response, object result) { string jsonData = result.ModelTofastJSON(); //.ModelToJson(); response.ContentType = "application/json"; response.ContentEncoding = Encoding.UTF8; response.Write(jsonData); } /// /// 通过接口反射文件,调用方法 /// /// /// /// private CloudDataEntity Invoke(string jsondata, string usersign) { var result = new CloudDataEntity(); try { CloudParamEntity param = jsondata.JsonToModel(); //其它接口调用 CloudAssemblyEntity assembly = new CloudAssemblyEntity(); assembly = ApiHelper.GetApi(param.ApiCode); //执行方法 if (!assembly.Success) { result.Success = false; result.MessageInfo = assembly.MessageInfo; } else { string path = CloudHelper.LibraryDirectory(); string f; if (string.IsNullOrEmpty(path)) { f = CloudHelper.AppPath() + "\\bin\\" + assembly.DllName; } else { f = path + "\\" + assembly.DllName; } if (!File.Exists(f)) { throw new ResultException(901, string.Format("文件{0}不存在!", f)); } else { //反射DLL文件名称 assembly.DllName = f; //通过接口执行自定义方法 if (param.CheckParameter("exec_custom_method_code") && !string.IsNullOrWhiteSpace(param.Get("exec_custom_method_code").Value)) { assembly.MethodName = param.Get("exec_custom_method_code").Value; } //签名变量传递 param.UserSign = usersign; //执行方法 result = DllHelper.InvokeDefaultMethod(assembly, param); } } } catch (Exception ex) { result.Success = false; result.Data = ""; result.MessageInfo = ex.Message + "" + ex.InnerException.Message; } return result; } /// /// 解析POST请求的数据 /// /// /// private Dictionary PostInput(HttpContext context) { try { System.IO.Stream s = context.Request.InputStream; int count = 0; byte[] buffer = new byte[1024]; StringBuilder builder = new StringBuilder(); while ((count = s.Read(buffer, 0, 1024)) > 0) { builder.Append(Encoding.UTF8.GetString(buffer, 0, count)); } s.Flush(); s.Close(); s.Dispose(); Dictionary dictionary = builder.ToString().Split('&').ToDictionary(x => x.Split('=')[0], y => y.Split('=')[1]); return dictionary; } catch (Exception ex) { throw ex; } } } }