颐和api

TemplateMessageController.cs 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. // For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
  10. namespace WechatApi.Controllers
  11. {
  12. [ApiVersion("6.0")]
  13. [Produces("application/json")]
  14. [Route("api/[controller]")]
  15. public class TemplateMessageController : BaseController
  16. {
  17. private readonly string AppId;
  18. private readonly string AppSecret;
  19. private readonly string TemplateId;
  20. private readonly string MessageUrl;
  21. public TemplateMessageController(IConfiguration configuration)
  22. {
  23. AppId = configuration["WechatStatic:AppId"];
  24. AppSecret = configuration["WechatStatic:AppSecret"];
  25. TemplateId= configuration["WechatStatic:TemplateId"];
  26. MessageUrl = configuration["WechatStatic:MessageUrl"];
  27. }
  28. /// <summary>
  29. /// 发送模板消息【需要安全验证】
  30. /// </summary>
  31. /// <returns></returns>
  32. [HttpGet("testsend")]
  33. public async Task<IActionResult> TestSendAsync(string openid)
  34. {
  35. //var url = "http://www.baidu.com";
  36. //var first = "你好,设备故障了!";
  37. //var result = await TemplateApi.SendTemplateMessageAsync(
  38. //AppId,
  39. //"ogNym1eye-hVDHjF_laRuxgbli1g",
  40. //"-8mPKpKQp12wVgmi3767sIb73W0VqM2Oe8dkUUNFako",
  41. //url,
  42. // new
  43. // {
  44. // first = new { value = first, color = "#173177" },
  45. // performance = new { value = "测试地点", color = "#173177" },
  46. // time = new { value = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), color = "#173177" },
  47. // remark = new { value = string.IsNullOrWhiteSpace("") ? "请尽快处理!" : "", color = "#173177" },
  48. // });
  49. //return Success("OK", result);
  50. if (string.IsNullOrEmpty(openid)) { openid = "ogNym1TbRgKYcwJrin0qo2W_N2AM"; }
  51. var url = "http://www.baidu.com";
  52. var result = await TemplateApi.SendTemplateMessageAsync(AppId, openid, TemplateId, url,
  53. new
  54. {
  55. first = new { value = "测试发送模板消息!", color = "#173177" },
  56. keyword1 = new { value = DateTime.Now.ToString("yyyyMMddHHmmssfff"), color = "#173177" },
  57. keyword2 = new { value = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), color = "#173177" },
  58. remark = new { value = "测试消息内容!", color = "#173177" }
  59. });
  60. return Success("OK", result);
  61. }
  62. /// <summary>
  63. /// 发送模板消息【需要安全验证】
  64. /// </summary>
  65. /// <returns></returns>
  66. [HttpPost("sendtemplatemessage")]
  67. public async Task<IActionResult> SendTemplateMessageAsync(string openid, string url, string jsondata, string templateid)
  68. {
  69. if (string.IsNullOrEmpty(templateid))
  70. {
  71. templateid = TemplateId;
  72. }
  73. if (MessageUrl.IndexOf("?") != -1)
  74. {
  75. url = MessageUrl + "&" + url;
  76. }
  77. else
  78. {
  79. url = MessageUrl + "?" + url;
  80. }
  81. var result = await TemplateApi.SendTemplateMessageAsync(AppId, openid, templateid, url, jsondata.ToJObject());
  82. return Success("OK", result);
  83. }
  84. /// <summary>
  85. /// 获取已添加至帐号下所有模板列表
  86. /// </summary>
  87. /// <returns></returns>
  88. public async Task<IActionResult> GetListAsync()
  89. {
  90. var result = await TemplateApi.GetPrivateTemplateAsync(AppId);
  91. return Success("ok", result);
  92. }
  93. /// <summary>
  94. /// 获取所有模板列表
  95. /// </summary>
  96. /// <returns></returns>
  97. public async Task<IActionResult> AddTemplatesync(string templateno)
  98. {
  99. var result = await TemplateApi.AddtemplateAsync(AppId, templateno);
  100. return Success("ok", result);
  101. }
  102. /// <summary>
  103. /// 删除模板
  104. /// </summary>
  105. /// <returns></returns>
  106. public async Task<IActionResult> DelTemplatesync(string templateid)
  107. {
  108. var result = await TemplateApi.DelPrivateTemplateAsync(AppId, templateid);
  109. return Success("ok", result);
  110. }
  111. }
  112. }