/// <summary>
        /// 【注册用户】
        /// </summary>
        /// <param name="newUserInfo"></param>
        /// <returns></returns>
        public ServiceBaseOutput RegistUser(RegistUserInput newUserInfo)
        {
            var result = new ServiceBaseOutput();
            //基本输入信息验证判断
            //判断用户是否存在
            var user = _userDAL.GetModels(con => con.UserName == newUserInfo.UserName)
                       .FirstOrDefault();
            var md5 = new MD5CryptoServiceProvider();

            if (user != null)
            {
                result.Status = false;
                result.Msg    = "用户名已经存在!";
                return(result);
            }
            if (newUserInfo.UserType == 1) //管理员用户
            {
                //检查创建班级名称是否存在
                var classInfo = _classInfoDAL.GetModels(con => con.ClassName == newUserInfo.ClassName).FirstOrDefault();
                if (classInfo != null)
                {
                    result.Status = false;
                    result.Msg    = "班级名称已经存在!";
                    return(result);
                }
                //创建用户
                user          = Mapper.Map <User>(newUserInfo);
                user.Password = BitConverter.ToString(md5.ComputeHash(Encoding.Default.GetBytes(user.UserName + user.Password))).Replace("-", "");
                //创建问题&答案
                //var qa = new List<ClassQuestion>();
                //newUserInfo.QuestionConfig.ForEach(item =>
                //    qa.Add(new ClassQuestion() { Question = item.Question, Answer = item.Answer })
                //);
                //创建班级
                var newClassInfo = new ClassInfo()
                {
                    ClassName  = newUserInfo.ClassName,
                    CreateUser = user,
                    User       = new List <User>()
                    {
                        user
                    },
                    adminUser = new List <User>()
                    {
                        user
                    }
                    //Introduce = newUserInfo.Introduce,
                    //adminUser = new List<User>() { user },
                    //ClassQustion = qa
                };
                _classInfoDAL.Add(newClassInfo);
                try
                {
                    _classInfoDAL.SaveChanges();
                    result.Status = true;
                }
                catch (Exception ex)
                {
                    result.Status = false;
                    result.Msg    = ex.ToString();
                    result.Data   = ex;
                }
            }
            else if (newUserInfo.UserType == 2) //普通用户
            {
                //问题答案验证
                var classInfo = _classInfoDAL.GetModels(con => con.Id == newUserInfo.ClassId).FirstOrDefault();

                var anSwerResult = true;
                classInfo.ClassQustion.ToList().ForEach(
                    item => {
                    if (newUserInfo.QuestionConfig.Find(con => con.Question == item.Question && con.Answer == item.Answer) is null)
                    {
                        anSwerResult = false;
                    }
                }
                    );
                if (!anSwerResult)
                {
                    result.Status = false;
                    result.Msg    = "班级问题验证失败!";
                }
                else
                {
                    user = Mapper.Map <User>(newUserInfo);
                    //密码加密
                    user.Password = BitConverter.ToString(md5.ComputeHash(Encoding.Default.GetBytes(user.UserName + user.Password))).Replace("-", "");
                    classInfo.User.Add(user);
                    _classInfoDAL.Update(classInfo);
                    try
                    {
                        _classInfoDAL.SaveChanges();
                        result.Status = true;
                    }
                    catch (Exception ex)
                    {
                        result.Status = false;
                        result.Msg    = ex.ToString();
                        result.Data   = ex;
                    }
                }
            }
            else
            {
                result.Status = false;
                result.Msg    = "用户类型不确定";
            }
            return(result);
        }
示例#2
0
        /// <summary>
        /// 【注册新用户接收方法】
        /// </summary>
        /// <returns></returns>
        public JsonResult Regist(RegistUserInput newUser)
        {
            //基本判断

            return(Json(_userService.RegistUser(newUser), JsonRequestBehavior.AllowGet));
        }