CommHelper.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using DingTalk.Api;
  2. using DingTalk.Api.Request;
  3. using DingTalk.Api.Response;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Configuration;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Web;
  11. using System.Web.Caching;
  12. namespace HH.YiDaSyncNC.Helper
  13. {
  14. class CommHelper
  15. {
  16. private static readonly string appKey = ConfigurationManager.AppSettings["AppKey"];
  17. private static readonly string appSecret = ConfigurationManager.AppSettings["AppSecret"];
  18. /// <summary>
  19. /// 时间转时间戳(秒)
  20. /// </summary>
  21. /// <param name="date">时间</param>
  22. /// <returns></returns>
  23. public static long GetTimeStamp(DateTime date)
  24. {
  25. //var unix = date.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, 0);
  26. //TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
  27. //return Convert.ToInt64(unix.TotalSeconds * 1000);
  28. var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
  29. return Convert.ToInt64((date.ToUniversalTime() - epoch).TotalSeconds);
  30. }
  31. /// <summary>
  32. /// 时间转时间戳(毫秒)
  33. /// </summary>
  34. /// <param name="date">日期</param>
  35. /// <returns></returns>
  36. public static long GetTimeStamps(DateTime date)
  37. {
  38. var unix = date.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, 0);
  39. TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
  40. return Convert.ToInt64(unix.TotalSeconds * 1000);
  41. }
  42. /// <summary>
  43. /// 时间戳转时间(毫秒)
  44. /// </summary>
  45. /// <param name="timestamp"></param>
  46. /// <returns></returns>
  47. public static DateTime GetDateTime(long timestamp)
  48. {
  49. System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));//当地时区
  50. var time = startTime.AddMilliseconds(timestamp);
  51. return time;
  52. }
  53. #region 获取AccessToken
  54. public static string GetAccessToken()
  55. {
  56. // 将AccessToken存储至缓存
  57. SetAccessToken(new CacheItemRemovedReason());
  58. // 输出缓存中存储的AccessToken
  59. return HttpRuntime.Cache.Get("Dingtalk_AccessToken").ToString();
  60. }
  61. #endregion
  62. #region 设置AccessToken(企业内部开发)
  63. public static void SetAccessToken(CacheItemRemovedReason reason)
  64. {
  65. //企业内部开发
  66. var accessToken = HttpRuntime.Cache.Get("Dingtalk_AccessToken");
  67. if (accessToken == null)
  68. {
  69. //var time = GetTimeStamp(DateTime.Now);
  70. //var CorpId = corpid;
  71. //var AgentId = agentid;
  72. //var CustomKey = customkey;
  73. //var CustomSecret = customsecret;
  74. //string stringToSign = time+"\n"+"suiteTicket";
  75. //var signature = getSHA256(CustomSecret, stringToSign);
  76. //DefaultDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/service/get_corp_token");
  77. //OapiServiceGetCorpTokenRequest req = new OapiServiceGetCorpTokenRequest();
  78. //req.AuthCorpid = CorpId;
  79. //OapiServiceGetCorpTokenResponse response = client.Execute(req, CustomKey, CustomSecret, "suiteTicket");
  80. //HttpRuntime.Cache.Insert("Dingtalk_AccessToken", response.AccessToken, null, DateTime.Now.AddSeconds(3600), Cache.NoSlidingExpiration);
  81. IDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
  82. OapiGettokenRequest request = new OapiGettokenRequest();
  83. request.Appkey = appKey;
  84. request.Appsecret = appSecret;
  85. request.SetHttpMethod("GET");
  86. OapiGettokenResponse response = client.Execute(request);
  87. HttpRuntime.Cache.Insert("Dingtalk_AccessToken", response.AccessToken, null, DateTime.Now.AddSeconds(3600), Cache.NoSlidingExpiration);
  88. }
  89. }
  90. #endregion
  91. }
  92. }