old_flsystem/PCRobot/Utils/UpdateClient.cs

132 lines
3.7 KiB
C#

using CsharpHttpHelper;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PCRobot.Utils
{
public class UpdateClient
{
private static string ExecName = "YiZhuanFa";
/// <summary>
/// 获取云端的文件列表
/// </summary>
public static string GetCloudFileHist()
{
try
{
int LastId = 1;
try
{
var id = GetVersionId();
LastId = int.Parse(id);
}
catch (Exception)
{
LastId = 1;
}
var http = new HttpHelper();
var item = new HttpItem()
{
URL = "http://file.api.52cmg.cn/file/QueryHist?Name=" + HttpHelper.URLEncode(ExecName, Encoding.UTF8) + "&LastId=" + LastId,
Timeout = 30000
};
var result = http.GetHtml(item).Html;
var data = JObject.Parse(result);
if ((bool)data["Ok"] && data["Data"] != null)
{
return ShowUpdate(data["Data"]);
}
}
catch (Exception)
{ }
return null;
}
private static IniHelper Ini { get; set; }
public static string ShowUpdate(JToken jToken)
{
var strb = new StringBuilder();
try
{
foreach (var item in jToken)
{
strb.AppendLine(((DateTime)item["CreateTime"]).ToString("yyyy.MM.dd.HHmm"));
strb.AppendLine(item["Message"].ToString());
strb.AppendLine();
}
}
catch (Exception ex)
{ }
return strb.ToString();
}
private static string GetVersionId()
{
Ini = new IniHelper(HttpExtend.MapFile("版本信息.ini"));
return Ini.GetValue("Version", "Id");
}
/// <summary>
/// 版本号
/// </summary>
/// <returns></returns>
public static string GetVersionName()
{
Ini = new IniHelper(HttpExtend.MapFile("版本信息.ini"));
return DateTime.Parse(Ini.GetValue("Version", "CreateTime")).ToString("yyyy.MM.dd.HHmm");
}
/// <summary>
/// 版本信息
/// </summary>
public class FileVersion
{
public int Id { get; set; }
public string Name { get; set; }
public bool IsEnable { get; set; }
private string _Message;
/// <summary>
/// 更新的内容
/// </summary>
public string Message
{
get { return ShowMessage(_Message); }
set { _Message = value; }
}
public DateTime CreateTime { get; set; }
private string ShowMessage(string Message)
{
try
{
var strb = new StringBuilder();
var UpdateContent = Message.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
strb.AppendLine($"{Name} - {CreateTime.ToString("yyyy.MM.dd.HHmmss")}");
strb.AppendLine();
for (int i = 0; i < UpdateContent.Length; i++)
{
strb.AppendLine(UpdateContent[i]);
}
}
catch (Exception ex)
{ }
return Message;
}
}
}
}