| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- using DingTalk.Api;
- using DingTalk.Api.Request;
- using DingTalk.Api.Response;
- using System;
- using System.Collections.Generic;
- using System.Configuration;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Web;
- using System.Web.Caching;
- namespace HH.YiDaSyncNC.Helper
- {
- class CommHelper
- {
- private static readonly string appKey = ConfigurationManager.AppSettings["AppKey"];
- private static readonly string appSecret = ConfigurationManager.AppSettings["AppSecret"];
- /// <summary>
- /// 时间转时间戳(秒)
- /// </summary>
- /// <param name="date">时间</param>
- /// <returns></returns>
- public static long GetTimeStamp(DateTime date)
- {
- //var unix = date.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, 0);
- //TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
- //return Convert.ToInt64(unix.TotalSeconds * 1000);
- var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
- return Convert.ToInt64((date.ToUniversalTime() - epoch).TotalSeconds);
- }
- /// <summary>
- /// 时间转时间戳(毫秒)
- /// </summary>
- /// <param name="date">日期</param>
- /// <returns></returns>
- public static long GetTimeStamps(DateTime date)
- {
- var unix = date.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, 0);
- TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
- return Convert.ToInt64(unix.TotalSeconds * 1000);
- }
- /// <summary>
- /// 时间戳转时间(毫秒)
- /// </summary>
- /// <param name="timestamp"></param>
- /// <returns></returns>
- public static DateTime GetDateTime(long timestamp)
- {
- System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));//当地时区
- var time = startTime.AddMilliseconds(timestamp);
- return time;
- }
- #region 获取AccessToken
- public static string GetAccessToken()
- {
- // 将AccessToken存储至缓存
- SetAccessToken(new CacheItemRemovedReason());
- // 输出缓存中存储的AccessToken
- return HttpRuntime.Cache.Get("Dingtalk_AccessToken").ToString();
- }
- #endregion
- #region 设置AccessToken(企业内部开发)
- public static void SetAccessToken(CacheItemRemovedReason reason)
- {
- //企业内部开发
- var accessToken = HttpRuntime.Cache.Get("Dingtalk_AccessToken");
- if (accessToken == null)
- {
- //var time = GetTimeStamp(DateTime.Now);
- //var CorpId = corpid;
- //var AgentId = agentid;
- //var CustomKey = customkey;
- //var CustomSecret = customsecret;
- //string stringToSign = time+"\n"+"suiteTicket";
- //var signature = getSHA256(CustomSecret, stringToSign);
- //DefaultDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/service/get_corp_token");
- //OapiServiceGetCorpTokenRequest req = new OapiServiceGetCorpTokenRequest();
- //req.AuthCorpid = CorpId;
- //OapiServiceGetCorpTokenResponse response = client.Execute(req, CustomKey, CustomSecret, "suiteTicket");
- //HttpRuntime.Cache.Insert("Dingtalk_AccessToken", response.AccessToken, null, DateTime.Now.AddSeconds(3600), Cache.NoSlidingExpiration);
- IDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
- OapiGettokenRequest request = new OapiGettokenRequest();
- request.Appkey = appKey;
- request.Appsecret = appSecret;
- request.SetHttpMethod("GET");
- OapiGettokenResponse response = client.Execute(request);
- HttpRuntime.Cache.Insert("Dingtalk_AccessToken", response.AccessToken, null, DateTime.Now.AddSeconds(3600), Cache.NoSlidingExpiration);
- }
- }
- #endregion
- }
- }
|