protected virtual async Task SendWeChatTemplateMessagAsync(NotificationInfo notification, UserIdentifier identifier) { var templateId = GetOrDefaultTemplateId(notification.Data); Logger.LogDebug($"Get wechat weapp template id: {templateId}"); var redirect = GetOrDefault(notification.Data, "RedirectPage", null); Logger.LogDebug($"Get wechat weapp redirect page: {redirect ?? "null"}"); var weAppState = GetOrDefault(notification.Data, "WeAppState", Options.DefaultWeAppState); Logger.LogDebug($"Get wechat weapp state: {weAppState ?? null}"); var weAppLang = GetOrDefault(notification.Data, "WeAppLanguage", Options.DefaultWeAppLanguage); Logger.LogDebug($"Get wechat weapp language: {weAppLang ?? null}"); var weChatWeAppNotificationData = new WeChatWeAppSendNotificationData(identifier.UserName, templateId, redirect, weAppState, weAppLang); // 写入模板数据 weChatWeAppNotificationData.WriteStandardData(NotificationData.ToStandardData(Options.DefaultMsgPrefix, notification.Data)); Logger.LogDebug($"Sending wechat weapp notification: {notification.Name}"); // 发送小程序订阅消息 await NotificationSender.SendAsync(weChatWeAppNotificationData); }
protected virtual async Task SendWeChatTemplateMessagAsync(NotificationInfo notification, UserIdentifier identifier, CancellationToken cancellationToken = default) { var templateId = GetOrDefaultTemplateId(notification.Data); if (templateId.IsNullOrWhiteSpace()) { Logger.LogWarning("Wechat weapp template id be empty, can not send notification!"); return; } Logger.LogDebug($"Get wechat weapp template id: {templateId}"); var redirect = GetOrDefault(notification.Data, "RedirectPage", null); Logger.LogDebug($"Get wechat weapp redirect page: {redirect ?? "null"}"); var weAppState = GetOrDefault(notification.Data, "WeAppState", Options.DefaultWeAppState); Logger.LogDebug($"Get wechat weapp state: {weAppState ?? null}"); var weAppLang = GetOrDefault(notification.Data, "WeAppLanguage", Options.DefaultWeAppLanguage); Logger.LogDebug($"Get wechat weapp language: {weAppLang ?? null}"); // TODO: 如果微信端发布通知,请组装好 openid 字段在通知数据内容里面 string weChatCode = GetOrDefault(notification.Data, AbpWeChatClaimTypes.OpenId, ""); var openId = !weChatCode.IsNullOrWhiteSpace() ? weChatCode : await UserWeChatOpenIdFinder.FindByUserIdAsync(identifier.UserId); var weChatWeAppNotificationData = new WeChatWeAppSendNotificationData(openId, templateId, redirect, weAppState, weAppLang); // 写入模板数据 weChatWeAppNotificationData.WriteStandardData(NotificationData.ToStandardData(Options.DefaultMsgPrefix, notification.Data)); Logger.LogDebug($"Sending wechat weapp notification: {notification.Name}"); // 发送小程序订阅消息 await NotificationSender.SendAsync(weChatWeAppNotificationData); }
public virtual async Task SendAsync(WeChatWeAppSendNotificationData notificationData, CancellationToken cancellationToken = default) { var weChatToken = await WeChatTokenProvider.GetTokenAsync(); var requestParamters = new Dictionary <string, string> { { "access_token", weChatToken.AccessToken } }; var weChatSendNotificationUrl = "https://api.weixin.qq.com"; var weChatSendNotificationPath = "/cgi-bin/message/subscribe/send"; var requestUrl = BuildRequestUrl(weChatSendNotificationUrl, weChatSendNotificationPath, requestParamters); var responseContent = await MakeRequestAndGetResultAsync(requestUrl, notificationData, cancellationToken); var weChatSenNotificationResponse = JsonSerializer.Deserialize <WeChatSendNotificationResponse>(responseContent); if (!weChatSenNotificationResponse.IsSuccessed) { Logger.LogWarning("Send wechat we app subscribe message failed"); Logger.LogWarning($"Error code: {weChatSenNotificationResponse.ErrorCode}, message: {weChatSenNotificationResponse.ErrorMessage}"); } // 失败是否抛出异常 // weChatSenNotificationResponse.ThrowIfNotSuccess(); }
protected virtual async Task <string> MakeRequestAndGetResultAsync(string url, WeChatWeAppSendNotificationData notificationData, CancellationToken cancellationToken = default) { var client = HttpClientFactory.CreateClient(SendNotificationClientName); var sendDataContent = JsonSerializer.Serialize(notificationData); var requestContent = new StringContent(sendDataContent); var requestMessage = new HttpRequestMessage(HttpMethod.Post, url) { Content = requestContent }; var response = await client.SendAsync(requestMessage, cancellationToken); if (!response.IsSuccessStatusCode) { throw new AbpException($"WeChat send subscribe message http request service returns error! HttpStatusCode: {response.StatusCode}, ReasonPhrase: {response.ReasonPhrase}"); } var resultContent = await response.Content.ReadAsStringAsync(); return(resultContent); }