/// <summary> /// 检查用户登录 /// </summary> private void CheckLogin() { var entities = FormulaHelper.GetEntities <CommonEntities>(); while (true) { try { #region 发送登录二维码给粉丝 foreach (var item in fansQueue.Where(c => c.State == 0)) { try { #region 获取图片并保存到本地 CookieContainer cc = item.Cookies; string sessionid = item.SessionID; var img = WxService.GetQRCode(ref cc, ref sessionid); item.Cookies = cc; item.SessionID = sessionid; string filename = string.Format("{0}.jpg", DateTime.Now.ToString("yyyMMddHHmmss")); img.Save(string.Format("{0}{1}", QrPhysicsPath, filename), ImageFormat.Jpeg); #endregion #region 到微信临时素材库 UploadTemporaryMediaResult wximgresult = null; try { wximgresult = MediaApi.UploadTemporaryMedia(accesstoken, UploadMediaFileType.image, string.Format("{0}{1}", QrPhysicsPath, filename)); } catch (Exception ex) { LogWriter.Error(ex, string.Format("openid为{0}的登录图片第一次上传失败", item.OpenID)); UpdateAccessToken(); try { wximgresult = MediaApi.UploadTemporaryMedia(accesstoken, UploadMediaFileType.image, string.Format("{0}{1}", QrPhysicsPath, filename)); } catch (Exception ex2) { LogWriter.Error(ex2, string.Format("openid为{0}的登录图片第二次上传失败", item.OpenID)); UpdateNewAccessToken(); try { wximgresult = MediaApi.UploadTemporaryMedia(accesstoken, UploadMediaFileType.image, string.Format("{0}{1}", QrPhysicsPath, filename)); } catch (Exception ex3) { LogWriter.Error(ex3, string.Format("openid为{0}的登录图片第三次上传失败", item.OpenID)); item.WxErrorCount++; continue; } } } #endregion #region 发送消息给用户 if (!string.IsNullOrEmpty(wximgresult.media_id)) { try { SendWxImg(item.OpenID, wximgresult.media_id); SendWxMsg(item.OpenID, LoginMessage); } catch { item.WxErrorCount++; throw; } } #endregion #region 更新用户状态 item.State = 1; item.SendQRTime = DateTime.Now; var ety = entities.Set <FansLogin>().Find(item.LoginID); if (ety != null) { ety.SessionID = sessionid; ety.IsSendQR = true; ety.SendQRTime = DateTime.Now; entities.SaveChanges(); } #endregion } catch (Exception ex) { LogWriter.Error(ex); } } #endregion #region 检查超时登录登录状态 foreach (var item in fansQueue.Where(c => (c.State == 1 || c.State == 2) && c.SendQRTime.AddMinutes(5) <= DateTime.Now)) { FinishFans(item, LoginOutTimeMessage); } lock (queuelock) { var count = fansQueue.RemoveAll(c => c.State == 5); while (fansQueue.Count < CheckFriendsMax && waitFansQueue.Any()) { var item = waitFansQueue.Peek(); fansQueue.Add(new FansData() { LoginID = item.LoginID, OpenID = item.OpenID, ApplyTime = item.ApplyTime, State = 0, Cookies = new CookieContainer(), }); LogWriter.Info(string.Format("waitadd {0}", JsonConvert.SerializeObject(new { LoginID = item.LoginID, OpenID = item.OpenID, ApplyTime = item.ApplyTime, State = 0, }))); waitFansQueue.Dequeue(); } } #endregion #region 检查登录状态 foreach (var item in fansQueue.Where(c => c.State == 1 || c.State == 2)) { CookieContainer cc = item.Cookies; string passticket = item.PassTicket; string skey = item.SKey; var login_result = WxService.LoginCheck(ref cc, item.SessionID); if (login_result is Image) { item.State = 2; } else if (login_result is string) { WxService.GetSidUid(ref cc, login_result as string, ref passticket, ref skey); item.State = 3; item.LoginTime = DateTime.Now; #region 更新用户状态 var ety = entities.Set <FansLogin>().Find(item.LoginID); if (ety != null) { ety.PassTicket = passticket; ety.Skey = skey; ety.IsLogin = true; ety.LoginTime = DateTime.Now; entities.SaveChanges(); } #endregion } item.Cookies = cc; item.PassTicket = passticket; item.SKey = skey; } 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(CheckLoginTime); } } }