Esempio n. 1
0
        /// <summary>
        /// 验证验证码
        /// </summary>
        /// <param name="captchaId">验证码ID</param>
        /// <param name="captchaText">验证码文本</param>
        /// <returns>表示异步验证验证码的任务,如果验证码错误,返回错误信息</returns>
        private async Task <string> ValidateCaptchaAsync(string captchaId, string captchaText)
        {
            // 如果用户直接请求该接口
            if (string.IsNullOrEmpty(captchaId) || string.IsNullOrEmpty(captchaText))
            {
                return(_msg.GetMessage("E007", "验证码"));
            }

            Captcha captcha = await _dbContext.Captcha.FindAsync(captchaId);

            if (captcha == null)
            {
                // 验证码已失效
                return(_msg.GetMessage("E011"));
            }
            else
            {
                // 及时将验证码删除
                _dbContext.Remove(captcha);
                await _dbContext.SaveChangesAsync();

                if (captcha.Text != captchaText.ToUpper())
                {
                    // 验证码错误
                    return(_msg.GetMessage("E001", "验证码"));
                }
            }

            return(null);
        }
Esempio n. 2
0
        /// <summary>
        /// 获取用户上一次登录时所在的房间ID
        /// </summary>
        /// <param name="uid">用户ID</param>
        /// <returns>表示获取用户上一次登录时所在的房间ID的任务</returns>
        public async Task <string> GetPreviousRoomIdAsync(int uid)
        {
            var connection = await _dbContext.Connection
                             .Where(conn => conn.UserId == uid && !conn.IsDeleted.Value)
                             .FirstOrDefaultAsync();


            // 连接信息没找到
            if (connection == null)
            {
                return(null);
            }

            var room = await _dbContext.ChatRoom.FindAsync(connection.RoomId);

            if (room == null)
            {
                // 如果房间没找到,但连接信息却找到了,说明之前的数据有问题
                // 删除连接信息
                _dbContext.Remove(connection);
                await _dbContext.SaveChangesAsync();

                return(null);
            }
            return(HashidsHelper.Encode(connection.RoomId));
        }
Esempio n. 3
0
        public async Task RemoveMemberAsync(string roomHashid, string memberHashid)
        {
            int roomId = HashidsHelper.Decode(roomHashid);
            int userId = HashidsHelper.Decode(Context.User.FindFirst("uid").Value);

            // 判断用户是否为房主,只有房主才有删除成员的权限
            var count = await _dbContext.ChatRoom
                        .CountAsync(room => room.Id == roomId && room.OwnerId == userId);

            if (count == 0)
            {
                return;
            }

            int memberId = HashidsHelper.Decode(memberHashid);

            var connection = await _dbContext.Connection
                             .FirstOrDefaultAsync(conn => conn.UserId == memberId);

            // 从数据库中删除
            _dbContext.Remove(connection);

            await _dbContext.SaveChangesAsync();

            // 通知被删除的用户
            await Clients.Client(connection.ConnectionId)
            .InvokeAsync("onRemoved",
                         _msg.GetMessage("I005", "你"));

            // 通知房间内其他用户
            await Clients.Group(roomHashid)
            .InvokeAsync("receiveSystemMessage",
                         _msg.GetMessage("I005", connection.Username));

            // 刷新成员列表
            await RefreshMemberListAsync(roomHashid, roomId);

            await Groups.RemoveAsync(connection.ConnectionId, roomHashid);
        }