From 73abe5603748145f7b5f20ba0f0d4add8a06325b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=C2=B7=C3=A6?= <279202647@qq.com> Date: Fri, 3 Sep 2021 11:02:26 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=E5=85=A5=E9=92=89=E9=92=89=E9=80=9A?= =?UTF-8?q?=E7=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../JdShopListener/DingApiHelper.cs | 149 ++++++++++++++++++ JdShopListener/JdShopListener/MainWindow.xaml | 28 +++- .../JdShopListener/MainWindowViewModel.cs | 73 ++++++++- 3 files changed, 247 insertions(+), 3 deletions(-) create mode 100644 JdShopListener/JdShopListener/DingApiHelper.cs diff --git a/JdShopListener/JdShopListener/DingApiHelper.cs b/JdShopListener/JdShopListener/DingApiHelper.cs new file mode 100644 index 0000000..12ed5f2 --- /dev/null +++ b/JdShopListener/JdShopListener/DingApiHelper.cs @@ -0,0 +1,149 @@ +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Net.Http; +using System.Security.Cryptography; +using System.Text; + +namespace JdShopListener +{ + public class DingApiHelper + { + /// + /// 钉钉Secret + /// + private string dDSecret = "SEC23f4c5aa97b598b5a81b29ecb4b4facc5863e15e6b3c38c02f406c6541fdbb8e"; + + /// + /// 钉钉的webHookUrl + /// + private string webHookUrl = "https://oapi.dingtalk.com/robot/send?access_token=ce194adc85ffc4438b894c20c895ba3e3eb2bfeea1cde4d0e9b4bd1122f905dc"; + + /// + /// 初始化 + /// + /// 钉钉Secret,机器人设置加签处获取 + /// 机器人设置完成的Webhook地址 + public DingApiHelper(string secret, string webHook) + { + dDSecret = secret; + webHookUrl = webHook; + } + + /// + /// 发送钉钉通知 + /// + /// 内容 + public bool SendNotify(string msg) + { + try + { + string timestamp = ((DateTime.Now.Ticks - TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)).Ticks) / 10000).ToString(); + + string stringToSign = $"{timestamp}\n{dDSecret}"; + + string hmac_code = GetHash(stringToSign, dDSecret); + + string sign = System.Web.HttpUtility.UrlEncode(hmac_code, Encoding.UTF8); + + //消息类型 + var msgtype = MsgTypeEnum.text.ToString(); + + //文本内容 + var text = new Text + { + Content = msg + }; + + //指定目标人群 + var at = new At() + { + AtMobiles = new List() { }, + IsAtAll = false + }; + + + string url = $"{webHookUrl}&sign={sign}×tamp={timestamp}"; + using (HttpClient httpClient = new HttpClient()) + { + HttpContent content = new StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(new + { + msgtype, + text, + at + })); + content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); + HttpResponseMessage response = httpClient.PostAsync(url, content).Result; + string html = response.Content.ReadAsStringAsync().Result; + } + return true; + + } + catch + { + return false; + } + } + + + public String GetHash(String text, String key) + { + // change according to your needs, an UTF8Encoding + // could be more suitable in certain situations + ASCIIEncoding encoding = new ASCIIEncoding(); + + Byte[] textBytes = encoding.GetBytes(text); + Byte[] keyBytes = encoding.GetBytes(key); + + Byte[] hashBytes; + + using (HMACSHA256 hash = new HMACSHA256(keyBytes)) + hashBytes = hash.ComputeHash(textBytes); + + return Convert.ToBase64String(hashBytes); + } + + } + + /// + /// 钉钉群机器人消息类型枚举 + /// + public enum MsgTypeEnum + { + text, + link, + markdown, + actionCard, + feedCard + } + + /// + /// 文本类型 + /// + public class Text + { + /// + /// 文本内容 + /// + [JsonProperty(PropertyName = "content")] + public string Content { get; set; } + } + + /// + /// @指定人 + /// + public class At + { + /// + /// @的联系人 + /// + [JsonProperty(PropertyName = "atMobiles")] + public List AtMobiles { set; get; } + + /// + /// 是否@所有人 + /// + [JsonProperty(PropertyName = "isAtAll")] + public bool IsAtAll { set; get; } + } +} diff --git a/JdShopListener/JdShopListener/MainWindow.xaml b/JdShopListener/JdShopListener/MainWindow.xaml index daf6392..62d9fdc 100644 --- a/JdShopListener/JdShopListener/MainWindow.xaml +++ b/JdShopListener/JdShopListener/MainWindow.xaml @@ -300,8 +300,32 @@ - - + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/JdShopListener/JdShopListener/MainWindowViewModel.cs b/JdShopListener/JdShopListener/MainWindowViewModel.cs index c3a5b04..ed71663 100644 --- a/JdShopListener/JdShopListener/MainWindowViewModel.cs +++ b/JdShopListener/JdShopListener/MainWindowViewModel.cs @@ -19,6 +19,8 @@ namespace JdShopListener { private static MainWindowViewModel _mainWindowViewModel = new MainWindowViewModel(); public MainWindow Window; + + string ddFileName = $"{System.Environment.CurrentDirectory}\\ddset.ini"; public static MainWindowViewModel MainViewModel { get @@ -27,6 +29,8 @@ namespace JdShopListener } } + DingApiHelper dingApi=null; + public string JDCookie { get; set; } public MainWindowViewModel() { @@ -48,11 +52,28 @@ namespace JdShopListener Btn_ShowList = new RelayCommand(ShowList); Btn_Start = new RelayCommand(Start); Btn_ShowData = new RelayCommand(ShowData); + Btn_SaveDD = new RelayCommand(SaveDD); ItemList = new System.Collections.ObjectModel.ObservableCollection(); SkuList = new System.Collections.ObjectModel.ObservableCollection(); SelectDate = DateList.FirstOrDefault(); SelectPro = ProList.FirstOrDefault(); Init(); + + if (System.IO.File.Exists(ddFileName)) + { + string json = System.IO.File.ReadAllText(ddFileName); + + var data = Newtonsoft.Json.JsonConvert.DeserializeObject(json); + if (data != null) + { + DDSecret = data.Secret; + Webhook = data.Webhook; + IsUseDingDing = data.IsUse; + } + + dingApi = new DingApiHelper(DDSecret, Webhook); + } + } /// @@ -163,6 +184,31 @@ namespace JdShopListener set { Set(ref _Desc, value); } } + private string _DDSecret; + + public string DDSecret + { + get { return _DDSecret; } + set { Set(ref _DDSecret, value); } + } + + private string _Webhook; + + public string Webhook + { + get { return _Webhook; } + set { Set(ref _Webhook, value); } + } + + private bool _IsUseDingDing; + + public bool IsUseDingDing + { + get { return _IsUseDingDing; } + set { Set(ref _IsUseDingDing, value); } + } + + /// /// 所有sku最后变化记录 /// @@ -183,6 +229,11 @@ namespace JdShopListener /// public RelayCommand Btn_Start { get; set; } + /// + /// 保存钉钉配置 + /// + public RelayCommand Btn_SaveDD { get; set; } + /// /// 刷新数据 /// @@ -636,6 +687,21 @@ namespace JdShopListener } } + + /// + /// 保存钉钉配置 + /// + public void SaveDD() + { + + System.IO.File.WriteAllText(ddFileName, Newtonsoft.Json.JsonConvert.SerializeObject(new { Secret = this.DDSecret, Webhook = this.Webhook,IsUse=this.IsUseDingDing })); + + dingApi = new DingApiHelper(DDSecret, Webhook); + + MessageBox.Show("保存成功!", "提示"); + + } + /// /// 根据Sku获取详情 /// @@ -844,10 +910,15 @@ namespace JdShopListener DbHelper.Db.AddItemChangeModel(item); + if (IsUseDingDing) + { + dingApi.SendNotify($"{sku.SkuId}监控完成!已产生变化记录"); + } + AddLog($"{sku.SkuId}监控完成!已产生变化记录"); } else { - AddLog($"{sku.SkuId}监控完成!未发生变化"); + AddLog($"{sku.SkuId}监控完成!未发生变化"); } Thread.Sleep(3000); }