public JsonResult UpdateUser(Entity.Users user) { if (user.ID == 0) { return(Json(new Result(false, "参数错误"), JsonRequestBehavior.AllowGet)); } IService.IUsersService usersService = ServiceFactory.Create <IUsersService>(); var dbData = usersService.GetEntity(user.ID, false); //如果不是管理员,是不允许禁用店铺管理员账号的,包括店铺管理员自己也不行 if (!CurrentInfo.IsAdministrator) { if (user.NeedAccount == false && user.ID == CurrentInfo.CurrentShop.AdminUserID) { return(Json(new Result(false, "店铺管理员必须有登录账号"), JsonRequestBehavior.AllowGet)); } } //是否需要账号 if (user.NeedAccount) { if (string.IsNullOrWhiteSpace(user.UserName) || string.IsNullOrWhiteSpace(user.Password)) { return(Json(new Result(false, "用户名和密码不能为空,若无需账号请选择‘不需要登录账号’"), JsonRequestBehavior.AllowGet)); } bool flage = usersService.Exists(t => t.ID != user.ID && t.UserName == user.UserName); if (flage) { return(Json(new Result(false, "已经存在同名用户,请更换"), JsonRequestBehavior.AllowGet)); } //判断用户是否修改了密码 if (dbData.Password != user.Password) { string endPassword = user.Password + dbData.PasswordSalt; user.Password = Common.SecureHelper.MD5(endPassword); } } else { user.UserName = null; user.Password = null; user.PasswordSalt = null; } //user.DefaultStoreID = dbData.DefaultStoreID; user.ShopsID = dbData.ShopsID; if (!string.IsNullOrWhiteSpace(user.Photo)) { if (dbData.Photo != user.Photo) { //生成缩略图 并删除原图 string fileFullName = FileHelper.Move(user.Photo, "/Upload/Reality/" + CurrentInfo.CurrentStore.ID + "/UserImg/"); string extension = System.IO.Path.GetExtension(fileFullName); //缩略图路径 string thumbnailPath = ImgHelper.GetThumbnailPathByWidth(fileFullName, 60); //生成缩略图 ImgHelper.MakeThumbnail( System.Web.HttpContext.Current.Server.MapPath(fileFullName), System.Web.HttpContext.Current.Server.MapPath(thumbnailPath), 60, 60, "W", extension ); FileHelper.DeleteFile(System.Web.HttpContext.Current.Server.MapPath(fileFullName)); user.Photo = thumbnailPath; } } else { user.Photo = dbData.Photo; } if (!string.IsNullOrWhiteSpace(user.IdcardPhoto)) { if (dbData.IdcardPhoto != user.IdcardPhoto) { user.IdcardPhoto = FileHelper.Move(user.IdcardPhoto, "/Upload/Reality/" + CurrentInfo.CurrentStore.ID + "/UserImg/"); } } else { user.IdcardPhoto = dbData.IdcardPhoto; } user.CreateUserID = dbData.CreateUserID; user.CreateTime = dbData.CreateTime; user.Disabled = dbData.Disabled; user.PasswordSalt = dbData.PasswordSalt; //保持上次登录的信息 user.LastLoginArea = dbData.LastLoginArea; user.LastLoginIP = dbData.LastLoginIP; user.LastLoginTime = dbData.LastLoginTime; user.LoginTimes = dbData.LoginTimes; bool success = false; using (TransactionScope transactionScope = TransactionScopeHelper.GetTran()) { success = usersService.UpdateEntity(user); //if (user.WorkTypeID != null) //{ // AdddRelationUserWorkType(user.ID, Convert.ToInt32(user.WorkTypeID)); //} transactionScope.Complete(); } //如果更新的是当前用户资料,则更新用户头像 if (success && CurrentInfo.CurrentUser.ID == user.ID) { CurrentInfo.CurrentUser.Photo = user.Photo; } return(Json(new Result(success, ResultType.Update), JsonRequestBehavior.AllowGet)); }
public JsonResult AddUser(Entity.Users user, int?shopType) { //如果是测试账号则默认门店设置在45下面 if (user.IsIntention == true) { if (shopType == 1) { user.DefaultStoreID = 45; } else if (shopType == 2) { user.DefaultStoreID = 101; } else { return(Json(new Result(false, "该版本还未开通,无法添加"), JsonRequestBehavior.AllowGet)); } } IService.IUsersService usersService = ServiceFactory.Create <IUsersService>(); //是否需要账号 if (user.NeedAccount) { if (string.IsNullOrWhiteSpace(user.UserName) || string.IsNullOrWhiteSpace(user.Password)) { return(Json(new Result(false, "用户名和密码不能为空,若无需账号请选择‘不需要登录账号’"), JsonRequestBehavior.AllowGet)); } bool flage = usersService.Exists(t => t.UserName == user.UserName); if (flage) { return(Json(new Result(false, "已经存在同名用户,请更换"), JsonRequestBehavior.AllowGet)); } //添加账号PasswordSalt随机 user.PasswordSalt = Common.TextFilter.GetPasswordSalt();//Common.TextFilter.Substring(Guid.NewGuid().ToString("N"), 10, false); string endPassword = user.Password + user.PasswordSalt; //计算密码 user.Password = Common.SecureHelper.MD5(endPassword); } //user.DefaultStoreID = CurrentInfo.CurrentStore.ID; IStoresService storesService = ServiceFactory.Create <IStoresService>(); var storesModel = storesService.GetEntity(user.DefaultStoreID.ToString().ToInt32()); user.ShopsID = storesModel.ShopId; if (!string.IsNullOrWhiteSpace(user.Photo)) { //生成缩略图 并删除原图 string fileFullName = FileHelper.Move(user.Photo, "/Upload/Reality/" + CurrentInfo.CurrentStore.ID + "/UserImg/"); string extension = System.IO.Path.GetExtension(fileFullName); //缩略图路径 string thumbnailPath = ImgHelper.GetThumbnailPathByWidth(fileFullName, 60); //生成缩略图 ImgHelper.MakeThumbnail( System.Web.HttpContext.Current.Server.MapPath(fileFullName), System.Web.HttpContext.Current.Server.MapPath(thumbnailPath), 60, 60, "W", extension ); FileHelper.DeleteFile(System.Web.HttpContext.Current.Server.MapPath(fileFullName)); user.Photo = thumbnailPath; //user.Photo = FileHelper.Move(user.Photo, "/Upload/Reality/" + CurrentInfo.CurrentStore.ID + "/UserImg/"); } if (!string.IsNullOrWhiteSpace(user.IdcardPhoto)) { user.IdcardPhoto = FileHelper.Move(user.IdcardPhoto, "/Upload/Reality/" + CurrentInfo.CurrentStore.ID + "/UserImg/"); } user.CreateUserID = CurrentInfo.CurrentUser.ID; user.CreateTime = DateTime.Now; user.Disabled = false; bool success = false; using (TransactionScope transactionScope = TransactionScopeHelper.GetTran()) { var data = usersService.AddEntity(user); //if (data.WorkTypeID != null) //{ // AdddRelationUserWorkType(data.ID, Convert.ToInt32(data.WorkTypeID)); //} transactionScope.Complete(); success = data != null; } return(Json(new Result(success, ResultType.Add), JsonRequestBehavior.AllowGet)); }