//发送订阅消息
public ResultMessage SendSubscribeMsg(SendSubscribeMsgRequestDto request)
{
ResultMessage resultMessage = new ResultMessage();
string token = GetAccessToken();
if (token.Length < 20)
{
resultMessage.Message = "token错误";
return resultMessage;
}
string url = string.Format("https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token={0}", token);
string resultdata = HttpServiceHelper.PostJson(url, JsonHelper.GetJsonString(request));
var result = JsonHelper.JsonDeserialize<SendSubscribeMsgResponseDto>(resultdata);
if (result.errcode == 0)
{
resultMessage.IsSuccess = true;
resultMessage.Message = "发送成功";
return resultMessage;
}
else
{
resultMessage.Message = result.errmsg;
}
return resultMessage;
}
发送请求
private string GetRequestPost(string url, string param)
{
HttpWebRequest webrequest = (HttpWebRequest)HttpWebRequest.Create(url);
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
byte[] arrB = encode.GetBytes(param.ToString());
webrequest.Method = "POST";
webrequest.ContentType = "application/x-www-form-urlencoded";
webrequest.ContentLength = arrB.Length;
Stream outStream = webrequest.GetRequestStream();
outStream.Write(arrB, 0, arrB.Length);
outStream.Close();
//接收HTTP做出的响应
WebResponse myResp = webrequest.GetResponse();
Stream ReceiveStream = myResp.GetResponseStream();
StreamReader readStream = new StreamReader(ReceiveStream, encode);
Char[] read = new Char[256];
int count = readStream.Read(read, 0, 256);
string strs = "";
while (count > 0)
{
strs += new String(read, 0, count);
count = readStream.Read(read, 0, 256);
}
readStream.Close();
myResp.Close();
return strs;
}
获取accesstoken
private string GetAccessToken()
{
lock (SyncLock)
{
var token = "";
if (CacheHelper.Exists(CacheKey))
{
CacheHelper.Get(CacheKey, out token);
}
else
{
var accessTokenUrl = string.Format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}", AppId, AppSecret);
var tokenResult = GetRequestPost(accessTokenUrl, "");
var _result = JsonHelper.JsonDeserialize<AccessTokenResponseDto>(tokenResult);
if (_result != null)
{
token = _result.access_token;
CacheHelper.Add(token, CacheKey);
}
}
return token;
}
}
public class SendSubscribeMsgRequestDto
{
/// <summary>
/// 接收者(用户)的 openid
/// </summary>
public string touser { get; set; }
/// <summary>
/// 所需下发的订阅模板id
/// </summary>
public string template_id { get; set; }
/// <summary>
/// 点击模板卡片后的跳转页面,仅限本小程序内的页面。支持带参数,(示例index?foo=bar)。该字段不填则模板无跳转。
/// </summary>
public string page { get; set; }
/// <summary>
/// 跳转小程序类型:developer为开发版;trial为体验版;formal为正式版;默认为正式版
/// </summary>
public string miniprogram_state { get; set; }
/// <summary>
/// 进入小程序查看”的语言类型,支持zh_CN(简体中文)、en_US(英文)、zh_HK(繁体中文)、zh_TW(繁体中文),默认为zh_CN
/// </summary>
public string lang { get; set; }
/// <summary>
/// 模板内容
/// </summary>
public Dictionary<string, DataValue> data { get; set; }
}
/// <summary>
/// 发送小程序订阅消息响应Dto
/// </summary>
public class SendSubscribeMsgResponseDto : WechatResponseDto
{
}
/// <summary>
/// 模板内容关键字
/// </summary>
public class DataValue
{
/// <summary>
/// 订阅消息参数值
/// </summary>
public string value { get; set; }
}
封装调用
public ResultMessage SendSubscribeMessage(string openId, string template_id, Dictionary<string, AppletAPI.DataValue> data, string pageUrl = "")
{
var sendMe = new AppletAPI.SendSubscribeMsgRequestDto()
{
page = pageUrl,
template_id = template_id,
data = data,
touser = openId,
miniprogram_state = "developer"//跳转小程序类型:developer为开发版;trial为体验版;formal为正式版;默认为正式版
};
AppletAPI appletAPI = new AppletAPI();
ResultMessage sendResult = appletAPI.SendSubscribeMsg(sendMe);
return sendResult;
}
业务调用
string template_id = "123asd";
Dictionary<string, AppletAPI.DataValue> data = new Dictionary<string, AppletAPI.DataValue>()
{
{"thing",new AppletAPI.DataValue{ value="测试" } },
{"date",new AppletAPI.DataValue{ value= DateTime.Now.ToString()} }
};
//点击消息卡片跳转的页面和参数
string pageUrl = "/pages/index?param=myParam" ;
SendSubscribeMessage(customer.FirstOpenId, template_id, data,pageUrl);
符号表示除中文、英文、数字外的常见符号,不能带有换行等控制字符。 时间格式支持HH:MM:SS或者HH:MM。 日期包含年月日,为y年m月d日,y年m月、m月d日格式,或者用‘-’、‘/’、‘.’符号连接,如2018-01-01,2018/01/01,2018.01.01,2018-01,01-01。