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.

59 lines
2.2 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 WechatWorkTokenJob : IJob
{
public static string WorkWeixinAccessTokenStoreKey = "WorkWeixinAccessToken";
ILog logger = LogManager.GetLogger("WechatWorkTokenJob");
public void Execute(IJobExecutionContext context)
{
if (MemoryCache.Default.Contains(WorkWeixinAccessTokenStoreKey))
{
return;
}
RunBillContext runBillContext = new RunBillContext();
var pCorpid = runBillContext.ParamSets.FirstOrDefault(p => p.PARAMNAME == "WorkWeixinCorpid");
var pCorpsecret = runBillContext.ParamSets.FirstOrDefault(p => p.PARAMNAME == "WorkWeixinCorpsecret");
if (pCorpid == null || pCorpsecret == null)
{
logger.Error("业务参数中不存在微信企业号配置参数,请升级系统数据库");
return;
}
if (string.IsNullOrWhiteSpace(pCorpid.PARAMVALUE) || string.IsNullOrWhiteSpace(pCorpsecret.PARAMVALUE))
{
logger.Error("业务参数中关于的微信企业号配置参数有误,请正确配置后再试");
return;
}
string resp = WebRequestHelper.DoGet($"https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={pCorpid.PARAMVALUE}&corpsecret={pCorpsecret.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(WorkWeixinAccessTokenStoreKey, jsonObj.access_token), new CacheItemPolicy() { AbsoluteExpiration = new DateTimeOffset(DateTime.Now.AddSeconds(jsonObj.expires_in).AddMinutes(-5)) });
}
}
}
}