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 { /// /// 小程序订阅消息接口 /// public class MessageHelper { #region 同步方法 /// /// 发送订阅消息 /// /// AccessToken或AppId(推荐使用AppId,需要先注册) /// 接收者(用户)的 openid /// 所需下发的订阅模板id /// 模板内容,格式形如 { "key1": { "value": any }, "key2": { "value": any } } /// 点击模板卡片后的跳转页面,仅限本小程序内的页面。支持带参数,(示例index?foo=bar)。该字段不填则模板无跳转。 /// /// 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); } } /// /// 【异步方法】发送订阅消息 /// /// AccessToken或AppId(推荐使用AppId,需要先注册) /// 接收者(用户)的 openid /// 所需下发的订阅模板id /// 模板内容,格式形如 { "key1": { "value": any }, "key2": { "value": any } } /// 点击模板卡片后的跳转页面,仅限本小程序内的页面。支持带参数,(示例index?foo=bar)。该字段不填则模板无跳转。 /// /// public static async Task 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 异步方法 } }