using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using WXERP.Services; namespace WXERP.Controllers { /// <summary> /// 消息控制器 /// </summary> [Route("api/[controller]")] [ApiController] public class MessageController : ControllerBase { private readonly MessageContext _context; /// <summary> /// 初始化消息 /// </summary> public MessageController() { _context = new MessageContext(); } /// <summary>微信消息</summary> /// <remarks>驗證消息來自於微信服務器</remarks> /// <param>微信加密簽名,signature結合了開發者填寫的token、timestamp、nonce</param> /// <param>時間戳</param> /// <param>隨機數</param> /// <param>隨機字符串</param> /// <returns></returns> [HttpGet("checkSignature")] [AllowAnonymous] public async void CheckSignature(string signature,string timestamp,string nonce,string echostr) { bool result = await _context.CheckSignature(signature, timestamp, nonce); if (result) { HttpContext.Response.ContentType = "text/plain; charset=utf-8"; await HttpContext.Response.WriteAsync(echostr); } else { HttpContext.Response.StatusCode = 409; HttpContext.Response.ContentType = "text/plain; charset=utf-8"; await HttpContext.Response.WriteAsync("error"); } } } }
九、调用小程序订阅消息,需要自己实现其他逻辑