public static void OwnerCertificationRecordPushRemind(OwnerCertificationRecordPushModel model) { try { var templateData = new { first = new TemplateDataItem("用户认证通知"), keyword1 = new TemplateDataItem(DateTime.Now.ToString("yyyy年MM月dd日 HH:mm:ss\r\n")), keyword2 = new TemplateDataItem(model.Status), keyword3 = new TemplateDataItem(model.Message), remark = new TemplateDataItem("详情", "#FF0000") }; var miniProgram = new TempleteModel_MiniProgram() { appid = GuoGuoCommunity_WxOpenAppId, pagepath = "pages/my/my" }; TemplateApi.SendTemplateMessage(AppId, model.OpenId, OwnerCertificationRecordTemplateId, null, templateData, miniProgram); } catch (Exception) { } }
/// <summary> /// 发送推送消息 /// </summary> /// <param name="model"></param> /// <param name="userId"></param> public async Task OrderPushRemind(OrderPushModel model, string userId) { var userEntity = await _userRepository.GetForIdAsync(userId); var weiXinUser = await _weiXinUserRepository.GetAsync(userEntity.UnionId); try { var templateData = new { first = new TemplateDataItem("您好,您的订单" + model.Type), keyword1 = new TemplateDataItem(model.Number), keyword2 = new TemplateDataItem(model.CreateTime.ToString("yyyy年MM月dd日 HH:mm:ss")), keyword3 = new TemplateDataItem(model.ReceiverName), keyword4 = new TemplateDataItem(model.PaymentPrice.ToString()), remark = new TemplateDataItem(">>点击查看详情<<", "#FF0000") }; var miniProgram = new TempleteModel_MiniProgram() { appid = GuoGuoCommunity_WxOpenAppId, pagepath = "pages/orderDetail/orderDetail?id=" + model.Id }; TemplateApi.SendTemplateMessage(AppId, weiXinUser?.OpenId, OrderTemplateId, null, templateData, miniProgram); } catch (Exception) { } }
/// <summary> /// 发送消息 /// </summary> /// <param name="weixinAppId">小程序AppID</param> /// <param name="appId">开发者ID(微信公众号)</param> /// <param name="openId">接收者ID(微信公众号)</param> /// <param name="pagepath">小程序跳转链接</param> /// <param name="templateId">消息模板</param> /// <param name="url"></param> /// <param name="title">标题</param> /// <param name="_first"></param> /// <param name="_remark"></param> /// <param name="_params">参数替换</param> /// <returns></returns> public string Send(string weixinAppId, string appId, string openId, string pagepath, string templateId, string url, string title, string _first, string _remark, string[] _params) { TempleteModel_MiniProgram mini; SendTemplateMessageResult result = new SendTemplateMessageResult(); var data = new WeiXinTemplate(templateId, "", title, _first, _remark, _params); mini = new TempleteModel_MiniProgram(); mini.appid = weixinAppId; mini.pagepath = pagepath; result = TemplateApi.SendTemplateMessage(appId, openId, data); return(JsonConvert.SerializeObject(result)); }
/// <summary> /// 发送推送消息 /// </summary> /// <param name="model"></param> /// <param name="smallDistrict"></param> public static async Task AnnouncementPushRemind(AnnouncementPushModel model, string smallDistrict) { IOwnerCertificationRecordRepository ownerCertificationRecordRepository = new OwnerCertificationRecordRepository(); IUserRepository userRepository = new UserRepository(); IWeiXinUserRepository weiXinUserRepository = new WeiXinUserRepository(); var userIdList = (await ownerCertificationRecordRepository.GetListForSmallDistrictIdAsync(new OwnerCertificationRecordDto { SmallDistrictId = smallDistrict })).Select(x => x.UserId).Distinct().ToList(); var userList = await userRepository.GetByIdsAsync(userIdList.ToList()); var weiXinUserList = await weiXinUserRepository.GetForUnionIdsAsync(userList.Select(x => x.UnionId).ToList()); foreach (var item in userIdList) { try { var user = userList.Where(x => x.Id.ToString() == item).FirstOrDefault(); var weiXinUser = weiXinUserList.Where(x => x.Unionid == user?.UnionId).FirstOrDefault(); var templateData = new { first = new TemplateDataItem("公告通知"), keyword1 = new TemplateDataItem(model.Title), keyword2 = new TemplateDataItem("发送成功"), keyword3 = new TemplateDataItem(DateTime.Now.ToString("yyyy年MM月dd日 HH:mm:ss\r\n")), remark = new TemplateDataItem(">>点击查看详情<<", "#FF0000") }; var miniProgram = new TempleteModel_MiniProgram() { appid = GuoGuoCommunity_WxOpenAppId, pagepath = "pages/noticeDetail/noticeDetail?con=" + JsonConvert.SerializeObject(model) }; TemplateApi.SendTemplateMessage(AppId, weiXinUser?.OpenId, AnnouncementTemplateId, null, templateData, miniProgram); } catch (Exception) { } } }
/// <summary> /// 使用异步Action测试异步模板消息接口 /// </summary> /// <param name="checkcode"></param> /// <returns></returns> public async Task <ActionResult> TemplateMessageTest(string checkcode) { var openId = CustomMessageHandler.TemplateMessageCollection.ContainsKey(checkcode) ? CustomMessageHandler.TemplateMessageCollection[checkcode] : null; if (openId == null) { return(Content("验证码已过期或不存在!请在“盛派网络小助手”公众号输入“tm”获取验证码。")); } else { CustomMessageHandler.TemplateMessageCollection.Remove(checkcode); var templateId = "cCh2CTTJIbVZkcycDF08n96FP-oBwyMVrro8C2nfVo4"; var testData = new //TestTemplateData() { first = new TemplateDataItem("【异步模板消息测试】"), keyword1 = new TemplateDataItem(openId), keyword2 = new TemplateDataItem("网页测试"), keyword3 = new TemplateDataItem(DateTime.Now.ToString("O")), remark = new TemplateDataItem("更详细信息,请到Senparc.Weixin SDK官方网站(http://sdk.weixin.senparc.com)查看!") }; var miniProgram = new TempleteModel_MiniProgram() { appid = "wxfcb0a0031394a51c",//【盛派互动(BookHelper)】小程序 pagepath = "pages/index/index" }; var result = await TemplateApi.SendTemplateMessageAsync(appId, openId, templateId, null, testData, miniProgram); return(Content("异步模板消息已经发送到【盛派网络小助手】公众号,请查看。此前的验证码已失效,如需继续测试,请重新获取验证码。")); } }
/// <summary> /// 【异步方法】模板消息接口 /// </summary> /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param> /// <param name="openId"></param> /// <param name="miniProgram">跳小程序所需数据,不需跳小程序可不用传该数据</param> /// <param name="templateMessageData"></param> /// <param name="timeOut">代理请求超时时间(毫秒)</param> /// <returns></returns> public static async Task <SendTemplateMessageResult> SendTemplateMessageAsync(string accessTokenOrAppId, string openId, ITemplateMessageBase templateMessageData, TempleteModel_MiniProgram miniProgram = null, int timeOut = Config.TIME_OUT) { return(await SendTemplateMessageAsync(accessTokenOrAppId, openId, templateMessageData.TemplateId, templateMessageData.Url, templateMessageData, miniProgram, timeOut)); }
/// <summary> /// 【异步方法】模板消息接口 /// </summary> /// <param name="accessTokenOrAppId">AccessToken或AppId(推荐使用AppId,需要先注册)</param> /// <param name="openId"></param> /// <param name="templateId"></param> /// <param name="url"></param> /// <param name="data"></param> /// <param name="miniProgram">跳小程序所需数据,不需跳小程序可不用传该数据</param> /// <param name="timeOut">代理请求超时时间(毫秒)</param> /// <returns></returns> public static async Task <SendTemplateMessageResult> SendTemplateMessageAsync(string accessTokenOrAppId, string openId, string templateId, string url, object data, TempleteModel_MiniProgram miniProgram = null, int timeOut = Config.TIME_OUT) { return(await ApiHandlerWapper.TryCommonApiAsync(async accessToken => { const string urlFormat = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={0}"; var msgData = new TempleteModel() { touser = openId, template_id = templateId, // topcolor = topcolor, url = url, miniprogram = miniProgram, data = data }; return await Senparc.Weixin.CommonAPIs.CommonJsonSend.SendAsync <SendTemplateMessageResult>(accessToken, urlFormat, msgData, timeOut: timeOut); }, accessTokenOrAppId)); }
public static SendTemplateMessageResult SendTemplateMessage(string accessTokenOrAppId, string openId, string templateId, string url, object data, TempleteModel_MiniProgram miniProgram = null, int timeOut = Config.TIME_OUT) { //文档:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1500374289_66bvB return(ApiHandlerWapper.TryCommonApi(accessToken => { string urlFormat = Config.ApiMpHost + "/cgi-bin/message/template/send?access_token={0}"; var msgData = new TempleteModel() { touser = openId, template_id = templateId, // topcolor = topcolor, url = url, miniprogram = miniProgram, data = data, }; return CommonJsonSend.Send <SendTemplateMessageResult>(accessToken, urlFormat, msgData, timeOut: timeOut); }, accessTokenOrAppId)); }
/// <summary> /// 发送模板消息 /// </summary> /// <param name="appId">appId</param> /// <param name="openId">openId</param> /// <param name="TemplateMessage">模板消息接口</param> /// <param name="miniProgram">跳小程序所需数据</param> /// <param name="timeOut">超时ms</param> /// <returns></returns> public static SendTemplateMessageResult SendTemplateMessage(string appId, string openId, WeixinTemplateMessage TemplateMessage, TempleteModel_MiniProgram miniProgram = null, int timeOut = 3000) { return(TemplateApi.SendTemplateMessage(appId, openId, TemplateMessage, miniProgram, timeOut)); }
public static SendTemplateMessageResult SendTemplateMessage(string accessTokenOrAppId, object data, TempleteModel_MiniProgram miniProgram = null, int timeOut = Config.TIME_OUT) { //文档:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1500374289_66bvB return(ApiHandlerWapper.TryCommonApi(accessToken => { string urlFormat = Config.ApiMpHost + "/cgi-bin/message/template/send?access_token={0}"; return CommonJsonSend.Send <SendTemplateMessageResult>(accessToken, urlFormat, data, timeOut: timeOut); }, accessTokenOrAppId)); }