You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
DS7/DSWeb/Areas/MvcShipping/Job/DingTalkTokenJob.cs

60 lines
2.1 KiB
C#

using DSWeb.Areas.Dispatch.Helper;
using DSWeb.Areas.MvcShipping.DB;
using log4net;
using Newtonsoft.Json;
using Quartz;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Runtime.Caching;
using System.Web;
namespace DSWeb.Areas.MvcShipping.Job
{
public class DingTalkTokenJob : IJob
{
public static string DingTalkAccessTokenStoreKey = "DingTalkToken";
ILog logger = LogManager.GetLogger("DingTalkTokenJob");
public void Execute(IJobExecutionContext context)
{
if (MemoryCache.Default.Contains(DingTalkAccessTokenStoreKey))
{
return;
}
DingTalkContext dingTalkContext = new DingTalkContext();
var pAppKey = dingTalkContext.ParamSets.FirstOrDefault(p => p.PARAMNAME == "DingTalkAppkey");
var pAppSecret = dingTalkContext.ParamSets.FirstOrDefault(p => p.PARAMNAME == "DingTalkAppSecret");
if (pAppKey == null || pAppSecret == null)
{
logger.Error("业务参数中不存在钉钉配置参数,请升级系统数据库");
return;
}
if (string.IsNullOrWhiteSpace(pAppKey.PARAMVALUE) || string.IsNullOrWhiteSpace(pAppSecret.PARAMVALUE))
{
logger.Error("业务参数中关于的钉钉配置参数有误,请正确配置后再试");
return;
}
string resp = WebRequestHelper.DoGet($"https://oapi.dingtalk.com/gettoken?appkey={pAppKey.PARAMVALUE}&appsecret={pAppSecret.PARAMVALUE}");
logger.Debug(resp);
var jsonObj = JsonConvert.DeserializeAnonymousType(resp, new
{
errcode = 0,
errmsg = "",
access_token = "",
expires_in = 0
});
if (jsonObj.errcode == 0)
{
MemoryCache.Default.Add(new CacheItem(DingTalkAccessTokenStoreKey, jsonObj.access_token), new CacheItemPolicy() { AbsoluteExpiration = new DateTimeOffset(DateTime.Now.AddSeconds(jsonObj.expires_in).AddMinutes(-5)) });
}
}
}
}