using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data; using Newtonsoft.Json; using System.IO; using System.Drawing; using System.Text; using System.Drawing.Imaging; namespace FangYar.WebUI.ashx { /// /// CarHandler 的摘要说明 /// public class ZYOaKnolageHandler : IHttpHandler { private FangYar.BLL.OA.OA_KNOWLEDGEBASE bll = new BLL.OA.OA_KNOWLEDGEBASE(); public void ProcessRequest(HttpContext context) { BLL.SysOperationLogHelp.AddSysOperationLog(context, Common.EnumOperationLogType.Other, "战训知识库请求", "", "1"); context.Response.ContentType = "text/plain"; string action = context.Request.Params["Action"]; string returnstr = ""; switch (action) { case "List": returnstr = List(context); break; case "Add": returnstr = Add(context); break; case "Edit": returnstr = Edit(context); break; case "Del": returnstr = Del(context); break; case "getDicdetail": returnstr = getDicdetail(context); break; case "getArtContById": returnstr = getArtContById(context); break; } context.Response.Write(returnstr); } //查询 private string List(HttpContext context) { string returnstr = ""; try { string OrgId = context.Request.Params["OrgId"]; string treeID = context.Request.Params["treeID"]; string is_content = context.Request.Params["is_content"]; string cls = context.Request.Params["cls"]; string ntype = context.Request.Params["ntype"]; string keyword = context.Request.Params["keyword"]; string page = context.Request.Params["page"]; string limit = context.Request.Params["limit"]; int pageIndex = 1; int pageSize = 10; if (!string.IsNullOrEmpty(page)) { pageIndex = int.Parse(page); } if (!string.IsNullOrEmpty(limit)) { pageSize = int.Parse(limit); } int startIndex = (pageIndex - 1) * pageSize; int endIndex = pageSize; string where = "1=1"; if (!string.IsNullOrEmpty(treeID) && treeID != OrgId) { if (is_content != "1") { where = " ( org_id in ( select o.org_id from fire_org o ,(select get_Org_child_list('" + treeID + "') cids ) s where find_in_set(org_id,cids) ) "; //获取单位直属上级 string sqlIds = " select get_Org_Parent_list('"+ treeID + "')"; DataTable idsDt = FangYar.Common.MySqlHelper.QueryTable(sqlIds); string ids = idsDt.Rows[0][0].ToString(); if (ids.IndexOf("D8DC637B8B984848A63F82A018AFAEB0") > 0) ids += ",734389c7ae364584aea9918693bba3de"; else if(ids.IndexOf("f4d9f25b27114172a66a9d04cc158b00") > 0) ids += ",0055c83cf8194051888061fb27971578"; else if (ids.IndexOf("40a1e65991764d4dac2d826bfbbfc849") > 0) ids += ",a3a158f3ed6a463d92ef65aa58c67bfa"; else if (ids.IndexOf("5806465fa4e845998f31a10d023f332e") > 0) ids += ",335ef5c069954872a50d012b4676393a"; else if (ids.IndexOf("b5b00dfb27a34e0a8bd86270930e02d6") > 0) ids += ",1cc32e7245694343b1e5ff41014eee74"; else if (ids.IndexOf("82487f0ebb6848e7ac4abc3d7cbc8504") > 0) ids += ",0cf00efb5e5f4f668b86e08aff0ea3c4"; else if (ids.IndexOf("9a1324ec8b244a7f9d6cbca88cd614fd") > 0) ids += ",2a0e0a66602145cfb456b98d92776977"; else if (ids.IndexOf("833b28c12b42495a99a5e41068eb6805") > 0) ids += ",ba4e41e82ddd4d819703f0a57f088d4e"; where += " or LOCATE(org_id, '" + ids + "') >0 )"; } else { where = " ORG_ID = '" + treeID + "'"; } } else { if (is_content != "1") { where = "( org_id in ( select o.org_id from fire_org o ,(select get_Org_child_list('" + OrgId + "') cids ) s where find_in_set(org_id,cids) ) "; //获取单位直属上级 string sqlIds = " select get_Org_Parent_list('" + OrgId + "')"; DataTable idsDt = FangYar.Common.MySqlHelper.QueryTable(sqlIds); string ids = idsDt.Rows[0][0].ToString(); if (ids.IndexOf("D8DC637B8B984848A63F82A018AFAEB0") > 0) ids += ",734389c7ae364584aea9918693bba3de"; else if (ids.IndexOf("f4d9f25b27114172a66a9d04cc158b00") > 0) ids += ",0055c83cf8194051888061fb27971578"; else if (ids.IndexOf("40a1e65991764d4dac2d826bfbbfc849") > 0) ids += ",a3a158f3ed6a463d92ef65aa58c67bfa"; else if (ids.IndexOf("5806465fa4e845998f31a10d023f332e") > 0) ids += ",335ef5c069954872a50d012b4676393a"; else if (ids.IndexOf("b5b00dfb27a34e0a8bd86270930e02d6") > 0) ids += ",1cc32e7245694343b1e5ff41014eee74"; else if (ids.IndexOf("82487f0ebb6848e7ac4abc3d7cbc8504") > 0) ids += ",0cf00efb5e5f4f668b86e08aff0ea3c4"; else if (ids.IndexOf("9a1324ec8b244a7f9d6cbca88cd614fd") > 0) ids += ",2a0e0a66602145cfb456b98d92776977"; else if (ids.IndexOf("833b28c12b42495a99a5e41068eb6805") > 0) ids += ",ba4e41e82ddd4d819703f0a57f088d4e"; where += " or LOCATE(org_id, '" + ids + "') >0 )"; } else { where = "ORG_ID = '" + OrgId + "'"; } } if (!string.IsNullOrEmpty(cls)) { where += " and CLS = '" + cls + "'"; } if (!string.IsNullOrEmpty(ntype)) { where += " and NTYPE = '" + ntype + "'"; } if (!string.IsNullOrEmpty(keyword)) { where += " and ART_TITLE like '%" + keyword + "%' "; } returnstr = "{\"code\":0,\"msg\":\"\","; int count = bll.GetRecordCount(where); returnstr += "\"count\":" + count + ",\"data\":"; if (count == 0) { returnstr += "[]"; } else { DataTable dt = bll.GetListByPage(where, " CREATE_TIME desc ", startIndex, endIndex).Tables[0]; returnstr += Common.JsonHelper.ToJson(dt); } returnstr += "}"; } catch (Exception e) { returnstr = "{\"code\":0,\"msg\":\"error\",\"count\":0,\"data\":[]}"; BLL.SysOperationLogHelp.AddSysOperationLog(context, Common.EnumOperationLogType.Error, "战训知识库请求", "查询异常:" + e.Message, "1"); } BLL.SysOperationLogHelp.AddSysOperationLog(context, Common.EnumOperationLogType.Query, "战训计划请求", "查询", "1"); return returnstr; } private string getDicdetail(HttpContext context) { string returnstr = ""; try { string mod_code = context.Request.Params["mod_code"]; string dic_fid = context.Request.Params["dic_fid"]; string where = " mod_code = '" + mod_code + "' "; if (!string.IsNullOrEmpty(dic_fid)) { where += " and dic_fid = '" + dic_fid + "' "; } else { where += " and (dic_fid is null or dic_fid ='')"; } FangYar.BLL.TBL.SysDicdetailBLL dicbll = new BLL.TBL.SysDicdetailBLL(); returnstr = "{\"code\":0,\"msg\":\"\","; returnstr += "\"count\":-1,\"data\":"; DataTable dt = dicbll.GetList(where).Tables[0]; returnstr += FangYar.Common.JsonHelper.ToJson(dt); returnstr += "}"; } catch(Exception e) { returnstr = "{\"code\":-1,\"msg\":\"error\",\"count\":0,\"data\":[]}"; BLL.SysOperationLogHelp.AddSysOperationLog(context, Common.EnumOperationLogType.Error, "战训知识库请求", "查询字典项异常:" + e.Message, "1"); } BLL.SysOperationLogHelp.AddSysOperationLog(context, Common.EnumOperationLogType.Query, "战训知识库请求", "查询字典项", "1"); return returnstr; } //添加 private string Add(HttpContext context) { string returnstr = ""; int code = -1; string msg = ""; try { string id = Guid.NewGuid().ToString("N"); string cls = context.Request.Params["cls"]; string ntype = context.Request.Params["ntype"]; string art_title = context.Request.Params["art_title"]; string art_come = context.Request.Params["art_come"]; string file_type = context.Request.Params["file_type"]; string file_url = context.Request.Params["file_url"]; string org_id = context.Request.Params["org_id"]; string org_name = context.Request.Params["org_name"]; string uid = context.Request.Params["uid"]; string uname = context.Request.Params["uname"]; // FangYar.Model.OA.OA_KNOWLEDGEBASE model = new Model.OA.OA_KNOWLEDGEBASE(); model.ID = id; model.RECORDSNUM = 0; model.USERS_UID = uid; model.USERS_NAME = uname; model.ORG_ID = org_id; model.ORG_NAME = org_name; model.CREATE_TIME = DateTime.Now; model.CLS = cls; model.NTYPE = ntype; model.ART_TITLE = art_title; model.ART_COME = art_come; model.EXTEND1 = file_type; model.EXTEND2 = file_url; if (bll.Add(model)) { msg = "添加成功!"; code = 1; } else { msg = "添加失败!"; } } catch(Exception e) { msg = "添加失败!"; BLL.SysOperationLogHelp.AddSysOperationLog(context, Common.EnumOperationLogType.Error, "战训知识库请求", "添加异常:" + e.Message, "1"); } returnstr = "{\"msg\":\"" + msg + "\",\"code\":" + code + "}"; BLL.SysOperationLogHelp.AddSysOperationLog(context, Common.EnumOperationLogType.Add, "战训知识库请求", "添加", "1"); return returnstr; } //图片上传 private string GetUploadImg(HttpContext context) { string returnStr = ""; try { string serverPath = "Upload/OA/Kon/"; string base64Str = context.Request.Params["art_pic"]; base64Str = base64Str.Replace("data:image/png;base64,", "").Replace("data:image/jgp;base64,", "") .Replace("data:image/jpg;base64,", "").Replace("data:image/jpeg;base64,", ""); //将base64头部信息替换 byte[] imageBytes = Convert.FromBase64String(base64Str); using (MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length)) { ms.Write(imageBytes, 0, imageBytes.Length); System.Drawing.Image img = Image.FromStream(ms, true);//将流中的图片转换为Image图片对象 Random ran = new Random((int)DateTime.Now.Ticks);//利用时间种子解决伪随机数短时间重复问题 if (!Directory.Exists(context.Server.MapPath("~/" + serverPath))) { DirectoryInfo directoryInfo = new DirectoryInfo(context.Server.MapPath("~/" + serverPath)); directoryInfo.Create(); } //文件保存位置及命名,精确到毫秒并附带一组随机数,防止文件重名,数据库保存路径为此变量 //string serverPath = "Upload/Device/"; string filename = serverPath + DateTime.Now.ToString("yyyyMMddhhmmssms") + ran.Next(99999) + ".jpg"; //路径映射为绝对路径 string path = context.Server.MapPath("~/") + filename; img.Save(path);//图片保存,JPEG格式图片较小 returnStr = filename; } } catch (Exception ex) { } return returnStr; } //修改 private string Edit(HttpContext context) { string returnstr = ""; int code = -1; string msg = ""; try { string id = context.Request.Params["ID"]; string cls = context.Request.Params["cls"]; string ntype = context.Request.Params["ntype"]; string art_title = context.Request.Params["art_title"]; string art_come = context.Request.Params["art_come"]; string file_type = context.Request.Params["file_type"]; string file_url = context.Request.Params["file_url"]; string org_id = context.Request.Params["org_id"]; string org_name = context.Request.Params["org_name"]; string uid = context.Request.Params["uid"]; string uname = context.Request.Params["uname"]; FangYar.Model.OA.OA_KNOWLEDGEBASE model = bll.GetModel(id); if (model != null) { model.ID = id; model.RECORDSNUM = 0; model.CLS = cls; model.NTYPE = ntype; model.ART_TITLE = art_title; model.ART_COME = art_come; model.EXTEND1 = file_type; model.EXTEND2 = file_url; model.UPDATE_TIME = DateTime.Now; if (bll.Update(model)) { msg = "编辑成功!"; code = 1; } else { msg = "编辑失败!"; } } else { msg = "编辑失败!"; } } catch(Exception e ) { msg = "编辑失败!"; BLL.SysOperationLogHelp.AddSysOperationLog(context, Common.EnumOperationLogType.Error, "战训知识库请求", "修改异常:" + e.Message, "1"); } returnstr = "{\"msg\":\"" + msg + "\",\"code\":" + code + "}"; BLL.SysOperationLogHelp.AddSysOperationLog(context, Common.EnumOperationLogType.Update, "战训知识库请求", "修改" , "1"); return returnstr; } //删除 private string Del(HttpContext context) { string returnstr = ""; int code = -1; string msg = ""; try { string id = context.Request.Params["id"]; if (bll.Delete(id)) { msg = "删除成功!"; code = 1; } else { msg = "删除失败!"; } } catch(Exception e) { msg = "删除失败!"; BLL.SysOperationLogHelp.AddSysOperationLog(context, Common.EnumOperationLogType.Error, "战训知识库请求", "删除异常:" + e.Message, "1"); } returnstr = "{\"code\":" + code + ",\"msg\":\"" + msg + "\"}"; BLL.SysOperationLogHelp.AddSysOperationLog(context, Common.EnumOperationLogType.Delete, "战训知识库请求", "删除", "1"); return returnstr; } /// /// 将Base64字符串转换为图片并保存到本地 /// /// base64字符串 /// 图片保存地址,如:/Content/Images/10000.png /// 保存路径 private string Base64StrToImage(string base64Str, string savePath) { var ret = ""; try { base64Str = base64Str.Replace("data:image/png;base64,", "").Replace("data:image/jgp;base64,", "") .Replace("data:image/jpg;base64,", "").Replace("data:image/jpeg;base64,", ""); //将base64头部信息替换 byte[] imageBytes = Convert.FromBase64String(base64Str); using (MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length)) { ms.Write(imageBytes, 0, imageBytes.Length); var bitmap = Image.FromStream(ms, true); if (bitmap != null) { //创建文件夹 var folderPath = savePath.Substring(0, savePath.LastIndexOf('\\')); ////FileHelper.CreateDir(folderPath); if (!Directory.Exists(folderPath)) { Directory.CreateDirectory(folderPath); } //图片后缀格式 var suffix = savePath.Substring(savePath.LastIndexOf('.') + 1, savePath.Length - savePath.LastIndexOf('.') - 1).ToLower(); var suffixName = suffix == "png" ? ImageFormat.Png : suffix == "jpg" || suffix == "jpeg" ? ImageFormat.Jpeg : suffix == "bmp" ? ImageFormat.Bmp : suffix == "gif" ? ImageFormat.Gif : ImageFormat.Jpeg; //这里复制一份对图像进行保存,否则会出现“GDI+ 中发生一般性错误”的错误提示 var bmpNew = new Bitmap(bitmap); bmpNew.Save(savePath, suffixName); bmpNew.Dispose(); bitmap.Dispose(); ret = savePath; } else { ret = ""; } } } catch (Exception ex) { ret = ""; } return ret; } public bool IsReusable { get { return false; } } /// /// 根据主键ID获取内容 /// /// 内容 /// private string getArtContById(HttpContext context) { string returnstr = ""; try { string id = context.Request.Params["id"]; DataTable dt = bll.getArtContById(id); string artCont = dt.Rows[0]["ART_CONT"].ToString(); artCont = artCont.Replace("\"", "${shuangyinhao}"); artCont = artCont.Replace(":", "${maohao}"); returnstr = artCont; } catch (Exception e) { returnstr = ""; } return returnstr; } } }