UpdateTask.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using GeekDesk.Constant;
  2. using GeekDesk.Control.Windows;
  3. using GeekDesk.Util;
  4. using GeekDesk.ViewModel;
  5. using Newtonsoft.Json.Linq;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Configuration;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. namespace GeekDesk.Task
  13. {
  14. internal class UpdateTask
  15. {
  16. private static AppConfig appConfig = MainWindow.appData.AppConfig;
  17. public static void Start()
  18. {
  19. System.Timers.Timer timer = new System.Timers.Timer
  20. {
  21. Enabled = true,
  22. Interval = 60 * 1000 * 60 * 12, //60秒 * 60 * 12 12小时触发一次
  23. //Interval = 6000,
  24. };
  25. timer.Start();
  26. timer.Elapsed += Timer_Elapsed; ;
  27. }
  28. private static void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
  29. {
  30. try
  31. {
  32. string updateUrl = ConfigurationManager.AppSettings["GiteeUpdateUrl"];
  33. string nowVersion = ConfigurationManager.AppSettings["Version"];
  34. if (appConfig != null)
  35. {
  36. switch (appConfig.UpdateType)
  37. {
  38. case UpdateType.GitHub:
  39. updateUrl = ConfigurationManager.AppSettings["GitHubUpdateUrl"];
  40. break;
  41. default:
  42. updateUrl = ConfigurationManager.AppSettings["GiteeUpdateUrl"];
  43. break;
  44. }
  45. }
  46. string updateInfo = HttpUtil.Get(updateUrl);
  47. if (!StringUtil.IsEmpty(updateInfo))
  48. {
  49. JObject jo = JObject.Parse(updateInfo);
  50. try
  51. {
  52. if (jo["statisticUrl"] != null)
  53. {
  54. string statisticUrl = jo["statisticUrl"].ToString();
  55. if (!string.IsNullOrEmpty(statisticUrl))
  56. {
  57. //用户统计 只通过uuid统计用户数量 不收集任何信息
  58. statisticUrl += "?uuid=" + CommonCode.GetUniqueUUID();
  59. HttpUtil.Get(statisticUrl);
  60. }
  61. }
  62. }
  63. catch (Exception) { }
  64. string onlineVersion = jo["version"].ToString();
  65. if (onlineVersion.CompareTo(nowVersion) > 0)
  66. {
  67. App.Current.Dispatcher.Invoke((Action)(() =>
  68. {
  69. //检测到版本更新
  70. UpdateWindow.Show(jo);
  71. }));
  72. }
  73. }
  74. }
  75. catch (Exception ex)
  76. {
  77. LogUtil.WriteErrorLog(ex, "获取更新失败!");
  78. }
  79. }
  80. }
  81. }