From c3425b00fe4f1c2783536b30b216860a21d151dd Mon Sep 17 00:00:00 2001 From: hwh <332078369@qq.com> Date: 星期二, 18 二月 2025 08:10:12 +0800 Subject: [PATCH] 企业微信 --- Wms/Utility/Tools/WeComTools.cs | 165 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 165 insertions(+), 0 deletions(-) diff --git a/Wms/Utility/Tools/WeComTools.cs b/Wms/Utility/Tools/WeComTools.cs new file mode 100644 index 0000000..42b707d --- /dev/null +++ b/Wms/Utility/Tools/WeComTools.cs @@ -0,0 +1,165 @@ +锘縰sing Newtonsoft.Json; +using System; +using System.Collections; +using System.Collections.Generic; +using System.Text; +using Talk.Extensions; + +namespace Utility.Tools +{ + /// <summary> + /// 浼佷笟寰俊 + /// </summary> + public class WeComTools + { + private static string access_token = ""; + public WeComTools() + { + access_token = GetToken(); + } + + public string GetToken() + { + Dictionary<string, string> dic = new Dictionary<string, string>(); + dic.Add("corpid", WeComConfig.corpid); + dic.Add("corpsecret", WeComConfig.corpsecret); + var response = HttpHelper.DoGet("https://qyapi.weixin.qq.com/cgi-bin/gettoken", dic); + var modResponse = JsonConvert.DeserializeObject<TokenResponse>(response); + if (modResponse.errcode == 0) + { + return modResponse.access_token; + } + else + { + throw Oops.Bah(modResponse.errmsg); + } + } + public bool SendNewsMessage(string title, string content, string touser = "@all") + { + MessageRequest request = new MessageRequest() + { + touser = touser, + agentid = WeComConfig.agentid, + msgtype = "mpnews", + mpnews = new Mpnews() + { + articles = new List<ArticlesItem>() + { + new ArticlesItem() + { + title = title, + thumb_media_id = WeComConfig.thumb_media_id, + content = content + } + } + } + }; + var response = HttpHelper.DoPost("https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token="+ access_token, JsonConvert.SerializeObject(request), "WeCom", "WMS"); + var modResponse = JsonConvert.DeserializeObject<TokenResponse>(response); + if (modResponse.errcode == 0) + { + return true; + } + else + { + return false; + } + } + public bool SendTextMessage(string content, string touser = "@all") + { + MessageRequest request = new MessageRequest() + { + touser = touser, + agentid = WeComConfig.agentid, + msgtype = "text", + text = new Text() { content = content } + }; + var response = HttpHelper.DoPost("https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token="+ access_token, JsonConvert.SerializeObject(request), "WeCom", "WMS"); + var modResponse = JsonConvert.DeserializeObject<TokenResponse>(response); + if (modResponse.errcode == 0) + { + return true; + } + else + { + return false; + } + } + } + + public class WeComConfig + { + public static string corpid { get; set; } + public static int agentid { get; set; } + public static string corpsecret { get; set; } + public static string thumb_media_id { get; set; } + } + public class TokenResponse + { + /// <summary> + /// + /// </summary> + public int errcode { get; set; } + /// <summary> + /// + /// </summary> + public string errmsg { get; set; } + /// <summary> + /// + /// </summary> + public string access_token { get; set; } + /// <summary> + /// + /// </summary> + public int expires_in { get; set; } + } + + public class ArticlesItem + { + /// <summary> + /// 鏍囬 + /// </summary> + public string title { get; set; } + /// <summary> + /// + /// </summary> + public string thumb_media_id { get; set; } + /// <summary> + /// + /// </summary> + public string content { get; set; } + } + + public class Mpnews + { + /// <summary> + /// + /// </summary> + public List<ArticlesItem> articles { get; set; } + } + public class Text + { + public string content { get; set; } + } + public class MessageRequest + { + /// <summary> + /// + /// </summary> + public string touser { get; set; } + /// <summary> + /// + /// </summary> + public string msgtype { get; set; } = "mpnews"; + /// <summary> + /// + /// </summary> + public int agentid { get; set; } + /// <summary> + /// + /// </summary> + public Mpnews mpnews { get; set; } + public Text text { get; set; } + } + +} -- Gitblit v1.8.0