using Api.Framework; using Api.Framework.Enums; using Api.Framework.Model; using Api.Framework.SDK; using Api.Framework.Tools; using Chat.Framework.Utils; using SqlSugar; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; using Weixin.WphCirclePromotion; using Weixin.WPHCirclePromotion; using Weixin.WPHCirclePromotion.Entitys; using Weixin.WPHCirclePromotion.Properties; using static Weixin.WPHCirclePromotion.Enums; namespace Weixin.WPHCirclePromotion { public class SendCircleFriendThread : TimerTask { WPHHelper wphHelper = new WPHHelper(); 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_wphcirclepromotion_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_wphcirclepromotion_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 pd_circle_promotion_temp = session.FindWphCirclePromotions().Where(f => f.onoff); if (pd_circle_promotion_temp != null) { if (pd_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 = Util.EncryptDES($"fl_{item.WeixinHao}_{(int)ChatType.微信}_CircleF"); var api = CpsClient.CreateWeipinhuiRequest(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(goods_infos[i].goods_id, GoodsSourceType.对象, member);//获取到自己返利链接等信息 var result = analyzeGoods.FindGoodsInfoToGoodsId(goods_infos[i].goodsId, member);//获取到自己返利链接等信息 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.fav).ToString(); var resultTmp = new WPHHelper().GetCpsUrl(api, new { goodsIdList = new List() { goods_infos[i].goodsId }, chanTag = adzone.adzone_pid, requestId = Util.GetUUID(), statParam = param }); if (resultTmp == null || resultTmp.returnCode == null || resultTmp.returnCode != "0") throw new Exception("唯品会转链异常"); var pict_url = string.IsNullOrWhiteSpace(result.goodsMainPicture) ? string.Empty : $"[图片={result.goodsMainPicture}]"; var couponUrl = (resultTmp.result != null && resultTmp.result.urlInfoList.Count != 0) ? resultTmp.result.urlInfoList[0].url : string.Empty; if (string.IsNullOrWhiteSpace(couponUrl)) return; var goods_desc = string.IsNullOrWhiteSpace(Class1.Config.Goods_Desc) ? result.goodsDesc : Class1.Config.Goods_Desc .Replace("劵", "券") //[商品原价]、[商品唯品价]、[券后唯品价] .Replace("[商品标题]", result.goodsName) .Replace("[商品描述]", result.goodsDesc) .Replace("[店铺名称]", result.storeName) .Replace("[优惠券门槛金额]", result.buy.ToString("0.00")) .Replace("[优惠券金额]", result.fav.ToString("0.00")) .Replace("[优惠券数量]", result.totalAmount.ToString()) .Replace("[商品原价]", result.marketPrice.ToString("0.00")) .Replace("[券后唯品价]", cprice) .Replace("[购买地址地址]", couponUrl) .Replace("[商品唯品价]", result.vipPrice.ToString("0.00")); var image = ApiClient.GetQRImage(result.goodsName, result.vipPrice.ToString("0.00"), result.fav.ToString("0.00"), cprice, result.goodsMainPicture, couponUrl, 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_wphcirclepromotion_goodsinfos set state = @state where id = @id", new { state = Enums.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}"); } } } }