using Newtonsoft.Json.Linq; using SqlSugar; using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Text; using TelegramService.Help; using TelegramService.Entity; using TelegramService.Help; using System.Threading.Tasks; namespace TelegramServiceV6.Bl { public class FundBl { /// /// 发送微信 /// /// /// /// /// /// /// public static void SendWeixinFund() { SendWeixinFundEastmoney(); } /// /// 发送微信 http://fund.eastmoney.com/ 天天基金 /// /// /// /// /// /// /// public static async Task SendWeixinFundEastmoney() { try { EntrustHelper.STCB("show", "[FundBl.SendWeixinFundEastmoney] 开始发送基金"); string code = string.Join(",", Appsettings.Conf.Fund); Dictionary form = new Dictionary(); form.Add("fcodes", code); Dictionary header = new Dictionary(); header.Add("Origin", "https://favor.fund.eastmoney.com"); header.Add("Referer", "https://favor.fund.eastmoney.com/"); HttpResult httpResult = null; string html = ""; for (int i = 0; i < 6; i++) { try { httpResult = await HttpHelper.PostFormAsync("https://api.fund.eastmoney.com/favor/GetFundsInfo?", form, header); if (httpResult.StatusCode == 200) { html = httpResult.Html; break; } } catch (Exception) { } } if (html.IsNull()) { EntrustHelper.STCB("show", "[FundBl.SendWeixinFundEastmoney] 开始发送基金,请求失败"); EntrustHelper.STCB("show", "[FundBl.SendWeixinFundEastmoney] "+html); EntrustHelper.STCB("show", "[FundBl.SendWeixinFundEastmoney] 开始发送基金,请求失败"); return; } JObject jsdata = JObject.Parse(html); if (jsdata["ErrCode"].ToString() != "0") { EntrustHelper.STCB("show", "[FundBl.SendWeixinFundEastmoney1] "+jsdata.ToString()); return; } StringBuilder sb = new StringBuilder(); StringBuilder sb1 = new StringBuilder(); sb1.Append("---------------------------------\n"); int index = 0; foreach (var data in jsdata["Data"]["KFS"]) { if (!code.Contains(data["FCODE"].ToString())) { continue; } if (data["RZDF"].ToDouble() >= 0) { if (sb.Length > 0) sb.Append("---------------------------------\n"); sb.Append(data["FSRQ"] + " (" + data["FCODE"].ToString() + ")\n"); sb.Append(data["SHORTNAME"] + "\n"); sb.Append(data["RZDF"] + "\n"); } else { sb1.Append("---------------------------------\n"); sb1.Append(data["FSRQ"] + " (" + data["FCODE"].ToString() + ")\n"); sb1.Append(data["SHORTNAME"] + "\n"); sb1.Append(data["RZDF"] + "\n"); } index++; } for (int i = 0; i < 2; i++) { EntrustHelper.STCB("show", "[FundBl.SendWeixinFundEastmoney] 请求微信api接口"); httpResult=await HttpHelper.GetAsync("https://111.230.99.204/weixin/api/msgyq?id=CaiMengNan&text=" + sb.ToString() + sb1.ToString()); if (httpResult.StatusCode==200 && httpResult.Html.IndexOfStr("\"errmsg\":\"ok\"")) { EntrustHelper.STCB("show", "[FundBl.SendWeixinFundEastmoney] 发送基金成功"); return; } } } catch (Exception ex) { EntrustHelper.STCB("show", "[FundBl.SendWeixinFundEastmoney] "+ex.Message); } finally { try { DateTime now = DateTime.Now; DateTime next22 = new DateTime(now.Year, now.Month, now.Day, 22, 0, 0); // 如果现在已经过了今天的 22:00,则计算明天的 22:00 if (now >= next22) { next22 = next22.AddDays(1); } double millisecondsUntilNext22 = (next22 - now).TotalMilliseconds; EntrustHelper.STCB("show", "[FundBl.SendWeixinFundEastmoney] 等待下次执行"); EntrustHelper.STCB("sendWeixinFund", millisecondsUntilNext22); } catch (Exception ex) { EntrustHelper.STCB("show", "[FundBl.SendWeixinFundEastmoney] " + ex.Message); } } } } }