You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
101 lines
4.3 KiB
101 lines
4.3 KiB
using Senparc.Weixin;
|
|
using Senparc.Weixin.CommonAPIs;
|
|
using Senparc.Weixin.Entities;
|
|
using Senparc.Weixin.Entities.TemplateMessage;
|
|
using Senparc.Weixin.WxOpen;
|
|
using System;
|
|
using System.Threading.Tasks;
|
|
using Znyc.Cloudcar.Admin.Commons;
|
|
using Znyc.Cloudcar.Admin.Commons.Log;
|
|
|
|
namespace Wx
|
|
{
|
|
/// <summary>
|
|
/// 小程序订阅消息接口
|
|
/// </summary>
|
|
public class MessageHelper
|
|
{
|
|
#region 同步方法
|
|
|
|
/// <summary>
|
|
/// 发送订阅消息
|
|
/// </summary>
|
|
/// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
|
|
/// <param name="toUser">接收者(用户)的 openid</param>
|
|
/// <param name="templateId">所需下发的订阅模板id</param>
|
|
/// <param name="data">模板内容,格式形如 { "key1": { "value": any }, "key2": { "value": any } }</param>
|
|
/// <param name="page">点击模板卡片后的跳转页面,仅限本小程序内的页面。支持带参数,(示例index?foo=bar)。该字段不填则模板无跳转。</param>
|
|
/// <param name="timeOut"></param>
|
|
/// <returns></returns>
|
|
public static WxJsonResult SendSubscribe(string accessTokenOrAppId, string toUser, string templateId,
|
|
TemplateMessageData data, string page = null, int timeOut = Config.TIME_OUT)
|
|
{
|
|
return WxOpenApiHandlerWapper.TryCommonApi(accessToken =>
|
|
{
|
|
string urlFormat = Config.ApiMpHost + "/cgi-bin/message/subscribe/send?access_token={0}";
|
|
var submitData = new
|
|
{
|
|
touser = toUser,
|
|
template_id = templateId,
|
|
page,
|
|
data
|
|
};
|
|
return CommonJsonSend.Send(accessToken, urlFormat, submitData, timeOut: timeOut);
|
|
}, accessTokenOrAppId);
|
|
}
|
|
|
|
#endregion 同步方法
|
|
|
|
#region 异步方法
|
|
|
|
public static async Task SendSubscribeAsync(TemplateMessageData templateMessageData, string templateId,
|
|
string toUser,
|
|
string pageUrl)
|
|
{
|
|
try
|
|
{
|
|
Senparc.Weixin.MP.Entities.AccessTokenResult accessTokenResult = await CommonHelper.GetTokenAsync(
|
|
Configs.GetConfigurationValue("SenparcWeixinSetting", "WxOpenAppId"),
|
|
Configs.GetConfigurationValue("SenparcWeixinSetting", "WxOpenAppSecret"));
|
|
string page = pageUrl;
|
|
WxJsonResult sU = await SendSubscribeAsync(accessTokenResult.access_token,
|
|
toUser, templateId, templateMessageData,
|
|
page);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log4NetHelper.Error("SendSubscribeAsync" + ex);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 【异步方法】发送订阅消息
|
|
/// </summary>
|
|
/// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param>
|
|
/// <param name="toUser">接收者(用户)的 openid</param>
|
|
/// <param name="templateId">所需下发的订阅模板id</param>
|
|
/// <param name="data">模板内容,格式形如 { "key1": { "value": any }, "key2": { "value": any } }</param>
|
|
/// <param name="page">点击模板卡片后的跳转页面,仅限本小程序内的页面。支持带参数,(示例index?foo=bar)。该字段不填则模板无跳转。</param>
|
|
/// <param name="timeOut"></param>
|
|
/// <returns></returns>
|
|
public static async Task<WxJsonResult> SendSubscribeAsync(string accessTokenOrAppId, string toUser,
|
|
string templateId, TemplateMessageData data, string page = null, int timeOut = Config.TIME_OUT)
|
|
{
|
|
return await WxOpenApiHandlerWapper.TryCommonApiAsync(async accessToken =>
|
|
{
|
|
string urlFormat = Config.ApiMpHost + "/cgi-bin/message/subscribe/send?access_token={0}";
|
|
var submitData = new
|
|
{
|
|
touser = toUser,
|
|
template_id = templateId,
|
|
page,
|
|
data
|
|
};
|
|
|
|
return await CommonJsonSend.SendAsync(accessToken, urlFormat, submitData, timeOut: timeOut);
|
|
}, accessTokenOrAppId);
|
|
}
|
|
|
|
#endregion 异步方法
|
|
}
|
|
}
|