星火直播PC
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

App.cs 22KB

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