feat(wechat): Enhance subscription message sending with improved error handling and logging
All checks were successful
continuous-integration/drone/push Build is passing

- Update notification templates to include action context in user nicknames (unlock, favorite, message, reminder)
- Refactor subscription message request to use manual HttpRequestMessage for better control
- Add empty response validation with specific warning log
- Enhance error logging to include ToUser identifier for better diagnostics
- Improve exception handling with ToUser context in error logs
- These changes provide better visibility into subscription message failures and clearer user action context in notifications
This commit is contained in:
zpc 2026-04-01 14:58:22 +08:00
parent b63139b5ae
commit 10b551ed6b
2 changed files with 22 additions and 8 deletions

View File

@ -251,7 +251,7 @@ public class NotificationService : INotificationService
targetUser.ServiceAccountOpenId,
NotificationTemplateType.Unlock,
"来访通知",
unlockerUser?.Nickname ?? "有人",
(unlockerUser?.Nickname ?? "有人") + " 解锁了您",
$"编号{unlockerUser?.XiangQinNo ?? ""}刚刚访问了您",
DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
"pages/interact/unlockedMe",
@ -311,7 +311,7 @@ public class NotificationService : INotificationService
targetUser.ServiceAccountOpenId,
NotificationTemplateType.Favorite,
"收藏通知",
favoriterUser?.Nickname ?? "有人",
(favoriterUser?.Nickname ?? "有人") + " 收藏了您",
$"编号{favoriterUser?.XiangQinNo ?? ""}收藏了您",
DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
"pages/interact/favoritedMe",
@ -371,7 +371,7 @@ public class NotificationService : INotificationService
targetUser.ServiceAccountOpenId,
NotificationTemplateType.FirstMessage,
"消息通知",
senderUser?.Nickname ?? "有人",
(senderUser?.Nickname ?? "有人") + " 联系了您",
$"编号{senderUser?.XiangQinNo ?? ""}给您发送了一条消息",
DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
"pages/chat/index",
@ -431,7 +431,7 @@ public class NotificationService : INotificationService
targetUser.ServiceAccountOpenId,
NotificationTemplateType.MessageReminder,
"消息提醒",
senderUser?.Nickname ?? "有人",
(senderUser?.Nickname ?? "有人") + " 等待您回复",
$"编号{senderUser?.XiangQinNo ?? ""}正在等待您的回复",
DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
"pages/chat/index",

View File

@ -346,12 +346,26 @@ public class WeChatService : IWeChatService
miniprogram_state = request.MiniprogramState
};
var response = await _httpClient.PostAsJsonAsync(url, requestBody);
var result = await response.Content.ReadFromJsonAsync<WeChatApiResponse>();
var jsonContent = System.Text.Json.JsonSerializer.Serialize(requestBody);
var httpRequest = new HttpRequestMessage(HttpMethod.Post, url)
{
Content = new StringContent(jsonContent, Encoding.UTF8, "application/json")
};
var response = await _httpClient.SendAsync(httpRequest);
var responseContent = await response.Content.ReadAsStringAsync();
if (string.IsNullOrWhiteSpace(responseContent))
{
_logger.LogWarning("发送订阅消息失败: 微信返回空响应, StatusCode={StatusCode}, ToUser={ToUser}", (int)response.StatusCode, request.ToUser);
return false;
}
var result = System.Text.Json.JsonSerializer.Deserialize<WeChatApiResponse>(responseContent);
if (result?.ErrCode != 0)
{
_logger.LogWarning("发送订阅消息失败: {ErrCode} - {ErrMsg}", result?.ErrCode, result?.ErrMsg);
_logger.LogWarning("发送订阅消息失败: {ErrCode} - {ErrMsg}, ToUser={ToUser}", result?.ErrCode, result?.ErrMsg, request.ToUser);
return false;
}
@ -360,7 +374,7 @@ public class WeChatService : IWeChatService
}
catch (Exception ex)
{
_logger.LogError(ex, "发送订阅消息异常");
_logger.LogError(ex, "发送订阅消息异常: ToUser={ToUser}", request.ToUser);
return false;
}
}