using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web; namespace FangYar.WebUI.ashx { /// /// A_CommonHandler 的摘要说明 /// public class A_CommonHandler : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; string action = context.Request.Params["Action"]; string returnstr = ""; switch (action) { case "InsInfo": returnstr = InsInfo(context); break; case "UpdInfo": returnstr = UpdInfo(context); break; case "DelInfo": returnstr = DelInfo(context); break; case "SelInfo": returnstr = SelInfo(context); break; case "SelInfoPage": returnstr = SelInfoPage(context); break; } context.Response.Write(returnstr); } /// /// 添加信息 /// /// /// private string InsInfo(HttpContext context) { string returnstr = ""; int code = 0; string msg = ""; try { //表名 string tbName = UrlDecode(context.Request.Params["tbName"]); //列名 string columns = UrlDecode(context.Request.Params["columns"]); //内容 string values = UrlDecode(context.Request.Params["values"]); StringBuilder strsql = new StringBuilder(); strsql.Append(" insert into " + tbName + " (" + columns + ") values (" + values + ") "); int i = FangYar.Common.MySqlHelper.Execute(strsql.ToString()); if (i > 0) { msg = "保存成功!"; code = 1; } else { msg = "保存失败!"; } } catch (Exception e) { code = 2; msg = e.Message; // 记录操作日志 BLL.SysOperationLogHelp.AddSysOperationLog(context, Common.EnumOperationLogType.Error, "消防要事日记操作请求", "保存异常:" + e); } returnstr = "{\"msg\":\"" + msg + "\",\"code\":" + code + "}"; // 记录操作日志 BLL.SysOperationLogHelp.AddSysOperationLog(context, Common.EnumOperationLogType.Add, "消防要事日记操作请求", "保存"); return returnstr; } /// /// 更新信息 /// /// /// private string UpdInfo(HttpContext context) { string returnstr = ""; int code = 0; string msg = ""; try { //表名 string tbName = UrlDecode(context.Request.Params["tbName"]); //设置信息 string sets = UrlDecode(context.Request.Params["sets"]); //条件 string wheres = context.Request.Params["wheres"]; StringBuilder strsql = new StringBuilder(); strsql.Append(" update " + tbName + " set " + sets + " where " + wheres); int i = FangYar.Common.MySqlHelper.Execute(strsql.ToString()); if (i > 0) { msg = "保存成功!"; code = 1; } else { msg = "保存失败!"; } } catch (Exception e) { code = 2; msg = e.Message; // 记录操作日志 BLL.SysOperationLogHelp.AddSysOperationLog(context, Common.EnumOperationLogType.Error, "消防要事日记操作请求", "保存异常:" + e); } returnstr = "{\"msg\":\"" + msg + "\",\"code\":" + code + "}"; // 记录操作日志 BLL.SysOperationLogHelp.AddSysOperationLog(context, Common.EnumOperationLogType.Add, "消防要事日记操作请求", "保存"); return returnstr; } /// /// 删除信息 /// /// /// private string DelInfo(HttpContext context) { string returnstr = ""; int code = 0; string msg = ""; try { //表名 string tbName = UrlDecode(context.Request.Params["tbName"]); //条件 string wheres = UrlDecode(context.Request.Params["wheres"]); StringBuilder strsql = new StringBuilder(); strsql.Append(" delete FROM " + tbName + " where " + wheres); int i = FangYar.Common.MySqlHelper.Execute(strsql.ToString()); if (i > 0) { msg = "保存成功!"; code = 1; } else { msg = "保存失败!"; } } catch (Exception e) { code = 2; msg = e.Message; // 记录操作日志 BLL.SysOperationLogHelp.AddSysOperationLog(context, Common.EnumOperationLogType.Error, "消防要事日记操作请求", "保存异常:" + e); } returnstr = "{\"msg\":\"" + msg + "\",\"code\":" + code + "}"; // 记录操作日志 BLL.SysOperationLogHelp.AddSysOperationLog(context, Common.EnumOperationLogType.Add, "消防要事日记操作请求", "保存"); return returnstr; } /// /// 分页查询信息 /// /// /// private string SelInfoPage(HttpContext context) { string returnstr = ""; try { //表名 string tbName = UrlDecode(context.Request.Params["tbName"]); //列名 string columns = UrlDecode(context.Request.Params["columns"]); //条件 string wheres = UrlDecode(context.Request.Params["wheres"]); //排序 string orderBy = UrlDecode(context.Request.Params["orderBy"]); if (string.IsNullOrWhiteSpace(orderBy)) { orderBy = " 1 "; } if (string.IsNullOrWhiteSpace(columns)) { columns = " * "; } if (string.IsNullOrWhiteSpace(wheres)) { wheres = " 1=1 "; } 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); } pageIndex = pageIndex < 1 ? 1 : pageIndex; pageSize = pageSize < 1 ? 1 : pageSize; int startnum = (pageIndex - 1) * pageSize; string sqlCount = " SELECT count(1) from " + tbName + " WHERE " + wheres; string sqlStr = " SELECT " + columns + " from " + tbName + " WHERE " + wheres + " order by " + orderBy + " limit " + startnum + ", " + pageSize; var dtCount = FangYar.Common.MySqlHelper.QueryTable(sqlCount); int rowCount = 0; if (dtCount.Rows.Count > 0) { int.TryParse(dtCount.Rows[0][0] + "", out rowCount); } returnstr = "{\"code\":0,\"msg\":\"\","; var dtList = FangYar.Common.MySqlHelper.QueryTable(sqlStr); returnstr += "\"count\":" + rowCount + ",\"data\":"; returnstr += FangYar.Common.JsonHelper.ToJson(dtList); returnstr += "}"; } catch (Exception e) { returnstr = "{\"code\":0,\"msg\":\"error\",\"count\":0,\"data\":[]}"; // 记录操作日志 BLL.SysOperationLogHelp.AddSysOperationLog(context, Common.EnumOperationLogType.Query, "消防要事日记操作请求", "分页查询异常:" + e); } // 记录操作日志 BLL.SysOperationLogHelp.AddSysOperationLog(context, Common.EnumOperationLogType.Query, "消防要事日记操作请求", "分页查询"); return returnstr; } /// /// 查询全部信息 /// /// /// private string SelInfo(HttpContext context) { string returnstr = ""; try { //表名 string tbName = UrlDecode(context.Request.Params["tbName"]); //列名 string columns = UrlDecode(context.Request.Params["columns"]); //条件 string wheres = UrlDecode(context.Request.Params["wheres"]); //排序 string orderBy = UrlDecode(context.Request.Params["orderBy"]); if (string.IsNullOrWhiteSpace(orderBy)) { orderBy = " 1 "; } if (string.IsNullOrWhiteSpace(columns)) { columns = " * "; } if (string.IsNullOrWhiteSpace(wheres)) { wheres = " 1=1 "; } string sqlCount = " SELECT count(1) from " + tbName + " WHERE " + wheres; string sqlStr = " SELECT " + columns + " from " + tbName + " WHERE " + wheres + " order by " + orderBy; var dtCount = FangYar.Common.MySqlHelper.QueryTable(sqlCount); int rowCount = 0; if (dtCount.Rows.Count > 0) { int.TryParse(dtCount.Rows[0][0] + "", out rowCount); } returnstr = "{\"code\":0,\"msg\":\"\","; var dtList = FangYar.Common.MySqlHelper.QueryTable(sqlStr); returnstr += "\"count\":" + rowCount + ",\"data\":"; returnstr += FangYar.Common.JsonHelper.ToJson(dtList); returnstr += "}"; } catch (Exception e) { returnstr = "{\"code\":0,\"msg\":\"error\",\"count\":0,\"data\":[]}"; // 记录操作日志 BLL.SysOperationLogHelp.AddSysOperationLog(context, Common.EnumOperationLogType.Query, "消防要事日记操作请求", "分页查询异常:" + e); } // 记录操作日志 BLL.SysOperationLogHelp.AddSysOperationLog(context, Common.EnumOperationLogType.Query, "消防要事日记操作请求", "分页查询"); return returnstr; } private static string UrlDecode(string code) { FangYar.Common.MyLogHelper.WriteMsg(new FangYar.Common.LogInfoMo() { message = code, msgType = FangYar.Common.EnumLogMsgTypeEnum.Error, path = "UrlCode" }); if (string.IsNullOrWhiteSpace(code)) { return code; } try { byte[] data = Convert.FromBase64String(code); string decodedData = System.Text.Encoding.UTF8.GetString(data); string str1 = System.Net.WebUtility.UrlDecode(decodedData); str1 = str1.Replace("###", "%"); str1 = str1.Replace("#beiqie#", " and "); return str1; } catch (Exception ex) { } string decodedUrl = System.Net.WebUtility.UrlDecode(code); decodedUrl = decodedUrl.Replace("###", "%"); decodedUrl = decodedUrl.Replace("#beiqie#", " and "); return decodedUrl; } public bool IsReusable { get { return false; } } } }