using System.Net.Http; using System.Net.Http.Json; namespace Admin.NET.Plugin.ApprovalFlow.Service; /// /// 企业微信推送(P4-16 - 首版 Webhook 机制) /// 通过配置的群机器人 Webhook URL(含 key)推送文本消息。 /// 不做企微应用消息(需要 WeixinId 与 AgentId,留待后续 P5 补齐 SysUser.WeixinId 字段)。 /// public class WorkWeixinNotifyPusher : INotifyPusher, ITransient { public string Channel => "WorkWeixin"; private static readonly HttpClient _httpClient = new() { Timeout = TimeSpan.FromSeconds(5) }; public bool IsEnabled(NotifyChannelConfig cfg) => cfg?.WorkWeixin == true && !string.IsNullOrWhiteSpace(cfg.WorkWeixinWebhookUrl); public async Task PushAsync(List userIds, FlowNotification notification) { var cfg = App.GetConfig("ApprovalFlow:Notify"); var url = cfg?.WorkWeixinWebhookUrl; if (string.IsNullOrWhiteSpace(url)) return FlowNotifyPushResult.Ok(0); var content = $"【{notification.Title}】\n{notification.Content}\n实例: {notification.InstanceId}"; var payload = new { msgtype = "text", text = new { content } }; try { var resp = await _httpClient.PostAsJsonAsync(url, payload); var body = await resp.Content.ReadAsStringAsync(); if (!resp.IsSuccessStatusCode) return FlowNotifyPushResult.Fail($"HTTP {(int)resp.StatusCode}: {body}"); if (body.Contains("\"errcode\":0") || body.Contains("errcode\": 0")) return FlowNotifyPushResult.Ok(userIds.Count); return FlowNotifyPushResult.Fail($"WorkWeixin resp: {body}"); } catch (Exception ex) { return FlowNotifyPushResult.Fail(ex.Message); } } }