コード例 #1
0
 public override void OnActionExecuting(ActionExecutingContext context)
 {
     _MyUserInfo = context.HttpContext.Session.Get <PyUserInfo>(context.HttpContext.Session.SessionKey());
     if (_MyUserInfo == null)
     {
         context.Result = new RedirectToActionResult(nameof(MemberController.Login), "Member", new { ReturnUrl = context.HttpContext.Request.Path });
     }
     ViewData["MyUserInfo"] = _MyUserInfo;
     base.OnActionExecuting(context);
 }
コード例 #2
0
        public async Task <IActionResult> ModifyUser(PyUserInfo pyUserInfo)
        {
            if (pyUserInfo.Id < 0)
            {
                this.MsgBox("修改失败,请稍后重试。");
                return(View(_MyUserInfo));
            }
            else if (string.IsNullOrWhiteSpace(pyUserInfo.NickName))
            {
                this.MsgBox("昵称不能为空!");
                return(View(_MyUserInfo));
            }

            _MyUserInfo.NickName = pyUserInfo.NickName;
            _MyUserInfo.Tel      = pyUserInfo.Tel;
            _MyUserInfo.Sex      = pyUserInfo.Sex;
            _MyUserInfo.Birthday = pyUserInfo.Birthday;

            _MyUserInfo.Blog      = pyUserInfo.Blog;
            _MyUserInfo.Introduce = pyUserInfo.Introduce;

            var user = _db.ToUserInfo
                       .Where(b => b.Id == _MyUserInfo.Id)
                       .SingleOrDefault();

            if (user == null)
            {
                this.MsgBox("修改失败,请稍后重试");
                return(View(_MyUserInfo));
            }

            user.NickName = _MyUserInfo.NickName;
            user.Tel      = _MyUserInfo.Tel;
            user.Sex      = _MyUserInfo.Sex;
            user.Birthday = _MyUserInfo.Birthday;

            user.Blog      = _MyUserInfo.Blog;
            user.Introduce = _MyUserInfo.Introduce;

            var result = await _db.SaveChangesAsync();

            if (result > 0)
            {
                HttpContext.Session.Set <PyUserInfo>(HttpContext.Session.SessionKey(), _MyUserInfo);
                this.MsgBox("修改成功!");
            }
            else
            {
                this.MsgBox("修改失败,请稍后重试!");
            }
            return(View(_MyUserInfo));
        }
コード例 #3
0
        public async Task <JsonResult> Register([Bind("UserName,UserPwd,ComfirmPwd")] PyRegisterUser registerUser)
        {
            var data = new PyStudioPromptData();

            if (ModelState.IsValid)
            {
                #region 验证
                if (_context.InfoUser.Any(b => b.UserName.ToUpper().Equals(registerUser.UserName.Trim().ToUpper())))
                {
                    data.IsOK = 2;
                    data.Msg  = "已存在相同的账号!";
                    return(Json(data));
                }
                #endregion

                InfoUser infoUser = new InfoUser();
                infoUser.UserName       = registerUser.UserName.Trim();
                infoUser.UserNickName   = registerUser.UserName;
                infoUser.UserPwd        = registerUser.UserPwd.Trim()._Md5();
                infoUser.UserSex        = 3;
                infoUser.UserHeadPhoto  = "/images/default.png";
                infoUser.UserStatus     = (int)EmUserStatus.启用;
                infoUser.UserCreateTime = DateTime.Now;
                infoUser.UserIps        = this.GetUserIp();

                _context.Add(infoUser);

                var result = await _context.SaveChangesAsync();

                if (result > 0)
                {
                    data.IsOK = 1;
                    data.Msg  = "注册成功";
                    var _pyUserInfo = new PyUserInfo
                    {
                        UserId         = infoUser.UserId,
                        UserName       = infoUser.UserName,
                        UserNickName   = infoUser.UserNickName,
                        UserEmail      = infoUser.UserEmail,
                        UserTel        = infoUser.UserTel,
                        UserSex        = infoUser.UserSex,
                        UserIntroduce  = infoUser.UserIntroduce,
                        UserHeadPhoto  = infoUser.UserHeadPhoto,
                        UserBirthday   = infoUser.UserBirthday,
                        UserAddress    = infoUser.UserAddress,
                        UserBlog       = infoUser.UserBlog,
                        UserStatus     = infoUser.UserStatus,
                        UserCreateTime = infoUser.UserCreateTime,
                        UserLoginTime  = infoUser.UserLoginTime,
                        UserIps        = infoUser.UserIps
                    };
                    HttpContext.Session.Set <PyUserInfo>(HttpContext.Session.SessionKey(), _pyUserInfo);

                    #region 操作日志记录
                    _context.SysLogger.Add(new SysLogger
                    {
                        LoggerUser        = _pyUserInfo.UserId,
                        LoggerDescription = $"用户{_pyUserInfo.UserName} {EmLogStatus.注册} ",
                        LoggerOperation   = (int)EmLogStatus.注册,
                        LoggerCreateTime  = DateTime.Now,
                        LoggerIps         = _pyUserInfo.UserIps
                    });

                    _context.SysLogger.Add(new SysLogger
                    {
                        LoggerUser        = _pyUserInfo.UserId,
                        LoggerDescription = $"用户{_pyUserInfo.UserName} {EmLogStatus.登录} ",
                        LoggerOperation   = (int)EmLogStatus.登录,
                        LoggerCreateTime  = DateTime.Now,
                        LoggerIps         = _pyUserInfo.UserIps
                    });

                    await _context.SaveChangesAsync();

                    #endregion
                }
                else
                {
                    data.IsOK = 0;
                    data.Msg  = "注册失败!请联系客服";
                }
            }
            else
            {
                data.IsOK = 0;
                data.Msg  = "注册失败!请联系客服";
            }
            return(Json(data));
        }
コード例 #4
0
        public async Task <JsonResult> Login([Bind("UserName,UserPwd,ReturnUrl")] PyLoginUser loginUser)
        {
            var data = new PyStudioPromptData();

            //if (ModelState.IsValid)
            //{
            var md5Pwd   = loginUser.UserPwd.Trim()._Md5();
            var userInfo = await _context.InfoUser.SingleOrDefaultAsync(b => b.UserName.Equals(loginUser.UserName, StringComparison.CurrentCultureIgnoreCase) && b.UserPwd.Equals(md5Pwd));

            if (userInfo == null)
            {
                data.IsOK = 2;
                data.Msg  = "用户名或密码错误!";
                return(Json(data));
            }
            else if (userInfo.UserStatus == (int)EmUserStatus.禁用)
            {
                data.IsOK = 2;
                data.Msg  = "该账号已被禁用,或许你可以重新注册一个账号或者联系管理员!";
                return(Json(data));
            }

            userInfo.UserIps       = this.GetUserIp();
            userInfo.UserLoginTime = DateTime.Now;

            var _pyUserInfo = new PyUserInfo
            {
                UserId         = userInfo.UserId,
                UserName       = userInfo.UserName,
                UserNickName   = userInfo.UserNickName,
                UserEmail      = userInfo.UserEmail,
                UserTel        = userInfo.UserTel,
                UserSex        = userInfo.UserSex,
                UserIntroduce  = userInfo.UserIntroduce,
                UserHeadPhoto  = userInfo.UserHeadPhoto,
                UserBirthday   = userInfo.UserBirthday,
                UserAddress    = userInfo.UserAddress,
                UserBlog       = userInfo.UserBlog,
                UserStatus     = userInfo.UserStatus,
                UserCreateTime = userInfo.UserCreateTime,
                UserLoginTime  = userInfo.UserLoginTime,
                UserIps        = userInfo.UserIps
            };

            HttpContext.Session.Set <PyUserInfo>(HttpContext.Session.SessionKey(), _pyUserInfo);

            _context.SysLogger.Add(new SysLogger
            {
                LoggerUser        = _pyUserInfo.UserId,
                LoggerDescription = $"用户{_pyUserInfo.UserName}{EmLogStatus.登录} ",
                LoggerOperation   = (int)EmLogStatus.登录,
                LoggerCreateTime  = DateTime.Now,
                LoggerIps         = _pyUserInfo.UserIps
            });

            await _context.SaveChangesAsync();

            data.IsOK = 1;
            data.Msg  = "登录成功";
            //}
            //else
            //{
            //    data.IsOK = 0;
            //    data.Msg = "登录失败!";
            //}
            return(Json(data));
        }
コード例 #5
0
        public async Task <IActionResult> Register([Bind("UserName,UserPwd,ComfirmPwd")] PyRegisterUser loginUser)
        {
            if (ModelState.IsValid)
            {
                #region 验证
                if (_context.ToUserInfo.Any(b => b.UserName.ToUpper() == loginUser.UserName.Trim().ToUpper()))
                {
                    this.MsgBox("已经存在相同的账号!");
                    return(View(loginUser));
                }
                #endregion

                #region Create
                ToUserInfo userInfo = new ToUserInfo();
                userInfo.UserName   = loginUser.UserName.Trim();
                userInfo.UserPwd    = PublicClass._Md5(loginUser.UserPwd.Trim());
                userInfo.NickName   = userInfo.UserName;
                userInfo.Status     = (int)EmUserStatus.启用;
                userInfo.CreateTime = DateTime.Now;
                userInfo.LevelNum   = (int)EmLevelNum.注册;
                userInfo.Ips        = this.GetUserIp();
                userInfo.HeadPhoto  = "/images/pystudio.png";
                userInfo.Sex        = false;
                _context.Add(userInfo);

                var result = await _context.SaveChangesAsync();

                if (result > 0)
                {
                    var pyUserInfo = new PyUserInfo {
                        Id         = userInfo.Id,
                        UserName   = userInfo.UserName,
                        NickName   = userInfo.NickName,
                        Addr       = userInfo.Addr,
                        Birthday   = userInfo.Birthday,
                        Blog       = userInfo.Blog,
                        CreateTime = userInfo.CreateTime,
                        Email      = userInfo.Email,
                        HeadPhoto  = userInfo.HeadPhoto,
                        Introduce  = userInfo.Introduce,
                        Ips        = userInfo.Ips,
                        LevelNum   = userInfo.LevelNum,
                        Sex        = userInfo.Sex,
                        Tel        = userInfo.Tel,
                        Status     = userInfo.Status,
                        LoginTime  = DateTime.Now
                    };
                    HttpContext.Session.Set <PyUserInfo>(HttpContext.Session.SessionKey(), pyUserInfo);

                    if (!string.IsNullOrWhiteSpace(pyUserInfo.Ips))
                    {
                        _context.ToUserLog.Add(new ToUserLog {
                            CodeId     = (int)EmLogCode.登录,
                            CreateTime = DateTime.Now,
                            Des        = $"IP:{pyUserInfo.Ips},登录时间:{pyUserInfo.LoginTime.ToString("yyyy-MM-dd HH:mm:ss")}",
                            UserId     = userInfo.Id
                        });
                    }

                    _context.ToUserLog.Add(new ToUserLog {
                        CodeId     = (int)EmLogCode.积分,
                        CreateTime = DateTime.Now,
                        Des        = $"【注册】+{ (int)EmLevelNum.注册}",
                        UserId     = userInfo.Id
                    });

                    await _context.SaveChangesAsync();

                    return(RedirectToAction(nameof(HomeController.Index), "Home"));
                }
                #endregion

                this.MsgBox("注册失败,请稍后重试。");
                return(View(loginUser));
            }
            return(View(loginUser));
        }
コード例 #6
0
        public async Task <IActionResult> Login([Bind("UserName,UserPwd,ReturnUrl")] PyLoginUser loginUser)
        {
            if (ModelState.IsValid)
            {
                #region Valid
                var md5Pwd   = PublicClass._Md5(loginUser.UserPwd.Trim());
                var userInfo = await _context.ToUserInfo.SingleOrDefaultAsync(b => b.UserName.Equals(loginUser.UserName, StringComparison.CurrentCultureIgnoreCase) && b.UserPwd.Equals(md5Pwd));

                if (userInfo == null)
                {
                    this.MsgBox("账号或密码错误!");
                    return(View(loginUser));
                }
                else if (userInfo.Status == (int)EmUserStatus.禁用)
                {
                    this.MsgBox("该账号已被禁用,或许你可以重新注册一个账号或者联系管理员!");
                    return(View(loginUser));
                }
                #endregion

                #region 更新登录信息
                userInfo.Ips       = this.GetUserIp();
                userInfo.LoginTime = DateTime.Now;
                userInfo.LevelNum += (int)EmLevelNum.登录;

                //记录Session
                var pyUserInfo = new PyUserInfo {
                    Id       = userInfo.Id,
                    UserName = userInfo.UserName,
                    NickName = userInfo.NickName,
                    Addr     = userInfo.Addr,
                    Birthday = userInfo.Birthday,

                    Blog       = userInfo.Blog,
                    CreateTime = userInfo.CreateTime,
                    Email      = userInfo.Email,
                    HeadPhoto  = userInfo.HeadPhoto,
                    Introduce  = userInfo.Introduce,

                    Ips      = userInfo.Ips,
                    LevelNum = userInfo.LevelNum,
                    Sex      = userInfo.Sex,
                    Tel      = userInfo.Tel,
                    Status   = userInfo.Status,

                    LoginTime = Convert.ToDateTime(userInfo.LoginTime)
                };

                HttpContext.Session.Set <PyUserInfo>(HttpContext.Session.SessionKey(), pyUserInfo);

                if (!string.IsNullOrWhiteSpace(pyUserInfo.Ips))
                {
                    _context.ToUserLog.Add(new ToUserLog {
                        CodeId     = (int)EmLogCode.登录,
                        CreateTime = DateTime.Now,
                        Des        = $"IP:{pyUserInfo.Ips},登录时间:{pyUserInfo.LoginTime.ToString("yyyy-MM-dd hh:mm:ss")}",
                        UserId     = userInfo.Id
                    });
                }

                _context.ToUserLog.Add(new ToUserLog {
                    CodeId     = (int)EmLogCode.积分,
                    CreateTime = DateTime.Now,
                    Des        = $"【登录】  +{(int)EmLevelNum.登录}",
                    UserId     = userInfo.Id
                });
                await _context.SaveChangesAsync();

                if (string.IsNullOrWhiteSpace(loginUser.ReturnUrl))
                {
                    return(RedirectToAction(nameof(HomeController.Index), "Home"));
                }
                else
                {
                    return(Redirect(loginUser.ReturnUrl));
                }
                #endregion
            }
            return(View(loginUser));
        }
コード例 #7
0
        public async Task <IActionResult> UpHeadPhoto([Bind("Id")] PyUserInfo pyUserInfo)
        {
            var file = Request.Form.Files
                       .Where(b => b.Name == "myHeadPhoto" && b.ContentType.Contains("image"))
                       .SingleOrDefault();

            if (file == null)
            {
                this.MsgBox("请选择上传的头像图片!");
                return(View(_MyUserInfo));
            }

            var maxSize = 1024 * 1024 * 4;

            if (file.Length > maxSize)
            {
                this.MsgBox("头像图片不能大于4M!");
                return(View(_MyUserInfo));
            }

            var fileExtend  = file.FileName.Substring(file.FileName.LastIndexOf('.'));
            var fileNewName = $"{DateTime.Now.ToString("yyyyMMddhhmmssfff")}{fileExtend}";
            var path        = Path.Combine(_selfSetting.UpHeadPhotoPath, fileNewName);

            using (var stream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite)) {
                await file.CopyToAsync(stream);
            }

            //更新数据
            var viewPath = $"{_selfSetting.ViewHeadPhotoPath}/{fileNewName}";

            var user = _db.ToUserInfo
                       .Where(b => b.Id == _MyUserInfo.Id)
                       .SingleOrDefault();

            if (user == null)
            {
                this.MsgBox("上传失败,请稍后重试!");
                return(View(_MyUserInfo));
            }

            user.HeadPhoto = viewPath;
            user.LevelNum += (int)EmLevelNum.修改头像;
            var resule = await _db.SaveChangesAsync();

            if (resule > 0)
            {
                _MyUserInfo.HeadPhoto = viewPath;
                _MyUserInfo.LevelNum  = user.LevelNum;
                HttpContext.Session.Set <PyUserInfo>(HttpContext.Session.SessionKey(), _MyUserInfo);
                this.MsgBox("上传成功!");

                _db.ToUserLog.Add(new ToUserLog {
                    CodeId     = (int)EmLogCode.积分,
                    CreateTime = DateTime.Now,
                    Des        = $"【修改头像】+{ (int)EmLevelNum.修改头像}",
                    UserId     = _MyUserInfo.Id
                });
                await _db.SaveChangesAsync();
            }
            else
            {
                this.MsgBox("上传失败,请稍后重试!");
            }

            return(View(_MyUserInfo));
        }
コード例 #8
0
        public async Task <JsonResult> UpHeadPhoto([Bind("UserId")] PyUserInfo pyUserInfo)
        {
            var data = new PyStudioPromptData();
            var file = Request.Form.Files.Where(b => b.Name == "myHeadPhoto" && b.ContentType.Contains("image")).SingleOrDefault();

            if (file == null)
            {
                data.Msg  = "请选择要上传的图片!";
                data.IsOK = 2;
                return(Json(data));
            }
            var maxSize = 1024 * 1024 * 4;

            if (file.Length > maxSize)
            {
                data.Msg  = "头像大小不能大于4M!";
                data.IsOK = 2;
                return(Json(data));
            }
            var fileExtend  = file.FileName.Substring(file.FileName.LastIndexOf('.'));
            var fileNewName = $"{DateTime.Now.ToString("yyyyMMddhhmmssfff")}{fileExtend}";
            var path        = Path.Combine($"{_hostingEnvironment.WebRootPath}/{_pySelfSetting.HeadPhotoPath}", fileNewName);

            using (var stream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite))
            {
                await file.CopyToAsync(stream);
            }

            var viewPath = $"{_pySelfSetting.HeadPhotoPath}/{fileNewName}";
            var user     = _context.InfoUser.Where(b => b.UserId == _MyUserInfo.UserId).SingleOrDefault();

            if (user == null)
            {
                data.Msg  = "上传失败,请稍后再试!";
                data.IsOK = 0;
                return(Json(data));
            }
            user.UserHeadPhoto = viewPath;
            var result = await _context.SaveChangesAsync();

            if (result > 0)
            {
                _MyUserInfo.UserHeadPhoto = viewPath;
                HttpContext.Session.Set <PyUserInfo>(HttpContext.Session.SessionKey(), _MyUserInfo);
                data.Msg  = "上传成功!";
                data.IsOK = 1;
                _context.SysLogger.Add(new SysLogger
                {
                    LoggerUser        = _MyUserInfo.UserId,
                    LoggerDescription = $"用户{_MyUserInfo.UserName}{EmLogStatus.修改}头像",
                    LoggerOperation   = (int)EmLogStatus.修改,
                    LoggerCreateTime  = DateTime.Now,
                    LoggerIps         = this.GetUserIp()
                });
                await _context.SaveChangesAsync();
            }
            else
            {
                data.Msg  = "上传失败,请稍后再试!";
                data.IsOK = 0;
                return(Json(data));
            }

            return(Json(data));
        }
コード例 #9
0
        public async Task <JsonResult> ModifyUserInfo([Bind("UserId,UserName,UserNickName,UserEmail,UserTel,UserIntroduce,UserBirthday,UserAddress,UserBlog")] PyUserInfo pyUserInfo)
        {
            var data = new PyStudioPromptData();

            if (ModelState.IsValid)
            {
                var userInfo = _context.InfoUser.Where(b => b.UserId.Equals(pyUserInfo.UserId)).FirstOrDefault();
                if (userInfo != null)
                {
                    userInfo.UserNickName  = pyUserInfo.UserNickName;
                    userInfo.UserEmail     = pyUserInfo.UserEmail;
                    userInfo.UserTel       = pyUserInfo.UserTel;
                    userInfo.UserIntroduce = pyUserInfo.UserIntroduce;
                    userInfo.UserBirthday  = pyUserInfo.UserBirthday;
                    userInfo.UserAddress   = pyUserInfo.UserAddress;
                    userInfo.UserBlog      = pyUserInfo.UserBlog;

                    _MyUserInfo.UserNickName  = pyUserInfo.UserNickName;
                    _MyUserInfo.UserEmail     = pyUserInfo.UserEmail;
                    _MyUserInfo.UserTel       = pyUserInfo.UserTel;
                    _MyUserInfo.UserIntroduce = pyUserInfo.UserIntroduce;
                    _MyUserInfo.UserBirthday  = pyUserInfo.UserBirthday;
                    _MyUserInfo.UserAddress   = pyUserInfo.UserAddress;
                    _MyUserInfo.UserBlog      = pyUserInfo.UserBlog;

                    var result = await _context.SaveChangesAsync();

                    if (result > 0)
                    {
                        HttpContext.Session.Set <PyUserInfo>(HttpContext.Session.SessionKey(), _MyUserInfo);
                        _MyUserInfo = HttpContext.Session.Get <PyUserInfo>(HttpContext.Session.SessionKey());

                        data.IsOK = 1;
                        data.Msg  = "修改成功";
                        _context.SysLogger.Add(new SysLogger
                        {
                            LoggerUser        = _MyUserInfo.UserId,
                            LoggerDescription = $"用户{_MyUserInfo.UserName}{EmLogStatus.修改}个人信息",
                            LoggerOperation   = (int)EmLogStatus.修改,
                            LoggerCreateTime  = DateTime.Now,
                            LoggerIps         = this.GetUserIp()
                        });
                        await _context.SaveChangesAsync();
                    }
                    else
                    {
                        data.IsOK = 0;
                        data.Msg  = "修改失败!请稍后再试...";
                        return(Json(data));
                    }
                }
                else
                {
                    data.IsOK = 0;
                    data.Msg  = "修改失败!请稍后再试...";
                    return(Json(data));
                }
            }
            else
            {
                data.IsOK = 0;
                data.Msg  = "修改失败!请稍后再试...";
                return(Json(data));
            }
            return(Json(data));
        }