Esempio n. 1
0
        public ActionResult Admin(SysAdmin model, int[] Roles)
        {
            if (model.UserName.IsNullOrEmpty())
            {
                return(JsonError("登录名不能为空"));
            }

            if (model.Name.IsNullOrEmpty())
            {
                return(JsonError("名称不能为空"));
            }

            if (model.Id > 0)
            {
                var sysAdmin = _sysAdminService.Get(m => m.Id == model.Id);
                if (sysAdmin != null && sysAdmin.Id > 0)
                {
                    sysAdmin.Name     = model.Name;
                    sysAdmin.UserName = model.UserName;
                    if (!model.Password.IsNullOrEmpty())
                    {
                        if (model.Password.IsContainCharAndNum())
                        {
                            return(JsonError("登录密码格式不正确"));
                        }
                        if (model.Password != GetFormString("ConfirmPassword"))
                        {
                            return(JsonError("两次密码输入不一样"));
                        }

                        sysAdmin.Password = Infrastructure.Security.DesEncrypt(model.Password);
                    }
                    sysAdmin.LastLoginIp   = Request.GetUserIP();
                    sysAdmin.LastLoginTime = DateTime.Now;
                    sysAdmin.IsAdmin       = GetFormCheck("IsAdmin");
                    sysAdmin.Enabled       = GetFormCheck("Enabled");
                    _sysAdminRoleService.Delete(m => m.AdminId == sysAdmin.Id);
                    if (_sysAdminService.Update(sysAdmin) > 0)
                    {
                        _sysAdminRoleService.Insert(Roles.Select(m => new SysAdminRole
                        {
                            AdminId = sysAdmin.Id,
                            RoleId  = m
                        }).ToArray());
                        return(JsonSuccess(sysAdmin.Id.ToString()));
                    }
                }
            }
            else
            {
                if (model.Password.IsContainCharAndNum())
                {
                    return(JsonError("登录密码格式不正确"));
                }
                if (model.Password != GetFormString("ConfirmPassword"))
                {
                    return(JsonError("两次密码输入不一样"));
                }

                var sysAdmin = new SysAdmin
                {
                    Name          = model.Name,
                    UserName      = model.UserName,
                    Password      = Infrastructure.Security.DesEncrypt(model.Password),
                    LastLoginIp   = Request.GetUserIP(),
                    LastLoginTime = DateTime.Now,
                    IsAdmin       = GetFormCheck("IsAdmin"),
                    Enabled       = GetFormCheck("Enabled")
                };
                if (_sysAdminService.Insert(sysAdmin) > 0)
                {
                    _sysAdminRoleService.Insert(Roles.Select(m => new SysAdminRole
                    {
                        AdminId = sysAdmin.Id,
                        RoleId  = m
                    }).ToArray());
                    return(JsonSuccess(sysAdmin.Id.ToString()));
                }
            }

            return(JsonError("操作失败,请稍后重试"));
        }