選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

80 行
1.9 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Learun.Util;
  7. namespace Learun.Application.WeChat
  8. {
  9. public class Token
  10. {
  11. public static Token _Token;
  12. public string access_token { get; set; }
  13. public int expires_in { get; set; }
  14. private DateTime createTokenTime = DateTime.Now;
  15. /// <summary>
  16. /// 到期时间(防止时间差,提前1分钟到期)
  17. /// </summary>
  18. /// <returns></returns>
  19. public DateTime TookenOverdueTime
  20. {
  21. get { return createTokenTime.AddSeconds(expires_in - 60); }
  22. }
  23. /// <summary>
  24. /// 刷新Token
  25. /// </summary>
  26. public static void Renovate()
  27. {
  28. if (_Token == null)
  29. {
  30. GetNewToken();
  31. }
  32. Token._Token.createTokenTime = DateTime.Now;
  33. }
  34. public static bool IsTimeOut()
  35. {
  36. if (_Token == null)
  37. {
  38. GetNewToken();
  39. }
  40. return DateTime.Now >= Token._Token.TookenOverdueTime;
  41. }
  42. public static Token GetNewToken()
  43. {
  44. string strulr = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={0}&corpsecret={1}";
  45. string corpID = Config.GetValue("CorpId"); //企业ID
  46. string Secret = Config.GetValue("CorpSecret");//管理员组ID
  47. HttpHelper http = new HttpHelper();
  48. string respone = http.Get(string.Format(strulr, corpID, Secret), Encoding.UTF8);
  49. var token = respone.ToObject<Token>();
  50. Token._Token = token;
  51. return token;
  52. }
  53. public static string GetToken()
  54. {
  55. if (_Token == null)
  56. {
  57. GetNewToken();
  58. }
  59. return _Token.access_token;
  60. }
  61. }
  62. }