Beispiel #1
0
        /// <summary>
        /// 根据IP获取实际地址
        /// </summary>
        /// <param name="ip">网络地址</param>
        /// <returns>返回实际地址</returns>
        public static string GetAddress(string ip)
        {
            using (Model2DataContext db = new Model2DataContext())
            {
                string address = "";
                IpToAddress ipToAddress = db.PondOfIpToAddress.FirstOrDefault(x => x.Ip == ip);
                if (ipToAddress != null)
                {
                    address = ipToAddress.Address;
                }
                else
                {
                    string path = "http://www.ip138.com/ips138.asp?ip=" + ip;
                    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(path);
                    request.Method = "GET";
                    request.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)";
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                    Stream stream = response.GetResponseStream();
                    StreamReader reader = new StreamReader(stream, Encoding.GetEncoding("gb2312"));
                    string html = reader.ReadToEnd();
                    stream.Close();

                    Regex reg = new Regex("<ul class=\"ul1\"><li>本站主数据:([\\w\\W]*)</li><li>参考数据");
                    address = reg.Match(html).Groups[1].Value;
                    IpToAddressManager.Create(ip, address);
                }

                return address;
            }
        }
Beispiel #2
0
 public byte[] ReadPic(string token)
 {
     using (Model2DataContext db = new Model2DataContext())
     {
         Picture pic = db.PondOfPicture.First(x => x.Token == token);
         return pic.Content;
     }
 }
Beispiel #3
0
 /// <summary>
 /// 声明已经阅读了某条
 /// </summary>
 /// <param name="messageId"></param>
 /// <param name="username"></param>
 public static void Read(int messageId, string username)
 {
     using (Model2DataContext db = new Model2DataContext())
     {
         Message message = db.PondOfMessage.FirstOrDefault(x => x.Id == messageId);
         if (message == null) { throw new Exception("消息不存在"); }
         if (message.Readed) { return; }
         message.Readed = true;
         db.SaveChanges();
     }
 }
Beispiel #4
0
        /// <summary>
        /// 发送一个新的聊天信息
        /// </summary>
        /// <param name="_from">发件人</param>
        /// <param name="_to">收件人</param>
        /// <param name="content">正文</param>
        /// <param name="ip">来源地址</param>
        /// <param name="address">来源地址</param>
        /// <param name="readed">已经阅读</param>
        /// <param name="isOfficial">一个布尔值 表示是否官方信息</param>
        /// <returns>返回被创建的聊天信息的封装</returns>
        public static Message Send(string _from, string _to, string content, string ip, string address
            , bool readed, bool isOfficial = false)
        {
            using (Model2DataContext db = new Model2DataContext())
            {
                Message message = new Message(_from, _to, content, ip, address, readed, isOfficial);
                db.PondOfMessage.Add(message);
                db.SaveChanges();

                return message;
            }
        }
 /// <summary>
 /// 创建一个新的Ip/实际地址对照
 /// </summary>
 /// <param name="ip">网络地址</param>
 /// <param name="address">实际地址</param>
 public static void Create(string ip, string address)
 {
     using (Model2DataContext db = new Model2DataContext())
     {
         db.PondOfIpToAddress.Where(x => x.Ip == ip)
             .ToList()
             .ForEach(x => db.PondOfIpToAddress.Remove(x));
         IpToAddress t = new IpToAddress(ip, address);
         db.PondOfIpToAddress.Add(t);
         db.SaveChanges();
     }
 }
Beispiel #6
0
        public static string Create(byte[] content)
        {
            using (Model2DataContext db = new Model2DataContext())
            {
                string token = Guid.NewGuid().ToString("N");
                Picture pic = new Picture(token, content);
                db.PondOfPicture.Add(pic);
                db.SaveChanges();

                return token;
            }
        }
Beispiel #7
0
 /// <summary>
 /// 读取未读信息列表
 /// </summary>
 /// <param name="targetUser">对方的用户名</param>
 /// <param name="self">自己的用户名</param>
 /// <returns>返回未读信息的列表</returns>
 public static List<MessageResult> ReadUnreadMessages(string targetUser, string self)
 {
     using (Model2DataContext db = new Model2DataContext())
     {
         List<MessageResult> result = new List<MessageResult>();
         List<Message> tList = db.PondOfMessage.Where(x => x.From == targetUser && x.To == self && !x.Readed)
             .ToList();
         tList.ForEach(x =>
             {
                 x.Readed = true;
                 MessageResult mr = new MessageResult(x, x.From == self);
                 result.Add(mr);
             });
         db.SaveChanges();
         return result;
     }
 }
Beispiel #8
0
 /// <summary>
 /// 读取聊天记录的分页列表
 /// </summary>
 /// <param name="targetUser">对方的用户名</param>
 /// <param name="self">自己的用户名</param>
 /// <param name="pageSize">每个页面包含信息条数</param>
 /// <returns>返回聊天记录的分页列表</returns>
 public static PaginationList<MessageResult> ReadMessages(string targetUser, string self, int page, int pageSize)
 {
     using (Model2DataContext db = new Model2DataContext())
     {
         int statrRow = (page - 1) * pageSize;
         Expression<Func<Message, bool>> pre = x => (x.To == self && x.From == targetUser)
             || (x.From == self && x.To == targetUser);
         int c = db.PondOfMessage.Count(pre);
         List<MessageResult> t = db.PondOfMessage.Where(pre)
             .OrderByDescending(x => x.CreatedTime)
             .Skip(statrRow)
             .Take(pageSize)
             .ToList()
             .ConvertAll(x => new MessageResult(x, x.From == self));
         return new PaginationList<MessageResult>(page, pageSize, c, t);
     }
 }
Beispiel #9
0
 /// <summary>
 /// 读取未读信息的数量
 /// </summary>
 /// <param name="self">自己的用户名</param>
 /// <returns>返回未读信息的数量</returns>
 public static List<UnreadMessageCountResult> ReadCountOfUnreadMessages(string self)
 {
     using (Model2DataContext db = new Model2DataContext())
     {
         List<UnreadMessageCountResult> result = new List<UnreadMessageCountResult>();
         List<string> tos = db.PondOfMessage.Where(x => x.To == self && !x.Readed)
             .Select(x => x.From)
             .Distinct()
             .ToList();
         tos.ForEach(sender =>
             {
                 int c = db.PondOfMessage.Count(x => x.To == self && x.From == sender && !x.Readed);
                 result.Add(new UnreadMessageCountResult(sender, c));
             });
         return result;
     }
 }