You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

206 lines
7.1 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using LitJson;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Security.Cryptography;
using System.Text;
namespace AutoUpdater
{
public class OpenApiUtils
{
private static object _lock = new object();
private static OpenApiUtils _instance = null;
private const string OPEN_URL = "{0}://{1}/openApi/open/api/extend";
public static OpenApiUtils Instance
{
get
{
if (_instance == null)
{
lock (_lock)
{
_instance = new OpenApiUtils();
if (_instance.Urls == null)
{
_instance.Urls = new List<ServerUrls>();
}
//可用服务
JsonData servers = null;
servers = JsonMapper.ToObject(Properties.Resources.ServerUrls);
foreach (JsonData json in servers)
{
ServerUrls url = new ServerUrls();
url.Id = json["id"].ToString();
url.Protocol = json["protocol"].ToString();
url.Url = json["url"].ToString();
url.Memo = json["memo"].ToString();
_instance.Urls.Add(url);
}
}
}
return _instance;
}
}
public List<ServerUrls> Urls { get; set; }
public string NextApi()
{
IRouting<ServerUrls> routing = null;
string balance = Properties.Resources.LoadBalance;
switch (balance)
{
case "0"://随机
routing = new RandomRouting<ServerUrls>();
break;
case "1"://轮询
routing = new RoundRobinRouting<ServerUrls>(Urls.Count);
break;
case "2"://一致性Hash
routing = new ConsistentHashRouting<ServerUrls>(Urls.Count);
break;
default:
routing = new RandomRouting<ServerUrls>();
break;
}
var apis = routing.select("juweiyun.cn" , Urls.ToArray());
return string.Format(OPEN_URL , apis.Protocol , apis.Url);
}
/// <summary>
/// 获取当前时间戳兼容Java服务端
/// </summary>
/// <returns></returns>
public string getTimestamp()
{
TimeSpan ts = new TimeSpan(DateTime.UtcNow.Ticks - new DateTime(1970 , 1 , 1 , 0 , 0 , 0).Ticks);
return Convert.ToString((long)ts.TotalMilliseconds);
}
public static VersionObject GetVersionObject(string tenantId , string terminalType , string storeNo, string posNo,string appSign,string versionType , string versionNum , string starter)
{
try
{
var url = OpenApiUtils.Instance.NextApi();
SortedList<string , string> parameters = new SortedList<string , string>();
parameters.Add("type" , "posversioncheck");
parameters.Add("tenantId" , tenantId);
parameters.Add("terminalType" , terminalType);
parameters.Add("storeNo", storeNo);
parameters.Add("posNo" , posNo);
parameters.Add("appSign" , appSign);
parameters.Add("versionType" , versionType);
parameters.Add("versionNum" , versionNum);
//Logger.Log(string.Format("版本检测参数 tenantId:{0} terminalType:{1} posNo:{2} appSign:{3} versionType:{4} versionNum:{5} storeNo:{6} url:{7}", tenantId, terminalType, posNo, appSign, versionType, versionNum, storeNo, url));
string response = RequestUtils.Post(url , parameters);
//Logger.Log("版本检测结果" + response);
//将服务端返回的JSON转为Map
var map = JsonMapper.ToObject(response);
string status = map.Keys.Contains("status") ? map["status"].ToString() : "0";
//判断返回值是否成功
if ("1".Equals(status))
{
VersionObject obj = new VersionObject();
JsonData json = map["data"];
obj.HasNew = Convert.ToBoolean(json["hasNew"].ToString());
if (obj.HasNew)
{
obj.AppSign = json["appSign"].ToString();
obj.AppName = json["appName"].ToString();
obj.TerminalType = json["terminalType"].ToString();
obj.VersionType = Convert.ToInt32(json["versionType"].ToString());
obj.VersionNum = json["versionNum"].ToString();
obj.NewVersionNum = json["newVersionNum"].ToString();
obj.MinVersionNum = json["minVersionNum"].ToString();
obj.DfsAccessDomain = json["dfsAccessDomain"].ToString();
obj.ForceUpload = Convert.ToInt32(json["forceUpload"].ToString());
obj.Length = Convert.ToInt32(json["length"].ToString());
obj.CheckNum = json["checkNum"].ToString();//MD5
obj.FileName = json["fileName"].ToString();
obj.UploadLog = json["uploadLog"].ToString();
obj.UploadFile = json["uploadFile"].ToString();
obj.Description = json["description"].ToString();
obj.Url = obj.DfsAccessDomain + "/" + obj.UploadFile;
obj.StartApplication = starter;
}
return obj;
}
else
{
string message = map.Keys.Contains("message") ? map["message"].ToString() : "未知的错误";
Logger.Log(message);
return null;
}
}
catch(Exception ex)
{
Logger.Log("版本检测异常:" + ex.Message);
return null;
}
}
/// <summary>
/// 指示是否有任何可用的网络连接。
/// </summary>
/// <returns></returns>
public static bool IsNetworkLink()
{
bool result = NetworkInterface.GetIsNetworkAvailable();
return result;
}
/// <summary>
/// 检测电脑是否联网
/// </summary>
/// <returns>true表示联网</returns>
public static bool IsAvailable()
{
int i = 0;
if (WindowsAPI.InternetGetConnectedState(out i , 0))
{
//已联网
return true;
}
else
{
//未联网
return false;
}
}
}
}