Ejemplo n.º 1
0
        /// <summary>
        /// 客服断连处理
        /// </summary>
        public void ServicerOnDisconnected(ServicerOnDisconnectedDto servicerOnDisconnectedDto)
        {
            ServiceConnectRecords ServiceConnectRecordsModel = _serviceConnectRecords.FirstOrDefault(e => e.ServiceId == servicerOnDisconnectedDto.ServiceId);

            ServiceConnectRecordsModel.ServiceState = OrderInfo.LoginState.OffLine;
            _serviceConnectRecords.Update(ServiceConnectRecordsModel);

            List <ServiceRecords> ServiceRecordsList = _serviceRecords.GetAllList(e => e.ServiceId == servicerOnDisconnectedDto.ServiceId && e.ServiceState == OrderInfo.LoginState.Online);

            foreach (var item in ServiceRecordsList)
            {
                item.ServiceUnContentDate = DateTime.Now;
                item.ServiceState         = OrderInfo.LoginState.OffLine;
                _serviceRecords.Update(item);

                ChatRecords ChatRecordsModel = new ChatRecords();
                ChatRecordsModel    = EntityHelper.CopyValue(item, ChatRecordsModel);
                ChatRecordsModel.Id = 0;
                ChatRecordsModel.ServiceRecordsId = item.Id;
                ChatRecordsModel.SendInfoType     = OrderInfo.SendInfoType.TextInfo;
                ChatRecordsModel.SendSource       = OrderInfo.TerminalRefer.system;
                ChatRecordsModel.SendContent      = "客服下线";
                ChatRecordsModel.SendDateTime     = DateTime.Now;
                ChatRecordsModel.ReceiveState     = OrderInfo.ReceiveState.Received;
                _chatRecords.Insert(ChatRecordsModel);
            }
        }
Ejemplo n.º 2
0
        async Task ExecuteLoadChatRecordsCommand()
        {
            if (IsBusy)
            {
                return;
            }

            IsBusy = true;

            try
            {
                ChatRecords.Clear();
                var chatRecords = await DataStore.GetChatRecordsAsync(true);

                ChatRecords.ReplaceRange(chatRecords);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
                MessagingCenter.Send(new MessagingCenterAlert
                {
                    Title   = "Error",
                    Message = "Unable to load items.",
                    Cancel  = "OK"
                }, "message");
            }
            finally
            {
                IsBusy = false;
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 用户断连处理
        /// </summary>
        public void CustomerOnDisconnected(CustomerOnDisconnectedDto customerOnDisconnectedDto)
        {
            ServiceRecords ServiceRecordsModel = _serviceRecords.FirstOrDefault(e => e.Id == customerOnDisconnectedDto.ServiceRecordId);

            ServiceRecordsModel.CustomerUnContentDate = DateTime.Now;
            ServiceRecordsModel.CustomerState         = OrderInfo.LoginState.OffLine;
            _serviceRecords.Update(ServiceRecordsModel);

            CustomerConnectRecords CustomerConnectRecordsModel = _customerConnectRecords.FirstOrDefault(e => e.DeviceId == customerOnDisconnectedDto.DeviceId);

            CustomerConnectRecordsModel.CustomerState = OrderInfo.LoginState.OffLine;
            _customerConnectRecords.Update(CustomerConnectRecordsModel);

            ChatRecords ChatRecordsModel = new ChatRecords();

            ChatRecordsModel    = EntityHelper.CopyValue(ServiceRecordsModel, ChatRecordsModel);
            ChatRecordsModel.Id = 0;
            ChatRecordsModel.ServiceRecordsId = customerOnDisconnectedDto.ServiceRecordId;
            ChatRecordsModel.SendInfoType     = OrderInfo.SendInfoType.TextInfo;
            ChatRecordsModel.SendSource       = OrderInfo.TerminalRefer.system;
            ChatRecordsModel.SendContent      = "用户下线";
            ChatRecordsModel.SendDateTime     = DateTime.Now;
            ChatRecordsModel.ReceiveState     = OrderInfo.ReceiveState.Received;
            _chatRecords.Insert(ChatRecordsModel);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 添加聊天记录
        /// </summary>
        /// <param name="UID"></param>
        /// <param name="SendUID"></param>
        /// <param name="MsgText"></param>
        /// <returns></returns>
        public int AddRecords(string UID, string SendUID, string MsgText)
        {
            if (string.IsNullOrEmpty(UID))
            {
                return(-2);//接收人UID不能为空
            }
            if (string.IsNullOrEmpty(SendUID))
            {
                return(-3);//发送人UID不能为空
            }
            if (string.IsNullOrEmpty(MsgText))
            {
                return(-4);//消息文本不能为空
            }
            UsersDAL uDAL = new UsersDAL();

            if (uDAL.Query(UID) == null)
            {
                return(-5);//接收人UID不存在
            }
            if (uDAL.Query(SendUID) == null)
            {
                return(-6);//发送人UID不存在
            }
            ChatRecords entity = new ChatRecords();

            return(dal.AddRecords(entity));
        }
Ejemplo n.º 5
0
        private void SendMessageHandler(SendMessage sendMessage)
        {
            var record = new ChatRecord()
            {
                UserName     = sendMessage.UserName,
                Content      = sendMessage.Content,
                SendDateTime = sendMessage.SendDateTime
            };

            ChatRecords.Add(record);
        }
Ejemplo n.º 6
0
        private void ConnectionLostHandler()
        {
            MessageBox.Show("与服务端的连接丢失");

            ChatUserInfos.Clear();
            ChatRecords.Clear();

            NetworkClient.Stop();

            GlobalValue.IsInRoom = false;
            MainWindow.VM.Status = "Not in room....";
        }
Ejemplo n.º 7
0
        /// <summary>
        /// 添加聊天记录
        /// </summary>
        /// <param name="addChatRecordsDto"></param>
        public BaseOutput AddChatRecords(AddChatRecordsDto addChatRecordsDto)
        {
            BaseOutput     output = new BaseOutput();
            ServiceRecords ServiceRecordsModel = _serviceRecords.GetAllList(e => e.ServiceId == addChatRecordsDto.ServicerId && e.CustomerDeviceId == addChatRecordsDto.CustomerDeviceId)
                                                 .OrderByDescending(e => e.CustomerContentDate).FirstOrDefault();
            ChatRecords ChatRecordsModel = new ChatRecords();

            ChatRecordsModel    = EntityHelper.CopyValue(ServiceRecordsModel, ChatRecordsModel);
            ChatRecordsModel.Id = 0;
            ChatRecordsModel.ServiceRecordsId = ServiceRecordsModel.Id;
            ChatRecordsModel.SendSource       = addChatRecordsDto.SendSource;
            ChatRecordsModel.SendDateTime     = DateTime.Now;
            ChatRecordsModel.SendContent      = addChatRecordsDto.SendContent;
            ChatRecordsModel.ReceiveState     = OrderInfo.ReceiveState.Received;
            _chatRecords.Insert(ChatRecordsModel);
            return(output);
        }
Ejemplo n.º 8
0
        private void RoomCloseHandler()
        {
            MessageBox.Show("当前房间已关闭");
            ChatUserInfos.Clear();
            ChatRecords.Clear();

            NetworkClient.Stop();

            if (GlobalValue.IsRoomMaster)
            {
                GlobalValue.IsRoomMaster = false;
                BroadcastServer.Stop();
                NetworkServer.Stop();
            }

            GlobalValue.IsInRoom = false;
            MainWindow.VM.Status = "Not in room....";
        }
Ejemplo n.º 9
0
        private void UserExitHandler(UserExit userExit)
        {
            if (isSelfExitRoom)
            {
                isSelfExitRoom = false;
                MessageBox.Show("你已经离开了房间");
                ChatUserInfos.Clear();
                ChatRecords.Clear();
                NetworkClient.Stop();
                GlobalValue.IsInRoom = false;
                MainWindow.VM.Status = "Not in room....";
                return;
            }

            ChatUserInfo chatUserInfo = ChatUserInfos.FirstOrDefault(c => c.UserName.Equals(userExit.UserName));

            if (chatUserInfo != null)
            {
                ChatUserInfos.Remove(chatUserInfo);
            }
        }
Ejemplo n.º 10
0
        private void HandleDataReceived(IAsyncResult ar)
        {
            TCPClientState state  = (TCPClientState)ar.AsyncState;
            NetworkStream  stream = state.NetworkStream;

            if (state != null)
            {
                int recv = 0;
                try
                {
                    recv = stream.EndRead(ar);
                    byte[] buff = new byte[recv];
                    Buffer.BlockCopy(state.Buffer, 0, buff, 0, recv);
                    String str         = Encoding.UTF8.GetString(buff);
                    string instruction = str.Substring(0, 4);
                    string content     = str.Substring(4);
                    switch (instruction)
                    {
                    case "@01@":
                        string[]           addInfo  = content.Split(',');
                        string             userName = addInfo[0];
                        string             password = addInfo[1];
                        string             sql      = "SELECT * FROM useraccount WHERE UserName=@UserName AND Password=@Password";
                        List <UserAccount> accounts = DBHelper.QueryToList <UserAccount>(sql, new MySqlParameter[] { new MySqlParameter("UserName", userName), new MySqlParameter("Password", password) });
                        if (accounts.Count > 0)
                        {
                            state.userId     = accounts[0].UserId;
                            state.clientName = accounts[0].NickName;
                            this.Invoke((EventHandler) delegate {
                                cbClientList.Items.Add(accounts[0].NickName);
                            });
                            string user = JsonConvert.SerializeObject(accounts[0]);
                            Send(state.TcpClient, Encoding.UTF8.GetBytes("@01@" + user));
                        }
                        else
                        {
                            Send(state.TcpClient, Encoding.UTF8.GetBytes("@01@0"));
                        }
                        break;

                    case "@2@":
                        this.Invoke(new MethodInvoker(() => {
                            this.tbChatContent.AppendText(state.clientName + " 发来:" + content + "\n");
                        }));
                        break;

                    case "@02@":
                        string             getFriends    = "SELECT * FROM useraccount WHERE UserId in (SELECT f.FriendId FROM friend f LEFT JOIN useraccount u ON f.SelfId=u.UserId WHERE u.UserId=@UserId);";
                        List <UserAccount> friends       = DBHelper.QueryToList <UserAccount>(getFriends, new MySqlParameter[] { new MySqlParameter("UserId", state.userId) });
                        string             jsonOfFriends = JsonConvert.SerializeObject(friends);
                        Send(state.TcpClient, Encoding.UTF8.GetBytes("@02@" + jsonOfFriends));
                        break;

                    case "@03@":
                        string             searchByNickName = "select * from useraccount where NickName like '%" + content + "%'";
                        List <UserAccount> userAccounts     = DBHelper.QueryToList <UserAccount>(searchByNickName, new MySqlParameter[] {});
                        string             jsonOfPersons    = JsonConvert.SerializeObject(userAccounts);
                        Send(state.TcpClient, Encoding.UTF8.GetBytes("@03@" + jsonOfPersons));
                        break;

                    case "@04@":
                        ChatRecords record = JsonConvert.DeserializeObject <ChatRecords>(content);
                        if (record != null)
                        {
                            string addRecord = "INSERT chatrecords VALUES(@RecordId,@FromId,@ToId,@SendTime,@Content)";
                            DBHelper.AddData(addRecord, new MySqlParameter[] { new MySqlParameter("RecordId", record.RecordId), new MySqlParameter("FromId", record.FromId)
                                                                               , new MySqlParameter("ToId", record.ToId), new MySqlParameter("SendTime", record.SendTime), new MySqlParameter("Content", record.Content) });

                            this.Invoke((EventHandler) delegate
                            {
                                tbChatContent.AppendText("" + record.FromId + "→" + record.ToId + ":" + record.Content + "\n");
                            });
                        }
                        var res = clientList.Where(u => u.userId == record.ToId).ToList();
                        if (res.Count > 0)
                        {
                            Send(res[0].TcpClient, Encoding.UTF8.GetBytes("@04@" + content));
                        }
                        break;

                    default:
                        break;
                    }
                }
                catch (Exception ex)
                {
                    recv = 0;
                    stream.Close();
                    state.TcpClient.Close();
                    clientList.Remove(state);
                    this.Invoke(new MethodInvoker(() => {
                        this.tbLog.AppendText(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss " + state.ClientAddr + " 断开连接!\n"));
                        this.cbClientList.Items.Clear();
                        foreach (TCPClientState s in clientList)
                        {
                            this.cbClientList.Items.Add(s.clientName);
                        }
                    }));
                    return;
                    //MessageBox.Show(ex.Message);
                }

                try
                {
                    stream.BeginRead(state.Buffer, 0, state.Buffer.Length, HandleDataReceived, state);
                }catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
Ejemplo n.º 11
0
        /// <summary>
        /// 添加客户,并建立连接
        /// </summary>
        /// <param name="addCustomerConnectRecordsDto"></param>
        public BaseDataOutput <int> AddServiceConnectRecords(AddCustomerConnectRecordsDto addCustomerConnectRecordsDto)
        {
            BaseDataOutput <int> output          = new BaseDataOutput <int>();
            int         CustomerConnectRecordsId = 0;
            int         ServiceRecordsId         = 0;
            ChatRecords ChatRecordsModel         = new ChatRecords();
            //处理客户记录表
            CustomerConnectRecords CustomerConnectRecordsModel = _CustomerConnectRecords.FirstOrDefault(e => e.DeviceId == addCustomerConnectRecordsDto.DeviceId);

            if (CustomerConnectRecordsModel == null)
            {
                CustomerConnectRecords NewCustomerConnectRecordsModel = new CustomerConnectRecords();
                NewCustomerConnectRecordsModel = EntityHelper.CopyValue(addCustomerConnectRecordsDto, NewCustomerConnectRecordsModel);
                NewCustomerConnectRecordsModel.CustomerState = OrderInfo.LoginState.Online;
                CustomerConnectRecordsId = _CustomerConnectRecords.InsertAndGetId(NewCustomerConnectRecordsModel);
            }
            else
            {
                CustomerConnectRecordsModel.CustomerState = OrderInfo.LoginState.Online;
                _CustomerConnectRecords.Update(CustomerConnectRecordsModel);
                CustomerConnectRecordsId = CustomerConnectRecordsModel.Id;
            }
            //处理连接记录表
            ServiceRecords ServiceRecordsModel = new ServiceRecords();

            ServiceRecordsModel = EntityHelper.CopyValue(addCustomerConnectRecordsDto, ServiceRecordsModel);
            ServiceRecordsModel.CustomerDeviceId = addCustomerConnectRecordsDto.DeviceId;
            //连接记录表-处理客户信息
            ServiceRecordsModel.CustomerConnectRecordsId = CustomerConnectRecordsId;
            ServiceRecordsModel.CustomerContentDate      = DateTime.Now;
            ServiceRecordsModel.CustomerState            = OrderInfo.LoginState.Online;
            //连接记录表-如果匹配到客服,则处理客服信息
            if (addCustomerConnectRecordsDto.ServiceId != null)
            {
                ServiceConnectRecords ServiceConnectRecordsModel = _ServiceConnectRecords.FirstOrDefault(e => e.ServiceId == addCustomerConnectRecordsDto.ServiceId);
                if (ServiceConnectRecordsModel != null)
                {
                    //连接记录表-处理客服信息
                    ServiceRecordsModel    = EntityHelper.CopyValue(ServiceConnectRecordsModel, ServiceRecordsModel);
                    ServiceRecordsModel.Id = 0;
                    ServiceRecordsModel.ServiceConnectRecordsId = ServiceConnectRecordsModel.Id;
                    ServiceRecordsModel.ServiceId            = addCustomerConnectRecordsDto.ServiceId;
                    ServiceRecordsModel.ServiceContentDate   = DateTime.Now;
                    ServiceRecordsModel.ServiceState         = OrderInfo.LoginState.Online;
                    ServiceConnectRecordsModel.ServiceCount += 1;
                    _ServiceConnectRecords.Update(ServiceConnectRecordsModel);
                    //聊天记录表-处理客服信息
                    ChatRecordsModel           = EntityHelper.CopyValue(ServiceConnectRecordsModel, ChatRecordsModel);
                    ChatRecordsModel.ServiceId = addCustomerConnectRecordsDto.ServiceId;
                }
            }
            ServiceRecordsId = _ServiceRecords.InsertAndGetId(ServiceRecordsModel);
            //聊天记录表-处理客户信息
            ChatRecordsModel    = EntityHelper.CopyValue(addCustomerConnectRecordsDto, ChatRecordsModel);
            ChatRecordsModel.Id = 0;
            ChatRecordsModel.CustomerDeviceId = addCustomerConnectRecordsDto.DeviceId;
            //聊天记录表-处理聊天信息
            ChatRecordsModel.ServiceRecordsId = ServiceRecordsId;
            ChatRecordsModel.SendInfoType     = OrderInfo.SendInfoType.TextInfo;
            ChatRecordsModel.SendSource       = OrderInfo.TerminalRefer.system;
            ChatRecordsModel.SendContent      = "客服[" + ChatRecordsModel.ServiceNickName + "]为您服务";
            ChatRecordsModel.SendDateTime     = DateTime.Now;
            ChatRecordsModel.ReceiveState     = OrderInfo.ReceiveState.Received;
            _ChatRecords.Insert(ChatRecordsModel);
            output.Data = ServiceRecordsId;
            return(output);
        }
Ejemplo n.º 12
0
 /// <summary>
 /// 添加聊天记录
 /// </summary>
 /// <param name="entity"></param>
 /// <returns></returns>
 public int AddRecords(ChatRecords entity)
 {
     db.ChatRecords.Add(entity);
     return(db.SaveChanges());
 }