Browse Source

打开推送队列

lcl 1 year ago
parent
commit
14b7511ae0
3 changed files with 82 additions and 7 deletions
  1. 1 1
      AppStart/StartHelper.cs
  2. 66 0
      Util/LogHelper.cs
  3. 15 6
      Util/Queue/PushHelper.cs

+ 1 - 1
AppStart/StartHelper.cs

@@ -13,7 +13,7 @@
             if(Environment == 2)
             {
                 SourceHelper.Instance.Start(); //获取原始数据队列
-                // PushHelper.Instance.Start(); //推送队列
+                PushHelper.Instance.Start(); //推送队列
                 // RePushHelper.Instance.Start(); //重新推送队列
 
                 // DefaultHelper.Instance.Start(); //队列程序

+ 66 - 0
Util/LogHelper.cs

@@ -0,0 +1,66 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Threading;
+using Library;
+using LitJson;
+using MySystem;
+
+public class LogHelper
+{
+    public readonly static LogHelper Instance = new LogHelper();
+    private LogHelper()
+    { }
+
+    public void WriteLog(string Content, string FileName, string BrandId = "0")
+    {
+        function.WriteLog(Content, FileName);
+        // Dictionary<string, string> dic = new Dictionary<string, string>();
+        // dic.Add("Topic", FileName);
+        // dic.Add("Content", Content);
+        // dic.Add("BrandId", BrandId);
+        // RedisDbconn.Instance.AddList("LogQueue", Newtonsoft.Json.JsonConvert.SerializeObject(dic));
+    }
+
+    public void Start()
+    {
+        Thread th = new Thread(DoWorks);
+        th.IsBackground = true;
+        th.Start();
+    }
+
+    public void DoWorks()
+    {
+        while (true)
+        {
+            string content = RedisDbconn.Instance.RPop<string>("LogQueue");
+            if (!string.IsNullOrEmpty(content))
+            {
+                try
+                {
+                    DoQueue(content);
+                }
+                catch (Exception ex)
+                {
+                    LogHelper.Instance.WriteLog(DateTime.Now.ToString() + "\n" + content + "\n" + ex, "SLS日志异常");
+                }
+            }
+            else
+            {
+                Thread.Sleep(5000);
+            }
+        }
+    }
+
+    public void DoQueue(string content)
+    { 
+        JsonData JsonObj = JsonMapper.ToObject(content);
+        string Topic = JsonObj["Topic"].ToString();
+        string Cont = JsonObj["Content"].ToString();
+        string BrandId = JsonObj["BrandId"].ToString();
+        // SLS.WriteLog(DateTime.Now, Topic, Cont, new Dictionary<string, string>()
+        // {
+        //     {"BrandId", BrandId}
+        // });
+    }
+}

+ 15 - 6
Util/Queue/PushHelper.cs

@@ -59,7 +59,7 @@ namespace MySystem
 
                     string QueryField = "";
                     List<string> PushField = new List<string>();
-                    Dictionary<string, string> Trans = new Dictionary<string, string>();;
+                    Dictionary<string, string> Trans = new Dictionary<string, string>();
                     JsonData fieldJson = JsonMapper.ToObject(FieldList);
                     for (int i = 0; i < fieldJson.Count; i++)
                     {
@@ -158,6 +158,10 @@ namespace MySystem
                                             val = num.ToString("f0");
                                         }
                                     }
+                                    if(dc.DataType == typeof(DateTime))
+                                    {
+                                        val = DateTime.Parse(val).ToString("yyyy-MM-dd HH:mm:ss");
+                                    }
                                     obj.Add(dc.ColumnName, val);
                                 }
                             }
@@ -168,7 +172,9 @@ namespace MySystem
                         if(EncryptMode == 1)
                         {
                             PushData = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
+                            LogHelper.Instance.WriteLog("原始数据:" + PushData, "推送数据日志");
                             string content = EncryptHelper.Encrypt1(obj, AesSecret);
+                            LogHelper.Instance.WriteLog("加密数据:" + content, "推送数据日志");
                             obj = new SortedList<string, string>();
                             obj.Add("type", Title);
                             obj.Add("notice_id", Guid.NewGuid().ToString());
@@ -176,11 +182,14 @@ namespace MySystem
                             obj.Add("content", content);
                             string requestJson = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
                             PushDataEncrypt = requestJson;
-                            // string result = function.PostWebRequest(NoticeUrl, requestJson, "application/json");
-                            // if(result.Contains("\"code\":\"200\""))
-                            // {
-                            //     Status = 1;
-                            // }
+                            LogHelper.Instance.WriteLog("请求参数:" + PushDataEncrypt, "推送数据日志");
+                            LogHelper.Instance.WriteLog("请求地址:" + NoticeUrl, "推送数据日志");
+                            string result = function.PostWebRequest(NoticeUrl, requestJson, "application/json");
+                            LogHelper.Instance.WriteLog("返回报文:" + result + "\n\n", "推送数据日志");
+                            if(result.Replace(" ", "").Contains("\"code\":\"200\""))
+                            {
+                                Status = 1;
+                            }
                         }
                         PushRecord rec = db.PushRecord.Add(new PushRecord()
                         {