コード例 #1
0
        public async Task <IActionResult> GoTopicDisable(int id, bool isDisable)
        {
            var data = new MoData();

            HttpContext.TryGetUserInfo(out var userInfo);
            var topic = await _uf.TopicRepository.GetByIdAsync(id);

            if (topic == null)
            {
                data.IsOk = false;
                data.Msg  = "找不到指定的帖子";
                return(Json(data));
            }

            if (!userInfo.Roles.Equals("SuperAdmin", StringComparison.OrdinalIgnoreCase))
            {
                if (!userInfo.Roles.Equals("Admin", StringComparison.OrdinalIgnoreCase) ||
                    !await _uf.BBSRepository.IsExistAsync(x => x.Id == topic.BbsId && x.UserId == userInfo.Id))
                {
                    data.IsOk = false;
                    data.Msg  = "您没有屏蔽此贴的权限";
                    return(Json(data));
                }
            }

            topic.TopicStatus = isDisable ? TopicStatus.Disabled : TopicStatus.Normal;
            _uf.TopicRepository.Update(topic);
            await _uf.SaveChangesAsync();

            data.IsOk = true;
            return(Json(data));
        }
コード例 #2
0
        public async Task <IActionResult> Login(MoLoginUser loginUser)
        {
            if (ModelState.IsValid == false || loginUser == null)
            {
                this.MsgBox("验证失败,请重试!");
                return(View());
            }

            User user;

            user = await _uf.UserRepository.GetUser(loginUser.UserName, loginUser.UserPwd);

            if (user == null)
            {
                this.MsgBox("账号或密码错误!");
                return(View(typeof(MoLoginUser), loginUser));
            }
            else if (user.UserStatus == UserStatus.未登录)
            {
                this.MsgBox("该账号已被禁用,或许你可以尝试重新注册一个账号!");
                return(View());
            }

            user.UserStatus = (int)UserStatus.启用;
            _uf.UserRepository.Update(user);

            var userToRole = _uf.UserToRoleRepository.GetAll(x => x.UserId == user.Id);
            await _uf.SaveChangesAsync();

            var userInfo = new MoUserInfo
            {
                Id         = user.Id,
                UserName   = user.UserName,
                Email      = user.Email,
                HeadPhoto  = user.HeadPhoto,
                UserStatus = (int)user.UserStatus,
                Roles      = userToRole.Any(x => x.Role.RoleName.Equals(RoleType.SuperAdmin.ToString(),
                                                                        StringComparison.OrdinalIgnoreCase)) ? RoleType.SuperAdmin.ToString() :
                             userToRole.Any(x => x.Role.RoleName.Equals(RoleType.Admin.ToString(), StringComparison.OrdinalIgnoreCase)) ?
                             RoleType.Admin.ToString() : RoleType.User.ToString()
            };

            HttpContext.AddUserInfo(userInfo);

            if (String.IsNullOrWhiteSpace(loginUser.ReturnUrl))
            {
                return(Redirect("http://localhost:17758/home/index"));
            }
            else
            {
                return(Redirect(loginUser.ReturnUrl));
            }
        }
コード例 #3
0
        private async Task <bool> InitChatAsync(int userId, int targetUserId)
        {
            if (!await _uf.UserRepository.IsExistAsync(x => x.Id == targetUserId))
            {
                // 不存在该用户
                await OnSendAsync("找不到对方的账号");

                return(false);
            }

            var pool = HubConnectionPool.Pool;

            // 用户添加或更新到对方连接池中
            if (pool.Contains(targetUserId, userId))
            {
                pool.TryUpdateValue(userId, targetUserId, this);
            }
            else
            {
                pool.TryAddValue(targetUserId, userId, this);
            }

            await _uf.SaveChangesAsync();

            return(true);
        }
コード例 #4
0
        public async Task <IActionResult> GoUserDisable(int id, bool isDisable)
        {
            var data = new MoData();

            HttpContext.TryGetUserInfo(out var userInfo);
            if (userInfo.Id == id)
            {
                data.IsOk = false;
                data.Msg  = "你不能禁用你自己";
                return(Json(data));
            }

            var user = await _uf.UserRepository.GetByIdAsync(id);

            if (user == null)
            {
                data.IsOk = false;
                data.Msg  = "找不到指定的用户";
                return(Json(data));
            }

            if (isDisable)
            {
                // 禁用
                if (user.UserStatus == UserStatus.禁用)
                {
                    data.IsOk = false;
                    data.Msg  = "该用户已被禁用";
                    return(Json(data));
                }

                user.UserStatus = UserStatus.禁用;
            }
            else
            {
                // 取消禁用
                if (user.UserStatus != UserStatus.禁用)
                {
                    data.IsOk = false;
                    data.Msg  = "该用户未被禁用";
                    return(Json(data));
                }

                user.UserStatus = UserStatus.启用;
            }

            _uf.UserRepository.Update(user);
            if (await _uf.SaveChangesAsync() > 0)
            {
                data.IsOk = true;
            }
            else
            {
                data.IsOk = false;
                data.Msg  = "禁用失败,请稍后再试";
            }

            return(Json(data));
        }
コード例 #5
0
        public async Task <IActionResult> GoTopicStar(int id, bool isLike)
        {
            var data = new MoData();

            var topic = await _uf.TopicRepository.GetByIdAsync(id);

            if (topic == null)
            {
                data.IsOk = false;
                data.Msg  = "找不到指定的帖子";
                return(Json(data));
            }

            HttpContext.TryGetUserInfo(out var userInfo);
            if (userInfo.Id == topic.UserId)
            {
                data.IsOk = false;
                data.Msg  = "你不能点赞你自己的帖子";
                return(Json(data));
            }

            if (isLike)
            {
                // 点赞
                var isTemp = await _uf.TopicStarRepository.IsExistAsync(x => x.TopicId == id && x.UserId == userInfo.Id);

                if (isTemp)
                {
                    data.IsOk = false;
                    data.Msg  = "你已经赞过了";
                    return(Json(data));
                }

                var topicStar = new TopicStar
                {
                    TopicId = topic.Id,
                    UserId  = userInfo.Id
                };


                topic.StarCount = topic.StarCount + 1;
                await _uf.TopicStarRepository.InsertAsync(topicStar);
            }
            else
            {
                var topicStar = await _uf.TopicStarRepository.GetAsync(x => x.TopicId == id && x.UserId == userInfo.Id);

                if (topicStar != null)
                {
                    topic.StarCount = topic.StarCount - 1;
                    _uf.TopicStarRepository.Delete(topicStar);
                }
            }

            _uf.TopicRepository.Update(topic);
            await _uf.SaveChangesAsync();

            data.IsOk = true;
            data.Msg  = topic.StarCount.ToString();
            return(Json(data));
        }
コード例 #6
0
        public async Task <IActionResult> UpHeadPhoto(int id)
        {
            HttpContext.TryGetUserInfo(out var userInfo);
            ViewData["Role"] = userInfo.Roles.ToLower();
            var file = HttpRequest.Form.Files.Where(x =>
                                                    x.Name == "myHeadPhoto" &&
                                                    x.ContentType.Contains("image"))
                       .SingleOrDefault();

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


            var maxSize = 1024 * 1024 * 4; // 图片大小不超过4M

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

            var directory     = RootConfiguration.Root + Path.DirectorySeparatorChar + _mapSetting.UpHeadPhotoPath;
            var directoryInfo = new DirectoryInfo(directory);

            directoryInfo.DeleteAll(userInfo.Email);

            var fileExtend  = file.FileName.Substring(file.FileName.LastIndexOf('.'));
            var fileNewName = $"{userInfo.Email}-{DateTime.Now.ToString("yyyyMMddHHssfff")}{fileExtend}";
            var path        = Path.Combine(directory, fileNewName);

            using (var stream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
            {
                await file.CopyToAsync(stream); // 读取上传的图片并保存
            }

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

            using (var trans = _uf.BeginTransaction())
            {
                try
                {
                    var user = await _uf.UserRepository.GetByIdAsync(userInfo.Id);

                    if (user == null)
                    {
                        this.MsgBox("上传失败,请稍后重试!");
                        return(View(userInfo));
                    }
                    user.HeadPhoto = $"../../wwwroot{viewPath}";
                    _uf.UserRepository.Update(user);
                    var result = await _uf.SaveChangesAsync();

                    if (result > 0)
                    {
                        userInfo.HeadPhoto = user.HeadPhoto;

                        HttpContext.DeleteUserInfo();
                        HttpContext.AddUserInfo(userInfo);

                        this.MsgBox("上传成功!");
                        trans.Commit();
                    }
                    else
                    {
                        this.MsgBox("上传失败,请稍后再试!");
                        trans.Rollback();
                    }
                }
                catch (Exception ex)
                {
                    this.MsgBox("上传失败,请稍后再试!");
                    trans.Rollback();
                    _logger.LogError(userInfo.Id, "上传头像操作失败");
                    _uf.SaveChanges();
                    trans.Commit();
                }
            }

            return(View(userInfo));
        }
コード例 #7
0
        public async Task <IActionResult> CreateTopic(MoTopicDes topicDes)
        {
            ViewData["BBSId"]   = topicDes.BBSId;
            ViewData["BBSName"] = topicDes.BBSName;
            if (!ModelState.IsValid &&
                (topicDes.ReplyType == ReplyType.Text && String.IsNullOrWhiteSpace(topicDes.TopicDes)))
            {
                return(ReturnViewMsg("发帖失败,请检查您的标题和内容是否为空"));
            }

            HttpContext.TryGetUserInfo(out var userInfo);
            var topic = new Topic(userInfo.Id, topicDes.BBSId, topicDes.TopicName);

            try
            {
                await _uf.TopicRepository.InsertAsync(topic);

                if (await _uf.SaveChangesAsync() == 0)
                {
                    return(ReturnViewMsg("发帖失败,请稍后再试"));
                }
                if (topicDes.ReplyType == ReplyType.Image)
                {
                    var(success, resultMsg) = SaveImg(topic.Id,
                                                      HttpRequest.Form.Files.Where(x => x.ContentType.Contains("image")).SingleOrDefault());
                    if (!success)
                    {
                        this.MsgBox(resultMsg);
                        return(View());
                    }

                    topicDes.TopicDes = resultMsg;
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(userInfo.Id, $"发表新帖失败-{ex.Message.Substring(0, 50)}-{DateTime.Now.ToStandardFormatString()}");
                return(ReturnViewMsg("发帖失败,请稍后再试"));
            }

            var reply = new Reply
            {
                UserId     = userInfo.Id,
                TopicId    = topic.Id,
                UserName   = userInfo.UserName,
                TopicName  = topic.TopicName,
                ReplyType  = topicDes.ReplyType,
                Message    = topicDes.TopicDes,
                ReplyIndex = 1
            };

            UpdateIntegrate(userInfo.Id, (int)IntegrateType.CreateTopic);
            _uf.ReplyRepository.Insert(reply);
            if (_uf.SaveChanges() == 0)
            {
                _uf.TopicRepository.Delete(topic);
                _uf.SaveChanges();
                this.MsgBox("发帖失败,请稍后再试");
            }

            AddIndex(topic, reply);
            ViewData["TopicId"] = topic.Id;
            this.MsgBox("发帖成功");
            return(View());
        }