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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  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. /// <summary>
  234. /// 摄像头
  235. /// </summary>
  236. public static CameraWindow W_CameraWindow = null;
  237. #endregion
  238. #endregion
  239. /// <summary>
  240. /// 启动入口
  241. /// </summary>
  242. /// <param name="args">参数1:账号加密字符串 参数1:密码加密字符串</param>
  243. [STAThread]
  244. private static void Main(string[] args)
  245. {
  246. myloading = new LoadDialog();
  247. StopSameProcess();
  248. Killffmpeg();
  249. try
  250. {
  251. if (args != null&&args.Length>1)
  252. {
  253. AESAccountStr = args[0];
  254. AESPassWordStr = args[1];
  255. ////解密
  256. //string Account= AESHelper.AESDecrypt(AESAccountStr);
  257. //string PassWord = AESHelper.AESDecrypt(AESPassWordStr);
  258. }
  259. }
  260. catch (Exception ex)
  261. {
  262. LogHelper.WriteErrLog("参数接收失败:" + ex.Message, ex);
  263. throw;
  264. }
  265. try
  266. {
  267. WindowsIdentity identity = WindowsIdentity.GetCurrent();
  268. WindowsPrincipal principal = new WindowsPrincipal(identity);
  269. try
  270. {
  271. if (Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + "temp"))//清除临时文件
  272. {
  273. Directory.Delete(AppDomain.CurrentDomain.BaseDirectory + "temp", true);
  274. }
  275. }
  276. catch (Exception)
  277. {
  278. }
  279. //如果本地文件为空,弹出弹窗 输密匙
  280. //本地文件不为空, 解析
  281. if (!File.Exists(APP.dataPath + "signature.txt"))//上次登陆时间为空 或者签名为空 或者上次登陆时间不是今天 都需要输入密匙验证 ,否则只验证本地的签名
  282. {
  283. if (principal.IsInRole(WindowsBuiltInRole.Administrator))
  284. {
  285. ////如果是管理员,则直接运行
  286. //KeyVerification wins = new KeyVerification();
  287. // 定义Application对象作为整个应用程序入口
  288. //Application app = new Application();
  289. //app.Run(wins);
  290. W_LoginWindow = new LoginWindow();
  291. Application app = new Application();
  292. app.Run(W_LoginWindow);
  293. }
  294. else
  295. {
  296. //创建启动对象
  297. System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo
  298. {
  299. UseShellExecute = true,
  300. WorkingDirectory = Environment.CurrentDirectory,
  301. FileName = Assembly.GetExecutingAssembly().Location,
  302. //设置启动动作,确保以管理员身份运行
  303. Verb = "runas"
  304. };
  305. try
  306. {
  307. System.Diagnostics.Process.Start(startInfo);
  308. }
  309. catch
  310. {
  311. return;
  312. }
  313. //退出
  314. //Current.Shutdown();
  315. Environment.Exit(0);
  316. }
  317. }
  318. else
  319. {
  320. if (principal.IsInRole(WindowsBuiltInRole.Administrator))
  321. {
  322. //ProductVerification wins = new ProductVerification();
  323. //Application app = new Application();
  324. //app.Run(wins);
  325. W_LoginWindow = new LoginWindow();
  326. Application app = new Application();
  327. app.Run(W_LoginWindow);
  328. }
  329. else
  330. {
  331. //创建启动对象
  332. System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo
  333. {
  334. UseShellExecute = true,
  335. WorkingDirectory = Environment.CurrentDirectory,
  336. FileName = Assembly.GetExecutingAssembly().Location,
  337. //设置启动动作,确保以管理员身份运行
  338. Verb = "runas"
  339. };
  340. try
  341. {
  342. System.Diagnostics.Process.Start(startInfo);
  343. }
  344. catch
  345. {
  346. return;
  347. }
  348. //退出
  349. //Current.Shutdown();
  350. Environment.Exit(0);
  351. }
  352. }
  353. }
  354. catch (Exception ex)
  355. {
  356. string ErrMessage = "【进程】(Main):进程意外关闭。 " + ex.Message;
  357. LogHelper.WriteErrLog(ErrMessage, ex);
  358. }
  359. }
  360. #region 杀死已存在的进程
  361. /// <summary>
  362. /// 结束已运行的程序
  363. /// </summary>
  364. private static void StopSameProcess()
  365. {
  366. Process current = Process.GetCurrentProcess();
  367. Process[] processList = Process.GetProcesses();
  368. foreach (Process process in processList)
  369. {
  370. if (process.ProcessName.ToUpper() == "星火直播")
  371. {
  372. if (process.Id != current.Id) //忽略当前进程
  373. {
  374. try
  375. {
  376. process.Kill(); //结束进程
  377. }
  378. catch (Exception)
  379. {
  380. }
  381. }
  382. }
  383. }
  384. }
  385. /// <summary>
  386. /// 杀死正在运行的ffmpeg
  387. /// </summary>
  388. public static void Killffmpeg()
  389. {
  390. Process[] KillProcessArray = Process.GetProcessesByName("ffmpeg");
  391. foreach (Process KillProcess in KillProcessArray)
  392. {
  393. KillProcess.Kill();
  394. }
  395. }
  396. #endregion
  397. /// <summary>
  398. /// 错误处理
  399. /// </summary>
  400. /// <param name="sender"></param>
  401. /// <param name="e"></param>
  402. private void MyApp_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
  403. {
  404. LogHelper.WriteErrLog("未标示错误:" + e.Exception.Message, e.Exception);
  405. MessageWindow.Show("程序异常." + Environment.NewLine + e.Exception.Message);
  406. e.Handled = true;
  407. }
  408. #region 检测解压ffmpeg
  409. public void UnZipFFmpeg()
  410. {
  411. new Thread(o =>
  412. {
  413. if (!FileToolsCommon.IsExistDirectory(FileToolsCommon.GetFileAbsolutePath("/ffmpeg/")))
  414. {
  415. try
  416. {
  417. //根据系统解压ffmpeg
  418. if (Environment.Is64BitOperatingSystem)
  419. {
  420. //64
  421. ZipHelper.UnZip(FileToolsCommon.GetFileAbsolutePath("/ffmpegx64.zip"), FileToolsCommon.GetFileAbsolutePath());
  422. }
  423. else
  424. {
  425. //32
  426. ZipHelper.UnZip(FileToolsCommon.GetFileAbsolutePath("/ffmpegx32.zip"), FileToolsCommon.GetFileAbsolutePath());
  427. }
  428. }
  429. catch (Exception)
  430. {
  431. ZipHelper.UnZip(FileToolsCommon.GetFileAbsolutePath("/ffmpegx64.zip"), FileToolsCommon.GetFileAbsolutePath());
  432. }
  433. }
  434. }).Start();
  435. }
  436. #endregion
  437. #region 检测安装Screen Capturer Recorder
  438. /// <summary>
  439. /// 检测是否安装了Screen Capturer Recorder
  440. /// </summary>
  441. /// <returns></returns>
  442. public static bool CheckScreenCapturerRecorder()
  443. {
  444. if (FileToolsCommon.CheckSoftWartInstallState("Screen Capturer Recorder"))
  445. {
  446. //已经安装
  447. return true;
  448. }
  449. else
  450. {
  451. return false;
  452. }
  453. }
  454. /// <summary>
  455. /// 安装Screen Capturer Recorder
  456. /// </summary>
  457. /// <returns></returns>
  458. public static bool InstallScreenCapturerRecorder()
  459. {
  460. Process InstallProcess = new Process();
  461. string StandardError = "";
  462. string StandardOutput = "";
  463. try
  464. {
  465. InstallProcess.StartInfo.FileName = FileToolsCommon.GetFileAbsolutePath(@"/SSCR/Setup Screen Capturer Recorder v0.12.10.exe"); //绝对路径
  466. InstallProcess.StartInfo.UseShellExecute = false; //不使用操作系统外壳程序启动
  467. InstallProcess.StartInfo.RedirectStandardError = true; //重定向标准错误输出
  468. InstallProcess.StartInfo.RedirectStandardOutput = true;
  469. InstallProcess.StartInfo.CreateNoWindow = true; //不显示程序窗口
  470. InstallProcess.StartInfo.RedirectStandardInput = true; //用于模拟该进程控制台的输入
  471. //外部程序(这里是FFMPEG)输出流时候产生的事件,这里是把流的处理过程转移到下面的方法中,详细请查阅MSDN
  472. InstallProcess.ErrorDataReceived += new DataReceivedEventHandler(InstallProcess_ErrorDataReceived);
  473. InstallProcess.Start(); //启动线程
  474. InstallProcess.WaitForExit(); //阻塞等待进程结束
  475. StandardError = InstallProcess.StandardError.ReadToEnd();
  476. StandardOutput = InstallProcess.StandardOutput.ReadToEnd();
  477. int ExitCode = InstallProcess.ExitCode;
  478. InstallProcess.Close(); //关闭进程
  479. InstallProcess.Dispose(); //释放资源
  480. if (ExitCode == 0) //非0则为安装失败 1为取消安装
  481. {
  482. return true;
  483. }
  484. else
  485. {
  486. MessageBoxResult br = MessageWindow.Show("不安装环境将无法使用微课录制系统,是否现在安装?", "安装", MessageBoxButton.OKCancel);
  487. if (br == MessageBoxResult.OK)
  488. {
  489. return InstallScreenCapturerRecorder();
  490. }
  491. else
  492. {
  493. return false;
  494. }
  495. }
  496. }
  497. catch (Exception ex)
  498. {
  499. LogHelper.WriteErrLog("【ScreenCapturerRecorder安装】(installScreenCapturerRecorder)安装失败:" + ex.Message, ex);
  500. if (string.IsNullOrWhiteSpace(StandardError))
  501. {
  502. LogHelper.WriteErrLog("【ScreenCapturerRecorder安装】(installScreenCapturerRecorder)安装失败:" + StandardError, null);
  503. }
  504. if (string.IsNullOrWhiteSpace(StandardOutput))
  505. {
  506. LogHelper.WriteErrLog("【ScreenCapturerRecorder安装】(installScreenCapturerRecorder)安装返回信息:" + StandardOutput, null);
  507. }
  508. return false;
  509. }
  510. }
  511. /// <summary>
  512. /// 安装失败信息
  513. /// </summary>
  514. /// <param name="sender"></param>
  515. /// <param name="e"></param>
  516. private static void InstallProcess_ErrorDataReceived(object sender, DataReceivedEventArgs e)
  517. {
  518. LogHelper.WriteErrLog("【ScreenCapturerRecorder安装】(installScreenCapturerRecorder)安装失败:" + e.Data, null);
  519. //MessageWindow.Show(e.Data);
  520. }
  521. #endregion
  522. #region 内存处理 -创建人:赵耀 -创建时间:2020年8月12日
  523. /// <summary>
  524. /// 内存释放压缩
  525. /// </summary>
  526. /// <param name="e"></param>
  527. protected override void OnStartup(StartupEventArgs e)
  528. {
  529. //private LierdaCracker cracker = new LierdaCracker();
  530. //cracker.Cracker(5000);//间隔时间
  531. //base.OnStartup(e);
  532. new Thread(o =>
  533. {
  534. while (true)
  535. {
  536. try
  537. {
  538. //FreeMemoryHelper.SetDate();
  539. FreeMemoryHelper.ReallocateMemory();
  540. Thread.Sleep(20000);//暂定每20秒释放一次
  541. }
  542. catch (Exception ex)
  543. {
  544. LogHelper.WriteErrLog("【释放内存(MyApp)" + ex.Message, ex);
  545. }
  546. }
  547. }).Start();
  548. }
  549. #endregion 内存处理 -创建人:赵耀 -创建时间:2020年8月12日
  550. #region 测试
  551. public static void AddTestData()
  552. {
  553. testdata = 1;
  554. }
  555. /// <summary>
  556. /// 图片真实路径
  557. /// </summary>
  558. /// <returns></returns>
  559. public static string classRoomImagePath()
  560. {
  561. //string path = $"{nginxHtml}school\\{ZCommonData.loginUser.schoolid}\\class\\{ZCommonData.roomid}\\";
  562. string path = AppDomain.CurrentDomain.BaseDirectory + "temp\\"; ;
  563. return path;
  564. }
  565. /// <summary>
  566. /// 本地服务器存储路径
  567. /// </summary>
  568. /// <returns></returns>
  569. public static string dbImagePath()
  570. {
  571. //string path = $"school\\{ZCommonData.loginUser.schoolid}\\class\\{ZCommonData.roomid}\\";
  572. string path = AppDomain.CurrentDomain.BaseDirectory + "temp\\";
  573. return path;
  574. }
  575. /// <summary>
  576. /// 获取图片保存路径 设置服务器存储路径 创建人:赵耀 创建时间:2020年8月11日
  577. /// </summary>
  578. /// <returns></returns>
  579. public static string GetImagePath(out string ServerSavePath)
  580. {
  581. string imagepath = APP.classRoomImagePath();
  582. if (!Directory.Exists(imagepath))
  583. {
  584. Directory.CreateDirectory(imagepath);
  585. }
  586. TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
  587. string timestr = Convert.ToInt64(ts.TotalMilliseconds).ToString();
  588. string filepath = Path.Combine(imagepath, timestr + ".jpg");
  589. ServerSavePath = APP.dbImagePath() + timestr + ".jpg";
  590. return filepath;
  591. }
  592. #endregion
  593. }
  594. }