public static int GetWxFansCount() { string token = WXFansHelper.GetToken(); if (string.IsNullOrEmpty(token)) { return(0); } IList <WXUserCumulate> list = new List <WXUserCumulate>(); string url = $"https://api.weixin.qq.com/datacube/getusercumulate?access_token={token}"; DateTime dateTime = DateTime.Now; dateTime = dateTime.AddDays(-1.0); DateTime date = dateTime.Date; dateTime = DateTime.Now; dateTime = dateTime.AddDays(-1.0); string wXPostResult = WXFansHelper.GetWXPostResult(url, WXFansHelper.GetPostData(date, dateTime.Date)); if (!string.IsNullOrEmpty(wXPostResult)) { WXUserCumulateResult wXUserCumulateResult = JsonHelper.ParseFormJson <WXUserCumulateResult>(wXPostResult); if (wXUserCumulateResult != null && wXUserCumulateResult.list != null && wXUserCumulateResult.list.Count > 0) { list = list.Concat(wXUserCumulateResult.list).ToList(); } } if (list.Count > 0) { return(list.Sum((WXUserCumulate u) => u.cumulate_user)); } return(0); }
public static int SynchroWXFansData(DateTime startDate, DateTime endDate, out bool isSettingErr) { isSettingErr = false; IDictionary <string, string> dictionary = new Dictionary <string, string>(); dictionary.Add("startDate", startDate.ToString()); dictionary.Add("endDate", endDate.ToString()); SiteSettings masterSettings = SettingsManager.GetMasterSettings(); dictionary.Add("AppId", masterSettings.WeixinAppId); dictionary.Add("AppSecret", masterSettings.WeixinAppSecret); try { string token = WXFansHelper.GetToken(); if (string.IsNullOrEmpty(token)) { isSettingErr = true; Globals.AppendLog(dictionary, "token为空", "", "", "SynchroWXFansData"); return(0); } DateTime dateTime = startDate; DateTime t = dateTime; int num = (endDate - startDate).Days + 1; int num2 = (num % 7 == 0) ? (num / 7).ToInt(0) : ((num / 7).ToInt(0) + 1); IList <WXUserSummary> list = new List <WXUserSummary>(); IList <WXUserCumulate> list2 = new List <WXUserCumulate>(); DateTime dateTime2; for (int i = 0; i < num2; i++) { dateTime2 = t.AddDays((double)((i != 0) ? 1 : 0)); dateTime = dateTime2.Date; dateTime2 = dateTime.AddDays(6.0); t = dateTime2.Date; if (t > endDate) { t = endDate; } string wXPostResult = WXFansHelper.GetWXPostResult($"https://api.weixin.qq.com/datacube/getusersummary?access_token={token}", WXFansHelper.GetPostData(dateTime.Date, t.Date)); if (!string.IsNullOrEmpty(wXPostResult)) { WXUserSummaryResult wXUserSummaryResult = JsonHelper.ParseFormJson <WXUserSummaryResult>(wXPostResult); if (wXUserSummaryResult != null && wXUserSummaryResult.list != null) { list = list.Concat(wXUserSummaryResult.list).ToList(); } } else { Globals.AppendLog(dictionary, "获取粉丝增减数据为空", "", "", "SynchroWXFansData"); } wXPostResult = WXFansHelper.GetWXPostResult($"https://api.weixin.qq.com/datacube/getusercumulate?access_token={token}", WXFansHelper.GetPostData(dateTime.Date, t.Date)); if (string.IsNullOrEmpty(wXPostResult)) { return(0); } WXUserCumulateResult wXUserCumulateResult = JsonHelper.ParseFormJson <WXUserCumulateResult>(wXPostResult); if (wXUserCumulateResult == null || wXUserCumulateResult.list == null || wXUserCumulateResult.list.Count <= 0) { return(0); } list2 = list2.Concat(wXUserCumulateResult.list).ToList(); } if (num != 1 || !new WXFansStatisticsDao().IsExistData(startDate)) { for (int j = 0; j < num; j++) { WXFansStatisticsInfo fansItem = new WXFansStatisticsInfo(); WXFansStatisticsInfo wXFansStatisticsInfo = fansItem; dateTime2 = startDate.AddDays((double)j); wXFansStatisticsInfo.StatisticalDate = dateTime2.Date; fansItem.NewUser = (from u in list where u.ref_date == fansItem.StatisticalDate.ToString("yyyy-MM-dd") select u).Sum((WXUserSummary u) => u.new_user); fansItem.CancelUser = (from u in list where u.ref_date == fansItem.StatisticalDate.ToString("yyyy-MM-dd") select u).Sum((WXUserSummary u) => u.cancel_user); fansItem.NetGrowthUser = fansItem.NewUser - fansItem.CancelUser; fansItem.CumulateUser = (from u in list2 where u.ref_date == fansItem.StatisticalDate.ToString("yyyy-MM-dd") select u).Sum((WXUserCumulate u) => u.cumulate_user); new WXFansStatisticsDao().Add(fansItem, null); } } return(num); } catch (Exception ex) { Globals.WriteExceptionLog(ex, null, "SynchroWXFansDataErr"); return(0); } }