diff --git a/smsmanager/Controllers/HomeController.cs b/smsmanager/Controllers/HomeController.cs index db69d27..4a81c38 100644 --- a/smsmanager/Controllers/HomeController.cs +++ b/smsmanager/Controllers/HomeController.cs @@ -494,6 +494,49 @@ public IActionResult EmailfowardStatusChange(string kg, string smtp,string smtpp return View("Emailfoward"); } + public IActionResult Wechatfoward() + { + string orgCodePath = AppDomain.CurrentDomain.BaseDirectory + "loginpassw.xml"; + XmlDocument xmldoc = new XmlDocument(); + xmldoc.Load(orgCodePath); + XmlNodeList topM = xmldoc.SelectNodes("//userSettings"); + foreach (XmlElement element in topM) + { + ViewBag.status = element.GetElementsByTagName("WeChatQYFowardStatus")[0].InnerText == "0" ? "" : "checked=\"\""; + ViewBag.qyid = element.GetElementsByTagName("WeChatQYID")[0].InnerText; + ViewBag.apid = element.GetElementsByTagName("WeChatQYApplicationID")[0].InnerText; + ViewBag.ApplicationSecret = element.GetElementsByTagName("WeChatQYApplicationSecret")[0].InnerText; + } + return View(); + } + [HttpPost] + public IActionResult WechatfowardStatusChange(string kg, string qyid, string apid,string ApplicationSecret) + { + string orgCodePath = AppDomain.CurrentDomain.BaseDirectory + "loginpassw.xml"; + XmlDocument MyXml = new XmlDocument(); + MyXml.Load(orgCodePath); + //获取节点的所有子节点 + XmlNodeList topM = MyXml.SelectNodes("//userSettings"); + //遍历下的所有子节点 + foreach (XmlElement element in topM) + { + element.GetElementsByTagName("WeChatQYFowardStatus")[0].InnerText = kg == "false" ? "0" : "1"; + element.GetElementsByTagName("WeChatQYID")[0].InnerText = qyid; + element.GetElementsByTagName("WeChatQYApplicationID")[0].InnerText = apid; + element.GetElementsByTagName("WeChatQYApplicationSecret")[0].InnerText = ApplicationSecret; + } + MyXml.Save(orgCodePath); + + foreach (XmlElement element in topM) + { + ViewBag.status = element.GetElementsByTagName("WeChatQYFowardStatus")[0].InnerText == "0" ? "" : "checked=\"\""; + ViewBag.qyid = element.GetElementsByTagName("WeChatQYID")[0].InnerText; + ViewBag.apid = element.GetElementsByTagName("WeChatQYApplicationID")[0].InnerText; + ViewBag.ApplicationSecret = element.GetElementsByTagName("WeChatQYApplicationSecret")[0].InnerText; + } + return View("Wechatfoward"); + } + public IActionResult EditPwd() { if (HttpContext.Session.GetString("uname") == null) diff --git a/smsmanager/HttpHelper.cs b/smsmanager/HttpHelper.cs new file mode 100644 index 0000000..03cc389 --- /dev/null +++ b/smsmanager/HttpHelper.cs @@ -0,0 +1,49 @@ +using Newtonsoft.Json.Linq; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Net; +using System.Text; +using System.Threading.Tasks; + +namespace smsmanager +{ + public class HttpHelper + { + + public static string HttpGet(string Url) + { + ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true; + HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url); + request.Method = "GET"; + request.ContentType = "text/html;charset=UTF-8"; + HttpWebResponse response = (HttpWebResponse)request.GetResponse(); + Stream myResponseStream = response.GetResponseStream(); + StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8); + string retString = myStreamReader.ReadToEnd(); + myStreamReader.Close(); + myResponseStream.Close(); + return retString; + } + + public static string Post(string url, JObject obj) + { + ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true; + HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); + request.Method = "POST"; + request.ContentType = "application/json;charset=UTF-8"; + var streamWriter = new StreamWriter(request.GetRequestStream()); + streamWriter.Write(obj); + streamWriter.Flush(); + streamWriter.Close(); + HttpWebResponse response = (HttpWebResponse)request.GetResponse(); + Stream myResponseStream = response.GetResponseStream(); + StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8); + string retString = myStreamReader.ReadToEnd(); + myStreamReader.Close(); + myResponseStream.Close(); + return retString; + } + } +} diff --git a/smsmanager/Program.cs b/smsmanager/Program.cs index fad6afe..17cfeb2 100644 --- a/smsmanager/Program.cs +++ b/smsmanager/Program.cs @@ -2,6 +2,7 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; +using Newtonsoft.Json.Linq; using System; using System.Collections; using System.Collections.Generic; @@ -45,6 +46,10 @@ public static void isXmlExist() CreateNode(xmlDoc, root, "emailKey", ""); CreateNode(xmlDoc, root, "sendEmial", ""); CreateNode(xmlDoc, root, "reciveEmial", ""); + CreateNode(xmlDoc, root, "WeChatQYFowardStatus", ""); + CreateNode(xmlDoc, root, "WeChatQYID", ""); + CreateNode(xmlDoc, root, "WeChatQYApplicationID", ""); + CreateNode(xmlDoc, root, "WeChatQYApplicationSecret", ""); try { xmlDoc.Save(orgCodePath); @@ -66,7 +71,9 @@ public static void CreateNode(XmlDocument xmlDoc, XmlNode parentNode, string nam public static void emailForward() { Hashtable ht = new Hashtable(); + Hashtable htWc = new Hashtable(); string orgCodePath = AppDomain.CurrentDomain.BaseDirectory + "loginpassw.xml"; + //Console.WriteLine(File.Exists(orgCodePath)); while (File.Exists(orgCodePath)) { XmlDocument xmldoc = new XmlDocument(); @@ -83,6 +90,8 @@ public static void emailForward() foreach (XmlElement element in topM) { string status = element.GetElementsByTagName("emailFowardStatus")[0].InnerText; + string qystatus = element.GetElementsByTagName("WeChatQYFowardStatus")[0].InnerText; + //Console.WriteLine(qystatus); if (status=="1") { Thread.Sleep(1000); @@ -139,6 +148,94 @@ public static void emailForward() } } } + if (qystatus == "1") + { + Thread.Sleep(1000); + var psi = new System.Diagnostics.ProcessStartInfo("mmcli", " -m 0 --messaging-list-sms"); + psi.RedirectStandardOutput = true; + using (var process = System.Diagnostics.Process.Start(psi)) + { + var output = process.StandardOutput.ReadToEnd(); + if (output != string.Empty && output.Trim() != "No sms messages were found") + { + //int count = 0; + string[] qline = output.Split(Environment.NewLine.ToCharArray()); + for (int i = 0; i < qline.Count() - 1; i++) + { + string[] theRow = qline[i].Split("("); + if (theRow[1].Trim() == "received)") + { + if (!htWc.Contains(theRow[0].Trim().Split("SMS/")[1].ToString().Trim())) + { + string sid = theRow[0].Trim().Split("SMS/")[1].ToString().Trim(); + var psi2 = new System.Diagnostics.ProcessStartInfo("mmcli", " -m 0 -s " + sid); + psi2.RedirectStandardOutput = true; + using (var process2 = System.Diagnostics.Process.Start(psi2)) + { + var output2 = process2.StandardOutput.ReadToEnd(); + if (output2 != string.Empty) + { + try + { + string[] qline2 = output2.Split(Environment.NewLine.ToCharArray()); + string tel = qline2[3].Split("|")[1].Trim().Split(":")[1].Trim(); + string text = qline2[4].Split("|")[1].Trim().Split(":")[1].Trim(); + string corpid = element.GetElementsByTagName("WeChatQYID")[0].InnerText; + string corpsecret = element.GetElementsByTagName("WeChatQYApplicationSecret")[0].InnerText; + string agentid = element.GetElementsByTagName("WeChatQYApplicationID")[0].InnerText; + string url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=" + corpid + "&corpsecret=" + corpsecret; + string result = HttpHelper.HttpGet(url); + JObject jsonObj = JObject.Parse(result); + string errcode = jsonObj["errcode"].ToString(); + string errmsg = jsonObj["errmsg"].ToString(); + if (errcode == "0" && errmsg == "ok") + { + string access_token = jsonObj["access_token"].ToString(); + JObject obj = new JObject(); + JObject obj1 = new JObject(); + obj.Add("touser", "@all"); + obj.Add("toparty", ""); + obj.Add("totag", ""); + obj.Add("msgtype", "text"); + obj.Add("agentid", agentid); + obj1.Add("content", "����ת��_" + tel + "\n" + text); + obj.Add("text", obj1); + obj.Add("safe", 0); + obj.Add("enable_id_trans", 0); + obj.Add("enable_duplicate_check", 0); + obj.Add("duplicate_check_interval", 1800); + string msgurl = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=" + access_token; + string msgresult = HttpHelper.Post(msgurl, obj); + JObject jsonObjresult = JObject.Parse(msgresult); + string errcode1 = jsonObjresult["errcode"].ToString(); + string errmsg1 = jsonObjresult["errmsg"].ToString(); + if (errcode == "0" && errmsg == "ok") + { + htWc.Add(sid, tel + "_" + text); + Console.WriteLine("��ҵ΢��ת���ɹ�"); + } + else + { + Console.WriteLine(errmsg); + } + } + else + { + Console.WriteLine(errmsg); + } + } + catch (Exception ex) + { + Console.WriteLine(ex); + } + } + } + } + } + } + } + } + } } } } diff --git a/smsmanager/Views/Home/Wechatfoward.cshtml b/smsmanager/Views/Home/Wechatfoward.cshtml new file mode 100644 index 0000000..60e1020 --- /dev/null +++ b/smsmanager/Views/Home/Wechatfoward.cshtml @@ -0,0 +1,82 @@ + +@{ + Layout = null; +} + + + + + + + 企业微信转发信息 + + + + + + + + +
+
+
+
+ +
+ +
+ +
+
+
+ +
+ +
+
+
+ +
+ +
+
+
+ +
+ +
+
+
+
+ +
+
+
+
+
+
+ + + + + diff --git a/smsmanager/wwwroot/layuimini/api/init.json b/smsmanager/wwwroot/layuimini/api/init.json index 3f530e5..3aafeea 100644 --- a/smsmanager/wwwroot/layuimini/api/init.json +++ b/smsmanager/wwwroot/layuimini/api/init.json @@ -44,6 +44,12 @@ "href": "Home/Emailfoward", "icon": "fa fa-file-text", "target": "_self" + }, + { + "title": "企业微信转发", + "href": "Home/Wechatfoward", + "icon": "fa fa-file-text", + "target": "_self" } ] }