using Api.Framework; using Api.Framework.Enums; using Api.Framework.Model; using Api.Framework.SDK; using Api.Framework.Tools; using SqlSugar; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using Weixin.SNCirclePromotion.Entitys; using Weixin.SNCirclePromotion.Properties; namespace Weixin.SNCirclePromotion { public class SendCircleFriendThread : TimerTask { SNHelper snHelper = new SNHelper(); AnalyzeGoods analyzeGoods = new AnalyzeGoods(); StringBuilder strb = new StringBuilder(); bool isSend = false; bool isSendTime = false;//这里是用来判断是否在任务时间段里面,如果满足这会赋值为true SqlSugarClient session = ApiClient.GetSession(); private static object objLock = new object(); public override void Run(object state, bool timedOut) { #region 定时清空采集的数据 if (!string.IsNullOrWhiteSpace(Class1.Config.AutoClearTaskTimes)) { if (Class1.Config.Auto_IsClear == SwitchType.开启) { try { if (DateTime.Now.AddSeconds(-3).ToString("HHmm") == DateTime.Parse(Class1.Config.AutoClearTaskTimes).ToString("HHmm")) { session.ExcuteSQL("Delete from fl_plugin_sncirclepromotion_goodsinfos", new { }); } } catch (Exception ex) { EventClient.OnEvent(this, "定时清理异常" + ex.Message + "-" + ex.StackTrace); } } } #endregion isSendTime = false; var now = DateTime.Now; for (int i = 0; i < Class1.Config.SendTaskTimes.Length; i++) { if (!string.IsNullOrWhiteSpace(Class1.Config.SendTaskTimes[i])) { var times = Class1.Config.SendTaskTimes[i].Trim().Split('-'); if (times.Length == 2) { var begin = DateTime.Parse(times[0].Trim()); var end = DateTime.Parse(times[1].Trim()); if (begin <= now && now <= end) { isSendTime = true; break; } } } } if (!isSendTime) return; if ((DateTime.Now - Class1.Config.RecordSendTime).TotalMinutes < Class1.Config.Interval_Circle) return; try { lock (objLock) { session.ExcuteSQL("Delete from fl_plugin_sncirclepromotion_goodsinfos where cur_time <= @time", new { time = DateTime.Today.AddDays(-15) }); isSend = false; #region 采集数据 if (Class1.Config.Switch == SwitchType.开启) { var num = session.Queryable().Count(f => f.state == StateType.未推广); if (num <= Class1.Config.GatherTouchNum) analyzeGoods.GatherFactory(); } #endregion var goods_infos_temp = new List(); var goods_infos = session.Queryable().Where(f => f.state == StateType.未推广).OrderBy(f => f.cur_time, OrderByType.Desc).ToPageList(1, (Class1.Config.SendGoodsImageNum <= 0 ? 1 : Class1.Config.SendGoodsImageNum)); if (goods_infos != null && goods_infos.Count != 0) { var clients = Chat.Framework.ChatClient.WXClient.Values.ToList(); foreach (var item in clients) { try { if ((item.WeixinType == Chat.Framework.WXSdk.Implement.WeixinType.Grpc微信 || item.WeixinType == Chat.Framework.WXSdk.Implement.WeixinType.Hook微信) && item.Status == Chat.Framework.WXSdk.WxStatus.在线) { var robot_info = session.FindRobotInfo(item.WeixinHao, ChatType.微信); if (robot_info != null) { var sn_circle_promotion_temp = session.FindSnCirclePromotions().Where(f => f.onoff); if (sn_circle_promotion_temp != null) { if (sn_circle_promotion_temp.FirstOrDefault(f => f.name == robot_info.name) != null) continue; } var adzone = session.FindAdzoneInfos().FirstOrDefault(f => f.alliance_id == (int)CpsType.苏宁易购 && f.custom_type == Resources.SoftwareType && f.robot_id == robot_info.id); if (adzone != null) { var member = CpsClient.Members.FirstOrDefault(f => f.cpstype == CpsType.苏宁易购 && f.username == adzone.adzone_pid_cps_name); if (member != null) { //string param = $"fl_{item.WeixinHao}_{(int)ChatType.微信}_朋友圈推广"; var api = CpsClient.CreateSuNingRequest(member); if (api == null) { return; } strb.Length = 0; strb.AppendLine(Class1.Config.Content_Circle); int count = 0; for (int i = 0; i < goods_infos.Count; i++) { try { goods_infos_temp.Add(goods_infos[i]); var goodsinfo = goods_infos[i]; count = 0; Next: var result = analyzeGoods.FindGoodsInfoToGoodsId(api, goods_infos[i].goodsid + "-" + goods_infos[i].supplierCode, GoodsSourceType.对象);//获取到自己返利链接等信息 if (result == null) { count++; if (count <= 5) { goodsinfo = session.Queryable().Where(f => f.state == StateType.未推广).OrderBy(f => f.cur_time, OrderByType.Desc).First(); goods_infos_temp.Add(goodsinfo); goto Next; } else continue; } else { var cprice = (result.vipPrice - result.coupon_discount).ToString(); #region 转链 //pid=(?.+?)\sr=(?<机器人名>.*?)\st=(?<类型>\d+)\su=(?<会员名>.+?)\smid=(?<店铺ID>\d+)\sgid=(?<商品ID>\d+) var custom = Util.EncryptDES($"pid={adzone.adzone_pid}"); var extension = api.GetExtensionUrl(custom, result.buyUrl, result.couponUrl); if (extension.sn_responseContent.sn_body == null) throw new Exception(); #endregion var goods_desc = string.IsNullOrWhiteSpace(Class1.Config.Goods_Desc) ? result.goodsdesc : Class1.Config.Goods_Desc .Replace("[商品标题]", result.goodsname) .Replace("[商品描述]", result.goodsdesc) .Replace("[已售数量]", result.sold_quantity.ToString()) .Replace("[店铺名称]", result.supplierName) .Replace("[优惠券门槛金额]", result.coupon_min_order_amount.ToString()) .Replace("[优惠券金额]", result.coupon_discount.ToString()) .Replace("[劵后价]", cprice) .Replace("[购买地址]", extension.sn_responseContent.sn_body.getExtensionlink.shortLink); var image = ApiClient.GetQRImage(result.goodsname, result.vipPrice.ToString(), result.coupon_discount.ToString(), cprice, result.picurl, extension.sn_responseContent.sn_body.getExtensionlink.shortLink, Class1.Config.Model, CpsType.苏宁易购, result.goodsdesc); //合成附带二维码等信息的宝贝图片 strb.Append($"[图片={image}]"); } } catch { } } var json = item.SendCircle(strb.ToString()); if (!string.IsNullOrWhiteSpace(Class1.Config.AdditionalComment)) { var reg = Regex.Match(json.Replace("", ""), @"(?\d{15,})");//获取返回的朋友圈id if (reg.Success) item.SendCircleComment(item.User.Username, reg.Groups["id"].Value, Class1.Config.AdditionalComment); } isSend = true;//认定发送过了... } } } } } catch (Exception ex) { //EventClient.OnEvent(this, $"拼多多推广:{item.WeixinHao},朋友圈推广失败,{ex.Message}"); } } if (isSend) { for (int i = 0; i < goods_infos_temp.Count; i++) { session.ExcuteSQL("update fl_plugin_sncirclepromotion_goodsinfos set state = @state where id = @id", new { state = StateType.已推广, id = goods_infos_temp[i].id }); } Class1.Config.RecordSendTime = DateTime.Now; Util.Save(Class1.Config); } } } } catch (Exception ex) { EventClient.OnEvent(this, $"拼多多推广:{ex.Message},{ex.StackTrace}"); } } } }