• C# 封装微信的模板消息


    1.先新建一个类库,以方便以后移植到其他的项目上继续使用,如何新建类库就自己去百度了哈,这里就不描述了,若有不会的朋友请留言哈。标红了的都要注意下咯。

    2.先看看WxTemplate这个类文件的代码

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace WxTemplateMsg
    {
        public class WxTemplate
        {
            /// <summary>
            /// 目标用户OpenId
            /// </summary>
            public string touser { get; set; }
            /// <summary>
            /// 模板ID
            /// </summary>
            public string template_id { get; set; }
            /// <summary>
            /// 模板消息顶部颜色(16进制),默认为#FF0000
            /// </summary>
            public string topcolor { get; set; }
            /// <summary>
            /// 模板跳转链接
            /// </summary>
            public string url { get; set; }
            /// <summary>
            /// 数据
            /// </summary>
            public object data { get; set; }
            public WxTemplate()
            {
                topcolor = "#FF0000";
            }
        }
    
        public class TemplateDataItem
        {
            /// <summary>
            /// 项目值
            /// </summary>
            public string value { get; set; }
            /// <summary>
            /// 16进制颜色代码,如:#FF0000
            /// </summary>
            public string color { get; set; }
            /// <summary>
            /// 
            /// </summary>
            /// <param name="v">value</param>
            /// <param name="c">color</param>
            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
        {
            /// <summary>
            /// 标题
            /// </summary>
            public TemplateDataItem first { get; set; }
            /// <summary>
            /// 日期
            /// </summary>
            public TemplateDataItem date { get; set; }
            /// <summary>
            /// 变动金额
            /// </summary>
            public TemplateDataItem adCharge { get; set; }
            /// <summary>
            /// 
            /// </summary>
            public TemplateDataItem type { get; set; }
            /// <summary>
            /// 卡内余额
            /// </summary>
            public TemplateDataItem cashBalance { get; set; }
            /// <summary>
            /// 备注
            /// </summary>
            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

    /// <summary>
        /// 发送模板消息
        /// </summary>
        /// <returns></returns>
        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;
            }

    在此完毕了,有疑问的朋友请留言哈,若没能及时回复还请见谅哈。

  • 相关阅读:
    10,EasyNetQ-发布确认
    9,EasyNetQ-版本化消息
    一个小程序云开发的项目,图书借还系统
    利用canvas对图片进行切割
    微信小程序添加卡券到微信卡包,使用wx.addCard()方法传参及整体流程
    git合并时忽略某个文件
    小程序接入云通信IM
    小程序插件使用wx.createSelectorQuery()获取不到节点信息
    小程序插件开发流程及注意事项
    小米6使用charles抓包https
  • 原文地址:https://www.cnblogs.com/LoveQin/p/8809404.html
Copyright © 2020-2023  润新知