/// <summary>
        /// 获取群组详情
        /// </summary>
        /// <returns></returns>
        public object GM_GroupInfo(IGM_GroupInfo model)
        {
            var group      = new IM_Group();
            var resMembers = Get_GroupMembers(model.GroupId);

            return(CommonJson.camelObject(resMembers));
        }
        /// <summary>
        /// 获取-单聊记录(分页)
        /// </summary>
        public object His_SingleHistory(IHis_SingleHistory model)
        {
            var user = LOGIN_USER;
            List <OSingle_SendText> db_Result = new List <OSingle_SendText>();

            using (var db = new ModelAC())
            {
                var userB = db.IM_User.SingleOrDefault(p => p.App_Id == APP_GUID && p.UserId == model.ChatId);

                #region 数据查询
                if (model.MsgId == null)
                {//获取最新5条消息
                    db_Result = db.IM_MsgSingle
                                .Where(p => (p.From_UserId == user.Id && p.To_UserId == userB.Id) || (p.From_UserId == userB.Id && p.To_UserId == user.Id))
                                .OrderByDescending(p => p.SortId)
                                .Take(5)
                                .Select(p => new OSingle_SendText
                    {
                        #region 输出
                        Id          = p.Id.ToString(),
                        MsgClientId = p.MsgClientId,
                        From_UserId = p.From_UserId == user.Id ? user.UserId : userB.UserId,
                        To_UserId   = p.From_UserId == user.Id ? user.UserId : userB.UserId,
                        MsgTime     = p.MsgTime,
                        IsRead      = p.IsRead,
                        MsgType     = p.MsgType,
                        MsgBody     = p.MsgBody,
                        #endregion
                    })
                                .ToList();
                }
                else
                {//获取MsgId之前5条消息
                    var msg = db.IM_MsgSingle.SingleOrDefault(p => p.App_Id == APP_GUID && p.Id == model.MsgId);
                    db_Result = db.IM_MsgSingle
                                .Where(p => p.SortId < msg.SortId)
                                .Where(p => (p.From_UserId == user.Id && p.To_UserId == userB.Id) || (p.From_UserId == userB.Id && p.To_UserId == user.Id))
                                .OrderByDescending(p => p.SortId)
                                .Take(5)
                                .Select(p => new OSingle_SendText
                    {
                        #region 输出
                        Id          = p.Id.ToString(),
                        MsgClientId = p.MsgClientId,
                        From_UserId = p.From_UserId == user.Id ? user.UserId : userB.UserId,
                        To_UserId   = p.From_UserId == user.Id ? user.UserId : userB.UserId,
                        MsgTime     = p.MsgTime,
                        IsRead      = p.IsRead,
                        MsgType     = p.MsgType,
                        MsgBody     = p.MsgBody,
                        #endregion
                    })
                                .ToList();
                }
                #endregion
            }

            var jsonResult = CommonJson.camelObject(db_Result);
            return(jsonResult);
        }
        /// <summary>
        /// 群主-加人
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public object GM_AddUsers(IGM_AddUsers model)
        {
            var mOGM_AddUsers = new OGM_AddUsers();
            var group         = new IM_Group();
            var users_add     = new List <IM_User>();

            var db_result = 0;

            using (var db = new ModelAC())
            {
                #region db
                group = db.IM_Group.SingleOrDefault(p => p.App_Id == APP_GUID && p.GroupId == model.GroupId);

                users_add = db.IM_User.Where(p => p.App_Id == APP_GUID && model.Accounts.Contains(p.UserId)).ToList();

                foreach (var user in users_add)
                {
                    var mIm_UserGroup = new Im_UserGroup();
                    mIm_UserGroup.IM_User_Id    = user.Id;
                    mIm_UserGroup.IM_Group_Id   = group.Id;
                    mIm_UserGroup.GroupPosition = IM_GroupPosition.Memeber;
                    db.Im_UserGroup.Add(mIm_UserGroup);
                }

                db_result = db.SaveChanges();
                #endregion
            }

            if (db_result > 0)
            {
                mOGM_AddUsers.Success = true;
                mOGM_AddUsers.Msg     = "添加群成员成功!";

                #region 连接组维护
                var app_accounts  = users_add.Select(p => HubKey.App__Account(app: APP, account: p.UserId)).ToList();
                var connectionIDs = Base_GetConnectionIds(app_accounts);

                foreach (var connectionId in connectionIDs)
                {
                    //将连接添加到指定的组
                    Groups.Add(connectionId, group.Id.ToString());
                }
                #endregion

                #region 更新相关群组聊表信息-发送系统通知
                Update_GM_AddUsers(group);
                #endregion

                return(CommonJson.camelObject(mOGM_AddUsers));
            }
            else
            {
                mOGM_AddUsers.Success = false;
                mOGM_AddUsers.Msg     = "添加群成员失败!";

                return(CommonJson.camelObject(mOGM_AddUsers));
            }
        }
        /// <summary>
        /// 发送-图片消息
        /// </summary>
        /// <returns></returns>
        public object Group_SendPicture(IGroup_SendText model)
        {
            var user      = LOGIN_USER;
            var msg       = new IM_MsgGroup();
            var db_result = 0;

            using (var db = new ModelAC())
            {
                #region db
                msg.App_Id = APP_GUID;

                msg.MsgClientId = model.MsgClientId;
                msg.From_UserId = user.Id;
                msg.To_GroupId  =
                    db.IM_Group.SingleOrDefault(p => p.App_Id == APP_GUID && p.GroupId == model.To_GroupId).Id;

                msg.MsgTime    = DateTime.Now;
                msg.IsFullRead = false;
                msg.MsgType    = model.MsgType;
                msg.MsgBody    = model.MsgBody;

                db.IM_MsgGroup.Add(msg);
                db_result = db.SaveChanges();
                #endregion
            }

            var session_result = Group_updateSession(msg);

            if (db_result == 1)
            {
                var mOGroup_SendText = new OGroup_SendText()
                {
                    #region 输出
                    Id          = msg.Id.ToString(),
                    MsgClientId = msg.MsgClientId,
                    From_UserId = model.From_UserId,
                    //From_UserImg = SITE_URL + user.PortraitUri,
                    To_GroupId = model.To_GroupId,
                    MsgTime    = msg.MsgTime,
                    IsFullRead = msg.IsFullRead,
                    MsgType    = msg.MsgType,
                    MsgBody    = msg.MsgBody,
                    #endregion
                };
                var jsonResult = CommonJson.camelObject(mOGroup_SendText);

                #region 消息分发

                Clients.OthersInGroup(msg.To_GroupId.ToString()).Group_SendPicture(jsonResult);
                #endregion

                return(jsonResult);
            }
            else
            {
                return(null);
            }
        }
        /// <summary>
        /// 群主-创建群
        /// </summary>
        public object GM_Create(IGM_Create model)
        {
            var mOGM_Create = new OGM_Create();

            if (ImGroupExists(model.GroupId))
            {
                mOGM_Create.Success = false;
                mOGM_Create.Msg     = "已存在该群组编号";

                return(CommonJson.camelObject(mOGM_Create));
            }

            var user          = LOGIN_USER;
            var group         = new IM_Group();
            var mIm_UserGroup = new Im_UserGroup();
            var db_result     = 0;

            using (var db = new ModelAC())
            {
                #region db
                group.App_Id    = APP_GUID;
                group.GroupId   = model.GroupId;
                group.GroupName = model.GroupName;

                mIm_UserGroup.IM_User_Id    = user.Id;
                mIm_UserGroup.IM_Group_Id   = group.Id;
                mIm_UserGroup.GroupPosition = IM_GroupPosition.Founder;

                db.IM_Group.Add(group);
                db.Im_UserGroup.Add(mIm_UserGroup);
                db_result = db.SaveChanges();
                #endregion
            }

            if (db_result > 0)
            {
                mOGM_Create.Success = true;
                mOGM_Create.Msg     = "群聊创建成功!";

                //将连接添加到指定的组
                Groups.Add(Context.ConnectionId, group.Id.ToString());
                return(CommonJson.camelObject(mOGM_Create));
            }
            else
            {
                mOGM_Create.Success = false;
                mOGM_Create.Msg     = "服务器错误!";

                return(CommonJson.camelObject(mOGM_Create));
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// 获取-好友
        /// </summary>
        /// <returns></returns>
        public object FG_GetFriends()
        {
            List <OFG_GetFriends> resFriends = null;

            using (var db = new ModelAC())
            {
                var friends = db.IM_User.Where(p => p.App_Id == APP_GUID && p.UserId != ACCOUNT).OrderBy(p => p.Name);
                resFriends = friends.Select(p => new OFG_GetFriends
                {
                    Account = p.UserId,
                    Name    = p.Name,
                    Img     = SITE_URL + p.PortraitUri
                }).ToList();
            }

            return(CommonJson.camelObject(resFriends));
        }
        /// <summary>
        /// 会话-重置未读消息数
        /// </summary>
        /// <returns></returns>
        public object Chatter_ResetUnreadMsg(IChatter_ResetUnreadMsg model)
        {
            var             user         = LOGIN_USER;
            IM_ChatterActor chatterActor = null;
            var             db_result    = 0;

            using (var db = new ModelAC())
            {
                if (model.SessionType == IM_ChatType.Single)
                {
                    var userB = db.IM_User.SingleOrDefault(p => p.App_Id == APP_GUID && p.UserId == model.ChatId);
                    chatterActor = db.IM_ChatterActor
                                   .SingleOrDefault(p => p.App_Id == APP_GUID && p.IM_Chatter.ChatType == IM_ChatType.Single && p.UserId == user.Id &&
                                                    ((p.IM_Chatter.Single_A_UserId == user.Id && p.IM_Chatter.Single_B_UserId == userB.Id) ||
                                                     (p.IM_Chatter.Single_A_UserId == userB.Id && p.IM_Chatter.Single_B_UserId == user.Id)));

                    chatterActor.UnreadMsgCount = 0;
                    db_result = db.SaveChanges();
                }
                else if (model.SessionType == IM_ChatType.Group)
                {
                    var group = db.IM_Group.SingleOrDefault(p => p.App_Id == APP_GUID && p.GroupId == model.ChatId);
                    chatterActor = db.IM_ChatterActor
                                   .SingleOrDefault(p => p.App_Id == APP_GUID && p.IM_Chatter.ChatType == IM_ChatType.Group && p.UserId == user.Id && p.IM_Chatter.Group_GroupId == group.Id);

                    chatterActor.UnreadMsgCount = 0;
                    db_result = db.SaveChanges();
                }
            }
            if (db_result == 1)
            {
                var jsonResult = CommonJson.camelObject(new OChatter_ResetUnreadMsg
                {
                    ChatId         = model.ChatId,
                    SessionId      = chatterActor.ChatterId.ToString(),
                    SessionType    = model.SessionType,
                    UnreadMsgCount = chatterActor.UnreadMsgCount
                });
                return(jsonResult);
            }
            else
            {
                return(null);
            }
        }
        /// <summary>
        /// 通知→群组人员变更
        /// </summary>
        /// <param name="group"></param>
        private void Update_GM_AddUsers(IM_Group group)
        {//通知所有群组成员
            var resMembers    = Get_GroupMembers(group.GroupId);
            var app_accounts  = resMembers.Select(p => HubKey.App__Account(app: APP, account: p.Account)).ToList();
            var connectionIDs = Base_GetConnectionIds(app_accounts);

            foreach (var connectionId in connectionIDs)
            {
                var groupInfo = new OFG_GetGroups()
                {
                    Id            = group.Id.ToString(),
                    GroupId       = group.GroupId,
                    GroupName     = group.GroupName,
                    GroupPosition = IM_GroupPosition.Memeber,//管理员功能暂不实现
                    Members       = resMembers.ToArray <object>(),
                };
                //发送系统通知
                Clients.Client(connectionId).GM_AddUsers(CommonJson.camelObject(groupInfo));
            }
        }
        /// <summary>
        /// 发送-图片消息
        /// </summary>
        /// <returns></returns>
        public object Single_SendPicture(ISingle_SendText model)
        {
            var msg       = new IM_MsgSingle();
            var db_result = 0;

            using (var db = new ModelAC())
            {
                #region db
                msg.App_Id = APP_GUID;

                msg.MsgClientId = model.MsgClientId;
                msg.From_UserId =
                    db.IM_User.SingleOrDefault(p => p.App_Id == APP_GUID && p.UserId == model.From_UserId).Id;
                msg.To_UserId =
                    db.IM_User.SingleOrDefault(p => p.App_Id == APP_GUID && p.UserId == model.To_UserId).Id;

                msg.MsgTime = DateTime.Now;
                msg.IsRead  = false;
                msg.MsgType = model.MsgType;
                msg.MsgBody = model.MsgBody;

                db.IM_MsgSingle.Add(msg);
                db_result = db.SaveChanges();
                #endregion
            }

            var session_result = Single_updateSession(msg);

            if (db_result == 1)
            {
                var mOSingle_SendText = new OSingle_SendText()
                {
                    #region 输出
                    Id          = msg.Id.ToString(),
                    MsgClientId = msg.MsgClientId,
                    From_UserId = model.From_UserId,
                    To_UserId   = model.To_UserId,
                    MsgTime     = msg.MsgTime,
                    IsRead      = msg.IsRead,
                    MsgType     = msg.MsgType,
                    MsgBody     = msg.MsgBody,
                    #endregion
                };
                var jsonResult = CommonJson.camelObject(mOSingle_SendText);

                #region 消息分发
                string to__Account = HubKey.App__Account(app: APP, account: model.To_UserId);
                using (var db = new SignalR_MapUsersModel())
                {
                    var connections = db.Connections.Where(p => p.User_UserName == to__Account && p.Connected == true).ToList();
                    for (int i = 0; i < connections.Count(); i++)
                    {
                        Clients.Client(connections[i].ConnectionID).Single_SendPicture(jsonResult);
                    }
                }
                #endregion

                return(jsonResult);
            }
            else
            {
                return(null);
            }
        }
        /// <summary>
        /// 获取-群聊记录(分页)
        /// </summary>
        public object His_GroupHistory(IHis_GroupHistory model)
        {
            var user = LOGIN_USER;
            List <OGroup_SendText> db_Result = new List <OGroup_SendText>();

            using (var db = new ModelAC())
            {
                var group = db.IM_Group.SingleOrDefault(p => p.App_Id == APP_GUID && p.GroupId == model.ChatId);

                #region 数据查询
                if (model.MsgId == null)
                {//获取最新5条消息
                    db_Result = db.IM_MsgGroup
                                .Where(p => p.To_GroupId == group.Id)
                                .OrderByDescending(p => p.SortId)
                                .Take(5)
                                .Select(p => new OGroup_SendText
                    {
                        #region 输出
                        Id          = p.Id.ToString(),
                        MsgClientId = p.MsgClientId,
                        From_UserId = p.From_IM_User.UserId,
                        //From_UserImg = SITE_URL + p.From_IM_User.PortraitUri,
                        To_GroupId = group.GroupId,
                        MsgTime    = p.MsgTime,
                        IsFullRead = p.IsFullRead,
                        MsgType    = p.MsgType,
                        MsgBody    = p.MsgBody,
                        #endregion
                    })
                                .ToList();
                }
                else
                {//获取MsgId之前5条消息
                    var msg = db.IM_MsgGroup.SingleOrDefault(p => p.App_Id == APP_GUID && p.Id == model.MsgId);
                    db_Result = db.IM_MsgGroup
                                .Where(p => p.SortId < msg.SortId)
                                .Where(p => p.To_GroupId == group.Id)
                                .OrderByDescending(p => p.SortId)
                                .Take(5)
                                .Select(p => new OGroup_SendText
                    {
                        #region 输出
                        Id          = p.Id.ToString(),
                        MsgClientId = p.MsgClientId,
                        From_UserId = p.From_IM_User.UserId,
                        //From_UserImg = SITE_URL + p.From_IM_User.PortraitUri,
                        To_GroupId = group.GroupId,
                        MsgTime    = p.MsgTime,
                        IsFullRead = p.IsFullRead,
                        MsgType    = p.MsgType,
                        MsgBody    = p.MsgBody,
                        #endregion
                    })
                                .ToList();
                }
                #endregion
            }

            var jsonResult = CommonJson.camelObject(db_Result);
            return(jsonResult);
        }
Ejemplo n.º 11
0
        public async Task <IHttpActionResult> vSessionByUser(Guid APP_GUID, Guid userId)
        {
            List <OChatter_ChatterInfo> db_Result = new List <OChatter_ChatterInfo>();

            using (var db = new ModelAC())
            {
                List <OChatter_ChatterInfo> sessions = new List <OChatter_ChatterInfo>();
                #region 单聊会话
                var single = db.IM_ChatterActor
                             .Where(p => p.App_Id == APP_GUID && p.IM_Chatter.ChatType == IM_ChatType.Single && p.UserId == userId)
                             .Select(p => new OChatter_ChatterInfo
                {
                    ChatId         = p.IM_Chatter.Single_A_UserId == userId ? p.IM_Chatter.Single_B_IM_User.UserId : p.IM_Chatter.Single_A_IM_User.UserId,
                    ChatName       = p.IM_Chatter.Single_A_UserId == userId ? p.IM_Chatter.Single_B_IM_User.Name : p.IM_Chatter.Single_A_IM_User.Name,
                    ChatImg        = string.Empty + p.IM_User.PortraitUri,
                    SessionId      = p.ChatterId.ToString(),
                    SessionType    = p.IM_Chatter.ChatType,
                    UnreadMsgCount = p.UnreadMsgCount,
                    ChatterTime    = p.ChatterTime,
                    Messages       = new HashSet <object>()
                    {
                        db.IM_MsgSingle.Where(s => s.Id == p.Last_MsgId)
                        .Select(m => new OSingle_SendText {
                            Id          = m.Id.ToString(),
                            MsgClientId = m.MsgClientId,
                            From_UserId = m.From_IM_User.UserId,
                            To_UserId   = m.To_IM_User.UserId,
                            MsgTime     = m.MsgTime,
                            IsRead      = m.IsRead,
                            MsgType     = m.MsgType,
                            MsgBody     = m.MsgBody,
                        }).FirstOrDefault()
                    },
                });
                #endregion
                #region 群聊会话
                var group = db.IM_ChatterActor
                            .Where(p => p.App_Id == APP_GUID && p.IM_Chatter.ChatType == IM_ChatType.Group && p.UserId == userId)
                            .Select(p => new OChatter_ChatterInfo
                {
                    ChatId         = p.IM_Chatter.Group_IM_Group.GroupId,
                    ChatName       = p.IM_Chatter.Group_IM_Group.GroupName,
                    ChatImg        = null,
                    SessionId      = p.ChatterId.ToString(),
                    SessionType    = p.IM_Chatter.ChatType,
                    UnreadMsgCount = p.UnreadMsgCount,
                    ChatterTime    = p.ChatterTime,
                    Messages       = new HashSet <object>()
                    {
                        db.IM_MsgSingle.Where(s => s.Id == p.Last_MsgId)
                        .Select(m => new OSingle_SendText {
                            Id          = m.Id.ToString(),
                            MsgClientId = m.MsgClientId,
                            From_UserId = m.From_IM_User.UserId,
                            To_UserId   = m.To_IM_User.UserId,
                            MsgTime     = m.MsgTime,
                            IsRead      = m.IsRead,
                            MsgType     = m.MsgType,
                            MsgBody     = m.MsgBody,
                        }).FirstOrDefault()
                    },
                });
                #endregion
                sessions.AddRange(single);
                sessions.AddRange(group);
                db_Result = sessions.OrderByDescending(p => p.ChatterTime).ToList();
            }
            var jsonResult = CommonJson.camelObject(db_Result);
            return(Ok(jsonResult));
        }
Ejemplo n.º 12
0
        /// <summary>
        /// 获取-群组
        /// </summary>
        /// <returns></returns>
        public object FG_GetGroups()
        {
            IList <OFG_GetGroups> resGroups = Base_GetGroups();

            return(CommonJson.camelObject(resGroups));
        }