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(); /// /// 单人获取手机cid /// /// 用户uid 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 ""; } } /// /// 多人获取手机cids /// /// 用户uid 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; } /// /// 单人推送 /// /// 用户CID /// 标题 /// 内容 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 ""; } } /// /// 单人推送 /// /// 用户CID /// 标题 /// 内容 /// 内容 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 ""; } } /// /// 订餐APP消息推送 /// /// /// /// /// /// 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 ""; } } /// /// 订餐微信消息推送 /// /// /// /// /// /// 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 ""; } } /// /// 多人推送 /// /// 用户CID组(,分割) /// 内容 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 ""; } } /// /// 调用指定的url以post方式调用,默认超时时间为3000毫秒。 /// /// 待调用链接。 /// 参数列表,格式如 api=value1&arg1=value2 /// 超时时间(单位毫秒),默认值为3000。 /// 执行结果。 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; } /// /// 调用指定的url以post方式调用,默认超时时间为3000毫秒。 /// /// 待调用链接。 /// 参数列表,格式如 api=value1&arg1=value2 /// 超时时间(单位毫秒),默认值为3000。 /// 执行结果。 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; } } }