Example #1
0
        /// <summary>
        /// 处理重置密码的请求,并返回处理结果
        /// </summary>
        /// <param name="boVM"></param>
        /// <returns></returns>
        public async Task <IActionResult> SaveResetUserPassword([Bind("Id,Password,ConfirmPassword")] ApplicationUserResetPasswordVM boVM)
        {
            var validateMessage = new ValidateMessage();

            if (ModelState.IsValid)
            {
                var user = await _UserManager.FindByIdAsync(boVM.Id);

                // 获取重置密码的令牌
                string resetToken = await _UserManager.GeneratePasswordResetTokenAsync(user);

                // 重置密码
                await _UserManager.ResetPasswordAsync(user, resetToken, boVM.Password);

                validateMessage.IsOK = true;
                validateMessage.ValidateMessageItems.Add(
                    new ValidateMessageItem
                {
                    IsPropertyName = false,
                    MessageName    = "",
                    Message        = "密码重置成功,请关闭对话框。"
                });

                return(Json(validateMessage));
            }
            else
            {
                validateMessage.IsOK = false;
                var errCollection = from errKey in ModelState.Keys
                                    from errMessage in ModelState[errKey].Errors
                                    where ModelState[errKey].Errors.Count > 0
                                    select(new { errKey, errMessage.ErrorMessage });
                foreach (var errItem in errCollection)
                {
                    var vmItem = new ValidateMessageItem()
                    {
                        IsPropertyName = true,
                        MessageName    = errItem.errKey,
                        Message        = errItem.ErrorMessage
                    };
                    validateMessage.ValidateMessageItems.Add(vmItem);
                }
                return(Json(validateMessage));
            }
        }
Example #2
0
        /// <summary>
        /// 保存新建用户数据处理部分
        /// </summary>
        /// <param name="boVM">用户视图模型的实例</param>
        /// <returns></returns>
        public async Task <IActionResult> SaveCreateApplicationUser([Bind("Id,IsNew,RoleItemIdCollection,UserName,Name,MobileNumber,EMail,Password,ConfirmPassword,Description")] ApplicationUserVM boVM)
        {
            var validateMessage = new ValidateMessage();

            if (ModelState.IsValid)
            {
                // 检查重名
                var isNewUser = await _UserManager.FindByNameAsync(boVM.UserName);

                if (_HasTheSameUser(boVM.UserName))
                {
                    validateMessage.IsOK = false;
                    validateMessage.ValidateMessageItems.Add(
                        new ValidateMessageItem()
                    {
                        IsPropertyName = true, MessageName = "UserName", Message = "用户选择的用户名已经被使用了。"
                    });
                    return(Json(validateMessage));
                }

                #region 新增用户的常规处理
                var user = new ApplicationUser(boVM.UserName)
                {
                    FirstName       = "",
                    ChineseFullName = boVM.Name,
                    Email           = boVM.EMail,
                    MobileNumber    = boVM.MobileNumber
                };

                // 校验密码强度
                var passwordValidator = new Microsoft.AspNetCore.Identity.PasswordValidator <ApplicationUser>();
                var result            = await passwordValidator.ValidateAsync(_UserManager, user, boVM.Password);

                if (!result.Succeeded)
                {
                    validateMessage.IsOK = false;
                    validateMessage.ValidateMessageItems.Add(
                        new ValidateMessageItem()
                    {
                        IsPropertyName = true, MessageName = "Password", Message = "您输入的密码强度未满足要求:密码长度大于6个字符,且必须包含数字、大写字母、小写字母和非字母字符如 @ 。"
                    });
                    return(Json(validateMessage));
                }

                await _UserManager.CreateAsync(user);

                await _UserManager.AddPasswordAsync(user, boVM.Password);  // 添加密码

                if (boVM.RoleItemIdCollection != null)
                {
                    foreach (var item in boVM.RoleItemIdCollection)             // 遍历所选择的用户组的Id集合,加入到相关的角色组
                    {
                        var role = await _RoleManager.FindByIdAsync(item);

                        if (role != null)
                        {
                            await _UserManager.AddToRoleAsync(user, role.Name);  // 将用户添加到相应的用户组内
                        }
                    }
                }
                #endregion

                validateMessage.IsOK = true;
                validateMessage.ValidateMessageItems.Add(
                    new ValidateMessageItem
                {
                    IsPropertyName = false,
                    MessageName    = "Succeed",
                    Message        = ""
                });

                return(Json(validateMessage));   // 返回处理结果
            }
            else
            {
                #region 1.待选择的用户组数据
                var user = await _UserManager.FindByIdAsync(boVM.Id.ToString());

                var role      = _RoleManager.Roles;
                var roleItems = new List <PlainFacadeItem>();
                foreach (var item in _RoleManager.Roles.OrderBy(x => x.SortCode))
                {
                    var rItem = new PlainFacadeItem()
                    {
                        Id = item.Id, Name = item.Name, DisplayName = item.DisplayName, SortCode = item.SortCode
                    };
                    roleItems.Add(rItem);
                }
                boVM.RoleItemColection = roleItems;
                #endregion

                validateMessage.IsOK = false;
                var errCollection = from errKey in ModelState.Keys
                                    from errMessage in ModelState[errKey].Errors
                                    where ModelState[errKey].Errors.Count > 0
                                    select(new { errKey, errMessage.ErrorMessage });

                foreach (var errItem in errCollection)
                {
                    var vmItem = new ValidateMessageItem()
                    {
                        IsPropertyName = true,
                        MessageName    = errItem.errKey,
                        Message        = errItem.ErrorMessage
                    };
                    validateMessage.ValidateMessageItems.Add(vmItem);
                }
                return(Json(validateMessage));
            }
        }
Example #3
0
        /// <summary>
        /// 保存用户资料
        /// </summary>
        /// <param name="boVM">用户的视图模型</param>
        /// <returns></returns>
        public async Task <IActionResult> SaveEditApplicationUser([Bind("Id,RoleId,IsNew,RoleItemIdCollection,UserName,Name,MobileNumber,EMail,Password,ConfirmPassword,Description")] ApplicationUserForEditVM boVM)
        {
            var validateMessage = new ValidateMessage();

            if (ModelState.IsValid)
            {
                if (boVM.RoleItemIdCollection == null)
                {
                    boVM.RoleItemIdCollection = new List <string>();
                }
                var user = await _UserManager.FindByIdAsync(boVM.Id.ToString());

                #region 1.用户基本资料更新
                user.FirstName       = "";
                user.ChineseFullName = boVM.Name;
                user.Email           = boVM.EMail;
                user.MobileNumber    = boVM.MobileNumber;
                await _UserManager.UpdateAsync(user);

                #endregion

                #region 2.归属角色组的处理
                var roleCollection = _RoleManager.Roles;
                var addRoles       = new List <string>();
                var removeRoles    = new List <string>();
                foreach (var roleItem in roleCollection)
                {
                    // 当前的用户是否归属角色组:roleItem
                    var h = await _UserManager.IsInRoleAsync(user, roleItem.Name);

                    if (!h)
                    {
                        // 不归属,但前端已经选择了相应的角色组,则将当前用户加入相应的角色组。
                        if (boVM.RoleItemIdCollection.Contains(roleItem.Id))
                        {
                            addRoles.Add(roleItem.Name);
                        }
                    }
                    else
                    {
                        // 归属,但前端并未选择相应的角色组,则将当前用户从相关的组中移除。
                        if (!boVM.RoleItemIdCollection.Contains(roleItem.Id))
                        {
                            removeRoles.Add(roleItem.Name);
                        }
                    }
                }
                await _UserManager.AddToRolesAsync(user, addRoles);

                await _UserManager.RemoveFromRolesAsync(user, removeRoles);

                #endregion
                var resultRoleId = "";
                if (boVM.RoleId.ToString() != "00000000-0000-0000-0000-000000000000")
                {
                    resultRoleId = boVM.RoleId.ToString();
                }
                validateMessage.IsOK = true;
                validateMessage.ValidateMessageItems.Add(
                    new ValidateMessageItem
                {
                    IsPropertyName = false,
                    MessageName    = "",
                    Message        = resultRoleId
                });

                return(Json(validateMessage));
            }
            else
            {
                #region 处理用户归属的用户组的数据
                #region 1.待选择的用户组数据
                var user = await _UserManager.FindByIdAsync(boVM.Id.ToString());

                var role      = _RoleManager.Roles;
                var roleItems = new List <PlainFacadeItem>();
                foreach (var item in _RoleManager.Roles.OrderBy(x => x.SortCode))
                {
                    var rItem = new PlainFacadeItem()
                    {
                        Id = item.Id, Name = item.Name, DisplayName = item.DisplayName, SortCode = item.SortCode
                    };
                    roleItems.Add(rItem);
                }
                boVM.RoleItemColection = roleItems;
                #endregion
                #region 2.已经归属的用户组部分
                boVM.RoleItemIdCollection = (
                    from roleItem in _RoleManager.Roles
                    where roleItem.Users.Select(x => x.UserId).Contains(user.Id)
                    select roleItem.Id
                    ).ToList();;

                #endregion
                #endregion

                validateMessage.IsOK = false;
                var errCollection = from errKey in ModelState.Keys
                                    from errMessage in ModelState[errKey].Errors
                                    where ModelState[errKey].Errors.Count > 0
                                    select(new { errKey, errMessage.ErrorMessage });
                foreach (var errItem in errCollection)
                {
                    var vmItem = new ValidateMessageItem()
                    {
                        IsPropertyName = true,
                        MessageName    = errItem.errKey,
                        Message        = errItem.ErrorMessage
                    };
                    validateMessage.ValidateMessageItems.Add(vmItem);
                }
                return(Json(validateMessage));
            }
        }
Example #4
0
        /// <summary>
        /// 保存新建用户数据处理部分
        /// </summary>
        /// <param name="boVM">用户视图模型的实例</param>
        /// <returns></returns>
        public async Task <IActionResult> SaveCreateApplicationUser([Bind("ID,IsNew,RoleItemIDCollection,UserName,Name,MobileNumber,EMail,Password,ConfirmPassword,Description")] ApplicationUserVM boVM)
        {
            var validateMessage = new ValidateMessage();

            if (ModelState.IsValid)
            {
                // 检查重名
                var isNewUser = await _UserManager.FindByNameAsync(boVM.UserName);

                if (_HasTheSameUser(boVM.UserName))
                {
                    validateMessage.IsOK = false;
                    validateMessage.ValidateMessageItems.Add(
                        new ValidateMessageItem()
                    {
                        IsPropertyName = false, MessageName = "RemoteErr", Message = "用户选择的用户名已经被使用了"
                    });
                    return(Json(validateMessage));
                }

                #region 新增用户的常规处理
                var user = new ApplicationUser(boVM.UserName)
                {
                    FirstName       = "",
                    ChineseFullName = boVM.Name,
                    Email           = boVM.EMail,
                    MobileNumber    = boVM.MobileNumber
                };
                await _UserManager.CreateAsync(user);

                await _UserManager.AddPasswordAsync(user, boVM.Password);  // 添加密码

                var roleItem = await _RoleManager.FindByNameAsync("Adimn");

                foreach (var item in boVM.RoleItemIDCollection)             // 遍历所选择的用户组的ID集合,加入到相关的角色组
                {
                    var role = await _RoleManager.FindByIdAsync(item);

                    if (role != null)
                    {
                        await _UserManager.AddToRoleAsync(user, role.Name);
                    }
                }
                #endregion

                validateMessage.IsOK = true;
                validateMessage.ValidateMessageItems.Add(
                    new ValidateMessageItem
                {
                    IsPropertyName = false,
                    MessageName    = "Succeed",
                    Message        = ""
                });

                return(Json(validateMessage));
            }
            else
            {
                #region 1.待选择的用户组数据
                var user = await _UserManager.FindByIdAsync(boVM.ID.ToString());

                var role      = _RoleManager.Roles;
                var roleItems = new List <PlainFacadeItem>();
                foreach (var item in _RoleManager.Roles.OrderBy(x => x.SortCode))
                {
                    var rItem = new PlainFacadeItem()
                    {
                        ID = item.Id, Name = item.Name, DisplayName = item.DisplayName, SortCode = item.SortCode
                    };
                    roleItems.Add(rItem);
                }
                boVM.RoleItemColection = roleItems;
                #endregion

                validateMessage.IsOK = false;
                var errCollection = from errKey in ModelState.Keys
                                    from errMessage in ModelState[errKey].Errors
                                    where ModelState[errKey].Errors.Count > 0
                                    select(new { errKey, errMessage.ErrorMessage });

                foreach (var errItem in errCollection)
                {
                    var vmItem = new ValidateMessageItem()
                    {
                        IsPropertyName = true,
                        MessageName    = errItem.errKey,
                        Message        = errItem.ErrorMessage
                    };
                    validateMessage.ValidateMessageItems.Add(vmItem);
                }
                return(Json(validateMessage));
            }
        }
Example #5
0
        /// <summary>
        /// 普通读者用户资料注册管理
        /// </summary>
        /// <param name="jsonRegisetrInformation"></param>
        /// <returns></returns>
        public async Task <IActionResult> Register(string jsonRegisterInformation)
        {
            var validateMessage = new ValidateMessage();
            var loUser          = Newtonsoft.Json.JsonConvert.DeserializeObject <LogonInformation>(jsonRegisterInformation);//获取前端数据放到用户视图模型里边

            if (loUser.ConfirmPassword != loUser.Password)
            {
                validateMessage.IsOK = false;
                validateMessage.ValidateMessageItems.Add(
                    new ValidateMessageItem
                {
                    IsPropertyName = false,
                    MessageName    = "Succeed",
                    Message        = "密码和确认密码不一致",
                });
                return(Json(validateMessage));
            }
            if (ModelState.IsValid)
            {
                var isNewUser = await _userManager.FindByNameAsync(loUser.UserName);

                if (isNewUser != null)
                {
                    validateMessage.IsOK = false;
                    validateMessage.ValidateMessageItems.Add(
                        new ValidateMessageItem
                    {
                        IsPropertyName = false,
                        MessageName    = "Succeed",
                        Message        = "登录名重复",
                    });
                    return(Json(validateMessage));
                }
                var user = new ApplicationUser()
                {
                    Name               = loUser.UserName,
                    UserName           = loUser.UserName,
                    TwoFactorEnabled   = true,
                    Email              = loUser.Email,
                    NormalizedUserName = loUser.UserName,
                    SecurityStamp      = Guid.NewGuid().ToString(),
                    Avatar             = await _businessImageExtension.GetAll().Where(x => x.Name == "头像" && x.IsSystem).FirstOrDefaultAsync(),
                    RegisterTime       = DateTime.Now,
                    Power              = AnJurisdiction.Ordinary,
                };
                _userExtension.Add(user);
                user.PasswordHash = new PasswordHasher <ApplicationUser>(new OptionsWrapper <PasswordHasherOptions>(new PasswordHasherOptions())).HashPassword(user, loUser.Password);
                await _userExtension.SaveAsyn();

                validateMessage.IsOK = true;
                validateMessage.ValidateMessageItems.Add(
                    new ValidateMessageItem
                {
                    IsPropertyName = false,
                    MessageName    = "Succeed",
                    Message        = "注册成功,请登录"
                });

                return(Json(validateMessage));
            }
            else
            {
                validateMessage.IsOK = false;
                var errCollection = from errKey in ModelState.Keys
                                    from errMessage in ModelState[errKey].Errors
                                    where ModelState[errKey].Errors.Count > 0
                                    select(new { errKey, errMessage.ErrorMessage });

                foreach (var errItem in errCollection)
                {
                    var vmItem = new ValidateMessageItem()
                    {
                        IsPropertyName = true,
                        MessageName    = errItem.errKey,
                        Message        = errItem.ErrorMessage
                    };
                    validateMessage.ValidateMessageItems.Add(vmItem);
                }
                return(Json(validateMessage));
            }
        }
Example #6
0
        public async Task <IActionResult> SaveSettled([Bind("ID,IsNew,Step,Name,Address,UserName,IDCar,MobilePhone,Telephone,LicenceID")] SK_WM_ShopSttledVM boVM)
        {
            var validateMessage = new ValidateMessage();

            if (ModelState.IsValid)
            {
                var username = User.Identity.Name;
                var user     = await _UserManager.FindByNameAsync(username);

                var hasDuplicateNameShop = await _ShopSelltedRepository.HasInstanceAsyn(x => x.Name == boVM.Name);

                if (hasDuplicateNameShop && boVM.IsNew)
                {
                    validateMessage.IsOK = false;
                    validateMessage.ValidateMessageItems.Add(
                        new ValidateMessageItem()
                    {
                        IsPropertyName = false, MessageName = "Name", Message = "已经有店铺叫这个名了,换一个试试看吧"
                    });
                    return(Json(validateMessage));
                }

                var bo = new SK_WM_ShopSttled();
                bo.ID = boVM.ID;
                if (!boVM.IsNew)
                {
                    bo = await _ShopSelltedRepository.GetSingleAsyn(boVM.ID);
                }

                // 处理一般的属性数据
                boVM.MapToShop(bo);

                var saveStatus = false;

                //处理上传文件
                var  serverPath     = "";
                long size           = 0;
                var  files          = Request.Form.Files;
                var  boImg          = _ImageRepository.GetAll().Where(x => x.RelevanceObjectID == bo.ID);
                var  FrontIDCarimg  = boImg.Where(x => x.Description == "FrontIDCar").FirstOrDefault();
                var  backIDCarimg   = boImg.Where(x => x.Description == "BackIDCar").FirstOrDefault();
                var  Environmentimg = boImg.Where(x => x.Description == "Environment").FirstOrDefault();
                var  Licenceimg     = boImg.Where(x => x.Description == "Licence").FirstOrDefault();

                var fName = files.Where(x => x.Name == "FrontIDCar").FirstOrDefault();
                var bName = files.Where(x => x.Name == "BackIDCar").FirstOrDefault();
                var eName = files.Where(x => x.Name == "Environment").FirstOrDefault();
                var lName = files.Where(x => x.Name == "Environment").FirstOrDefault();

                if (boVM.IsNew == false)
                {
                    if (/*(backIDCarimg == null || FrontIDCarimg == null)&& */ (fName == null || bName == null) && boVM.Step == 1)
                    {
                        validateMessage.IsOK = false;
                        validateMessage.ValidateMessageItems.Add(
                            new ValidateMessageItem()
                        {
                            IsPropertyName = false, MessageName = "IdImg", Message = "请将身份信息补充完整再执行下一步"
                        });
                        return(Json(validateMessage));
                    }
                    if (/*(backIDCarimg != null || FrontIDCarimg != null)*//*&&(Environmentimg == null || Licenceimg == null) && */ (eName == null || lName == null) && (fName != null || bName != null) && boVM.Step == 2)
                    {
                        validateMessage.IsOK = false;
                        validateMessage.ValidateMessageItems.Add(
                            new ValidateMessageItem()
                        {
                            IsPropertyName = false, MessageName = "SettledImg", Message = "请将店铺信息补充完整再执行下一步"
                        });
                        return(Json(validateMessage));
                    }
                }
                if (files.Count() > 0)
                {
                    foreach (var file in files)
                    {
                        var fileName = ContentDispositionHeaderValue
                                       .Parse(file.ContentDisposition)
                                       .FileName
                                       .Trim('"')
                                       .Substring(files[0].FileName.LastIndexOf("\\") + 1);

                        fileName = bo.ID + "_" + fileName;

                        var boPath = "../../images/ShopSettledImg/" + fileName;
                        fileName   = _HostingEnv.WebRootPath + $@"\images\ShopSettledImg\{fileName}";
                        serverPath = fileName;
                        size      += file.Length;
                        using (FileStream fs = System.IO.File.Create(fileName))
                        {
                            file.CopyTo(fs);
                            fs.Flush();
                        }

                        var businessIamge = new BusinessImage()
                        {
                            DisplayName       = bo.Name,
                            RelevanceObjectID = bo.ID,
                            UploadPath        = boPath,

                            Description = file.Name
                        };

                        if (file.Name == "Licence")
                        {
                            bo.Licence    = businessIamge;
                            ViewBag.LiImg = businessIamge.UploadPath;
                        }
                        else if (file.Name == "Environment")
                        {
                            bo.Environment = businessIamge;
                            ViewBag.EnIMg  = businessIamge.UploadPath;
                        }
                        else if (file.Name == "BackIDCar")
                        {
                            if (backIDCarimg == null)
                            {
                                bo.BackIDCar  = businessIamge;
                                ViewBag.BaImg = businessIamge.UploadPath;
                            }
                            else
                            {
                                backIDCarimg.UploadPath = businessIamge.UploadPath;
                                bo.BackIDCar            = backIDCarimg;
                                ViewBag.BaImg           = backIDCarimg.UploadPath;
                            }
                        }
                        else if (file.Name == "FrontIDCar")
                        {
                            if (FrontIDCarimg == null)
                            {
                                bo.FrontIDCar = businessIamge;
                                ViewBag.FCimg = businessIamge.UploadPath;
                            }
                            else
                            {
                                FrontIDCarimg.UploadPath = businessIamge.UploadPath;
                                bo.FrontIDCar            = FrontIDCarimg;
                                ViewBag.FCimg            = FrontIDCarimg.UploadPath;
                            }
                        }
                    }
                }

                bo.ShopForUser    = user;
                bo.BelongToUserID = user.Id;
                saveStatus        = await _ShopSelltedRepository.AddOrEditAndSaveAsyn(bo);

                if (saveStatus)
                {
                    validateMessage.IsOK = true;
                    validateMessage.ValidateMessageItems.Add(
                        new ValidateMessageItem
                    {
                        IsPropertyName = false,
                        MessageName    = "Succeed",
                        Message        = ""
                    });

                    return(Json(validateMessage));
                }
                else
                {
                    validateMessage.IsOK = false;
                    validateMessage.ValidateMessageItems.Add(
                        new ValidateMessageItem()
                    {
                        IsPropertyName = false, MessageName = "RemoteErr", Message = "数据保存异常"
                    });
                    return(Json(validateMessage));
                }
            }
            else
            {
                //return View("../../Views/BusinessOrganization/BusinessBG/CreateOrEdit", boVM);
                validateMessage.IsOK = false;
                var errCollection = from errKey in ModelState.Keys
                                    from errMessage in ModelState[errKey].Errors
                                    where ModelState[errKey].Errors.Count > 0
                                    select(new { errKey, errMessage.ErrorMessage });

                foreach (var errItem in errCollection)
                {
                    var vmItem = new ValidateMessageItem()
                    {
                        IsPropertyName = true,
                        MessageName    = errItem.errKey,
                        Message        = errItem.ErrorMessage
                    };
                    validateMessage.ValidateMessageItems.Add(vmItem);
                }
                return(Json(validateMessage));
            }
        }
Example #7
0
        public async Task <IActionResult> Register([Bind("ID,IsNew,RoleItemIDCollection,UserName,Name,MobileNumber,EMail,Password,ConfirmPassword,Description")] ApplicationUserVM boVM)
        {
            var validateMessage = new ValidateMessage();

            if (ModelState.IsValid)
            {
                // 检查重名
                var isNewUser = await _UserManager.FindByNameAsync(boVM.UserName);


                if (_HasTheSameUser(boVM.UserName))
                {
                    validateMessage.IsOK = false;
                    validateMessage.ValidateMessageItems.Add(
                        new ValidateMessageItem()
                    {
                        IsPropertyName = false,
                        MessageName    = "UserName",
                        Message        = "用户选择的用户名已经被使用了"
                    });
                    return(Json(validateMessage));
                }


                var user = new ApplicationUser(boVM.UserName)
                {
                    FirstName       = "",
                    ChineseFullName = boVM.Name,
                    Email           = boVM.EMail,
                    MobileNumber    = boVM.MobileNumber
                };

                var a1 = await _UserManager.CreateAsync(user);

                var a2 = await _UserManager.AddPasswordAsync(user, boVM.Password);        // 添加密码

                //var roleItem = await _RoleManager.FindByNameAsync("Admin");

                //if (roleItem != null)
                //{
                //    await _UserManager.AddToRoleAsync(user, roleItem.Name);
                //}
                //var a3 = await _UserManager.AddToRoleAsync(user, "AverageUser");    // 加入到 AverageUser 角色组

                var roleAverageUser = await _RoleManager.FindByNameAsync("AverageUser");

                if (roleAverageUser != null)
                {
                    await _UserManager.AddToRoleAsync(user, roleAverageUser.Name);  // 将用户添加到相应的用户组内
                }


                validateMessage.IsOK = true;
                validateMessage.ValidateMessageItems.Add(
                    new ValidateMessageItem
                {
                    IsPropertyName = false,
                    MessageName    = "Succeed",
                    Message        = ""
                });

                return(Json(validateMessage));
            }
            else
            {
                validateMessage.IsOK = false;
                var errCollection = from errKey in ModelState.Keys
                                    from errMessage in ModelState[errKey].Errors
                                    where ModelState[errKey].Errors.Count > 0
                                    select(new { errKey, errMessage.ErrorMessage });

                foreach (var errItem in errCollection)
                {
                    var vmItem = new ValidateMessageItem()
                    {
                        IsPropertyName = true,
                        MessageName    = errItem.errKey,
                        Message        = errItem.ErrorMessage
                    };
                    validateMessage.ValidateMessageItems.Add(vmItem);
                }
                return(Json(validateMessage));
            }

            #region 早期的代码
            //var userVM = Newtonsoft.Json.JsonConvert.DeserializeObject<ApplicationUserVM>(jsonRegisterInformation);
            //var user = await _UserManager.FindByNameAsync(userVM.UserName);
            //if (user == null)
            //{
            //    user = new ApplicationUser(userVM.UserName);
            //    user.FirstName = "";
            //    user.ChineseFullName = userVM.Name;
            //    user.Email = userVM.EMail;
            //    user.MobileNumber = userVM.MobileNumber;
            //    var a1 = await _UserManager.CreateAsync(user);
            //    var a2 = await _UserManager.AddPasswordAsync(user, userVM.Password);        // 添加密码
            //    /*var a3 = await _UserManager.AddToRoleAsync(user, "AverageUser");  */  // 加入到 AverageUser 角色组
            //    //var a3 = await _UserManager.AddToRoleAsync(user, "RegisterReadersRole");    // 加入到 RegisterReadersRole 角色组
            //    return Json(new { result = true, message = "注册成功,恭喜您成为食刻中的一员,祝您购物愉快!" });
            //}
            //else
            //{
            //    return Json(new { result = false, message = "已经存在相同用户名的用户,请另外选择一个用户名。" });
            //}

            #endregion
        }
Example #8
0
        public async Task <IActionResult> Save([Bind("ID,IsNew,Name,GoodsCategoryID,Description,SortCode,ShelvesTime,ModifyTime,Price,FacadePrice,Unit,SalesVolume,Stock,State")] SK_WM_GoodsVM boVM)
        {
            var validateMessage = new ValidateMessage();

            if (ModelState.IsValid)
            {
                var username = User.Identity.Name;
                var user     = await _UserManager.FindByNameAsync(username);

                var shop = await _ShopRepository.FindByAsyn(x => x.BelongToUserID == user.Id);

                var hasDuplicateNmaeGoods = await _BoRepository.HasInstanceAsyn(x => x.Name == boVM.Name);

                if (hasDuplicateNmaeGoods && boVM.IsNew)
                {
                    validateMessage.IsOK = false;
                    validateMessage.ValidateMessageItems.Add(
                        new ValidateMessageItem()
                    {
                        IsPropertyName = false, MessageName = "Name", Message = "商品名重复,换一个试试看吧"
                    });
                    return(Json(validateMessage));
                }

                var bo = new SK_WM_Goods();
                if (!boVM.IsNew)
                {
                    bo = await _BoRepository.GetSingleAsyn(boVM.ID);

                    if (bo.BelongToUserID != user.Id)
                    {
                        validateMessage.IsOK = false;
                        validateMessage.ValidateMessageItems.Add(
                            new ValidateMessageItem()
                        {
                            IsPropertyName = false, MessageName = "Name", Message = "该商品不是你的,你没有权限编辑"
                        });
                        return(Json(validateMessage));
                    }
                }

                // 处理一般的属性数据
                boVM.MapToBo(bo);

                // 处理关联数据
                if (!String.IsNullOrEmpty(boVM.GoodsCategoryID))
                {
                    var skwmgclssID = Guid.Parse(boVM.GoodsCategoryID);
                    var skwmgclss   = _BoRepository.EntitiesContext.SK_WM_GoodsCategory.FirstOrDefault(x => x.ID == skwmgclssID);
                    bo.SK_WM_GoodsCategory = skwmgclss;
                }

                var saveStatus = false;

                //处理上传文件
                var  serverPath = "";
                long size       = 0;
                var  files      = Request.Form.Files;

                if (files.Count() > 0)
                {
                    int i = 0;
                    foreach (var file in files)
                    {
                        var fileName = ContentDispositionHeaderValue
                                       .Parse(file.ContentDisposition)
                                       .FileName
                                       .Trim('"')
                                       .Substring(files[0].FileName.LastIndexOf("\\") + 1);

                        fileName = bo.ID + "_" + fileName;

                        var boPath = "../../images/goodsImg/" + fileName;
                        fileName   = _HostingEnv.WebRootPath + $@"\images\goodsImg\{fileName}";
                        serverPath = fileName;
                        size      += file.Length;
                        using (FileStream fs = System.IO.File.Create(fileName))
                        {
                            file.CopyTo(fs);
                            fs.Flush();
                        }

                        var businessIamge = new BusinessImage()
                        {
                            DisplayName       = bo.Name,
                            RelevanceObjectID = bo.ID,
                            UploadPath        = boPath
                        };
                        if (i == 0)
                        {
                            businessIamge.IsForTitle = true;
                        }

                        bo.GoodsIMG = businessIamge;
                        i++;
                        await _ImageRepository.AddOrEditAndSaveAsyn(businessIamge);
                    }
                }

                bo.ShopForUser    = user;
                bo.BelongToUserID = user.Id;
                bo.BelongToShopID = shop.FirstOrDefault().ID.ToString();
                bo.Shop           = shop.FirstOrDefault();
                saveStatus        = await _BoRepository.AddOrEditAndSaveAsyn(bo);

                if (saveStatus)
                {
                    validateMessage.IsOK = true;
                    validateMessage.ValidateMessageItems.Add(
                        new ValidateMessageItem
                    {
                        IsPropertyName = false,
                        MessageName    = "Succeed",
                        Message        = ""
                    });

                    return(Json(validateMessage));
                }
                else
                {
                    validateMessage.IsOK = false;
                    validateMessage.ValidateMessageItems.Add(
                        new ValidateMessageItem()
                    {
                        IsPropertyName = false, MessageName = "RemoteErr", Message = "数据保存异常"
                    });
                    return(Json(validateMessage));
                }
            }
            else
            {
                //return View("../../Views/BusinessOrganization/BusinessBG/CreateOrEdit", boVM);
                validateMessage.IsOK = false;
                var errCollection = from errKey in ModelState.Keys
                                    from errMessage in ModelState[errKey].Errors
                                    where ModelState[errKey].Errors.Count > 0
                                    select(new { errKey, errMessage.ErrorMessage });

                foreach (var errItem in errCollection)
                {
                    var vmItem = new ValidateMessageItem()
                    {
                        IsPropertyName = true,
                        MessageName    = errItem.errKey,
                        Message        = errItem.ErrorMessage
                    };
                    validateMessage.ValidateMessageItems.Add(vmItem);
                }
                return(Json(validateMessage));
            }
        }