1.先新建一个类库,以方便以后移植到其他的项目上继续使用,如何新建类库就自己去百度了哈,这里就不描述了,若有不会的朋友请留言哈。标红了的都要注意下咯。
2.先看看WxTemplate这个类文件的代码
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace WxTemplateMsg{ public class WxTemplate { ////// 目标用户OpenId /// public string touser { get; set; } ////// 模板ID /// public string template_id { get; set; } ////// 模板消息顶部颜色(16进制),默认为#FF0000 /// public string topcolor { get; set; } ////// 模板跳转链接 /// public string url { get; set; } ////// 数据 /// public object data { get; set; } public WxTemplate() { topcolor = "#FF0000"; } } public class TemplateDataItem { ////// 项目值 /// public string value { get; set; } ////// 16进制颜色代码,如:#FF0000 /// public string color { get; set; } ////// /// /// value /// color public TemplateDataItem(string v, string c = "#173177") { value = v; color = c; } }}
TemplateModel文件夹是放不同模板的类文件,我这里的是金额变动的微信模板消息
MemRechargeTemplate.cs文件代码如下:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace WxTemplateMsg.TemplateModel{ public class MemRechargeTemplate { ////// 标题 /// public TemplateDataItem first { get; set; } ////// 日期 /// public TemplateDataItem date { get; set; } ////// 变动金额 /// public TemplateDataItem adCharge { get; set; } ////// /// public TemplateDataItem type { get; set; } ////// 卡内余额 /// public TemplateDataItem cashBalance { get; set; } ////// 备注 /// public TemplateDataItem remark { get; set; } }}
3.怎么调用呢,看下面代码:
#region 发送模板消息 WxTemplateMsg.WxTemplate wxTemp = new WxTemplateMsg.WxTemplate(); wxTemp.touser = openid; wxTemp.template_id = template_id; wxTemp.topcolor = "#FF0000"; wxTemp.url = "http://" + PubFunction.curParameter.strDoMain + "/mobile/member/bill.aspx"; MemRechargeTemplate memRecTemp = new MemRechargeTemplate(); memRecTemp.first = new WxTemplateMsg.TemplateDataItem(title); memRecTemp.date = new WxTemplateMsg.TemplateDataItem(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")); memRecTemp.adCharge = new WxTemplateMsg.TemplateDataItem((dclTotalRechargeMoney).ToString("#0.00")); memRecTemp.type = new WxTemplateMsg.TemplateDataItem(""); memRecTemp.cashBalance = new WxTemplateMsg.TemplateDataItem(modelMem.MemMoney.ToString("#0.00")); memRecTemp.remark = new WxTemplateMsg.TemplateDataItem("点击“详情“立即查阅您的帐户财务记录。"); wxTemp.data = memRecTemp; string res = PubFunction.PushTempleModel(wxTemp, access_token); if (!res.Contains("ok")) { //PubFunction.LogError("会员充值推送错误》" + res);//这里是把错误日志进入到数据库的表中,这个就自行补脑了哈 } #endregion
上面的代码中有一个方法:PushTempleModel
////// 发送模板消息 /// ///public static string PushTempleModel(WxTemplateMsg.WxTemplate tm, string access_token) { string url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + access_token; HttpRequestHelper httpRequest = new HttpRequestHelper(); string postData = JavaScriptConvert.SerializeObject(tm); string response = httpRequest.Reqeust(url, postData);//调用HTTP通信接口提交数据 return response; }
HttpRequestHelper.cs文件的代码:
public string Reqeust(string url, string postText) { System.Net.HttpWebRequest request; request = (System.Net.HttpWebRequest)WebRequest.Create(url); //Post请求方式 request.Method = "POST"; // 内容类型 request.ContentType = "application/x-www-form-urlencoded"; //// 参数经过URL编码 //string paraUrlCoded = System.Web.HttpUtility.UrlEncode(postText); byte[] payload; //将URL编码后的字符串转化为字节 payload = System.Text.Encoding.UTF8.GetBytes(postText); //设置请求的 ContentLength request.ContentLength = payload.Length; //获得请 求流 System.IO.Stream writer = request.GetRequestStream(); //将请求参数写入流 writer.Write(payload, 0, payload.Length); // 关闭请求流 writer.Close(); System.Net.HttpWebResponse response; // 获得响应流 response = (System.Net.HttpWebResponse)request.GetResponse(); System.IO.StreamReader reader = new System.IO.StreamReader(response.GetResponseStream(), Encoding.UTF8); string responseText = reader.ReadToEnd(); reader.Close(); return responseText; }
在此完毕了,有疑问的朋友请留言哈,若没能及时回复还请见谅哈。