Skip to content

Commit

Permalink
新增短信转发至企业微信自建应用
Browse files Browse the repository at this point in the history
  • Loading branch information
lkiuyu committed Apr 10, 2022
1 parent 2619061 commit e307e39
Show file tree
Hide file tree
Showing 5 changed files with 277 additions and 0 deletions.
43 changes: 43 additions & 0 deletions smsmanager/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
//获取<Rule>节点的所有子节点
XmlNodeList topM = MyXml.SelectNodes("//userSettings");
//遍历<Rule>下的所有子节点
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)
Expand Down
49 changes: 49 additions & 0 deletions smsmanager/HttpHelper.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
97 changes: 97 additions & 0 deletions smsmanager/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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();
Expand All @@ -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);
Expand Down Expand Up @@ -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);
}
}
}
}
}
}
}
}
}
}
}
}
Expand Down
82 changes: 82 additions & 0 deletions smsmanager/Views/Home/Wechatfoward.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@

@{
Layout = null;
}

<!DOCTYPE html>

<html>
<head>
<meta charset="utf-8">
<title>企业微信转发信息</title>
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<link rel="stylesheet" href="~/layuimini/lib/layui-v2.5.5/css/layui.css" media="all">
<link rel="stylesheet" href="~/layuimini/css/public.css" media="all">
<style>
.layui-form-item .layui-input-company {
width: auto;
padding-right: 10px;
line-height: 38px;
}
</style>
</head>
<body>
<div class="layuimini-container">
<form class="layui-form" action="WechatfowardStatusChange" method="post">
<div class="layuimini-main">
<div class="layui-form layuimini-form">
<div class="layui-form-item" style="display:none">
<label class="layui-form-label required">kg</label>
<div class="layui-input-block">
<input type="text" name="kg" id="kg" value="" lay-verify="required" class="layui-input">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label required">企业微信转发开关:</label>
<div class="layui-input-block">
<input type="checkbox" @ViewBag.status name="open" lay-skin="switch" lay-filter="switchTest" lay-text="ON|OFF">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label required">企业ID:</label>
<div class="layui-input-block">
<input type="text" name="qyid" value="@ViewBag.qyid" lay-verify="required" class="layui-input" lay-reqtext="企业ID不能为空" placeholder="请输入企业ID">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label required">自建应用ID:</label>
<div class="layui-input-block">
<input type="text" name="apid" value="@ViewBag.apid" lay-verify="required" class="layui-input" lay-reqtext="自建应用ID不能为空" placeholder="请输入自建应用ID">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label required">自建应用密钥:</label>
<div class="layui-input-block">
<input type="text" name="ApplicationSecret" value="@ViewBag.ApplicationSecret" lay-verify="required|number" class="layui-input" lay-reqtext="自建应用密钥不能为空" placeholder="请输入自建应用密钥">
</div>
</div>
<div class="layui-form-item">
<div class="layui-input-block">
<input type="submit" class="layui-btn layui-btn-normal" value="提交" name="sub" />
</div>
</div>
</div>
</div>
</form>
</div>
<script src="~/layuimini/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="~/layuimini/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script>
//监听指定开关
layui.use(['form'], function () {
var form = layui.form
form.on('switch(switchTest)', function (data) {
document.getElementById("kg").value = this.checked ? 'true' : 'false';
});
});
</script>
</body>
</html>
6 changes: 6 additions & 0 deletions smsmanager/wwwroot/layuimini/api/init.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
]
}
Expand Down

0 comments on commit e307e39

Please sign in to comment.