public void WxAppCard(string messageId, string appId, string openId, string title, string pagePath, string wxAppId, string imageId, PerformContext context = null) { var messageStatus = new MessageStatus(_redis, messageId); messageStatus.Sended(openId); context.WriteLine("向用户「{0}」@「{1}」发送小程序卡片...", openId, appId); var accessToken = _redis.StringGet(CacheKey.UserAccessTokenPrefix + appId); if (accessToken.HasValue) { var resp = MessageApi.SendWxAppCard(accessToken, openId, title, wxAppId, pagePath, imageId); if (resp.ErrCode == 0) { messageStatus.Success(openId); context.WriteLine("消息发送成功..."); } else { messageStatus.SendError(openId); context.WriteLine("消息发送失败:{0}...", resp.ErrMsg); throw new ServiceException(resp.ErrCode, resp.ErrMsg); } } else { messageStatus.SendError(openId); context.WriteLine("消息发送失败:无法获取AccessToken..."); throw new ServiceException(-1, "消息发送失败:无法获取AccessToken..."); } }
public void News(string messageId, string appId, string openId, string title, string description, string url, string picUrl, bool isWxApp, PerformContext context) { var messageStatus = new MessageStatus(_redis, messageId); messageStatus.Sended(openId); context.WriteLine("向用户「{0}」@「{1}」发送图文消息...", openId, appId); var accessToken = _redis.StringGet(CacheKey.UserAccessTokenPrefix + appId); if (accessToken.HasValue) { var resp = MessageApi.SendNews(accessToken, openId, title, description, url, picUrl, isWxApp); if (resp.ErrCode == 0) { messageStatus.Success(openId); context.WriteLine("消息发送成功..."); } else { messageStatus.SendError(openId); context.WriteLine("消息发送失败:{0}...", resp.ErrMsg); throw new ServiceException(resp.ErrCode, resp.ErrMsg); } } else { messageStatus.SendError(openId); context.WriteLine("消息发送失败:无法获取AccessToken..."); throw new ServiceException(-1, "消息发送失败:无法获取AccessToken..."); } }
public void Text(string messageId, string appId, string openId, string content, PerformContext context) { var messageStatus = new MessageStatus(_redis, messageId); messageStatus.Sended(openId); context.WriteLine("向用户「{0}」@「{1}」发送文本消息...", openId, appId); var accessToken = _redis.StringGet(CacheKey.UserAccessTokenPrefix + appId); if (accessToken.HasValue) { var resp = MessageApi.SendText(accessToken, openId, content); if (resp.ErrCode == 0) { messageStatus.Success(openId); context.WriteLine("消息发送成功..."); } else { messageStatus.SendError(openId); context.WriteLine("消息发送失败:{0}...", resp.ErrMsg); throw new ServiceException(resp.ErrCode, resp.ErrMsg); } } else { messageStatus.SendError(openId); context.WriteLine("消息发送失败:无法获取AccessToken..."); throw new ServiceException(-1, "消息发送失败:无法获取AccessToken..."); } }
public void Template(string messageId, string appId, string openId, string templateId, string url, Dictionary <string, WxWebSendTemplateRequest.DataItem> data, string formId, PerformContext context) { var messageStatus = new MessageStatus(_redis, messageId); messageStatus.Sended(openId); context.WriteLine("向用户「{0}」@「{1}」发送模版消息「{2}」...", openId, appId, templateId); var accessToken = _redis.StringGet(CacheKey.UserAccessTokenPrefix + appId); if (accessToken.HasValue) { if (string.IsNullOrEmpty(formId)) { var resp = WxWebApi.SendTemplate(accessToken, openId, templateId, url, data); if (resp.ErrCode == 0) { messageStatus.SetTemplateMap(resp.MsgId.ToString()); context.WriteLine("消息发送成功...[MsgId: {0}]", resp.MsgId); } else { messageStatus.SendError(openId); context.WriteLine("消息发送失败:{0}...", resp.ErrMsg); throw new ServiceException(resp.ErrCode, resp.ErrMsg); } } else { var tmpData = new Dictionary <string, WxAppSendTemplateRequest.DataItem>(); foreach (var item in data) { tmpData.Add(item.Key, new WxAppSendTemplateRequest.DataItem { Value = item.Value.Value }); } var resp = WxAppApi.SendTemplate(accessToken, openId, templateId, url, formId, tmpData); if (resp.ErrCode == 0) { messageStatus.Success(openId); context.WriteLine("消息发送成功..."); } else { messageStatus.SendError(openId); context.WriteLine("消息发送失败:{0}...", resp.ErrMsg); throw new ServiceException(resp.ErrCode, resp.ErrMsg); } } } else { messageStatus.SendError(openId); context.WriteLine("消息发送失败:无法获取AccessToken..."); throw new ServiceException(-1, "消息发送失败:无法获取AccessToken..."); } }