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

App.cs 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. using Common;
  2. using Common.system;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.IO;
  7. using System.Reflection;
  8. using System.Security.Principal;
  9. using System.Threading;
  10. using System.Windows;
  11. using System.Windows.Threading;
  12. namespace XHZB.Desktop
  13. {
  14. public partial class APP : Application
  15. {
  16. #region 全局变量
  17. /// <summary>
  18. /// 是否为测试版
  19. /// </summary>
  20. public static bool isDebug = FileToolsCommon.GetConfigValue("IsDebug") != "0";
  21. /// <summary>
  22. /// RTMP服务器地址
  23. /// </summary>
  24. public static string RTMPServerPath = FileToolsCommon.GetConfigValue("ServerPath");
  25. /// <summary>
  26. /// AES账号加密字符串
  27. /// </summary>
  28. public static string AESAccountStr = null;
  29. /// <summary>
  30. /// AES密码加密字符串
  31. /// </summary>
  32. public static string AESPassWordStr = null;
  33. /// <summary>
  34. /// 接口地址
  35. /// </summary>
  36. public static string apiUrl = isDebug ? "http://schoolapitest.xhkjedu.com" : FileToolsCommon.GetConfigValue("APIRequestAddress");
  37. /// <summary>
  38. /// 图片地址
  39. /// </summary>
  40. public static string uploadUrl = isDebug ? "http://schoolfiletest.xhkjedu.com/" : FileToolsCommon.GetConfigValue("FileRequestAddress");
  41. /// <summary>
  42. /// 展示文件
  43. /// </summary>
  44. public static string showImageUrl = isDebug ? "http://schoolfiletest.xhkjedu.com/static/" : FileToolsCommon.GetConfigValue("SchoolfileRequestAddress") + "/static/";
  45. /// <summary>
  46. /// 认证接口地址
  47. /// </summary>
  48. public static string certapiUrl = isDebug ? "http://certapitest.xhkjedu.com" : FileToolsCommon.GetConfigValue("CertapiRequestAddress");
  49. /// <summary>
  50. /// 数据存放目录
  51. /// </summary>
  52. public static string dataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\XHZB\\";
  53. /// <summary>
  54. /// 签名
  55. /// </summary>
  56. public static string Signature = "";
  57. public static string secretKey = "nanhuakaizhangjianwangni";
  58. /// <summary>
  59. /// 录屏工具
  60. /// </summary>
  61. public static FFMpeg FFmpeg = new FFMpeg();
  62. /// <summary>
  63. /// 登陆状态 false 未登录 true已登陆
  64. /// </summary>
  65. public static bool IsLoginType = false;
  66. /// <summary>
  67. /// 防止图片地址重复
  68. /// </summary>
  69. public static long num = 9999;
  70. /// <summary>
  71. /// 导入图片路径
  72. /// </summary>
  73. public static string[] Paths = new string[] { };
  74. /// <summary>
  75. /// 截图图片路径
  76. /// </summary>
  77. public static string[] JPaths = new string[999];
  78. public static string ImgPath = string.Empty;
  79. /// <summary>
  80. /// 页码IP
  81. /// </summary>
  82. public static string[] OutPut = new string[] { };
  83. public static string CameraPosition = string.Empty;
  84. /// <summary>
  85. /// 数据存放目录
  86. /// </summary>
  87. public static string DataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\XHZB\\";
  88. /// <summary>
  89. /// 是否正在
  90. /// </summary>
  91. public static bool IsUpLoad = false;
  92. #region 页面
  93. /// <summary>
  94. /// 主页面
  95. /// </summary>
  96. public static MainWindow W_MainWindow = null;
  97. #endregion
  98. #endregion
  99. /// <summary>
  100. /// 启动入口
  101. /// </summary>
  102. /// <param name="args">参数1:账号加密字符串 参数1:密码加密字符串</param>
  103. [STAThread]
  104. private static void Main(string[] args)
  105. {
  106. //myloading = new LoadDialog();
  107. StopSameProcess();
  108. Killffmpeg();
  109. try
  110. {
  111. if (args != null)
  112. {
  113. AESAccountStr = args[0];
  114. AESPassWordStr = args[1];
  115. ////解密
  116. //string Account= AESHelper.AESDecrypt(AESAccountStr);
  117. //string PassWord = AESHelper.AESDecrypt(AESPassWordStr);
  118. }
  119. }
  120. catch (Exception ex)
  121. {
  122. LogHelper.WriteErrLog("参数接收失败:" + ex.Message, ex);
  123. throw;
  124. }
  125. try
  126. {
  127. WindowsIdentity identity = WindowsIdentity.GetCurrent();
  128. WindowsPrincipal principal = new WindowsPrincipal(identity);
  129. try
  130. {
  131. if (Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + "temp"))//清除临时文件
  132. {
  133. Directory.Delete(AppDomain.CurrentDomain.BaseDirectory + "temp", true);
  134. }
  135. }
  136. catch (Exception)
  137. {
  138. }
  139. //如果本地文件为空,弹出弹窗 输密匙
  140. //本地文件不为空, 解析
  141. if (!File.Exists(APP.dataPath + "signature.txt"))//上次登陆时间为空 或者签名为空 或者上次登陆时间不是今天 都需要输入密匙验证 ,否则只验证本地的签名
  142. {
  143. if (principal.IsInRole(WindowsBuiltInRole.Administrator))
  144. {
  145. ////如果是管理员,则直接运行
  146. //KeyVerification wins = new KeyVerification();
  147. // 定义Application对象作为整个应用程序入口
  148. //Application app = new Application();
  149. //app.Run(wins);
  150. W_MainWindow = new MainWindow();
  151. Application app = new Application();
  152. app.Run(W_MainWindow);
  153. }
  154. else
  155. {
  156. //创建启动对象
  157. System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo
  158. {
  159. UseShellExecute = true,
  160. WorkingDirectory = Environment.CurrentDirectory,
  161. FileName = Assembly.GetExecutingAssembly().Location,
  162. //设置启动动作,确保以管理员身份运行
  163. Verb = "runas"
  164. };
  165. try
  166. {
  167. System.Diagnostics.Process.Start(startInfo);
  168. }
  169. catch
  170. {
  171. return;
  172. }
  173. //退出
  174. //Current.Shutdown();
  175. Environment.Exit(0);
  176. }
  177. }
  178. else
  179. {
  180. if (principal.IsInRole(WindowsBuiltInRole.Administrator))
  181. {
  182. //ProductVerification wins = new ProductVerification();
  183. //Application app = new Application();
  184. //app.Run(wins);
  185. W_MainWindow = new MainWindow();
  186. Application app = new Application();
  187. app.Run(W_MainWindow);
  188. }
  189. else
  190. {
  191. //创建启动对象
  192. System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo
  193. {
  194. UseShellExecute = true,
  195. WorkingDirectory = Environment.CurrentDirectory,
  196. FileName = Assembly.GetExecutingAssembly().Location,
  197. //设置启动动作,确保以管理员身份运行
  198. Verb = "runas"
  199. };
  200. try
  201. {
  202. System.Diagnostics.Process.Start(startInfo);
  203. }
  204. catch
  205. {
  206. return;
  207. }
  208. //退出
  209. //Current.Shutdown();
  210. Environment.Exit(0);
  211. }
  212. }
  213. }
  214. catch (Exception ex)
  215. {
  216. string ErrMessage = "【进程】(Main):进程意外关闭。 " + ex.Message;
  217. LogHelper.WriteErrLog(ErrMessage, ex);
  218. }
  219. }
  220. #region 杀死已存在的进程
  221. /// <summary>
  222. /// 结束已运行的程序
  223. /// </summary>
  224. private static void StopSameProcess()
  225. {
  226. Process current = Process.GetCurrentProcess();
  227. Process[] processList = Process.GetProcesses();
  228. foreach (Process process in processList)
  229. {
  230. if (process.ProcessName.ToUpper() == "星火直播")
  231. {
  232. if (process.Id != current.Id) //忽略当前进程
  233. {
  234. try
  235. {
  236. process.Kill(); //结束进程
  237. }
  238. catch (Exception)
  239. {
  240. }
  241. }
  242. }
  243. }
  244. }
  245. /// <summary>
  246. /// 杀死正在运行的ffmpeg
  247. /// </summary>
  248. public static void Killffmpeg()
  249. {
  250. Process[] KillProcessArray = Process.GetProcessesByName("ffmpeg");
  251. foreach (Process KillProcess in KillProcessArray)
  252. {
  253. KillProcess.Kill();
  254. }
  255. }
  256. #endregion
  257. /// <summary>
  258. /// 错误处理
  259. /// </summary>
  260. /// <param name="sender"></param>
  261. /// <param name="e"></param>
  262. private void MyApp_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
  263. {
  264. LogHelper.WriteErrLog("未标示错误:" + e.Exception.Message, e.Exception);
  265. MessageWindow.Show("程序异常." + Environment.NewLine + e.Exception.Message);
  266. e.Handled = true;
  267. }
  268. #region 检测解压ffmpeg
  269. public void UnZipFFmpeg()
  270. {
  271. new Thread(o =>
  272. {
  273. if (!FileToolsCommon.IsExistDirectory(FileToolsCommon.GetFileAbsolutePath("/ffmpeg/")))
  274. {
  275. try
  276. {
  277. //根据系统解压ffmpeg
  278. if (Environment.Is64BitOperatingSystem)
  279. {
  280. //64
  281. ZipHelper.UnZip(FileToolsCommon.GetFileAbsolutePath("/ffmpegx64.zip"), FileToolsCommon.GetFileAbsolutePath());
  282. }
  283. else
  284. {
  285. //32
  286. ZipHelper.UnZip(FileToolsCommon.GetFileAbsolutePath("/ffmpegx32.zip"), FileToolsCommon.GetFileAbsolutePath());
  287. }
  288. }
  289. catch (Exception)
  290. {
  291. ZipHelper.UnZip(FileToolsCommon.GetFileAbsolutePath("/ffmpegx64.zip"), FileToolsCommon.GetFileAbsolutePath());
  292. }
  293. }
  294. }).Start();
  295. }
  296. #endregion
  297. #region 检测安装Screen Capturer Recorder
  298. /// <summary>
  299. /// 检测是否安装了Screen Capturer Recorder
  300. /// </summary>
  301. /// <returns></returns>
  302. public static bool CheckScreenCapturerRecorder()
  303. {
  304. if (FileToolsCommon.CheckSoftWartInstallState("Screen Capturer Recorder"))
  305. {
  306. //已经安装
  307. return true;
  308. }
  309. else
  310. {
  311. return false;
  312. }
  313. }
  314. /// <summary>
  315. /// 安装Screen Capturer Recorder
  316. /// </summary>
  317. /// <returns></returns>
  318. public static bool InstallScreenCapturerRecorder()
  319. {
  320. Process InstallProcess = new Process();
  321. string StandardError = "";
  322. string StandardOutput = "";
  323. try
  324. {
  325. InstallProcess.StartInfo.FileName = FileToolsCommon.GetFileAbsolutePath(@"/SSCR/Setup Screen Capturer Recorder v0.12.10.exe"); //绝对路径
  326. InstallProcess.StartInfo.UseShellExecute = false; //不使用操作系统外壳程序启动
  327. InstallProcess.StartInfo.RedirectStandardError = true; //重定向标准错误输出
  328. InstallProcess.StartInfo.RedirectStandardOutput = true;
  329. InstallProcess.StartInfo.CreateNoWindow = true; //不显示程序窗口
  330. InstallProcess.StartInfo.RedirectStandardInput = true; //用于模拟该进程控制台的输入
  331. //外部程序(这里是FFMPEG)输出流时候产生的事件,这里是把流的处理过程转移到下面的方法中,详细请查阅MSDN
  332. InstallProcess.ErrorDataReceived += new DataReceivedEventHandler(InstallProcess_ErrorDataReceived);
  333. InstallProcess.Start(); //启动线程
  334. InstallProcess.WaitForExit(); //阻塞等待进程结束
  335. StandardError = InstallProcess.StandardError.ReadToEnd();
  336. StandardOutput = InstallProcess.StandardOutput.ReadToEnd();
  337. int ExitCode = InstallProcess.ExitCode;
  338. InstallProcess.Close(); //关闭进程
  339. InstallProcess.Dispose(); //释放资源
  340. if (ExitCode == 0) //非0则为安装失败 1为取消安装
  341. {
  342. return true;
  343. }
  344. else
  345. {
  346. MessageBoxResult br = MessageWindow.Show("不安装环境将无法使用微课录制系统,是否现在安装?", "安装", MessageBoxButton.OKCancel);
  347. if (br == MessageBoxResult.OK)
  348. {
  349. return InstallScreenCapturerRecorder();
  350. }
  351. else
  352. {
  353. return false;
  354. }
  355. }
  356. }
  357. catch (Exception ex)
  358. {
  359. LogHelper.WriteErrLog("【ScreenCapturerRecorder安装】(installScreenCapturerRecorder)安装失败:" + ex.Message, ex);
  360. if (string.IsNullOrWhiteSpace(StandardError))
  361. {
  362. LogHelper.WriteErrLog("【ScreenCapturerRecorder安装】(installScreenCapturerRecorder)安装失败:" + StandardError, null);
  363. }
  364. if (string.IsNullOrWhiteSpace(StandardOutput))
  365. {
  366. LogHelper.WriteErrLog("【ScreenCapturerRecorder安装】(installScreenCapturerRecorder)安装返回信息:" + StandardOutput, null);
  367. }
  368. return false;
  369. }
  370. }
  371. /// <summary>
  372. /// 安装失败信息
  373. /// </summary>
  374. /// <param name="sender"></param>
  375. /// <param name="e"></param>
  376. private static void InstallProcess_ErrorDataReceived(object sender, DataReceivedEventArgs e)
  377. {
  378. LogHelper.WriteErrLog("【ScreenCapturerRecorder安装】(installScreenCapturerRecorder)安装失败:" + e.Data, null);
  379. //MessageWindow.Show(e.Data);
  380. }
  381. #endregion
  382. #region 内存处理 -创建人:赵耀 -创建时间:2020年8月12日
  383. /// <summary>
  384. /// 内存释放压缩
  385. /// </summary>
  386. /// <param name="e"></param>
  387. protected override void OnStartup(StartupEventArgs e)
  388. {
  389. //private LierdaCracker cracker = new LierdaCracker();
  390. //cracker.Cracker(5000);//间隔时间
  391. //base.OnStartup(e);
  392. new Thread(o =>
  393. {
  394. while (true)
  395. {
  396. try
  397. {
  398. //FreeMemoryHelper.SetDate();
  399. FreeMemoryHelper.ReallocateMemory();
  400. Thread.Sleep(20000);//暂定每20秒释放一次
  401. }
  402. catch (Exception ex)
  403. {
  404. LogHelper.WriteErrLog("【释放内存(MyApp)" + ex.Message, ex);
  405. }
  406. }
  407. }).Start();
  408. }
  409. #endregion 内存处理 -创建人:赵耀 -创建时间:2020年8月12日
  410. }
  411. }