Exemplo n.º 1
0
        public IActionResult AddAdmin(IFormCollection fc)
        {
            string userName = fc["UserName"];
            string newPwd   = fc["PassWord"];
            string renewPwd = fc["PassWord2"];
            string roleid   = fc["RoleId"];
            string realname = fc["RealName"];

            if (!Utils.IsInt(roleid))
            {
                tip.Message = "请选择一个管理组!";
                return(Json(tip));
            }

            if (string.IsNullOrEmpty(userName))
            {
                tip.Message = "登录用户名不能为空!";
                return(Json(tip));
            }
            if (Utils.GetStringLength(userName.Trim()) < 5)
            {
                tip.Message = "登录用户名不能小于5个字节!";
                return(Json(tip));
            }
            if (string.IsNullOrEmpty(newPwd))
            {
                tip.Message = "密码不能为空!";
                return(Json(tip));
            }
            if (newPwd.Length < 5)
            {
                tip.Message = "密码不能小于5个字符!";
                return(Json(tip));
            }
            if (newPwd != renewPwd)
            {
                tip.Message = "两次输入密码不一致,请重新输入!";
                return(Json(tip));
            }
            //验证用户名
            if (Core.Admin.FindCount(Core.Admin._.UserName == userName, null, null, 0, 0) > 0)
            {
                tip.Message = "该用户名已经存在,请选择其他用户名!";
                return(Json(tip));
            }

            Core.Admin entity = new Core.Admin();
            entity.UserName = userName;
            entity.RealName = realname;
            entity.Salt     = Utils.GetRandomChar(10);
            entity.PassWord = Utils.MD5(entity.Salt + newPwd);
            entity.RoleId   = int.Parse(roleid);
            entity.Insert();
            tip.Status    = JsonTip.SUCCESS;
            tip.Message   = "添加管理员成功!";
            tip.ReturnUrl = "close";
            Core.Admin.WriteLogActions($"添加新管理员({entity.UserName});");
            return(Json(tip));
        }