You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
336 lines
13 KiB
336 lines
13 KiB
9 months ago
|
using System;
|
||
|
using System.Text;
|
||
|
using System.IO;
|
||
|
using System.Net;
|
||
|
using System.Data;
|
||
|
|
||
|
namespace FangYar.FYMQTT
|
||
|
{
|
||
|
public class MessagePush
|
||
|
{
|
||
|
//string PushUrl = "http://192.168.31.194:5055/political/push/msg";
|
||
|
public static string PushUrl = System.Configuration.ConfigurationManager.AppSettings["PushUrl"].ToString();
|
||
|
public static string OpenIdPushUrl = System.Configuration.ConfigurationManager.AppSettings["OpenIdPushUrl"].ToString();
|
||
|
public static string OpenIdJumpUrl = System.Configuration.ConfigurationManager.AppSettings["OpenIdJumpUrl"].ToString();
|
||
|
|
||
|
/// <summary>
|
||
|
/// 单人获取手机cid
|
||
|
/// </summary>
|
||
|
/// <param name="uid">用户uid</param>
|
||
|
public string GetCid(string uid)
|
||
|
{
|
||
|
FangYar.OracleDAL.TBL.SysEmpDAL dal = new OracleDAL.TBL.SysEmpDAL();
|
||
|
DataTable dt = dal.getEmpModelByUId(uid);
|
||
|
if (dt != null && dt.Rows.Count != 0 && !string.IsNullOrEmpty(dt.Rows[0]["CID"].ToString()) && dt.Rows[0]["CID"].ToString() != "null")
|
||
|
{
|
||
|
return dt.Rows[0]["CID"].ToString();
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
return "";
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 多人获取手机cids
|
||
|
/// </summary>
|
||
|
/// <param name="uid">用户uid</param>
|
||
|
public string GetCids(string uids)
|
||
|
{
|
||
|
string uidsStr = "";
|
||
|
string[] uidsarry = uids.Split(',');
|
||
|
|
||
|
for (int i = 0; i < uidsarry.Length; i++)
|
||
|
{
|
||
|
if (i == 0)
|
||
|
{
|
||
|
uidsStr += "'" + uidsarry[i] + "'";
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
uidsStr += ",'" + uidsarry[i] + "'";
|
||
|
}
|
||
|
}
|
||
|
|
||
|
FangYar.OracleDAL.TBL.SysEmpDAL dal = new OracleDAL.TBL.SysEmpDAL();
|
||
|
DataTable dt = dal.GetCidsByUids(uidsStr);
|
||
|
|
||
|
string cid_s = "";
|
||
|
for (int i = 0; i < dt.Rows.Count; i++)
|
||
|
{
|
||
|
if (i == 0)
|
||
|
{
|
||
|
cid_s += dt.Rows[i]["CID"].ToString();
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
cid_s += "," + dt.Rows[i]["CID"].ToString();
|
||
|
}
|
||
|
}
|
||
|
return cid_s;
|
||
|
}
|
||
|
|
||
|
|
||
|
/// <summary>
|
||
|
/// 单人推送
|
||
|
/// </summary>
|
||
|
/// <param name="cid">用户CID</param>
|
||
|
/// <param name="title">标题</param>
|
||
|
/// <param name="content">内容</param>
|
||
|
public string AloneMsgPush(string uid, string title, string content)
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
string cid = GetCid(uid);
|
||
|
string param = "json={\"cid\":\"" + cid + "\",\"title\":\"" + title + "\",\"content\":\"" + content + "\"}&type=1";
|
||
|
return "";
|
||
|
//return InvokeWebApiPost(PushUrl, param);
|
||
|
}
|
||
|
catch (Exception e)
|
||
|
{
|
||
|
return "";
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 单人推送
|
||
|
/// </summary>
|
||
|
/// <param name="cid">用户CID</param>
|
||
|
/// <param name="title">标题</param>
|
||
|
/// <param name="content">内容</param>
|
||
|
/// <param name="payLoad">内容</param>
|
||
|
public string AloneMsgPush(string uid, string title, string content, string payLoad)
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
FangYar.OracleDAL.TBL.SysEmpDAL dal = new OracleDAL.TBL.SysEmpDAL();
|
||
|
DataTable dt = dal.getEmpModelByUId(uid);
|
||
|
string cid = "";
|
||
|
string openid = "";
|
||
|
string realName = "";
|
||
|
string mobile = "";
|
||
|
string res = "";
|
||
|
|
||
|
if (dt != null && dt.Rows.Count != 0)
|
||
|
{
|
||
|
if (!string.IsNullOrEmpty(dt.Rows[0]["CID"].ToString()) &&
|
||
|
dt.Rows[0]["CID"].ToString() != "null")
|
||
|
{
|
||
|
cid = dt.Rows[0]["CID"].ToString();
|
||
|
}
|
||
|
if (!string.IsNullOrEmpty(dt.Rows[0]["OPENID"].ToString()) &&
|
||
|
dt.Rows[0]["OPENID"].ToString() != "null")
|
||
|
{
|
||
|
openid = dt.Rows[0]["OPENID"].ToString();
|
||
|
}
|
||
|
if (!string.IsNullOrEmpty(dt.Rows[0]["EMP_NAME"].ToString()) &&
|
||
|
dt.Rows[0]["EMP_NAME"].ToString() != "null")
|
||
|
{
|
||
|
realName = dt.Rows[0]["EMP_NAME"].ToString();
|
||
|
}
|
||
|
if (!string.IsNullOrEmpty(dt.Rows[0]["EMP_MOBILE"].ToString()) &&
|
||
|
dt.Rows[0]["EMP_MOBILE"].ToString() != "null")
|
||
|
{
|
||
|
mobile = dt.Rows[0]["EMP_MOBILE"].ToString();
|
||
|
}
|
||
|
}
|
||
|
string param = "json={\"cid\":\"" + cid + "\",\"title\":\"" + title + "\",\"content\":\"" + content + "\",\"payload\":\"" + payLoad + "\"}&type=3";
|
||
|
try
|
||
|
{
|
||
|
if (!string.IsNullOrEmpty(cid))
|
||
|
{
|
||
|
res = InvokeWebApiPost(PushUrl, param);
|
||
|
}
|
||
|
}
|
||
|
catch(Exception ex){ }
|
||
|
try
|
||
|
{
|
||
|
if (!string.IsNullOrEmpty(openid) && !string.IsNullOrEmpty(title))
|
||
|
{
|
||
|
if (title.Contains("请假") ||
|
||
|
title.Contains("公差") ||
|
||
|
title.Contains("用车") ||
|
||
|
title.Contains("申请") ||
|
||
|
title.Contains("审批"))
|
||
|
{
|
||
|
string pushContent = "";
|
||
|
if (!string.IsNullOrEmpty(realName))
|
||
|
{
|
||
|
pushContent = realName + "待办,待处理";
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
pushContent = "您有一条待办,待处理";
|
||
|
}
|
||
|
string openParam = "msgData={\"thing1\":\"" + title + "\",\"thing2\":\"" + pushContent + "\",\"openid\":\"" + openid + "\",\"page\":\"" + OpenIdJumpUrl + "\",\"template_id\":\"6HZayiNCF72QQK0YH0nzd7jipxLHGLY_gdiHCfLpew0\"}";
|
||
|
//string openParam = "{\"uid\":\"" + uid + "\",\"realName\":\"" + realName + "\",\"mobile\":\"" + mobile + "\",\"openId\":\"" + openid + "\",\"fromTitle\":\"" + title + "\",\"pushUrlWx\":\"" + OpenIdJumpUrl + "\",\"pushContent\":\"" + pushContent + "\"}";
|
||
|
res = InvokeWebApiPost(OpenIdPushUrl, openParam);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
catch (Exception exx){ }
|
||
|
return "";
|
||
|
}
|
||
|
catch (Exception e)
|
||
|
{
|
||
|
return "";
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 订餐APP消息推送
|
||
|
/// </summary>
|
||
|
/// <param name="uid"></param>
|
||
|
/// <param name="title"></param>
|
||
|
/// <param name="content"></param>
|
||
|
/// <param name="payLoad"></param>
|
||
|
/// <returns></returns>
|
||
|
public string DiningAPPMsgPush(string uid, string title, string content, string payLoad)
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
string cid = GetCid(uid);
|
||
|
string param = "json={\"cid\":\"" + cid + "\",\"title\":\"" + title + "\",\"content\":\"" + content + "\",\"payload\":{\"url\":\"" + payLoad + "\"}}&type=3";
|
||
|
|
||
|
return InvokeWebApiPost(PushUrl, param);
|
||
|
}
|
||
|
catch (Exception e)
|
||
|
{
|
||
|
return "";
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
/// <summary>
|
||
|
/// 订餐微信消息推送
|
||
|
/// </summary>
|
||
|
/// <param name="uid"></param>
|
||
|
/// <param name="title"></param>
|
||
|
/// <param name="content"></param>
|
||
|
/// <param name="payLoad"></param>
|
||
|
/// <returns></returns>
|
||
|
public string DiningAPPMsgPush_WX(string uid, string title, string content, string payLoad)
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
string cid = GetCid(uid);
|
||
|
string param = "json={\"cid\":\"" + cid + "\",\"title\":\"" + title + "\",\"content\":\"" + content + "\",\"payload\":{\"url\":\"" + payLoad + "\"}}&type=3";
|
||
|
return InvokeWebApiPost(PushUrl, param);
|
||
|
}
|
||
|
catch (Exception e)
|
||
|
{
|
||
|
return "";
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
/// <summary>
|
||
|
/// 多人推送
|
||
|
/// </summary>
|
||
|
/// <param name="cids">用户CID组(,分割)</param>
|
||
|
/// <param name="content">内容</param>
|
||
|
public string MultiMsgPush(string uids, string content)
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
string cids = GetCids(uids);
|
||
|
|
||
|
string json = "[";
|
||
|
string[] cid_s = cids.Split(',');
|
||
|
for (var i = 0; i < cid_s.Length; i++)
|
||
|
{
|
||
|
if (i == 0)
|
||
|
{
|
||
|
json += "{\"cid\":\"" + cid_s[i] + "\",\"content\":\"" + content + "\"}";
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
json += ",{\"cid\":\"" + cid_s[i] + "\",\"content\":\"" + content + "\"}";
|
||
|
}
|
||
|
}
|
||
|
json += "]";
|
||
|
string param = "json=" + json + "&type=2";
|
||
|
return InvokeWebApiPost(PushUrl, param);
|
||
|
}
|
||
|
catch (Exception e)
|
||
|
{
|
||
|
return "";
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 调用指定的url以post方式调用,默认超时时间为3000毫秒。
|
||
|
/// </summary>
|
||
|
/// <param name="url">待调用链接。</param>
|
||
|
/// <param name="param">参数列表,格式如 api=value1&arg1=value2</param>
|
||
|
/// <param name="timeout">超时时间(单位毫秒),默认值为3000。</param>
|
||
|
/// <returns>执行结果。</returns>
|
||
|
public static string InvokeWebApiPost(string url, string param, int timeout = 3000)
|
||
|
{
|
||
|
// 将参数字节化
|
||
|
byte[] postdatabyte = Encoding.UTF8.GetBytes(param);
|
||
|
|
||
|
// 配置读写函数
|
||
|
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
|
||
|
request.Method = "POST";
|
||
|
request.Timeout = timeout;
|
||
|
request.ContentType = "application/x-www-form-urlencoded";
|
||
|
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; rv:19.0) Gecko/20100101 Firefox/19.0";
|
||
|
request.ContentLength = postdatabyte.Length;
|
||
|
request.AllowAutoRedirect = false;
|
||
|
request.KeepAlive = false;
|
||
|
|
||
|
// 调用函数
|
||
|
using (Stream stream = request.GetRequestStream())
|
||
|
stream.Write(postdatabyte, 0, postdatabyte.Length);
|
||
|
|
||
|
// 获取返回值。
|
||
|
string returned = null;
|
||
|
using (Stream responseStream = request.GetResponse().GetResponseStream())
|
||
|
{
|
||
|
StreamReader streamReader = new StreamReader(responseStream, Encoding.UTF8);
|
||
|
returned = streamReader.ReadToEnd();
|
||
|
}
|
||
|
return returned;
|
||
|
}
|
||
|
|
||
|
|
||
|
/// <summary>
|
||
|
/// 调用指定的url以post方式调用,默认超时时间为3000毫秒。
|
||
|
/// </summary>
|
||
|
/// <param name="url">待调用链接。</param>
|
||
|
/// <param name="param">参数列表,格式如 api=value1&arg1=value2</param>
|
||
|
/// <param name="timeout">超时时间(单位毫秒),默认值为3000。</param>
|
||
|
/// <returns>执行结果。</returns>
|
||
|
public static string InvokeWebApiPost2(string url, string param, int timeout = 3000)
|
||
|
{
|
||
|
// 将参数字节化
|
||
|
byte[] postdatabyte = Encoding.UTF8.GetBytes(param);
|
||
|
|
||
|
// 配置读写函数
|
||
|
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
|
||
|
request.Method = "POST";
|
||
|
request.Timeout = timeout;
|
||
|
request.ContentType = "application/json";
|
||
|
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; rv:19.0) Gecko/20100101 Firefox/19.0";
|
||
|
request.ContentLength = postdatabyte.Length;
|
||
|
request.AllowAutoRedirect = false;
|
||
|
request.KeepAlive = false;
|
||
|
|
||
|
// 调用函数
|
||
|
using (Stream stream = request.GetRequestStream())
|
||
|
stream.Write(postdatabyte, 0, postdatabyte.Length);
|
||
|
|
||
|
// 获取返回值。
|
||
|
string returned = null;
|
||
|
using (Stream responseStream = request.GetResponse().GetResponseStream())
|
||
|
{
|
||
|
StreamReader streamReader = new StreamReader(responseStream, Encoding.UTF8);
|
||
|
returned = streamReader.ReadToEnd();
|
||
|
}
|
||
|
return returned;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
}
|