颐和api

SendMessageController.cs 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using MadRunFabric.Common;
  6. using Microsoft.AspNetCore.Mvc;
  7. using Microsoft.Extensions.Configuration;
  8. using Senparc.Weixin.MP.AdvancedAPIs;
  9. using Senparc.Weixin.MP.Containers;
  10. using Senparc.Weixin.MP.Entities;
  11. namespace WechatApi.Controllers
  12. {
  13. [ApiVersion("6.0")]
  14. [Produces("application/json")]
  15. [Route("api/[controller]")]
  16. public class SendMessageController : BaseController
  17. {
  18. private readonly string AppId;
  19. private readonly string AppSecret;
  20. private readonly string MessageUrl;
  21. public SendMessageController(IConfiguration configuration)
  22. {
  23. AppId = configuration["WechatStatic:AppId"];
  24. AppSecret = configuration["WechatStatic:AppSecret"];
  25. MessageUrl = configuration["WechatStatic:MessageUrl"];
  26. }
  27. /// <summary>
  28. /// 发送模板消息【需要安全验证】
  29. /// </summary>
  30. /// <returns></returns>
  31. [HttpGet("testsend")]
  32. public async Task<IActionResult> TestSendAsync(string openid)
  33. {
  34. List<Article> articles = new List<Article>();
  35. Article ae = new Article();
  36. ae.Title = "标题";
  37. ae.Url = "http://www.baidu.com";
  38. ae.Description = "内容";
  39. articles.Add(ae);
  40. var accessToken = AccessTokenContainer.GetAccessToken(AppId);
  41. var result = await CustomApi.SendNewsAsync(accessToken, openid, articles);
  42. return Success("OK", result);
  43. }
  44. /// <summary>
  45. /// 发送模板消息【需要安全验证】
  46. /// </summary>
  47. /// <returns></returns>
  48. [HttpPost("sendmessage")]
  49. public async Task<IActionResult> SendMessageAsync(string openid, string title, string content,string url)
  50. {
  51. if (MessageUrl.IndexOf("?") != -1)
  52. {
  53. url = MessageUrl + "&" + url;
  54. }
  55. else
  56. {
  57. url = MessageUrl + "?" + url;
  58. }
  59. List<Article> articles = new List<Article>();
  60. Article ae = new Article();
  61. ae.Title = title;
  62. ae.Url = url;
  63. ae.Description = content;
  64. articles.Add(ae);
  65. var accessToken = AccessTokenContainer.GetAccessToken(AppId);
  66. var result = await CustomApi.SendNewsAsync(accessToken, openid, articles);
  67. return Success("OK", result);
  68. }
  69. }
  70. }