89 lines
2.2 KiB
C#
89 lines
2.2 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using Learun.Util;
|
||
|
||
namespace Learun.Application.WeChat
|
||
{
|
||
public class Token
|
||
{
|
||
public static Token _Token;
|
||
|
||
public string access_token { get; set; }
|
||
|
||
public int expires_in { get; set; }
|
||
|
||
private DateTime createTokenTime = DateTime.Now;
|
||
|
||
/// <summary>
|
||
/// 到期时间(防止时间差,提前1分钟到期)
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public DateTime TookenOverdueTime
|
||
{
|
||
get { return createTokenTime.AddSeconds(expires_in - 60); }
|
||
}
|
||
|
||
/// <summary>
|
||
/// 刷新Token
|
||
/// </summary>
|
||
public static void Renovate()
|
||
{
|
||
if (_Token == null)
|
||
{
|
||
GetNewToken();
|
||
}
|
||
|
||
Token._Token.createTokenTime = DateTime.Now;
|
||
}
|
||
|
||
public static bool IsTimeOut()
|
||
{
|
||
if (_Token == null)
|
||
{
|
||
GetNewToken();
|
||
}
|
||
|
||
return DateTime.Now >= Token._Token.TookenOverdueTime;
|
||
}
|
||
|
||
public static Token GetNewToken(string tokenType = "sync")
|
||
{
|
||
string strulr = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={0}&corpsecret={1}";
|
||
|
||
string corpID = Config.GetValue("CorpId"); //企业ID
|
||
string Secret = string.Empty;//管理员组密钥
|
||
|
||
switch (tokenType)
|
||
{
|
||
case "sync":
|
||
Secret = Config.GetValue("CorpSecret");
|
||
break;
|
||
case "msg":
|
||
Secret = Config.GetValue("AppSecret");
|
||
break;
|
||
default:
|
||
break;
|
||
}
|
||
|
||
HttpHelper http = new HttpHelper();
|
||
|
||
string respone = http.Get(string.Format(strulr, corpID, Secret), Encoding.UTF8);
|
||
|
||
var token = respone.ToObject<Token>();
|
||
|
||
Token._Token = token;
|
||
|
||
return token;
|
||
}
|
||
|
||
public static string GetToken(string tokenType = "sync")
|
||
{
|
||
GetNewToken(tokenType);
|
||
return _Token.access_token;
|
||
}
|
||
}
|
||
}
|