星火直播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 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  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. using XHZB.Model;
  13. namespace XHZB.Desktop
  14. {
  15. public partial class APP : Application
  16. {
  17. #region 全局变量
  18. public static int testdata=0;
  19. /// <summary>
  20. /// 是否为测试版
  21. /// </summary>
  22. public static bool isDebug = FileToolsCommon.GetConfigValue("IsDebug") != "0";
  23. /// <summary>
  24. /// RTMP服务器地址
  25. /// </summary>
  26. public static string RTMPServerPath = FileToolsCommon.GetConfigValue("ServerPath");
  27. /// <summary>
  28. /// AES账号加密字符串
  29. /// </summary>
  30. public static string AESAccountStr = null;
  31. /// <summary>
  32. /// AES密码加密字符串
  33. /// </summary>
  34. public static string AESPassWordStr = null;
  35. /// <summary>
  36. /// 接口地址
  37. /// </summary>
  38. public static string apiUrl = isDebug ? "http://schoolapitest.xhkjedu.com" : FileToolsCommon.GetConfigValue("APIRequestAddress");
  39. /// <summary>
  40. /// 图片地址
  41. /// </summary>
  42. public static string uploadUrl = isDebug ? "http://schoolfiletest.xhkjedu.com/" : FileToolsCommon.GetConfigValue("FileRequestAddress");
  43. /// <summary>
  44. /// 展示文件
  45. /// </summary>
  46. public static string showImageUrl = isDebug ? "http://schoolfiletest.xhkjedu.com/static/" : FileToolsCommon.GetConfigValue("SchoolfileRequestAddress") + "/static/";
  47. /// <summary>
  48. /// 认证接口地址
  49. /// </summary>
  50. public static string certapiUrl = isDebug ? "http://certapitest.xhkjedu.com" : FileToolsCommon.GetConfigValue("CertapiRequestAddress");
  51. /// <summary>
  52. /// 数据存放目录
  53. /// </summary>
  54. public static string dataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\XHZB\\";
  55. /// <summary>
  56. /// 签名
  57. /// </summary>
  58. public static string Signature = "";
  59. public static string secretKey = "nanhuakaizhangjianwangni";
  60. /// <summary>
  61. /// 录屏工具
  62. /// </summary>
  63. public static FFMpeg FFmpeg = new FFMpeg();
  64. /// <summary>
  65. /// 登陆状态 false 未登录 true已登陆
  66. /// </summary>
  67. public static bool IsLoginType = false;
  68. /// <summary>
  69. /// 防止图片地址重复
  70. /// </summary>
  71. public static long num = 9999;
  72. /// <summary>
  73. /// 导入图片路径
  74. /// </summary>
  75. public static string[] Paths = new string[] { };
  76. /// <summary>
  77. /// 截图图片路径
  78. /// </summary>
  79. public static string[] JPaths = new string[999];
  80. public static string ImgPath = string.Empty;
  81. /// <summary>
  82. /// 页码IP
  83. /// </summary>
  84. public static string[] OutPut = new string[] { };
  85. public static string CameraPosition = string.Empty;
  86. public static string fileStorageAddress = "";
  87. /// <summary>
  88. /// 数据存放目录
  89. /// </summary>
  90. public static string DataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\XHZB\\";
  91. /// <summary>
  92. /// 是否正在
  93. /// </summary>
  94. public static bool IsUpLoad = false;
  95. /// <summary>
  96. /// 后台线程帮助类
  97. /// </summary>
  98. public static BackgroundWorkerHelper BackgroundWorkerHelper => BackgroundWorkerHelper.GetInstance();
  99. /// <summary>
  100. /// 提示信息
  101. /// </summary>
  102. public static string ErrorMessage = string.Empty;
  103. /// <summary>
  104. /// 服务调用成功代码
  105. /// </summary>
  106. public const int ServerScuessCode = 0;
  107. public static int classid;
  108. public static long roomid;
  109. public static long askid;
  110. public static int lsbid;
  111. /// <summary>
  112. /// 登陆数据
  113. /// </summary>
  114. public static LoginModel LoginUser;
  115. private static List<ClassListModel> _ClassList;
  116. /// <summary>
  117. /// 教师任教班级出参
  118. /// </summary>
  119. public static List<ClassListModel> ClassList
  120. {
  121. get
  122. {
  123. if (_ClassList == null)
  124. {
  125. _ClassList = new List<ClassListModel>();
  126. }
  127. return _ClassList;
  128. }
  129. set => _ClassList = value;
  130. }
  131. private static List<TsubjectbookListModel> _TsubjectbookList;
  132. /// <summary>
  133. /// 教师教材列表
  134. /// </summary>
  135. public static List<TsubjectbookListModel> TsubjectbookList
  136. {
  137. get
  138. {
  139. if (_TsubjectbookList == null)
  140. {
  141. _TsubjectbookList = new List<TsubjectbookListModel>();
  142. }
  143. return _TsubjectbookList;
  144. }
  145. set => _TsubjectbookList = value;
  146. }
  147. private static List<DirectorListModel> _DirectorList;
  148. /// <summary>
  149. /// 章节列表 出参
  150. /// </summary>
  151. public static List<DirectorListModel> DirectorList
  152. {
  153. get
  154. {
  155. if (_DirectorList == null)
  156. {
  157. _DirectorList = new List<DirectorListModel>();
  158. }
  159. return _DirectorList;
  160. }
  161. set => _DirectorList = value;
  162. }
  163. private static REQStartClassModel _REQStartClass;
  164. /// <summary>
  165. /// 开始课堂入参
  166. /// </summary>
  167. public static REQStartClassModel REQStartClass
  168. {
  169. get
  170. {
  171. if (_REQStartClass == null)
  172. {
  173. _REQStartClass = new REQStartClassModel();
  174. }
  175. return _REQStartClass;
  176. }
  177. set => _REQStartClass = value;
  178. }
  179. /// <summary>
  180. /// 抢答
  181. /// </summary>
  182. public static List<RollCallModel> RollCallList = new List<RollCallModel>();
  183. /// <summary>
  184. /// 在线用户
  185. /// </summary>
  186. public static List<OnlineUserModel> OnlineUserList = new List<OnlineUserModel>();
  187. private static List<ClassStudentListModel> _ClassStudentList;
  188. /// <summary>
  189. /// 班级学生--列表 出参
  190. /// </summary>
  191. public static List<ClassStudentListModel> ClassStudentList
  192. {
  193. get
  194. {
  195. if (_ClassStudentList == null)
  196. {
  197. _ClassStudentList = new List<ClassStudentListModel>();
  198. }
  199. return _ClassStudentList;
  200. }
  201. set => _ClassStudentList = value;
  202. }
  203. #region 页面
  204. public static LoadDialog myloading;
  205. /// <summary>
  206. /// 主页面
  207. /// </summary>
  208. public static LoginWindow W_LoginWindow = null;
  209. /// <summary>
  210. /// 工具栏
  211. /// </summary>
  212. public static ToolbarWindow W_ToolbarWindow = null;
  213. /// <summary>
  214. /// 个人空间
  215. /// </summary>
  216. public static UserCenterWindow W_UserCenterWindow = null;
  217. /// <summary>
  218. /// 抢答点名
  219. /// </summary>
  220. public static RollCallWindow W_RollCallWindow = null;
  221. /// <summary>
  222. /// 考勤
  223. /// </summary>
  224. public static AttendanceWindow W_AttendanceWindow = null;
  225. /// <summary>
  226. /// 黑板
  227. /// </summary>
  228. public static ZBlackboardWindow W_ZBlackboardWindow = null;
  229. /// <summary>
  230. /// 批注
  231. /// </summary>
  232. public static PracticeWindow W_PracticeWindow = null;
  233. #endregion
  234. #endregion
  235. /// <summary>
  236. /// 启动入口
  237. /// </summary>
  238. /// <param name="args">参数1:账号加密字符串 参数1:密码加密字符串</param>
  239. [STAThread]
  240. private static void Main(string[] args)
  241. {
  242. myloading = new LoadDialog();
  243. StopSameProcess();
  244. Killffmpeg();
  245. try
  246. {
  247. if (args != null&&args.Length>1)
  248. {
  249. AESAccountStr = args[0];
  250. AESPassWordStr = args[1];
  251. ////解密
  252. //string Account= AESHelper.AESDecrypt(AESAccountStr);
  253. //string PassWord = AESHelper.AESDecrypt(AESPassWordStr);
  254. }
  255. }
  256. catch (Exception ex)
  257. {
  258. LogHelper.WriteErrLog("参数接收失败:" + ex.Message, ex);
  259. throw;
  260. }
  261. try
  262. {
  263. WindowsIdentity identity = WindowsIdentity.GetCurrent();
  264. WindowsPrincipal principal = new WindowsPrincipal(identity);
  265. try
  266. {
  267. if (Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + "temp"))//清除临时文件
  268. {
  269. Directory.Delete(AppDomain.CurrentDomain.BaseDirectory + "temp", true);
  270. }
  271. }
  272. catch (Exception)
  273. {
  274. }
  275. //如果本地文件为空,弹出弹窗 输密匙
  276. //本地文件不为空, 解析
  277. if (!File.Exists(APP.dataPath + "signature.txt"))//上次登陆时间为空 或者签名为空 或者上次登陆时间不是今天 都需要输入密匙验证 ,否则只验证本地的签名
  278. {
  279. if (principal.IsInRole(WindowsBuiltInRole.Administrator))
  280. {
  281. ////如果是管理员,则直接运行
  282. //KeyVerification wins = new KeyVerification();
  283. // 定义Application对象作为整个应用程序入口
  284. //Application app = new Application();
  285. //app.Run(wins);
  286. W_LoginWindow = new LoginWindow();
  287. Application app = new Application();
  288. app.Run(W_LoginWindow);
  289. }
  290. else
  291. {
  292. //创建启动对象
  293. System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo
  294. {
  295. UseShellExecute = true,
  296. WorkingDirectory = Environment.CurrentDirectory,
  297. FileName = Assembly.GetExecutingAssembly().Location,
  298. //设置启动动作,确保以管理员身份运行
  299. Verb = "runas"
  300. };
  301. try
  302. {
  303. System.Diagnostics.Process.Start(startInfo);
  304. }
  305. catch
  306. {
  307. return;
  308. }
  309. //退出
  310. //Current.Shutdown();
  311. Environment.Exit(0);
  312. }
  313. }
  314. else
  315. {
  316. if (principal.IsInRole(WindowsBuiltInRole.Administrator))
  317. {
  318. //ProductVerification wins = new ProductVerification();
  319. //Application app = new Application();
  320. //app.Run(wins);
  321. W_LoginWindow = new LoginWindow();
  322. Application app = new Application();
  323. app.Run(W_LoginWindow);
  324. }
  325. else
  326. {
  327. //创建启动对象
  328. System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo
  329. {
  330. UseShellExecute = true,
  331. WorkingDirectory = Environment.CurrentDirectory,
  332. FileName = Assembly.GetExecutingAssembly().Location,
  333. //设置启动动作,确保以管理员身份运行
  334. Verb = "runas"
  335. };
  336. try
  337. {
  338. System.Diagnostics.Process.Start(startInfo);
  339. }
  340. catch
  341. {
  342. return;
  343. }
  344. //退出
  345. //Current.Shutdown();
  346. Environment.Exit(0);
  347. }
  348. }
  349. }
  350. catch (Exception ex)
  351. {
  352. string ErrMessage = "【进程】(Main):进程意外关闭。 " + ex.Message;
  353. LogHelper.WriteErrLog(ErrMessage, ex);
  354. }
  355. }
  356. #region 杀死已存在的进程
  357. /// <summary>
  358. /// 结束已运行的程序
  359. /// </summary>
  360. private static void StopSameProcess()
  361. {
  362. Process current = Process.GetCurrentProcess();
  363. Process[] processList = Process.GetProcesses();
  364. foreach (Process process in processList)
  365. {
  366. if (process.ProcessName.ToUpper() == "星火直播")
  367. {
  368. if (process.Id != current.Id) //忽略当前进程
  369. {
  370. try
  371. {
  372. process.Kill(); //结束进程
  373. }
  374. catch (Exception)
  375. {
  376. }
  377. }
  378. }
  379. }
  380. }
  381. /// <summary>
  382. /// 杀死正在运行的ffmpeg
  383. /// </summary>
  384. public static void Killffmpeg()
  385. {
  386. Process[] KillProcessArray = Process.GetProcessesByName("ffmpeg");
  387. foreach (Process KillProcess in KillProcessArray)
  388. {
  389. KillProcess.Kill();
  390. }
  391. }
  392. #endregion
  393. /// <summary>
  394. /// 错误处理
  395. /// </summary>
  396. /// <param name="sender"></param>
  397. /// <param name="e"></param>
  398. private void MyApp_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
  399. {
  400. LogHelper.WriteErrLog("未标示错误:" + e.Exception.Message, e.Exception);
  401. MessageWindow.Show("程序异常." + Environment.NewLine + e.Exception.Message);
  402. e.Handled = true;
  403. }
  404. #region 检测解压ffmpeg
  405. public void UnZipFFmpeg()
  406. {
  407. new Thread(o =>
  408. {
  409. if (!FileToolsCommon.IsExistDirectory(FileToolsCommon.GetFileAbsolutePath("/ffmpeg/")))
  410. {
  411. try
  412. {
  413. //根据系统解压ffmpeg
  414. if (Environment.Is64BitOperatingSystem)
  415. {
  416. //64
  417. ZipHelper.UnZip(FileToolsCommon.GetFileAbsolutePath("/ffmpegx64.zip"), FileToolsCommon.GetFileAbsolutePath());
  418. }
  419. else
  420. {
  421. //32
  422. ZipHelper.UnZip(FileToolsCommon.GetFileAbsolutePath("/ffmpegx32.zip"), FileToolsCommon.GetFileAbsolutePath());
  423. }
  424. }
  425. catch (Exception)
  426. {
  427. ZipHelper.UnZip(FileToolsCommon.GetFileAbsolutePath("/ffmpegx64.zip"), FileToolsCommon.GetFileAbsolutePath());
  428. }
  429. }
  430. }).Start();
  431. }
  432. #endregion
  433. #region 检测安装Screen Capturer Recorder
  434. /// <summary>
  435. /// 检测是否安装了Screen Capturer Recorder
  436. /// </summary>
  437. /// <returns></returns>
  438. public static bool CheckScreenCapturerRecorder()
  439. {
  440. if (FileToolsCommon.CheckSoftWartInstallState("Screen Capturer Recorder"))
  441. {
  442. //已经安装
  443. return true;
  444. }
  445. else
  446. {
  447. return false;
  448. }
  449. }
  450. /// <summary>
  451. /// 安装Screen Capturer Recorder
  452. /// </summary>
  453. /// <returns></returns>
  454. public static bool InstallScreenCapturerRecorder()
  455. {
  456. Process InstallProcess = new Process();
  457. string StandardError = "";
  458. string StandardOutput = "";
  459. try
  460. {
  461. InstallProcess.StartInfo.FileName = FileToolsCommon.GetFileAbsolutePath(@"/SSCR/Setup Screen Capturer Recorder v0.12.10.exe"); //绝对路径
  462. InstallProcess.StartInfo.UseShellExecute = false; //不使用操作系统外壳程序启动
  463. InstallProcess.StartInfo.RedirectStandardError = true; //重定向标准错误输出
  464. InstallProcess.StartInfo.RedirectStandardOutput = true;
  465. InstallProcess.StartInfo.CreateNoWindow = true; //不显示程序窗口
  466. InstallProcess.StartInfo.RedirectStandardInput = true; //用于模拟该进程控制台的输入
  467. //外部程序(这里是FFMPEG)输出流时候产生的事件,这里是把流的处理过程转移到下面的方法中,详细请查阅MSDN
  468. InstallProcess.ErrorDataReceived += new DataReceivedEventHandler(InstallProcess_ErrorDataReceived);
  469. InstallProcess.Start(); //启动线程
  470. InstallProcess.WaitForExit(); //阻塞等待进程结束
  471. StandardError = InstallProcess.StandardError.ReadToEnd();
  472. StandardOutput = InstallProcess.StandardOutput.ReadToEnd();
  473. int ExitCode = InstallProcess.ExitCode;
  474. InstallProcess.Close(); //关闭进程
  475. InstallProcess.Dispose(); //释放资源
  476. if (ExitCode == 0) //非0则为安装失败 1为取消安装
  477. {
  478. return true;
  479. }
  480. else
  481. {
  482. MessageBoxResult br = MessageWindow.Show("不安装环境将无法使用微课录制系统,是否现在安装?", "安装", MessageBoxButton.OKCancel);
  483. if (br == MessageBoxResult.OK)
  484. {
  485. return InstallScreenCapturerRecorder();
  486. }
  487. else
  488. {
  489. return false;
  490. }
  491. }
  492. }
  493. catch (Exception ex)
  494. {
  495. LogHelper.WriteErrLog("【ScreenCapturerRecorder安装】(installScreenCapturerRecorder)安装失败:" + ex.Message, ex);
  496. if (string.IsNullOrWhiteSpace(StandardError))
  497. {
  498. LogHelper.WriteErrLog("【ScreenCapturerRecorder安装】(installScreenCapturerRecorder)安装失败:" + StandardError, null);
  499. }
  500. if (string.IsNullOrWhiteSpace(StandardOutput))
  501. {
  502. LogHelper.WriteErrLog("【ScreenCapturerRecorder安装】(installScreenCapturerRecorder)安装返回信息:" + StandardOutput, null);
  503. }
  504. return false;
  505. }
  506. }
  507. /// <summary>
  508. /// 安装失败信息
  509. /// </summary>
  510. /// <param name="sender"></param>
  511. /// <param name="e"></param>
  512. private static void InstallProcess_ErrorDataReceived(object sender, DataReceivedEventArgs e)
  513. {
  514. LogHelper.WriteErrLog("【ScreenCapturerRecorder安装】(installScreenCapturerRecorder)安装失败:" + e.Data, null);
  515. //MessageWindow.Show(e.Data);
  516. }
  517. #endregion
  518. #region 内存处理 -创建人:赵耀 -创建时间:2020年8月12日
  519. /// <summary>
  520. /// 内存释放压缩
  521. /// </summary>
  522. /// <param name="e"></param>
  523. protected override void OnStartup(StartupEventArgs e)
  524. {
  525. //private LierdaCracker cracker = new LierdaCracker();
  526. //cracker.Cracker(5000);//间隔时间
  527. //base.OnStartup(e);
  528. new Thread(o =>
  529. {
  530. while (true)
  531. {
  532. try
  533. {
  534. //FreeMemoryHelper.SetDate();
  535. FreeMemoryHelper.ReallocateMemory();
  536. Thread.Sleep(20000);//暂定每20秒释放一次
  537. }
  538. catch (Exception ex)
  539. {
  540. LogHelper.WriteErrLog("【释放内存(MyApp)" + ex.Message, ex);
  541. }
  542. }
  543. }).Start();
  544. }
  545. #endregion 内存处理 -创建人:赵耀 -创建时间:2020年8月12日
  546. #region 测试
  547. public static void AddTestData()
  548. {
  549. testdata = 1;
  550. }
  551. /// <summary>
  552. /// 图片真实路径
  553. /// </summary>
  554. /// <returns></returns>
  555. public static string classRoomImagePath()
  556. {
  557. //string path = $"{nginxHtml}school\\{ZCommonData.loginUser.schoolid}\\class\\{ZCommonData.roomid}\\";
  558. string path = AppDomain.CurrentDomain.BaseDirectory + "temp\\"; ;
  559. return path;
  560. }
  561. /// <summary>
  562. /// 本地服务器存储路径
  563. /// </summary>
  564. /// <returns></returns>
  565. public static string dbImagePath()
  566. {
  567. //string path = $"school\\{ZCommonData.loginUser.schoolid}\\class\\{ZCommonData.roomid}\\";
  568. string path = AppDomain.CurrentDomain.BaseDirectory + "temp\\";
  569. return path;
  570. }
  571. /// <summary>
  572. /// 获取图片保存路径 设置服务器存储路径 创建人:赵耀 创建时间:2020年8月11日
  573. /// </summary>
  574. /// <returns></returns>
  575. public static string GetImagePath(out string ServerSavePath)
  576. {
  577. string imagepath = APP.classRoomImagePath();
  578. if (!Directory.Exists(imagepath))
  579. {
  580. Directory.CreateDirectory(imagepath);
  581. }
  582. TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
  583. string timestr = Convert.ToInt64(ts.TotalMilliseconds).ToString();
  584. string filepath = Path.Combine(imagepath, timestr + ".jpg");
  585. ServerSavePath = APP.dbImagePath() + timestr + ".jpg";
  586. return filepath;
  587. }
  588. #endregion
  589. }
  590. }