/// <summary> /// 检查好友 /// </summary> private void CheckFriends() { var entities = FormulaHelper.GetEntities <CommonEntities>(); while (true) { try { if (fansQueue.Count > 0) { #region 加载联系人信息并发送开始提示信息 foreach (var item in fansQueue.Where(c => c.State == 3)) { try { #region 加载并保存用户信息 CookieContainer cc = item.Cookies; IDictionary <string, string> synckey = new Dictionary <string, string>(); JObject init_result = WxService.WxInit(ref cc, item.PassTicket, ref synckey); item.SyncKey = synckey; if (init_result == null) { continue; } else if (init_result["BaseResponse"]["Ret"].ToString() == LogoutErrorCode) { FinishFans(item, LoginErrorTimeMessage); } else { var fansinfo = entities.Set <FansInfo>().Where(c => c.OpenID == item.OpenID).FirstOrDefault(); if (fansinfo == null) { fansinfo = new FansInfo(); fansinfo.ID = FormulaHelper.CreateGuid(); fansinfo.OpenID = item.OpenID; fansinfo.NickName = init_result["User"]["NickName"].ToString(); fansinfo.HeadImgUrl = init_result["User"]["HeadImgUrl"].ToString(); fansinfo.RemarkName = init_result["User"]["RemarkName"].ToString(); fansinfo.Sex = int.Parse(init_result["User"]["Sex"].ToString()); fansinfo.Signature = init_result["User"]["Signature"].ToString(); fansinfo.VerifyFlag = int.Parse(init_result["User"]["VerifyFlag"].ToString()); fansinfo.SnsFlag = int.Parse(init_result["User"]["SnsFlag"].ToString()); entities.Set <FansInfo>().Add(fansinfo); } else { fansinfo.NickName = init_result["User"]["NickName"].ToString(); fansinfo.HeadImgUrl = init_result["User"]["HeadImgUrl"].ToString(); fansinfo.RemarkName = init_result["User"]["RemarkName"].ToString(); fansinfo.Sex = int.Parse(init_result["User"]["Sex"].ToString()); fansinfo.Signature = init_result["User"]["Signature"].ToString(); fansinfo.VerifyFlag = int.Parse(init_result["User"]["VerifyFlag"].ToString()); fansinfo.SnsFlag = int.Parse(init_result["User"]["SnsFlag"].ToString()); } } JObject contact_result = WxService.GetContact(ref cc); if (init_result == null) { continue; } else if (init_result["BaseResponse"]["Ret"].ToString() == LogoutErrorCode) { FinishFans(item, LoginErrorTimeMessage); } else { entities.Set <FansFriends>().Delete(c => c.OpenID == item.OpenID); item.Friends = new List <FansFriend>(); foreach (JObject contact in contact_result["MemberList"]) //完整好友名单 { var user = new FansFriends(); user.ID = FormulaHelper.CreateGuid(); user.OpenID = item.OpenID; user.NickName = contact["NickName"].ToString(); user.HeadImgUrl = contact["HeadImgUrl"].ToString(); user.ContactFlag = int.Parse(contact["ContactFlag"].ToString()); user.RemarkName = contact["RemarkName"].ToString(); user.Sex = int.Parse(contact["Sex"].ToString()); user.Signature = contact["Signature"].ToString(); user.VerifyFlag = int.Parse(contact["VerifyFlag"].ToString()); user.StarFriend = int.Parse(contact["StarFriend"].ToString()); user.Province = contact["Province"].ToString(); user.City = contact["City"].ToString(); user.Alias = contact["Alias"].ToString(); user.SnsFlag = int.Parse(contact["SnsFlag"].ToString()); user.KeyWord = contact["KeyWord"].ToString(); entities.Set <FansFriends>().Add(user); var username = contact["UserName"].ToString(); if (user.VerifyFlag == 0 && !SpecialUsers.Contains(username) && !username.Contains("@@") && username != init_result["User"]["UserName"].ToString()) { FansFriend ff = new FansFriend(); ff.UserName = username; ff.NickName = string.IsNullOrEmpty(user.RemarkName) ? user.NickName : user.RemarkName; ff.State = 0; item.Friends.Add(ff); } } } item.Cookies = cc; item.SyncKey = synckey; try { entities.SaveChanges(); } catch (DbEntityValidationException dbEx) { LogWriter.Error(dbEx); throw dbEx; } #endregion #region 发送微信登录提示信息 if (!string.IsNullOrEmpty(CheckStartMessage)) { try { SendWxMsg(item.OpenID, string.Format(CheckStartMessage, item.Friends.Count, (item.Friends.Count / CheckFriendsCount) + 1)); } catch { item.WxErrorCount++; throw; } } #endregion #region 更新用户状态 item.State = 4; var ety = entities.Set <FansLogin>().Find(item.LoginID); if (ety != null) { ety.IsLogin = true; ety.LoginTime = DateTime.Now; entities.SaveChanges(); } #endregion } catch (Exception ex) { LogWriter.Error(ex); } } #endregion #region 检查好友状态 foreach (var item in fansQueue.Where(c => c.State == 4)) { //每次拉群的时间间隔都要超过30秒,不然微信不让调用网页版接口 if (item.LastCheckTime.AddSeconds(CheckFriendsSingleTime) > DateTime.Now) { continue; } CookieContainer cc = item.Cookies; JObject temp = null; #region 添加好友 if (item.ChartRoomUsers.Count() == 0) { var checkfriends = item.Friends.Where(c => c.State == 0).Take(CheckFriendsCount); if (string.IsNullOrEmpty(item.ChartRoomName)) { temp = WxService.CreateRoom(checkfriends.Select(c => c.UserName).ToList(), ref cc, item.SKey, item.PassTicket); item.LastCheckTime = DateTime.Now; item.Cookies = cc; if (temp["BaseResponse"]["Ret"].ToString() == "0") { item.ChartRoomName = temp["ChatRoomName"].ToString(); item.ChartRoomUsers = checkfriends.Select(c => c.UserName).ToList(); } //操作太频繁 else if (temp["BaseResponse"]["Ret"].ToString() == "1" || temp["BaseResponse"]["ErrMsg"].ToString().Contains("频")) { FinishFans(item, WebMaxErrorMsg); } else { item.WebErrorCount++; continue; } } else { temp = WxService.AddMember(item.ChartRoomName, checkfriends.Select(c => c.UserName).ToList(), ref cc, item.SKey, item.PassTicket); item.LastCheckTime = DateTime.Now; if (temp["BaseResponse"]["Ret"].ToString() == "0") { item.ChartRoomName = temp["ChatRoomName"].ToString(); item.ChartRoomUsers = checkfriends.Select(c => c.UserName).ToList(); } //操作太频繁 else if (temp["BaseResponse"]["Ret"].ToString() == "1" || temp["BaseResponse"]["ErrMsg"].ToString().Contains("频")) { FinishFans(item, WebMaxErrorMsg); } else { item.WebErrorCount++; continue; } } item.Cookies = cc; foreach (JObject obj in temp["MemberList"]) { if (obj["MemberStatus"].ToString() == "4") { item.Friends.Where(c => c.UserName == obj["UserName"].ToString()).AsQueryable().Update(c => c.State = 3); } else if (obj["MemberStatus"].ToString() == "3") { item.Friends.Where(c => c.UserName == obj["UserName"].ToString()).AsQueryable().Update(c => c.State = 2); } else { item.Friends.Where(c => c.UserName == obj["UserName"].ToString()).AsQueryable().Update(c => c.State = 1); } } } #endregion #region 移除好友 else { temp = WxService.DeleteMember(item.ChartRoomName, item.ChartRoomUsers, ref cc, item.SKey, item.PassTicket); item.LastCheckTime = DateTime.Now; if (temp["BaseResponse"]["Ret"].ToString() == "0") { item.ChartRoomUsers.Clear(); } //操作太频繁 else if (temp["BaseResponse"]["Ret"].ToString() == "1" || temp["BaseResponse"]["ErrMsg"].ToString().Contains("频")) { FinishFans(item, WebMaxErrorMsg); } else { item.WebErrorCount++; } if (item.Friends.All(c => c.State != 0)) { WxService.Logout(ref cc, item.SKey); var delete = item.Friends.Count(c => c.State == 3); var block = item.Friends.Count(c => c.State == 2); if (delete == 0 && block == 0) { FinishFans(item, CheckGoodMessage); } else { FinishFans(item, string.Format(CheckGoodMessage , delete == 0 ? "没有" : string.Join(",", item.Friends.Where(c => c.State == 3).Select(c => c.NickName)) , block == 0 ? "没有" : string.Join(",", item.Friends.Where(c => c.State == 2).Select(c => c.NickName)))); } } } #endregion item.Cookies = cc; } #endregion #region 检查登录状态 foreach (var item in fansQueue.Where(c => c.State == 4)) { CookieContainer cc = item.Cookies; IDictionary <string, string> synckey = item.SyncKey; var sync_flag = WxService.WxSyncCheck(ref cc, ref synckey, item.SKey); if (sync_flag == LogoutErrorJS) { FinishFans(item, LoginErrorTimeMessage); } else { item.Cookies = cc; item.SyncKey = synckey; } if (item.WxErrorCount > InterfaceMaxError) { FinishFans(item, ""); } if (item.WebErrorCount > InterfaceMaxError) { FinishFans(item, WebMaxErrorMsg); } } foreach (var item in fansQueue.Where(c => new int[] { 1, 2, 3 }.Contains(c.State) && c.WxErrorCount > InterfaceMaxError)) { FinishFans(item, ""); } #endregion } } catch (Exception ex) { LogWriter.Error(ex); } finally { Thread.Sleep(CheckFriendsTime); } } }