/// <summary> /// 压缩需求 /// </summary> /// <param name="strFileData"></param> /// <param name="UserInfo"></param> /// <returns></returns> public string CompressZip(string strFileData, JH_Auth_QY QYinfo) { Dictionary <String, String> DATA = new Dictionary <String, String>(); DATA.Add("data", strFileData); HttpWebResponse ResponseData = CommonHelp.CreatePostHttpResponse(QYinfo.FileServerUrl.TrimEnd('/') + "/" + QYinfo.QYCode + "/document/zipfolder", DATA, 0, "", null); return(CommonHelp.GetResponseString(ResponseData)); }
/// <summary> /// 上传文件到服务器 /// </summary> /// <param name="uploadUrl"></param> /// <param name="fileName"></param> /// <param name="uploadFile"></param> /// <returns></returns> public string SaveFile( JH_Auth_QY QYinfo, string fileName, HttpPostedFile uploadFile) { try { string uploadUrl = QYinfo.FileServerUrl.TrimEnd('/') + "/document/fileupload/" + QYinfo.QYCode; string result = ""; string boundary = "----------" + DateTime.Now.Ticks.ToString("x"); HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(uploadUrl); webrequest.ContentType = "multipart/form-data; boundary=" + boundary; webrequest.Method = "POST"; StringBuilder sb = new StringBuilder(); sb.Append("--"); sb.Append(boundary); sb.Append("\r\n"); sb.Append("Content-Disposition: form-data; name=\"file"); sb.Append("\"; filename=\"" + fileName + "\""); sb.Append("\""); sb.Append("\r\n"); sb.Append("Content-Type: application/octet-stream"); sb.Append("\r\n"); sb.Append("\r\n"); string postHeader = sb.ToString(); byte[] postHeaderBytes = Encoding.UTF8.GetBytes(postHeader); byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n"); webrequest.ContentLength = uploadFile.InputStream.Length + postHeaderBytes.Length + boundaryBytes.Length; Stream requestStream = webrequest.GetRequestStream(); requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length); byte[] buffer = new Byte[(int)uploadFile.InputStream.Length]; //声明文件长度的二进制类型 uploadFile.InputStream.Read(buffer, 0, buffer.Length); //将文件转成二进制 requestStream.Write(buffer, 0, buffer.Length); //赋值二进制数据 requestStream.Write(boundaryBytes, 0, boundaryBytes.Length); webrequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)"; WebResponse responce = webrequest.GetResponse(); requestStream.Close(); using (Stream s = responce.GetResponseStream()) { using (StreamReader sr = new StreamReader(s)) { result = sr.ReadToEnd(); } } responce.Close(); return result; } catch (Exception ex) { return ""; } }
public void YZCOMPANYQYH(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo) { JH_Auth_QY company = new JH_Auth_QY(); company = JsonConvert.DeserializeObject <JH_Auth_QY>(P1); if (string.IsNullOrEmpty(company.corpSecret) || string.IsNullOrEmpty(company.corpId)) { msg.ErrorMsg = "初始化企业号信息失败,corpId,corpSecret 不能为空"; return; } if (!new JH_Auth_QYB().Update(company)) { msg.ErrorMsg = "初始化企业号信息失败"; return; } }
public WXHelp(JH_Auth_QY QY) { //获取企业信息 Qyinfo = QY; }
/// <summary> /// /// </summary> /// <param name="uploadUrl"></param> /// <param name="fileToUpload"></param> /// <param name="poststr"></param> /// <returns></returns> public static string PostFile(JH_Auth_QY QYinfo, string fileToUpload, string poststr = "") { string result = ""; string uploadUrl = QYinfo.FileServerUrl.TrimEnd('/') + "/document/fileupload/" + QYinfo.QYCode; try { string boundary = "----------" + DateTime.Now.Ticks.ToString("x"); HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(uploadUrl); webrequest.ContentType = "multipart/form-data; boundary=" + boundary; webrequest.Method = "POST"; StringBuilder sb = new StringBuilder(); if (poststr != "") { foreach (string c in poststr.Split('&')) { string[] item = c.Split('='); if (item.Length != 2) { break; } string name = item[0]; string value = item[1]; sb.Append("–" + boundary); sb.Append("\r\n"); sb.Append("Content-Disposition: form-data; name=\"" + name + "\""); sb.Append("\r\n\r\n"); sb.Append(value); sb.Append("\r\n"); } } sb.Append("--"); sb.Append(boundary); sb.Append("\r\n"); sb.Append("Content-Disposition: form-data; name=\"file"); //sb.Append(fileFormName); sb.Append("\"; filename=\""); sb.Append(Path.GetFileName(fileToUpload)); sb.Append("\""); sb.Append("\r\n"); sb.Append("Content-Type: application/octet-stream"); //sb.Append(contenttype); sb.Append("\r\n"); sb.Append("\r\n"); string postHeader = sb.ToString(); byte[] postHeaderBytes = Encoding.UTF8.GetBytes(postHeader); byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n"); FileStream fileStream = new FileStream(fileToUpload, FileMode.Open, FileAccess.Read); long length = postHeaderBytes.Length + fileStream.Length + boundaryBytes.Length; webrequest.ContentLength = length; Stream requestStream = webrequest.GetRequestStream(); requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length); byte[] buffer = new Byte[(int)fileStream.Length]; int bytesRead = 0; while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0) { requestStream.Write(buffer, 0, bytesRead); } requestStream.Write(boundaryBytes, 0, boundaryBytes.Length); fileStream.Close(); WebResponse responce = webrequest.GetResponse(); requestStream.Close(); using (Stream s = responce.GetResponseStream()) { using (StreamReader sr = new StreamReader(s)) { result = sr.ReadToEnd(); } } } catch (Exception ex) { CommonHelp.WriteLOG(uploadUrl + "|||" + fileToUpload + "|||" + ex.ToString()); } return(result); }
public APIHandler() : base() { Msg_Result msg = new Msg_Result() { Action = "", ErrorMsg = "" }; JH_Auth_UserB.UserInfo UserInfo = new JH_Auth_UserB.UserInfo(); Before += ctx => { try { //Logger.LogInfo("请求地址" + ctx.Request.Path); if (ctx.Request.Path == "/adminapi/login") { return(ctx.Response); } else if (ctx.Request.Path == "/adminapi/getuser") { return(ctx.Response); } else if (ctx.Request.Path == "/") { return(Response.AsRedirect("/Web/Login.html")); } else if (ctx.Request.Path.StartsWith("/adminapi/dfile")) { return(ctx.Response); } else if (ctx.Request.Path.StartsWith("/adminapi/ExeActionPub")) { return(ctx.Response); } else { string strUser = ""; string strpasd = ""; string strDay = ""; string filecode = ""; List <user> users = new List <user>(); if (ctx.Request.Cookies.ContainsKey("user")) { strUser = ctx.Request.Cookies["user"]; } if (ctx.Request.Cookies.ContainsKey("filecode")) { filecode = ctx.Request.Cookies["filecode"]; } if (string.IsNullOrEmpty(filecode)) { filecode = ctx.Request.Query["filecode"]; } if (string.IsNullOrEmpty(filecode)) { filecode = ctx.Request.Form["filecode"]; } if (!string.IsNullOrEmpty(filecode)) { strUser = new CacheHelp().Get(filecode); users = new JH_Auth_UserB().GetEntities(d => d.username == strUser).ToList(); new CacheHelp().Set(filecode, strUser); } if (users.Count() != 1) { msg.ErrorMsg = "NOSESSIONCODE"; return(Response.AsText(JsonConvert.SerializeObject(msg), "text/html; charset=utf-8")); } else { UserInfo.User = users[0]; } return(ctx.Response); } } catch (Exception ex) { msg.ErrorMsg = ex.Message.ToString(); return(Response.AsText(JsonConvert.SerializeObject(msg), "text/html; charset=utf-8")); } }; Get["/"] = p => { return(Response.AsRedirect("/Web/Login.html")); //return View["login"]; }; ///获取文件 Get["/adminapi/dfile/{fileid}"] = p => { int fileId = 0; int.TryParse(p.fileid.Value.Split(',')[0], out fileId); FT_File file = new FT_FileB().GetEntity(d => d.ID == fileId); string strzyid = p.fileid.Value; if (file != null) { strzyid = file.zyid; } string width = Context.Request.Query["width"].Value ?? ""; string height = Context.Request.Query["height"].Value ?? ""; Qycode qy = new QycodeB().GetALLEntities().FirstOrDefault(); string filename = Context.Request.Url.SiteBase + "/" + qy.Code + "/document/" + strzyid; if (width + height != "") { filename = Context.Request.Url.SiteBase + "/" + qy.Code + "/document/image/" + strzyid + (width + height != "" ? ("/" + width + "/" + height) : ""); } return(Response.AsRedirect(filename)); }; //登录接口 Post["/adminapi/login"] = p => { string strUser = Context.Request.Form["user"]; string strpasd = Context.Request.Form["pasd"]; var users = new JH_Auth_UserB().GetEntities(d => d.username == strUser && d.pasd == CommonHelp.GetMD5(strpasd)); if (users.Count() == 1) { msg.Result = "Y"; string strToken = CommonHelp.GenerateToken(strUser); msg.Result2 = strToken; new CacheHelp().Set(strToken, strUser); } else { msg.Result = "N"; } //JsonConvert.SerializeObject(Model).Replace("null", "\"\""); return(Response.AsJson(msg)); }; Post["/adminapi/getuser"] = p => { string strUser = Context.Request.Form["user"]; string Secret = Context.Request.Form["Secret"]; string strlotusSecret = CommonHelp.GetConfig("lotusSecret", "www.qijiekeji.com"); Logger.LogError("用户" + strUser + strlotusSecret + "获取Code" + Secret); if (Secret == strlotusSecret) { var users = new JH_Auth_UserB().GetEntities(d => d.username == strUser); if (users.Count() == 1) { string strToken = CommonHelp.GenerateToken(strUser); msg.Result = strToken; new CacheHelp().Set(strToken, strUser); } else { msg.ErrorMsg = "找不到用户信息"; return(Response.AsText(JsonConvert.SerializeObject(msg), "text/html; charset=utf-8")); } } else { msg.ErrorMsg = "Secret不匹配"; return(Response.AsText(JsonConvert.SerializeObject(msg), "text/html; charset=utf-8")); } //JsonConvert.SerializeObject(Model).Replace("null", "\"\""); return(Response.AsJson(msg)); }; //普通用户接口 Post["/adminapi/ExeAction/{action}"] = p => { JObject JsonData = new JObject(); foreach (string item in Context.Request.Form.Keys) { JsonData.Add(item, Context.Request.Form[item].Value); } string PostData = ""; string P1 = JsonData["P1"] == null ? "" : JsonData["P1"].ToString(); string P2 = JsonData["P2"] == null ? "" : JsonData["P2"].ToString(); string strAction = p.action; Qycode qy = new QycodeB().GetALLEntities().FirstOrDefault(); JH_Auth_QY QYinfo = new JH_Auth_QY(); QYinfo.FileServerUrl = Context.Request.Url.SiteBase; QYinfo.QYCode = qy.Code; UserInfo.QYinfo = QYinfo; //Dictionary<string, string> results3 = JsonConvert.DeserializeObject<Dictionary<string, string>>(PostData.ToString()); var function = Activator.CreateInstance(typeof(QYWDManage)) as QYWDManage; var method = function.GetType().GetMethod(strAction.ToUpper()); method.Invoke(function, new object[] { JsonData, msg, P1, P2, UserInfo }); IsoDateTimeConverter timeConverter = new IsoDateTimeConverter(); timeConverter.DateTimeFormat = "yyyy-MM-dd HH:mm:ss"; string Result = JsonConvert.SerializeObject(msg, Formatting.Indented, timeConverter).Replace("null", "\"\""); return(Response.AsText(Result, "text/html; charset=utf-8")); }; //管理用户接口 Post["/adminapi/ExeActionAuth/{action}"] = p => { JObject JsonData = new JObject(); foreach (string item in Context.Request.Form.Keys) { JsonData.Add(item, Context.Request.Form[item].Value); } string PostData = ""; string P1 = JsonData["P1"] == null ? "" : JsonData["P1"].ToString(); string P2 = JsonData["P2"] == null ? "" : JsonData["P2"].ToString(); string strAction = p.action; Qycode qy = new QycodeB().GetALLEntities().FirstOrDefault(); JH_Auth_QY QYinfo = new JH_Auth_QY(); QYinfo.FileServerUrl = Context.Request.Url.SiteBase; QYinfo.QYCode = qy.Code; UserInfo.QYinfo = QYinfo; //Dictionary<string, string> results3 = JsonConvert.DeserializeObject<Dictionary<string, string>>(PostData.ToString()); var function = Activator.CreateInstance(typeof(QYWDManage)) as QYWDManage; var method = function.GetType().GetMethod(strAction.ToUpper()); method.Invoke(function, new object[] { JsonData, msg, P1, P2, UserInfo }); IsoDateTimeConverter timeConverter = new IsoDateTimeConverter(); timeConverter.DateTimeFormat = "yyyy-MM-dd HH:mm:ss"; string Result = JsonConvert.SerializeObject(msg, Formatting.Indented, timeConverter).Replace("null", "\"\""); return(Response.AsText(Result, "text/html; charset=utf-8")); }; //外部接口 Post["/adminapi/ExeActionPub/{action}"] = p => { JObject JsonData = new JObject(); foreach (string item in Context.Request.Form.Keys) { JsonData.Add(item, Context.Request.Form[item].Value); } string PostData = ""; string P1 = JsonData["P1"] == null ? "" : JsonData["P1"].ToString(); string P2 = JsonData["P2"] == null ? "" : JsonData["P2"].ToString(); string strAction = p.action; Qycode qy = new QycodeB().GetALLEntities().FirstOrDefault(); JH_Auth_QY QYinfo = new JH_Auth_QY(); QYinfo.FileServerUrl = Context.Request.Url.SiteBase; QYinfo.QYCode = qy.Code; UserInfo.QYinfo = QYinfo; //Dictionary<string, string> results3 = JsonConvert.DeserializeObject<Dictionary<string, string>>(PostData.ToString()); var function = Activator.CreateInstance(typeof(PubManage)) as PubManage; var method = function.GetType().GetMethod(strAction.ToUpper()); method.Invoke(function, new object[] { JsonData, msg, P1, P2, UserInfo }); IsoDateTimeConverter timeConverter = new IsoDateTimeConverter(); timeConverter.DateTimeFormat = "yyyy-MM-dd HH:mm:ss"; string Result = JsonConvert.SerializeObject(msg, Formatting.Indented, timeConverter).Replace("null", "\"\""); return(Response.AsText(Result, "text/html; charset=utf-8")); }; After += ctx => { msg.Action = Context.Request.Path; //添加日志 }; }