使用说明
步骤一:获取模板ID
有两个方法可以获取模版ID
通过模版消息管理接口获取模版ID(详见模版消息管理)
在微信公众平台手动配置获取模版ID
登录https://mp.weixin.qq.com 获取模板,如果没有合适的模板,可以申请添加新模板,审核通过后可使用,详见模板审核说明
步骤二:页面的 <form/> 组件,属性report-submit为true时,可以声明为需发模板消息,此时点击按钮提交表单可以获取formId,用于发送模板消息。或者当用户完成支付行为,可以获取prepay_id用于发送模板消息。
步骤三:调用接口下发模板消息(详见发送模版消息)
发送模版消息
接口地址:https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=ACCESS_TOKEN
HTTP请求方式:POST
POST参数说明:
参数 必填 说明
touser 是 接收者(用户)的 openid
template_id 是 所需下发的模板消息的id
page 否 点击模板卡片后的跳转页面,仅限本小程序内的页面。支持带参数,(示例index?foo=bar)。该字段不填则模板无跳转。
form_id 是 表单提交场景下,为 submit 事件带上的 formId;支付场景下,为本次支付的 prepay_id
data 是 模板内容,不填则下发空模板
color 否 模板内容字体的颜色,不填默认黑色 【废弃】
emphasis_keyword 否 模板需要放大的关键词,不填则默认无放大
示例:
{ "touser": "OPENID", "template_id": "TEMPLATE_ID", "page": "index", "form_id": "FORMID", "data": { "keyword1": { "value": "339208499" }, "keyword2": { "value": "2015年01月05日 12:30" }, "keyword3": { "value": "粤海喜来登酒店" } , "keyword4": { "value": "广州市天河区天河路208号" } }, "emphasis_keyword": "keyword1.DATA" }
返回码说明:
在调用模板消息接口后,会返回JSON数据包。
正常时的返回JSON数据包示例:
{ "errcode": 0, "errmsg": "ok" }
错误时会返回错误码信息,说明如下:
返回码 说明
40037 template_id不正确
41028 form_id不正确,或者过期
41029 form_id已被使用
41030 page不正确
45009 接口调用超过限额(目前默认每个帐号日调用限额为100万)
本次开发中用到的是支付后发送模版消息,所有传递的form_id是统一下单返回的prepay_id;
ASP.NET WEB API发送代码实现如下(代码未做封装):
//从配置文件读取微信公众号appid与secret string public_appid = ConfigurationManager.AppSettings["yh_client_Appid"]; string pulic_secret = ConfigurationManager.AppSettings["yh_client_Secret"]; //根据appid,secret 获取access_token var url = @"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + public_appid + "&secret=" + pulic_secret; Dictionary<string, object> result = Get_Wx_DictionaryHelper.GetDictionary(url); string access_token = ""; if (result.ContainsKey("access_token")) { access_token = result["access_token"].ToString(); } Logger.Debug("Wx_Pay_Notify--access_token:" + DateTime.Now.ToString() + access_token); var sendUrl = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=" + access_token + ""; Wx_PaY_NotifyModel sendModel = Wx_yyhz_ym_activityBLL.Get_sigup_pay_send_detail(out_trade_no.ToString(), transaction_id); CYQ.Data.Cache.CacheManage cacheManage = CYQ.Data.Cache.CacheManage.Instance; var form_id = cacheManage.Get(out_trade_no).ToString();//从缓存中取到通一下单接口返回的prepay_id Logger.Debug("当前FORMID:" + form_id + "====="); Dictionary<string, object> sendDictionary = new Dictionary<string, object>(); sendDictionary.Add("touser", xml["openid"]); sendDictionary.Add("template_id", "Ap------------0gSSan15TqW4yLxsZ28");//从微信公众平台赋值的模版ID sendDictionary.Add("page", "pages/main/main"); sendDictionary.Add("form_id", form_id); Dictionary<string, object> senddata = new Dictionary<string, object>(); senddata.Add("keyword1", new Dictionary<string, object>() { {"value",sendModel.vip_name} }); senddata.Add("keyword2", new Dictionary<string, object>() { {"value",sendModel.total_fee+"元"} }); senddata.Add("keyword3", new Dictionary<string, object>() { {"value",sendModel.activity_title} }); senddata.Add("keyword4", new Dictionary<string, object>() { {"value",sendModel.activity_starttime} }); senddata.Add("keyword5", new Dictionary<string, object>() { {"value",sendModel.activity_adr} }); sendDictionary.Add("data", senddata); Logger.Debug("发送模版数据:" + sendDictionary.ToJson() + "======="); HttpClient httpClient = new HttpClient(); httpClient.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue("application/json")); HttpContent httpContent = new StringContent(sendDictionary.ToJson()); HttpResponseMessage ResponseMsg = httpClient.PostAsync(sendUrl, httpContent).Result; var response = ResponseMsg.Content.ReadAsStringAsync().Result; Logger.Debug("Wx_Pay_Notify--response:" + DateTime.Now.ToString() + response); cacheManage.Remove(out_trade_no);
本次开发中用到的是支付后发送模版消息,所有传递的form_id是统一下单返回的prepay_id;
原文:https://blog.csdn.net/MrTraum/article/details/80897372