.NET CORE3.1实现微信小程序发送订阅消息

一、appsettings.json定义小程序配置信息

"WX": {   "AppId": "wx88822730803edd44",   "AppSecret": "75b269042e8b5026e6ed14aa24ba9353",   "Templates": {   "Audit": {     "TemplateId": "aBaIjTsPBluYtj2tzotzpowsDDBGLhXQkwrScupnQsM",     "PageUrl": "/pages/index/formAudit?formId={0}&tableId={1}",     "MiniprogramState": "developer",     "Lang": "zh_TW",     "Data": {         "Title": "thing6",         "Content": "thing19",         "Date": "date9"       }     }   },   "SignatureToken": "aaaaaa",   "MessageSendUrl": "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token={0}",   "AccessTokenUrl": "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}" }

二、编写通用类加载配置

using System; using System.Text; using System.Security.Cryptography; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration.Json; namespace WXERP.Services {   /// <summary>   /// 项目公有静态类   /// </summary>   public class Common   {     /// <summary>     /// 獲取根目錄     /// </summary>     public static string AppRoot => Environment.CurrentDirectory;// AppContext.BaseDirectory;     /// <summary>     /// 獲取項目配置     /// </summary>     public static IConfiguration Configuration { get; set; }     /// <summary>     /// 加載項目配置     /// </summary>     static Common()     {       Configuration = new ConfigurationBuilder()       .Add(new JsonConfigurationSource       {         Path = "appsettings.json",         ReloadOnChange = true //当appsettings.json被修改时重新加载       })       .Build();     }     /// <summary>     /// SHA1加密     /// </summary>     /// <param>需要加密的字符串</param>     /// <returns>返回40位大寫字符串</returns>     public static string SHA1(string content)     {       try       {         SHA1 sha1 = new SHA1CryptoServiceProvider();         byte[] bytes_in = Encoding.UTF8.GetBytes(content);         byte[] bytes_out = sha1.ComputeHash(bytes_in);         sha1.Dispose();         string result = BitConverter.ToString(bytes_out);         result = result.Replace("-", "");         return result;       }       catch (Exception ex)       {         throw new Exception("Error in SHA1: " + ex.Message);       }     }   } }

三、编写HttpHelper请求类

using System; using System.Text; using System.Net.Http; using System.Net.Http.Headers; using System.Threading.Tasks; using System.Collections.Generic; namespace WXERP.Services {   /// <summary>   /// HTTP請求輔助類   /// </summary>   public class HttpHelper   {     /// <summary>     /// post同步請求     /// </summary>     /// <param>地址</param>     /// <param>數據</param>     /// <param>application/xml、application/json、application/text、application/x-www-form-urlencoded</param>     /// <param>請求頭</param>     /// <returns></returns>     public static string HttpPost(string url, string postData = null, string contentType = null, Dictionary<string, string> headers = null)     {       using HttpClient client = new HttpClient();       if (headers != null)       {         foreach (var header in headers)         client.DefaultRequestHeaders.Add(header.Key, header.Value);       }       postData ??= "";       using HttpContent httpContent = new StringContent(postData, Encoding.UTF8);       if (contentType != null)       httpContent.Headers.ContentType = new MediaTypeHeaderValue(contentType);       HttpResponseMessage response = client.PostAsync(url, httpContent).Result;       return response.Content.ReadAsStringAsync().Result;     }     /// <summary>     /// post異步請求     /// </summary>     /// <param>地址</param>     /// <param>數據</param>     /// <param>application/xml、application/json、application/text、application/x-www-form-urlencoded</param>     /// <param>請求超時時間</param>     /// <param>請求頭</param>     /// <returns></returns>     public static async Task<string> HttpPostAsync(string url, string postData = null, string contentType = null, int timeOut = 30, Dictionary<string, string> headers = null)     {       using HttpClient client = new HttpClient();       client.Timeout = new TimeSpan(0, 0, timeOut);       if (headers != null)       {         foreach (var header in headers)         client.DefaultRequestHeaders.Add(header.Key, header.Value);       }       postData ??= "";       using HttpContent httpContent = new StringContent(postData, Encoding.UTF8);       if (contentType != null)         httpContent.Headers.ContentType = new MediaTypeHeaderValue(contentType);       HttpResponseMessage response = await client.PostAsync(url, httpContent);       return await response.Content.ReadAsStringAsync();     }     /// <summary>     /// get同步請求     /// </summary>     /// <param>地址</param>     /// <param>請求頭</param>     /// <returns></returns>     public static string HttpGet(string url, Dictionary<string, string> headers = null)     {       using HttpClient client = new HttpClient();       if (headers != null)       {         foreach (var header in headers)         client.DefaultRequestHeaders.Add(header.Key, header.Value);       }       HttpResponseMessage response = client.GetAsync(url).Result;       return response.Content.ReadAsStringAsync().Result;     }     /// <summary>     /// get異步請求     /// </summary>     /// <param></param>     /// <param></param>     /// <returns></returns>     public static async Task<string> HttpGetAsync(string url, Dictionary<string, string> headers = null)     {       using HttpClient client = new HttpClient();       if (headers != null)       {         foreach (var header in headers)         client.DefaultRequestHeaders.Add(header.Key, header.Value);       }       HttpResponseMessage response = await client.GetAsync(url);       return await response.Content.ReadAsStringAsync();     }   } }

四、在sqlserver下存储并获取openid,这个主要是因为提交消息并不是在微信小程序端,如果是在微信小程序上发起订阅消息,可以忽略这个步骤

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/4fcdba8fbd80227d9ab75d0286a96833.html