using Api.Framework; using Api.Framework.SDK; using ProductClient.Properties; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using Api.Framework.Cps; using Api.Framework.Enums; using Api.Framework.Events; using Api.Framework.Utils; using Chat.Framework.WXSdk; using CsharpHttpHelper; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using ProductClient.Entitys; using Chat.Framework.WXSdk.Implement; using Api.Framework.Tools; namespace ProductClient { public class Class1 : Plugin { public Class1() { this.Name = "爆单-代运营"; this.Note = "专业的运营团队、代运营朋友圈、直播群!"; this.Logo = Resources.爆单; } #region 自定义变量 public static Config Config; private MainForm mainForm = null; #endregion public override void Start() { try { var session = ApiClient.GetSession(); #region 判断表是否存在,不存在创建表 //if (!session.TableExist<>()) //{ // session.CreateTable<>(); // session.AddIndex<>(""); //} #endregion //创建配置文件 Config = this.ReadConfig(); EventClient.DyyNoticeEvent += EventClient_DyyNoticeEvent; var thread = TimerTask.NewTimer(60) as MonitorThread; //EventClient_DyyNoticeEvent(null, new DyyNoticeEvent() //{ // //Data = "{\"Id\":\"122522636742819840\",\"TaskId\":\"122522636742819840\",\"IsGoods\":true}" // Data = "{\"TaskId\":\"142515582485397504\",\"CollectionSourceId\":\"129505086429724672\",\"IsGoods\":true}" //}); Task.Run(() => { thread.Sleep(1000 * 30); MonitorThread.GetDyyCircleGoodsLastTask(); }); } catch (Exception ex) { this.OnLog(ex.Message); } } public override void ShowForm() { try { if (mainForm == null || mainForm.IsDisposed) { mainForm = new MainForm(); mainForm.Show(); } mainForm.Activate(); } catch (Exception ex) { this.OnLog("执行时被" + ex.Message); } } public override void Stop() { try { EventClient.DyyNoticeEvent -= EventClient_DyyNoticeEvent; //TimerTask.Close(); if (mainForm != null) { mainForm.CloseForm(); mainForm = null; } } catch (Exception ex) { this.OnLog(ex.Message); } } private static Dictionary CacheDic = new Dictionary(); private void EventClient_DyyNoticeEvent(object sender, DyyNoticeEvent e) { try { EventClient.OnEvent(this, $"代运营新动态:{e.Data}"); var json = e.Data; //{"Id":"122522636742819840","TaskId":"122522636742819840","IsGoods":true} //var json = "{\"TaskId\":\"142515582485397504\",\"CollectionSourceId\":\"129505086429724672\",\"IsGoods\":true}"; var jObj = JObject.Parse(json); var taskId = jObj["TaskId"]?.Value(); if (string.IsNullOrWhiteSpace(taskId)) { return; } if (CacheDic.ContainsKey(taskId)) { return; } //发送任务来源Id var collectionSourceId = jObj["CollectionSourceId"]?.Value(); if (Config.sourceId == null) { Config.sourceId = "129505086429724672"; } if (Config.sourceId != collectionSourceId) { return; } CacheDic[taskId] = collectionSourceId; HttpHelper http = new HttpHelper(); var html = http .GetHtml( $"http://goods.api.52cmg.cn/api/WechatMomentsGoods-WechatMoments/GetTaskData?taskId={taskId}") .Html; var result = JsonConvert.DeserializeObject(html); if (result == null) { throw new Exception($"获取代运营朋友圈任务数据异常"); } if (result.Code != 0) { throw new Exception($"获取代运营朋友圈任务数据异常,{result.Code}"); } if (result.Result == null) { return; } //是否商品 var isGoods = jObj["IsGoods"]?.Value() ?? false; //发送的内容 var content = result.Result.OriginalXml; //发送图片 var images = result.Result.MediaList.Select(f => f.Url).ToArray(); //评论数据 var commentList = result.Result.CommentList; //中间页地址 string ZJYUrl = string.Empty; ////获取所有当前进程微信 并只获取个人微信 //var robotConnectionList = RobotWebSocketServer.GetConnectionAll().Where(f => f.RobotType == (int)RobotType.微信).ToList(); //foreach (var robotConnection in robotConnectionList) //{ // FollowOfficial(robotConnection, content, commentList, images); //} //检测是否在线 var clients = Chat.Framework.ChatClient.WXClient.Values.ToList().Where(f => (f.WeixinType == WeixinType.Grpc微信 || f.WeixinType == WeixinType.Hook微信) && f.Status == WxStatus.在线) .ToList(); if (clients.Count == 0) { return; } Class1.Config.SignId = long.Parse(taskId); Util.Save(Class1.Config); //string convertLink = string.Empty; //转链后 //string compositeQRCode = string.Empty; //合成的二维码图片 //string quanUrl = string.Empty; //券链接 //string itemUrl = string.Empty; //商品连接 //var appendQrcode = false; //是否合成优惠券图片 //CpsType cps = CpsType.京东联盟; #region 获取正文内容并删除正文中的地址 if (string.IsNullOrWhiteSpace(content)) { EventClient.OnEvent(this, $"未检测到要转发的正文数据,操作终止"); return; } //处理用户朋友圈中的地址信息(有就删除) var reg = Regex.Match(content, @"(?<地址信息>\)"); if (reg.Success) { content = content.Replace(reg.Groups["地址信息"].Value, string.Empty); } #endregion //是否为商品 //为商品获取去商品连接和优惠券链接 if (isGoods) { if (Class1.Config.Monitor_Interval != 0) { if (Class1.Config.LastSendTime == DateTime.MinValue || (Class1.Config.LastSendTime.AddMinutes(Class1.Config.Monitor_Interval) <= DateTime.Now)) { } else return; } //appendQrcode = message.ContainsKey("appendQrcode") && message["appendQrcode"].ToString().ToLower() == "true"; //if (message.ContainsKey("itemUrl"))//获取宝贝连接 // itemUrl = message["itemUrl"].ToString(); //if (message.ContainsKey("quanUrl"))//获取优惠券链接 // quanUrl = message["quanUrl"].ToString(); } else { Class1.Config.LastSendTime = DateTime.MinValue; } //要发送评论 List comments = new List(); #region 评语不为空,判断评语中是否包含变量 /*if (isGoods && ((!string.IsNullOrWhiteSpace(comment) && comment.Contains(Resources.BuyAddress)) || !string.IsNullOrWhiteSpace(comment)))*/ if (isGoods && commentList != null && commentList.Count > 0) { bool isTB = false; try { foreach (var comment in commentList) { try { if (comment.ItemData != null) { if (comment.ItemData.LmType != LianMengType.淘宝联盟) { return; } var tkl = MonitorThread.AlimamaConvertLink(comment.ItemData, comment.ItemData.ActivityId, true, out var zjyUrl, out var buyPassNoFrills, out string compositeQRCode); //阿里妈妈 #region 朋友圈内容中追加图片 if (!string.IsNullOrWhiteSpace(compositeQRCode)) { StringBuilder sb = new StringBuilder(); sb.Append(""); sb.Append("" + Tools.GenerateRandomCode(20) + ""); sb.Append("2"); sb.Append(""); sb.Append(""); sb.Append("0"); sb.Append("[图片=" + compositeQRCode + "]"); sb.Append("[图片=" + compositeQRCode + "]"); sb.Append(""); sb.Append(""); var index = content.IndexOf(""); if (index != -1) { content = content.Insert(index, sb.ToString()); } } #endregion ////处理转链,替换变量 //if (isTB == true && !Class1.Config.isPyTkl && comment.Contains(Resources.BuyAddress)) //{ // convertLink = ZJYUrl; //} //comment = comment.Replace(Resources.BuyAddress, convertLink) // .Replace(Resources.TKLBuyAddress, address); var commentTmp = comment.Comment.Replace("[购买口令]", tkl) .Replace("[推广连接]", buyPassNoFrills) .Replace("[购买地址]", zjyUrl) .Replace("[购买中间页地址]", zjyUrl); comments.Add(commentTmp); } else { comments.Add(comment.Comment); } } catch (Exception ex) { return; } } isTB = true; } catch (Exception ex) { } } #endregion MonitorThread._ExecuteSend(content, comments, isGoods, taskId); } catch (Exception ex) { LogHelper.GetSingleObj().Error("代运营处理", ex.Message); } } } }