Files
Web_BLSKafka_Server_Prod/UseSQLQueryData/Controllers/HiWeiController.cs
2025-11-21 08:48:01 +08:00

165 lines
5.6 KiB
C#

using System.Net;
using System.Text;
using Common;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NLog;
using RestSharp;
namespace UseSQLQueryData.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
public class HiWeiController : ControllerBase
{
public static string HiWeiKey = "HiWei_";
public static int ExpireTimer = 60 * 3;
public string? HiWeiAppId { get; set; }
public string? HiWeiAppSecret { get; set; }
public IConfiguration ConfigurationBuilder { get; set; }
public static Logger logger = NLog.LogManager.GetCurrentClassLogger();
public HiWeiController(IConfiguration config)
{
this.ConfigurationBuilder = config;
HiWeiAppId = config["HiWeiAppId"];
HiWeiAppSecret = config["HiWeiAppSecret"];
}
[HttpGet()]
//public ReturnInfo HiWeiOAuthRequest(string response_type, string client_id, string client_secret, string redirect_uri)
public ReturnInfo HiWeiOAuthRequest(string? response_type, string? client_id, string? client_secret, string? redirect_uri, string? code, string? refresh_token)
{
ReturnInfo returninfo = new ReturnInfo();
try
{
//code wijsv8hf5izejkny 6c1014dab24c41369068d12e4f7e145c http://tvos.hw-iot.com.cn/voice-service/token/auth/SKG9ZSC3|
logger.Error("Data:" + response_type + " " + client_id + " " + client_secret + " " + redirect_uri);
Random r = new Random();
int newcode = r.Next(1, 10000);
string id = HiWeiAppId;
string secret = HiWeiAppSecret;
string Key = HiWeiKey + newcode;
if (response_type.Equals("code") && client_id.Equals(id) && client_secret.Equals(secret))
{
var t1 = Guid.NewGuid().ToString();
var t2 = Guid.NewGuid().ToString();
var t3 = Guid.NewGuid().ToString();
var t4 = Guid.NewGuid().ToString();
var token = Convert.ToBase64String(Encoding.UTF8.GetBytes(t1 + "###" + t2));
var reftoken = Convert.ToBase64String(Encoding.UTF8.GetBytes(t3 + "###" + t4));
HiWeiToken g = new HiWeiToken();
g.access_token = token;
g.refresh_token = reftoken;
g.expires_in = new TimeSpan(20, 0, 0).TotalSeconds;
CSRedisCacheHelper.redis3.Set(Key, g, ExpireTimer);
CSRedisCacheHelper.redis3.Set(refresh_token, g, ExpireTimer);
var client1 = new RestClient(redirect_uri);
var request1 = new RestRequest("", Method.Get);
request1.AddQueryParameter("code", newcode);
var QQQ = client1.Execute(request1);
var content = QQQ.Content;
logger.Error("返回的数据为:" + content);
HttpStatusCode HHH = QQQ.StatusCode;
}
else
{
returninfo.isok = false;
returninfo.message = "没有授权";
}
Dictionary<string, int> codere = new Dictionary<string, int>();
codere.Add("code", newcode);
returninfo.isok = true;
returninfo.response = codere;
}
catch (Exception ex)
{
returninfo.isok = false;
returninfo.message = ex.Message;
}
return returninfo;
}
[HttpGet()]
public object mytoken(string code)
{
return "aaaaa";
}
[HttpPost()]
public object gettoken([FromBody] Data data)
{
//ReturnInfo re = new ReturnInfo();
try
{
var ttt = System.Text.Json.JsonSerializer.Serialize(data);
logger.Error("获取token:" + ttt);
string grant_type = data.grant_type;
string code = data.code;
if (grant_type.Equals("authorization_code"))
{
logger.Error("code: " + code);
string Key = HiWeiKey + code;
var hi = CSRedisCacheHelper.redis3.Get<HiWeiToken>(Key);
if (hi != null)
{
logger.Error("返回了");
return hi;
}
else
{
logger.Error("没有获取到");
return null;
}
}
else if (grant_type.Equals("refresh_token"))
{
var hi = CSRedisCacheHelper.redis3.Get<HiWeiToken>("11111232323");
if (hi != null)
{
return hi;
}
else
{
return hi;
}
}
return null;
}
catch (Exception ex)
{
return null;
}
}
}
public class Data
{
public string code { get; set; }
public string grant_type { get; set; }
public string client_secret { get; set; }
public string client_id { get; set; }
}
public class HiWeiToken
{
public string access_token { get; set; }
public string refresh_token { get; set; }
public double expires_in { get; set; }
}
}