You've already forked taptap2024_GJ_chidouren
100 lines
3.9 KiB
C#
100 lines
3.9 KiB
C#
using Framework.NetWork.ToffeeHttp;
|
|
using ToffeeHttp.Runtime.Core;
|
|
|
|
namespace System.OfflineSystem
|
|
{
|
|
public class OfflineTimeLoader
|
|
{
|
|
public const string TargetAPI = "https://api.m.taobao.com/rest/api3.do?api=mtop.common.getTimestamp"; //淘宝时间获取接口
|
|
public const string TargetAPI2 = "https://quan.suning.com/getSysTime.do"; //苏宁时间获取接口
|
|
public const string TargetAPI3 = "https://pikagame.cn/index.php/app/user/getGtmnqConf"; //超神网络时间获取接口
|
|
|
|
private Action<long> _timeCallback;
|
|
private bool _useLocalTime;
|
|
|
|
/// <summary>
|
|
/// 获取时间
|
|
/// </summary>
|
|
/// <param name="timeCallback"></param>
|
|
/// <param name="useLocalTime">在无法请求所有网络时间的情况下使用系统时间</param>
|
|
public void LoadTime(Action<long> timeCallback, bool useLocalTime = true)
|
|
{
|
|
this._timeCallback = timeCallback;
|
|
this._useLocalTime = useLocalTime;
|
|
// HttpClient.Instance.QuickHttpSend(TargetAPI3, new HttpRequestData(HttpRequestType.GET), OnResponseAPI3, 1);
|
|
HttpClient.Instance.QuickHttpSend(TargetAPI, new HttpRequestData(HttpRequestType.GET), OnResponseAPI1, 1);
|
|
}
|
|
|
|
private void OnResponseAPI3(HttpResponse response)
|
|
{
|
|
var callback = this._timeCallback;
|
|
|
|
if (!response.IsNetError && response.Code == 200)
|
|
{
|
|
this._timeCallback = null;
|
|
var jsonObject = response.ObjectData;
|
|
var key = "GTMNQ_TIMESTAMP";
|
|
var timeData = jsonObject != null && jsonObject.ContainsKey(key) ? jsonObject.GetObject<string>(key) : null;
|
|
if (timeData != null)
|
|
{
|
|
if (long.TryParse(timeData , out var times))
|
|
{
|
|
callback?.Invoke(times * 1000);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
HttpClient.Instance.QuickHttpSend(TargetAPI, new HttpRequestData(HttpRequestType.GET), OnResponseAPI1, 2);
|
|
}
|
|
|
|
private void OnResponseAPI1(HttpResponse response)
|
|
{
|
|
var callback = this._timeCallback;
|
|
|
|
if (!response.IsNetError)
|
|
{
|
|
this._timeCallback = null;
|
|
var jsonObject = response.ObjectData;
|
|
var key = "t";
|
|
var timeData = jsonObject != null && jsonObject.ContainsKey(key) ? jsonObject.GetObject<string>(key) : null;
|
|
if (timeData != null)
|
|
{
|
|
if (long.TryParse(timeData , out var times))
|
|
{
|
|
callback?.Invoke(times);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
HttpClient.Instance.QuickHttpSend(TargetAPI2, new HttpRequestData(HttpRequestType.GET), OnResponseAPI2, 2);
|
|
}
|
|
|
|
private void OnResponseAPI2(HttpResponse response)
|
|
{
|
|
var callback = this._timeCallback;
|
|
this._timeCallback = null;
|
|
|
|
if (!response.IsNetError)
|
|
{
|
|
var key = "sysTime2";
|
|
var timeData = response.JsonObject.ContainsKey(key) ? response.JsonObject.GetObject<string>(key) : null;
|
|
if (timeData != null)
|
|
{
|
|
if (DateTime.TryParse(timeData , out var dateTime))
|
|
{
|
|
callback?.Invoke(ToTimestamp(dateTime));
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
callback?.Invoke(this._useLocalTime ? ToTimestamp(DateTime.Now) : 0);
|
|
}
|
|
|
|
private long ToTimestamp(DateTime dateTime)
|
|
{
|
|
TimeSpan ts = dateTime - new DateTime(1970, 1, 1, 0, 0, 0, 0);
|
|
return Convert.ToInt64(ts.TotalMilliseconds);
|
|
}
|
|
}
|
|
} |