コード例 #1
0
        /// <summary>
        /// 聊天消息处理中心
        /// </summary>
        /// <param name="targetUserId"></param>
        /// <returns></returns>
        public override async Task OnReceiveAsync(string message)
        {
            var pool = HubConnectionPool.Pool;

            try
            {
                if (State == WebSocketState.Open)
                {
                    if (State != WebSocketState.Open || message == String.Empty)//连接关闭
                    {
                        pool.TryRemoveValue(_targetUserId, _userInfo.Id);
                        await OnDisconnectedAsync(false);

                        return;
                    }

                    var formatMessageResult = this.FormatMessage(message);
                    if (formatMessageResult.result)
                    {
                        // 客户端切换聊天对象
                        message = formatMessageResult.message;
                        await this.SwitchChatAsync(_userInfo.Id, _targetUserId, formatMessageResult.targetUserId);

                        _targetUserId = formatMessageResult.targetUserId;
                    }

                    var chat = new Chat
                    {
                        UserId       = _userInfo.Id,
                        TargetUserId = _targetUserId,
                        Message      = message
                    };

                    if (pool.TryGetValue(_userInfo.Id, _targetUserId, out GutsMvc.WebSocketHub.Hub targetHub) &&
                        targetHub != null &&
                        targetHub.State == WebSocketState.Open)
                    {
                        await targetHub.OnSendAsync($"{_userInfo.Id}:{message}");

                        chat.IsArrive = true;
                    }
                    else
                    {
                        chat.IsArrive = false;
                    }

                    _uf.ChatRepository.Insert(chat);
                    _uf.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                pool.TryRemove(_userInfo.Id);
                _logger.LogError(_userInfo.Id, $"发送消息时发生异常:{ex.Message}-{DateTime.Now.ToStandardFormatString()}");
            }
        }
コード例 #2
0
        public async Task <IActionResult> DeleteAdmin(int id)
        {
            var data = new MoData();

            HttpContext.TryGetUserInfo(out var userInfo);
            using (var trans = _uf.BeginTransaction())
            {
                try
                {
                    var bbs = await _uf.BBSRepository.GetAsync(x => x.UserId == id);

                    if (bbs == null)
                    {
                        data.IsOk = false;
                        return(Json(data));
                    }

                    bbs.UserId = userInfo.Id;
                    _uf.BBSRepository.Update(bbs);

                    var role = await _uf.RoleRepository.GetAsync(x => x.RoleName.Equals("User", StringComparison.OrdinalIgnoreCase));

                    var userRole = await _uf.UserToRoleRepository.GetAsync(x => x.UserId == id);

                    userRole.RoleId = role.Id;
                    _uf.UserToRoleRepository.Update(userRole);

                    if (await _uf.SaveChangesAsync() > 0)
                    {
                        data.IsOk = true;
                    }
                    else
                    {
                        data.IsOk = false;
                    }

                    trans.Commit();
                }
                catch (Exception ex)
                {
                    trans.Rollback();
                    _logger.LogError(id, $"撤销版主失败-{ex.Message}:{DateTime.Now.ToStandardFormatString()}");
                    _uf.SaveChanges();
                    data.IsOk = false;
                }
            }


            return(Json(data));
        }
コード例 #3
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));
        }
コード例 #4
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());
        }