using System; using System.Collections.Generic; using System.Data; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Linq; using System.Text; using System.Web; namespace FangYar.WebUI.ashx { /// /// OaKnolageHandler 的摘要说明 /// public class OaKnolageHandler : 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, "知识库操作请求", ""); 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; } 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)) "; } 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)) "; } 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, "", 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); } // 记录操作日志 BLL.SysOperationLogHelp.AddSysOperationLog(context, Common.EnumOperationLogType.Query, "知识库操作请求", "查询"); 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); } // 记录操作日志 BLL.SysOperationLogHelp.AddSysOperationLog(context, Common.EnumOperationLogType.Query, "知识库操作请求", "查询"); 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 art_pic = context.Request.Params["art_pic"]; string art_short = context.Request.Params["art_short"]; string art_cont = context.Request.Params["art_cont"]; 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"]; byte[] outputb = Convert.FromBase64String(art_cont); string orgStr = Encoding.Default.GetString(outputb); string art_conttext = System.Web.HttpUtility.UrlDecode(orgStr);//将Url中的编码转换为简体汉字 , System.Text.Encoding.GetEncoding("GB2312") ////存储图片 string picture = GetUploadImg(context); // 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.ART_PIC = picture; model.ART_SHORT = art_short; model.ART_CONT = art_conttext; if (bll.Add(model)) { msg = "添加成功!"; code = 1; } else { msg = "添加失败!"; } } catch (Exception e) { msg = "添加失败!"; // 记录操作日志 BLL.SysOperationLogHelp.AddSysOperationLog(context, Common.EnumOperationLogType.Error, "知识库操作请求", "添加异常:" + e); } returnstr = "{\"msg\":\"" + msg + "\",\"code\":" + code + "}"; // 记录操作日志 BLL.SysOperationLogHelp.AddSysOperationLog(context, Common.EnumOperationLogType.Add, "知识库操作请求", "添加"); 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) { // 记录操作日志 BLL.SysOperationLogHelp.AddSysOperationLog(context, Common.EnumOperationLogType.Error, "知识库操作请求", "图片上传异常:" + ex); } // 记录操作日志 BLL.SysOperationLogHelp.AddSysOperationLog(context, Common.EnumOperationLogType.Add, "知识库操作请求", "图片上传"); 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 art_pic = context.Request.Params["art_pic"]; string art_pictype = context.Request.Params["art_pictype"]; string art_short = context.Request.Params["art_short"]; string art_cont = context.Request.Params["art_cont"]; 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"]; //转码文本域内容 byte[] outputb = Convert.FromBase64String(art_cont); string orgStr = Encoding.Default.GetString(outputb); string art_conttext = System.Web.HttpUtility.UrlDecode(orgStr);//将Url中的编码转换为简体汉字 , System.Text.Encoding.GetEncoding("GB2312") //存储图片 string picture = ""; if (art_pictype == "0") { picture = GetUploadImg(context); } else { picture = art_pic; } 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.ART_PIC = picture; model.ART_SHORT = art_short; model.ART_CONT = art_conttext; 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); } returnstr = "{\"msg\":\"" + msg + "\",\"code\":" + code + "}"; // 记录操作日志 BLL.SysOperationLogHelp.AddSysOperationLog(context, Common.EnumOperationLogType.Update, "知识库操作请求", "修改"); 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); } returnstr = "{\"code\":" + code + ",\"msg\":\"" + msg + "\"}"; // 记录操作日志 BLL.SysOperationLogHelp.AddSysOperationLog(context, Common.EnumOperationLogType.Delete, "知识库操作请求", "删除"); 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; } } } }