62 lines
2.2 KiB
C#
62 lines
2.2 KiB
C#
using System;
|
|
using Newtonsoft.Json.Linq;
|
|
using Newtonsoft.Json;
|
|
using HZQR.Common;
|
|
using RedisHelp;
|
|
using System.Configuration;
|
|
|
|
namespace EShang.Common.GeneralMethod
|
|
{
|
|
/// <summary>
|
|
/// 安徽交控相关方法
|
|
/// </summary>
|
|
public class AHJKHelper
|
|
{
|
|
/// <summary>
|
|
/// 获取token
|
|
/// </summary>
|
|
/// <param name="tokenModel"></param>
|
|
/// <param name="redisHelper"></param>
|
|
/// <param name="failureStr">异常日志</param>
|
|
public static bool GetAHJKtoken(ref Model.tokenModel tokenModel, RedisHelper redisHelper, ref string failureStr)
|
|
{
|
|
//判断对应键值在缓存中是否存在,若存在直接从缓存取数据
|
|
if (redisHelper.KeyExists("AHJKtoken"))
|
|
{
|
|
//从缓存中取出token
|
|
string resultStr = redisHelper.StringGet("AHJKtoken");
|
|
tokenModel = JsonConvert.DeserializeObject<Model.tokenModel>(resultStr);
|
|
if (tokenModel.overdueTime > DateTime.Now)
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
//重新加载token
|
|
tokenModel.appKey = Config.AppSettings.AHJKappKey;
|
|
tokenModel.appSecret = Config.AppSettings.AHJKappSecret;
|
|
}
|
|
|
|
string result = HttpUtil.HttpUrlPost(JsonConvert.SerializeObject(tokenModel), Config.AppSettings.AHJKUrl +
|
|
"sys/auth/token/login?appKey=" + tokenModel.appKey + "&appSecret=" + tokenModel.appSecret, "application/json");
|
|
JObject keyValuePairs = JObject.Parse(result);
|
|
if (keyValuePairs["code"].ToString() == "0")
|
|
{
|
|
tokenModel.token = keyValuePairs["token"].ToString();
|
|
tokenModel.expire = keyValuePairs["expire"].TryParseToInt();
|
|
tokenModel.overdueTime = DateTime.Now.AddSeconds(tokenModel.expire);
|
|
//将token存入缓存中
|
|
redisHelper.StringSet("AHJKtoken", JsonConvert.SerializeObject(tokenModel));
|
|
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
failureStr = keyValuePairs["msg"].ToString();
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
}
|