using BizCom; using System; using System.Collections.Generic; using System.Text; using System.Web.Script.Serialization; namespace SiteCore { public partial class WxPush { //public static string public static string appid = "wx20432512482565e9"; public static string appsecret = "07492bc9cbbc5b303e5e135b4dccd6f0"; public static string hy_appid = "wx0669fa53c6f74698"; public static string hy_appsecret = "678776d4eec59ce289eb88ff36f3b4ef"; static string tokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}"; static string sendUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="; static object atObj = new Object(); private static object getAt(HttpHelper http) { object cat = WebCache.GetRunCache("hyAt"); if (cat == null) { lock (atObj) { //http = new HttpHelper(); HttpItem item = new HttpItem() { URL = string.Format(tokenUrl, hy_appid, hy_appsecret) }; HttpResult hResult = http.GetHtml(item); string html = hResult.Html; XLog.SaveLog(0, "getAt:" + html); JavaScriptSerializer jss = new JavaScriptSerializer(); Dictionary jObj = jss.Deserialize>(html); if (!jObj.ContainsKey("errcode")) { string at = jObj["access_token"].ToString(); WebCache.AddRunCacheTime("hyAt", at, 100); cat = at; } } } return cat; } public static void sendMonitorMsg(string title, string openid) { HttpHelper http = new HttpHelper(); HttpResult hResult = null; object cat = getAt(http); if (cat != null) { var tdata = new { first = new TemplateItem(title, "#333333"), keyword1 = new TemplateItem(title, "#333333"), keyword2 = new TemplateItem(DateTime.Now.ToString("yyyy-MM-dd HH:mm"), "#0E7700"), remark = new TemplateItem("请注意查看", "#333333") }; //var minidata = new //{ // appid = "wx20432512482565e9", // pagepath = "pages/lab/lab?remind=1" //}; var msgData = new TemplateData() { touser = openid, template_id = "Tsp5iFvM_L1Ok10nta3TvQzjnrZvas7PKIWbdX1t08M", url = "", data = tdata }; // miniprogram= minidata, string msg = Newtonsoft.Json.JsonConvert.SerializeObject(msgData); HttpItem item = new HttpItem() { URL = sendUrl + cat, Method = "POST", ContentType = "application/json", Postdata = msg, PostEncoding = Encoding.UTF8, Encoding = Encoding.UTF8 }; //item.Encoding = Encoding.UTF8; hResult = http.GetHtml(item); string html = hResult.Html; XLog.SaveLog(0, html); } } public static void sendSignDispatchMsg(string openid, string key1, string key2, string memo) { HttpHelper http = new HttpHelper(); HttpResult hResult = null; object cat = getAt(http); if (cat != null) { var tdata = new { first = new TemplateItem("您的电子小票已签收", "#333333"), keyword1 = new TemplateItem(key1, "#333333"), keyword2 = new TemplateItem(key2, "#0E7700"), keyword3 = new TemplateItem(DateTime.Now.ToString("yyyy-MM-dd HH:mm"), "#0E7700"), remark = new TemplateItem(memo, "#333333") }; var minidata = new { appid = mini_appid, pagepath = "pages/index" }; var msgData = new TemplateData() { touser = openid, template_id = "uDUEkjJt4-j2m9PhIB6bTXeLZrAvvNdi7a2xMBXzH1I", url = "", miniprogram = minidata, data = tdata }; string msg = Newtonsoft.Json.JsonConvert.SerializeObject(msgData); HttpItem item = new HttpItem() { URL = sendUrl + cat, Method = "POST", ContentType = "application/json", Postdata = msg, PostEncoding = Encoding.UTF8, Encoding = Encoding.UTF8 }; //item.Encoding = Encoding.UTF8; hResult = http.GetHtml(item); string html = hResult.Html; } } #region xiu public static void sendXiuPersonMsg(string openids, string cust, string con, string notify) { if (openids == "") return; string[] oArr = openids.Split(','); for (int i = 0; i < oArr.Length; i++) { sendXiuPersonMsg(oArr[i], "有新的维修单待受理", cust, con, notify, "请注意查看"); } } public static void sendXiuPersonMsg(string openid, string first, string cust, string con, string notify, string remark) { HttpHelper http = new HttpHelper(); HttpResult hResult = null; object cat = getAt(http); if (cat != null) { var tdata = new { first = new TemplateItem(first, "#333333"), keyword1 = new TemplateItem(cust, "#333333"), keyword2 = new TemplateItem(con, "#0E7700"), keyword3 = new TemplateItem(DateTime.Now.ToString("yyyy-MM-dd HH:mm"), "#0E7700"), keyword4 = new TemplateItem(notify, "#0E7700"), remark = new TemplateItem(remark, "#333333") }; //var minidata = new //{ // appid = "wx20432512482565e9", // pagepath = "pages/lab/lab?remind=1" //}; var msgData = new TemplateData() { touser = openid, template_id = "0sbCmaWrhC-5v_jL7z0-BR7PtrKJ75E74E2WJuu4Wk8", url = "", data = tdata }; // miniprogram= minidata, string msg = Newtonsoft.Json.JsonConvert.SerializeObject(msgData); HttpItem item = new HttpItem() { URL = sendUrl + cat, Method = "POST", ContentType = "application/json", Postdata = msg, PostEncoding = Encoding.UTF8, Encoding = Encoding.UTF8 }; //item.Encoding = Encoding.UTF8; hResult = http.GetHtml(item); string html = hResult.Html; XLog.SaveLog(0, html); } } public static void sendXiuCorpMsg(string openids, string type, string state, string log) { if (openids == "") return; string[] oArr = openids.Split(','); for (int i = 0; i < oArr.Length; i++) { sendXiuCorpMsg(oArr[i], "您的售后服务有新的进展", type, state, log, "请注意查看"); } } public static void sendXiuCorpMsg(string openid, string first, string type, string state, string log, string remark) { HttpHelper http = new HttpHelper(); HttpResult hResult = null; object cat = getAt(http); if (cat != null) { var tdata = new { first = new TemplateItem(first, "#333333"), HandleType = new TemplateItem(type, "#333333"), Status = new TemplateItem(state, "#0E7700"), RowCreateDate = new TemplateItem(DateTime.Now.ToString("yyyy-MM-dd HH:mm"), "#0E7700"), LogType = new TemplateItem(log, "#0E7700"), remark = new TemplateItem(remark, "#333333") }; //var minidata = new //{ // appid = "wx20432512482565e9", // pagepath = "pages/lab/lab?remind=1" //}; var msgData = new TemplateData() { touser = openid, template_id = "_OsQ7mmYeEjq3GsZDZVVnZDh5iIw_jbp7nUlkTTfwsk", url = "", data = tdata }; // miniprogram= minidata, string msg = Newtonsoft.Json.JsonConvert.SerializeObject(msgData); HttpItem item = new HttpItem() { URL = sendUrl + cat, Method = "POST", ContentType = "application/json", Postdata = msg, PostEncoding = Encoding.UTF8, Encoding = Encoding.UTF8 }; //item.Encoding = Encoding.UTF8; hResult = http.GetHtml(item); string html = hResult.Html; XLog.SaveLog(0, html); } } #endregion } } public class TemplateData { public string touser { get; set; } public string template_id { get; set; } public string topcolor { get; set; } public string url { get; set; } public object data { get; set; } public object miniprogram { get; set; } } public class TemplateItem { public string value { get; set; } public string color { get; set; } public TemplateItem(string _value) { value = _value; } public TemplateItem(string _value, string _color) { value = _value; color = _color; } }