using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace FangYar.WebUI.ashx { /// /// BaseUserHandler 的摘要说明 /// public class BaseUserHandler : IHttpHandler { private FangYar.BLL.BaseUserBLL bll = new BLL.BaseUserBLL(); public void ProcessRequest(HttpContext context) { // 记录操作日志 BLL.SysOperationLogHelp.AddSysOperationLog(context, Common.EnumOperationLogType.Other, "基础用户操作请求", ""); context.Response.ContentType = "text/json"; string action = context.Request.Params["Action"]; string returnstr = ""; switch (action) { case "List": returnstr = GetUserList(context); break; case "Add": returnstr = AddUser(context); break; case "Edit": returnstr = EditUser(context); break; case "Del": returnstr = DelUser(context); break; case "EditPwd": returnstr = EditPwd(context); break; } context.Response.Write(returnstr); } //查询 private string GetUserList(HttpContext context) { string returnstr = ""; try { string keywords = context.Request.Params["keywords"]; string where = null; if (!string.IsNullOrEmpty(keywords)) { where = "USERS_UID like '" + keywords + "' or USERS_NAME like '" + keywords + "' "; } returnstr = "{\"code\":0,\"msg\":\"\","; List list = bll.QueryList(1, 10, where, null); returnstr += "\"count\":" + list.Count + ",\"data\":"; if (list.Count == 0) { returnstr += "[]"; } else { returnstr += FangYar.Common.JsonHelper.ToJson(list); } 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 AddUser(HttpContext context) { string returnstr = ""; int code = -1; string msg = ""; try { string userUID = context.Request.Params["userUID"]; string userPwd = context.Request.Params["userPwd"]; string userType = context.Request.Params["userType"]; string userName = context.Request.Params["userName"]; if (string.IsNullOrEmpty(userUID)) { msg = "账号不能为空!"; } else if (userUID.Length < 4) { msg = "账号长度最少为4个字符!"; } else if (string.IsNullOrEmpty(userPwd)) { msg = "密码不能为空!"; } else if (userUID.Length < 4) { msg = "密码长度最少为4个字符!"; } else if (string.IsNullOrEmpty(userName)) { msg = "名称不能为空!"; } else if (bll.CheckLoginByUserID(userUID) != null) { msg = "用户已存在!"; } else { FangYar.Model.BaseUserModel model = new Model.BaseUserModel(); model.ID = Guid.NewGuid().ToString("N"); model.USERS_UID = userUID; model.USERS_PWD = FangYar.Common.Md5.GetMD5String(userPwd); model.USERS_NAME = userName; model.USERS_TYPE = userType; if (bll.AddUser(model)) { 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.Add, "基础用户操作请求", "添加"); return returnstr; } //修改 private string EditUser(HttpContext context) { string returnstr = ""; int code = -1; string msg = ""; try { string ID = context.Request.Params["ID"]; string userUID = context.Request.Params["userUID"]; string userPwd = context.Request.Params["userPwd"]; string userType = context.Request.Params["userType"]; string userName = context.Request.Params["userName"]; FangYar.Model.BaseUserModel model = bll.GetUserByID(ID); if (model == null) { msg = "用户不存在!"; } else { model.USERS_UID = userUID; model.USERS_PWD = FangYar.Common.Md5.GetMD5String(userPwd); model.USERS_NAME = userName; model.USERS_TYPE = userType; if (bll.EditUser(model)) { 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.Update, "基础用户操作请求", "修改"); return returnstr; } //删除 private string DelUser(HttpContext context) { string returnstr = ""; int code = -1; string msg = ""; try { string UIDList = context.Request.Params["UIDList"]; UIDList = UIDList.Replace(",", "','"); if (bll.DeleteUser(UIDList)) { 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; } //修改密码 private string EditPwd(HttpContext context) { string returnstr = ""; int code = -1; string msg = ""; try { string userUID = context.Request.Params["UsersUid"]; string userPwd = context.Request.Params["oldPwd"]; string newPwd = context.Request.Params["newPwd"]; string confirmPwd = context.Request.Params["confirmPwd"]; FangYar.Model.BaseUserModel model = bll.GetUserByUserID(userUID); if (string.IsNullOrEmpty(userPwd)) { msg = "密码不能为空!"; } else if (newPwd.Length < 4) { msg = "密码长度最少为4个字符!"; } else if (confirmPwd != newPwd) { msg = "两次输入密码不一致!"; } else if (model == null) { msg = "用户不存在!"; } else if (model.USERS_PWD == FangYar.Common.Md5.GetMD5String(newPwd)) { msg = "修改密码不能和旧密码一致!"; } else { model.USERS_PWD = FangYar.Common.Md5.GetMD5String(newPwd); if (bll.ChangePwd(model)) { 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.Update, "基础用户操作请求", "密码修改"); return returnstr; } public bool IsReusable { get { return false; } } } }