WxHelper.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using Infrastructure;
  2. using Infrastructure.Extensions;
  3. using Newtonsoft.Json;
  4. using System;
  5. using System.Security.Cryptography;
  6. using System.Text;
  7. using ZR.Common.Model;
  8. namespace ZR.Common
  9. {
  10. public class WxHelper
  11. {
  12. private static readonly string GetTokenUrl = "https://api.weixin.qq.com/cgi-bin/token";
  13. private static readonly string GetTicketUrl = "https://api.weixin.qq.com/cgi-bin/ticket/getticket";
  14. private static readonly string AppID = AppSettings.App(new string[] { "WxOpen", "AppID" });
  15. private static readonly string AppSECRET = AppSettings.App(new string[] { "WxOpen", "AppSecret" });
  16. /// <summary>
  17. /// 获取访问token
  18. /// </summary>
  19. /// <returns>
  20. /// {"errcode":0,"errmsg":"ok","access_token":"iCbcfE1OjfRhV0_io-CzqTNC0lnrudeW3oF5rhJKfmINaxLClLa1FoqAY_wEXtodYh_DTnrtAwZfzeb-NRXvwiOoqUTHx3i6QKLYcfBtF8y-xd5mvaeaf3e9mvTAPhmX0lkm1cLTwRLmoa1IwzgQ-QZEZcuIcntWdEMGseVYok3BwCGpC87bt6nNdgnekZdFVRp1uuaxoctDGlXpoQlQsA","expires_in":7200}
  21. /// </returns>
  22. private static WxTokenResult GetAccessToken()
  23. {
  24. if (AppID.IsEmpty() || AppSECRET.IsEmpty())
  25. {
  26. Console.WriteLine("公众号配置错误");
  27. throw new ArgumentException("公众号配置错误");
  28. };
  29. var Ck = "wx_token";
  30. string getTokenUrl = $"{GetTokenUrl}?grant_type=client_credential&appid={AppID}&secret={AppSECRET}";
  31. if (CacheHelper.Get(Ck) is WxTokenResult tokenResult)
  32. {
  33. return tokenResult;
  34. }
  35. else
  36. {
  37. string result = HttpHelper.HttpGet(getTokenUrl);
  38. tokenResult = JsonConvert.DeserializeObject<WxTokenResult>(result);
  39. if (tokenResult?.errcode == 0)
  40. {
  41. CacheHelper.SetCache(Ck, tokenResult, 110);
  42. }
  43. else
  44. {
  45. Console.WriteLine("GetAccessToken失败,结果=" + result);
  46. throw new Exception("获取AccessToken失败");
  47. }
  48. }
  49. return tokenResult;
  50. }
  51. /// <summary>
  52. /// 获取ticket
  53. /// </summary>
  54. /// <returns></returns>
  55. public static WxTokenResult GetTicket()
  56. {
  57. WxTokenResult token = GetAccessToken();
  58. string ticket = token?.access_token;
  59. var Ck = "wx_ticket";
  60. string getTokenUrl = $"{GetTicketUrl}?access_token={ticket}&type=jsapi";
  61. if (CacheHelper.Get(Ck) is WxTokenResult tokenResult)
  62. {
  63. return tokenResult;
  64. }
  65. else
  66. {
  67. string result = HttpHelper.HttpGet(getTokenUrl);
  68. tokenResult = JsonConvert.DeserializeObject<WxTokenResult>(result);
  69. if (tokenResult?.errcode == 0)
  70. {
  71. CacheHelper.SetCache(Ck, tokenResult, 110);
  72. }
  73. else
  74. {
  75. Console.WriteLine("GetTicket,结果=" + result);
  76. throw new Exception("获取ticket失败");
  77. }
  78. }
  79. return tokenResult;
  80. }
  81. /// <summary>
  82. /// 返回正确的签名
  83. /// </summary>
  84. /// <param name="jsapi_ticket"></param>
  85. /// <param name="timestamp"></param>
  86. /// <param name="noncestr"></param>
  87. /// <param name="url"></param>
  88. /// <returns></returns>
  89. public static string GetSignature(string jsapi_ticket, string timestamp, string noncestr, string url = null)
  90. {
  91. if (string.IsNullOrEmpty(jsapi_ticket) || string.IsNullOrEmpty(noncestr) || string.IsNullOrEmpty(timestamp) || string.IsNullOrEmpty(url))
  92. return null;
  93. //将字段添加到列表中。
  94. string[] arr = new[]
  95. {
  96. string.Format("jsapi_ticket={0}",jsapi_ticket),
  97. string.Format("noncestr={0}",noncestr),
  98. string.Format("timestamp={0}",timestamp),
  99. string.Format("url={0}",url)
  100. };
  101. //字典排序
  102. Array.Sort(arr);
  103. //使用URL键值对的格式拼接成字符串
  104. var temp = string.Join("&", arr);
  105. var sha1Arr = SHA1.HashData(Encoding.UTF8.GetBytes(temp));
  106. return BitConverter.ToString(sha1Arr).Replace("-", "").ToLower();
  107. }
  108. }
  109. }