UpdateWindow.xaml.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. using GeekDesk.Constant;
  2. using GeekDesk.Interface;
  3. using GeekDesk.Util;
  4. using GeekDesk.ViewModel;
  5. using Newtonsoft.Json.Linq;
  6. using System;
  7. using System.Configuration;
  8. using System.Diagnostics;
  9. using System.Net;
  10. using System.Windows;
  11. using System.Windows.Input;
  12. namespace GeekDesk.Control.Windows
  13. {
  14. /// <summary>
  15. /// UpdateWindow.xaml 的交互逻辑
  16. /// </summary>
  17. public partial class UpdateWindow : Window, IWindowCommon
  18. {
  19. private static AppConfig appConfig = MainWindow.appData.AppConfig;
  20. private static string githubUrl = "";
  21. private static string giteeUrl = "";
  22. private UpdateWindow(JObject jo)
  23. {
  24. try
  25. {
  26. WindowStartupLocation = WindowStartupLocation.CenterScreen;
  27. this.Topmost = true;
  28. InitializeComponent();
  29. DataHandle(jo);
  30. }
  31. catch (Exception)
  32. {
  33. }
  34. }
  35. /// <summary>
  36. /// 移动窗口
  37. /// </summary>
  38. /// <param name="sender"></param>
  39. /// <param name="e"></param>
  40. private void DragMove(object sender, System.Windows.Input.MouseButtonEventArgs e)
  41. {
  42. if (e.LeftButton == MouseButtonState.Pressed)
  43. {
  44. DragMove();
  45. }
  46. }
  47. private void DataHandle(JObject jo)
  48. {
  49. Title.Text = StringUtil.IsEmpty(jo["title"]) ? "" : jo["title"].ToString();
  50. SubTitle.Text = StringUtil.IsEmpty(jo["subTitle"]) ? "" : jo["subTitle"].ToString();
  51. MsgTitle.Text = StringUtil.IsEmpty(jo["msgTitle"]) ? "" : jo["msgTitle"].ToString();
  52. JArray ja = JArray.Parse(StringUtil.IsEmpty(jo["msg"]) ? "[]" : jo["msg"].ToString());
  53. githubUrl = StringUtil.IsEmpty(jo["githubUrl"]) ? ConfigurationManager.AppSettings["GitHubUrl"] : jo["githubUrl"].ToString();
  54. giteeUrl = StringUtil.IsEmpty(jo["giteeUrl"]) ? ConfigurationManager.AppSettings["GiteeUrl"] : jo["giteeUrl"].ToString();
  55. string msg = "";
  56. for (int i = 0; i < ja.Count; i++)
  57. {
  58. msg += "• " + ja[i].ToString() + "\n";
  59. }
  60. Msg.Text = msg;
  61. }
  62. private void Close_Click(object sender, RoutedEventArgs e)
  63. {
  64. this.Close();
  65. }
  66. private void Confirm_Click(object sender, RoutedEventArgs e)
  67. {
  68. string packageUrl;
  69. switch (appConfig.UpdateType)
  70. {
  71. case UpdateType.GitHub:
  72. packageUrl = githubUrl;
  73. break;
  74. default:
  75. packageUrl = giteeUrl;
  76. break;
  77. }
  78. Process.Start(new ProcessStartInfo("cmd", $"/c start {packageUrl}")
  79. {
  80. UseShellExecute = false,
  81. CreateNoWindow = true
  82. });
  83. this.Close();
  84. }
  85. private static System.Windows.Window window = null;
  86. public static void Show(JObject jo)
  87. {
  88. if (window == null || !window.Activate())
  89. {
  90. window = new UpdateWindow(jo);
  91. }
  92. window.Show();
  93. window.Activate();
  94. Keyboard.Focus(window);
  95. }
  96. public void OnKeyDown(object sender, KeyEventArgs e)
  97. {
  98. if (e.Key == Key.Escape)
  99. {
  100. this.DataContext = null;
  101. this.Close();
  102. }
  103. }
  104. private void StarBtnClick(object sender, RoutedEventArgs e)
  105. {
  106. string githubUrl = ConfigurationManager.AppSettings["GitHubUrl"];
  107. string giteeUrl = ConfigurationManager.AppSettings["GiteeUrl"];
  108. if(!ReqGitUrl(githubUrl))
  109. {
  110. if (!ReqGitUrl(giteeUrl))
  111. {
  112. OpenLinkUrl(githubUrl);
  113. } else
  114. {
  115. OpenLinkUrl(giteeUrl);
  116. }
  117. } else
  118. {
  119. OpenLinkUrl(githubUrl);
  120. }
  121. }
  122. private bool ReqGitUrl(String url)
  123. {
  124. HttpWebResponse myResponse = null;
  125. try
  126. {
  127. ServicePointManager.Expect100Continue = true;
  128. ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
  129. //创建Web访问对 象
  130. WebRequest myRequest = WebRequest.Create(url);
  131. myRequest.ContentType = "text/plain; charset=utf-8";
  132. //通过Web访问对象获取响应内容
  133. myResponse = (HttpWebResponse)myRequest.GetResponse();
  134. }
  135. catch
  136. {
  137. return false;
  138. }
  139. return myResponse != null && myResponse.StatusCode == HttpStatusCode.OK;
  140. }
  141. private void OpenLinkUrl(String url)
  142. {
  143. Process.Start(new ProcessStartInfo("cmd", $"/c start {url}")
  144. {
  145. UseShellExecute = false,
  146. CreateNoWindow = true
  147. });
  148. }
  149. }
  150. }