using learun.util;
using System;
using System.Text;
using System.Threading.Tasks;
namespace learun.wechat
{
///
/// 版 本 EasyCode EC管理后台
/// Copyright (c) 2019-present EC管理有限公司
/// 创建人:tobin
/// 日 期:2019.11.06
/// 描 述:Token
///
public class Token
{
///
///
///
public static Token _Token;
///
///
///
public string access_token { get; set; }
///
///
///
public int expires_in { get; set; }
private DateTime createTokenTime = DateTime.Now;
///
/// 到期时间(防止时间差,提前1分钟到期)
///
///
public DateTime TookenOverdueTime
{
get { return createTokenTime.AddSeconds(expires_in - 60); }
}
///
/// 刷新Token
///
public static async Task Renovate()
{
if (_Token == null)
{
await GetNewToken();
}
Token._Token.createTokenTime = DateTime.Now;
}
///
///
///
///
public static async Task IsTimeOut()
{
if (_Token == null)
{
await GetNewToken();
}
return DateTime.Now >= Token._Token.TookenOverdueTime;
}
///
///
///
///
public static async Task GetNewToken()
{
string strulr = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={0}&corpsecret={1}";
string corpID = ConfigHelper.GetConfig().CorpId; //企业ID
string Secret = ConfigHelper.GetConfig().CorpSecret;//管理员组ID
HttpHelper http = new HttpHelper();
string respone = await http.Get(string.Format(strulr, corpID, Secret), Encoding.UTF8);
var token = respone.ToObject();
Token._Token = token;
return token;
}
///
///
///
///
public static async Task GetToken()
{
if (_Token == null)
{
await GetNewToken();
}
return _Token.access_token;
}
}
}