星火微课系统客户端
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

App.xaml.cs 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  1. using ComeCapture;
  2. using Common.system;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Reflection;
  9. using System.Security.Principal;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. using System.Windows;
  13. using System.Windows.Threading;
  14. using TStudyDigitalPen.HID;
  15. using XHWK.Model;
  16. using XHWK.WKTool.Helpers;
  17. using XHWK.WKTool.Skin;
  18. namespace XHWK.WKTool
  19. {
  20. public partial class APP : Application
  21. {
  22. #region 全局变量
  23. #region 更新需改动
  24. /// <summary>
  25. /// 是否为测试版
  26. /// </summary>
  27. public static bool isDebug = FileToolsCommon.GetConfigValue("IsDebug") != "0";
  28. /// <summary>
  29. /// 参数是否加密
  30. /// </summary>
  31. public static bool IsParameterEncryption = FileToolsCommon.GetConfigValue("IsParameterEncryption") != "0";
  32. #endregion 更新需改动
  33. #region 接口地址
  34. /// <summary>
  35. /// 是否为校外接口
  36. /// </summary>
  37. public static bool IsOutsideSchool = FileToolsCommon.GetConfigValue("IsOutsideSchool") == "1";
  38. /// <summary>
  39. /// 服务地址
  40. /// </summary>
  41. public static Model_ServiceAddress ServiceAddress = null;
  42. /// <summary>
  43. /// 接口地址
  44. /// </summary>
  45. public static string apiUrl = isDebug ? "http://schoolapitest.xhkjedu.com" : FileToolsCommon.GetConfigValue("APIRequestAddress");
  46. /// <summary>
  47. /// 图片地址
  48. /// </summary>
  49. public static string uploadUrl = isDebug ? "http://schoolfiletest.xhkjedu.com/" : FileToolsCommon.GetConfigValue("FileRequestAddress");
  50. /// <summary>
  51. /// 展示文件
  52. /// </summary>
  53. public static string showImageUrl = isDebug ? "http://schoolstatictest.xhkjedu.com/static/" : FileToolsCommon.GetConfigValue("SchoolfileRequestAddress") + "/static/";
  54. /// <summary>
  55. /// 认证接口地址
  56. /// </summary>
  57. public static string certapiUrl = isDebug ? "http://scapitest.xhkjedu.com" : FileToolsCommon.GetConfigValue("CertapiRequestAddress");
  58. #endregion 接口地址
  59. #region 配置项
  60. /// <summary>
  61. /// 是否输出测试记录日志
  62. /// </summary>
  63. public static bool IsOutputInfoLog = FileToolsCommon.GetConfigValue("IsOutputInfoLog") != "0";
  64. /// <summary>
  65. /// 是否隐藏录屏工具栏
  66. /// </summary>
  67. public static bool IsHideSRTool = FileToolsCommon.GetConfigValue("IsHideSRTool") != "0";
  68. /// <summary>
  69. /// 皮肤样式 0白 1蓝 2黑色
  70. /// </summary>
  71. public static string SkinStyle = FileToolsCommon.GetConfigValue("SkinStyle");
  72. /// <summary>
  73. /// 数据存放目录
  74. /// </summary>
  75. public static string DataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\XHMicroLesson\\";
  76. /// <summary>
  77. /// 摄像头位置
  78. /// </summary>
  79. public static string CameraPosition = string.Empty;
  80. /// <summary>
  81. /// 摄像头名
  82. /// </summary>
  83. public static string CameraName = "";
  84. /// <summary>
  85. /// 麦克风名
  86. /// </summary>
  87. public static string MicrophoneName = "";
  88. #endregion 配置项
  89. #region 记录数据
  90. /// <summary>
  91. /// 全局页面数据变量
  92. /// </summary>
  93. public static readonly Model_Page PageContextData = new Model.Model_Page();
  94. /// <summary>
  95. /// 录屏工具
  96. /// </summary>
  97. public static FFMpeg FFmpeg = new FFMpeg();
  98. /// <summary>
  99. /// 后台线程帮助类
  100. /// </summary>
  101. public static BackgroundWorkerHelper BackgroundWorkerHelper => BackgroundWorkerHelper.GetInstance();
  102. /// <summary>
  103. /// 签名
  104. /// </summary>
  105. public static string Signature = "";
  106. public static string secretKey = "nanhuakaizhangjianwangni";
  107. /// <summary>
  108. /// 用户信息
  109. /// </summary>
  110. public static Model_UserInfo UserInfo = null;
  111. /// <summary>
  112. /// 接口返回消息
  113. /// </summary>
  114. public static string ServerMsg = string.Empty;
  115. /// <summary>
  116. /// 登录状态 false 未登录 true已登录
  117. /// </summary>
  118. public static bool IsLoginType = false;
  119. /// <summary>
  120. /// 截图地址
  121. /// </summary>
  122. public static string ImgPath = string.Empty;
  123. /// <summary>
  124. /// 教材列表
  125. /// </summary>
  126. public static List<Model_TsubjectbookList> TsubjectbookList = null;
  127. /// <summary>
  128. /// 章节列表
  129. /// </summary>
  130. public static List<Model_DirectorList> DirectorList = null;
  131. /// <summary>
  132. /// 上传个人空间 入参
  133. /// </summary>
  134. public static Model_ResourceAddTwo ResourceAddTwo = null;
  135. /// <summary>
  136. /// 是否正在上传
  137. /// </summary>
  138. public static bool IsUpLoad = false;
  139. #endregion 记录数据
  140. #region 微课数据记录
  141. /// <summary>
  142. /// 用户微课列表模型
  143. /// </summary>
  144. public static List<Model_WKData> WKDataList = null;
  145. /// <summary>
  146. /// 微课模型
  147. /// </summary>
  148. public static Model_WKData WKData = null;
  149. /// <summary>
  150. /// 微课视频列表
  151. /// </summary>
  152. public static List<Model_Video> VideoList = null;
  153. /// <summary>
  154. /// 画板模型
  155. /// </summary>
  156. public static List<Model_DrawData> PageDrawList = null;
  157. #endregion 微课数据记录
  158. #region 点阵笔
  159. /// <summary>
  160. /// 点阵笔
  161. /// </summary>
  162. public static DigitalPenHID digitalPen;
  163. /// <summary>
  164. /// 笔序号
  165. /// </summary>
  166. public static string PenSerial;
  167. /// <summary>
  168. /// 笔状态,是否已连接
  169. /// </summary>
  170. public static bool PenStatus = false;
  171. /// <summary>
  172. /// 手写板状态,是否已连接
  173. /// </summary>
  174. public static bool BoardStatus = false;
  175. #endregion 点阵笔
  176. #region 腾千里手写笔
  177. ///// <summary>
  178. ///// 铺码服务请求
  179. ///// </summary>
  180. //public static DAL_TmatrixCode dAL_TmatrixCode;
  181. /// <summary>
  182. /// 腾千里手写笔事件
  183. /// </summary>
  184. public static PenEvents TQLPenevents;
  185. /// <summary>
  186. /// 腾千里手写笔是否已连接
  187. /// </summary>
  188. public static bool TQLPenStatus = false;
  189. #region 打印
  190. public static bool gbGenerateBGWithVImage = false;
  191. public static bool gbGenerateVImage = false;
  192. public static bool gbGenerateBGWithoutVImage = false;
  193. public static bool gbGenerateBGWithImage = false;
  194. public static bool gbGenPageSet = false;
  195. public static int[] gPointType = new int[5];
  196. public static int[] gPointDPI = new int[5];
  197. public static string gTMXElementFileName;
  198. public static string gNewProjectDirectory;
  199. public static bool gbElementFileExist { get; set; }
  200. #endregion 打印
  201. #endregion 腾千里手写笔
  202. #region 页面
  203. /// <summary>
  204. /// 主页面
  205. /// </summary>
  206. public static MainWindow W_XHMicroLessonSystemWindow = null;
  207. /// <summary>
  208. /// 创建微课页面
  209. /// </summary>
  210. public static CreateAMicroLessonWindow W_CreateAMicroLessonWindow = null;
  211. /// <summary>
  212. /// 开始录像
  213. /// </summary>
  214. public static CountdownWindow W_CountdownWindow = null;
  215. /// <summary>
  216. /// 录像工具栏
  217. /// </summary>
  218. public static ScreenRecordingToolbarWindow W_ScreenRecordingToolbarWindow = null;
  219. /// <summary>
  220. /// 登录
  221. /// </summary>
  222. public static LoginWindow W_LoginWindow = null;
  223. /// <summary>
  224. /// 截图
  225. /// </summary>
  226. public static JieTuWindow W_JieTuWindow = null;
  227. /// <summary>
  228. /// 批注
  229. /// </summary>
  230. public static PracticeWindow W_PracticeWindow = null;
  231. /// <summary>
  232. /// 提示窗口
  233. /// </summary>
  234. public static PromptWindow W_PromptWindow = null;
  235. /// <summary>
  236. /// loding页面
  237. /// </summary>
  238. public static LoadDialog myloading;
  239. /// <summary>
  240. /// 上传页面
  241. /// </summary>
  242. public static UploadWindow W_UploadWindow = null;
  243. /// <summary>
  244. /// 最小化工具栏
  245. /// </summary>
  246. public static MinToolbar W_MinToolbar = null;
  247. /// <summary>
  248. /// 视频剪辑
  249. /// </summary>
  250. public static VideoClipWindow W_VideoClipWindow = null;
  251. #endregion 页面
  252. #endregion 全局变量
  253. public APP()
  254. {
  255. Console.WriteLine("初始化APP");
  256. LogHelper.InitLog4Net();
  257. myloading = new LoadDialog();
  258. StopSameProcess();
  259. Killffmpeg();
  260. try
  261. {
  262. UserInfo = new Model_UserInfo();
  263. WKDataList = new List<Model_WKData>();
  264. VideoList = new List<Model_Video>();
  265. #region 强制以管理员方式运行 修改人:赵耀 修改时间:2020年9月7日
  266. WindowsIdentity identity = WindowsIdentity.GetCurrent();
  267. WindowsPrincipal principal = new WindowsPrincipal(identity);
  268. #endregion 强制以管理员方式运行 修改人:赵耀 修改时间:2020年9月7日
  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. if (principal.IsInRole(WindowsBuiltInRole.Administrator))
  280. {
  281. SkinConfig.InitSkin();
  282. }
  283. else
  284. {
  285. //创建启动对象
  286. System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo
  287. {
  288. UseShellExecute = true,
  289. WorkingDirectory = Environment.CurrentDirectory,
  290. FileName = Assembly.GetExecutingAssembly().Location,
  291. //设置启动动作,确保以管理员身份运行
  292. Verb = "runas"
  293. };
  294. try
  295. {
  296. System.Diagnostics.Process.Start(startInfo);
  297. }
  298. catch
  299. {
  300. return;
  301. }
  302. //退出
  303. //Current.Shutdown();
  304. Environment.Exit(0);
  305. }
  306. }
  307. catch (Exception ex)
  308. {
  309. string ErrMessage = "【进程】(Main):进程意外关闭。 " + ex.Message;
  310. LogHelper.WriteErrLog(ErrMessage, ex);
  311. }
  312. }
  313. #region 杀死已存在的进程
  314. /// <summary>
  315. /// 结束已运行的程序
  316. /// </summary>
  317. private static void StopSameProcess()
  318. {
  319. Process current = Process.GetCurrentProcess();
  320. Process[] processList = Process.GetProcesses();
  321. foreach (Process process in processList)
  322. {
  323. if (process.ProcessName.ToUpper() == "星火微课")
  324. {
  325. if (process.Id != current.Id) //忽略当前进程
  326. {
  327. try
  328. {
  329. process.Kill(); //结束进程
  330. }
  331. catch (Exception)
  332. {
  333. }
  334. }
  335. }
  336. }
  337. }
  338. /// <summary>
  339. /// 杀死正在运行的ffmpeg
  340. /// </summary>
  341. public static void Killffmpeg()
  342. {
  343. Process[] KillProcessArray = Process.GetProcessesByName("ffmpeg");
  344. foreach (Process KillProcess in KillProcessArray)
  345. {
  346. KillProcess.Kill();
  347. }
  348. }
  349. #endregion 杀死已存在的进程
  350. private void RegisterEvents()
  351. {
  352. //Task线程内未捕获异常处理事件
  353. TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;//Task异常
  354. //UI线程未捕获异常处理事件(UI主线程)
  355. DispatcherUnhandledException += App_DispatcherUnhandledException;
  356. //非UI线程未捕获异常处理事件(例如自己创建的一个子线程)
  357. AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
  358. }
  359. private static void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
  360. {
  361. try
  362. {
  363. var exception = e.Exception as Exception;
  364. if (exception != null)
  365. {
  366. HandleException(exception);
  367. }
  368. }
  369. catch (Exception ex)
  370. {
  371. HandleException(ex);
  372. }
  373. finally
  374. {
  375. e.SetObserved();
  376. }
  377. }
  378. //非UI线程未捕获异常处理事件(例如自己创建的一个子线程)
  379. private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  380. {
  381. try
  382. {
  383. var exception = e.ExceptionObject as Exception;
  384. if (exception != null)
  385. {
  386. HandleException(exception);
  387. }
  388. }
  389. catch (Exception ex)
  390. {
  391. HandleException(ex);
  392. }
  393. finally
  394. {
  395. //ignore
  396. }
  397. }
  398. //UI线程未捕获异常处理事件(UI主线程)
  399. private static void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
  400. {
  401. try
  402. {
  403. HandleException(e.Exception);
  404. }
  405. catch (Exception ex)
  406. {
  407. HandleException(ex);
  408. }
  409. finally
  410. {
  411. e.Handled = true;
  412. }
  413. }
  414. private static void HandleException(Exception ex)
  415. {
  416. //记录日志
  417. LogHelper.WriteErrLog("未捕获异常:" + ex.Message, ex);
  418. Current.Shutdown();
  419. }
  420. #region 数据保存和读取
  421. /// <summary>
  422. /// 保存微课信息
  423. /// </summary>
  424. public static void SaveWkData()
  425. {
  426. try
  427. {
  428. VideoList = VideoList.Where((x, i) => VideoList.FindIndex(z => z.FileGuid == x.FileGuid) == i).ToList();
  429. WKData.VideoList = VideoList;
  430. if (WKDataList != null)
  431. {
  432. WKDataList.RemoveAll(x => x.WkPath == WKData.WkPath);
  433. }
  434. else
  435. {
  436. WKDataList = new List<Model_WKData>();
  437. }
  438. WKDataList.Add(WKData);
  439. //string WkDateXmlStr = XmlUtilHelper.Serializer(typeof(Model_WKData), WKData);
  440. string WkDateXmlStr = XmlUtilHelper.XmlSerialize(WKDataList);
  441. string SavePath = FileToolsCommon.GetFileAbsolutePath("/Data/");
  442. FileToolsCommon.CreateDirectory(SavePath);
  443. string SaveName = SavePath + "WkDate.d";
  444. FileToolsCommon.DeleteFile(SaveName);
  445. FileToolsCommon.WriteText(SaveName, WkDateXmlStr);
  446. }
  447. catch (Exception ex)
  448. {
  449. LogHelper.WriteErrLog("【微课数据保存】(SaveWkData)保存失败:" + ex.Message, ex);
  450. }
  451. }
  452. /// <summary>
  453. /// 读取微课信息
  454. /// </summary>
  455. public static void ReadWkData(string WkPath)
  456. {
  457. try
  458. {
  459. string SavePath = FileToolsCommon.GetFileAbsolutePath("/Data/");
  460. FileToolsCommon.CreateDirectory(SavePath);
  461. string SaveName = SavePath + "WkDate.d";
  462. //文件若存在则读取
  463. if (FileToolsCommon.IsExistFile(SaveName))
  464. {
  465. string WkDateXmlStr = FileToolsCommon.FileToString(SaveName);
  466. WKDataList = XmlUtilHelper.DESerializer<List<Model_WKData>>(WkDateXmlStr);
  467. foreach (Model_WKData wklist in WKDataList)
  468. {
  469. //移除找不到视频的微课数据
  470. wklist.VideoList.RemoveAll(x => !FileToolsCommon.IsExistFile(x.VideoPath));
  471. }
  472. if (WKDataList.Exists(x => x.WkPath == WkPath))
  473. {
  474. WKData = WKDataList.Find(x => x.WkPath == WkPath);
  475. VideoList = new List<Model_Video>();
  476. WKData.VideoList = WKData.VideoList.Where((x, i) => WKData.VideoList.FindIndex(z => z.FileGuid == x.FileGuid) == i).ToList();
  477. VideoList = WKData.VideoList;
  478. }
  479. }
  480. }
  481. catch (Exception ex)
  482. {
  483. LogHelper.WriteErrLog("【微课数据读取】(ReadWkData)读取失败:" + ex.Message, ex);
  484. }
  485. }
  486. /// <summary>
  487. /// 保存画板数据
  488. /// </summary>
  489. public static void SaveDraw()
  490. {
  491. try
  492. {
  493. if (PageDrawList == null)
  494. {
  495. return;
  496. }
  497. if (PageDrawList.Count < 1)
  498. {
  499. return;
  500. }
  501. FileToolsCommon.CreateDirectory(WKData.WkPath);
  502. string SavePath = WKData.WkPath + "PageData.d";
  503. FileToolsCommon.DeleteFile(SavePath);
  504. string PageDataXmlStr = XmlUtilHelper.XmlSerialize(PageDrawList);
  505. FileToolsCommon.WriteText(SavePath, PageDataXmlStr);
  506. }
  507. catch (Exception ex)
  508. {
  509. LogHelper.WriteErrLog("【画板数据保存】(SaveDraw)保存失败:" + ex.Message, ex);
  510. }
  511. }
  512. /// <summary>
  513. /// 读取文件
  514. /// </summary>
  515. public static void ReadDrawData()
  516. {
  517. try
  518. {
  519. PageDrawList = new List<Model_DrawData>();
  520. string SavePath = WKData.WkPath + "PageData.d";
  521. if (FileToolsCommon.IsExistFile(SavePath))
  522. {
  523. string PageDataXmlStr = FileToolsCommon.FileToString(SavePath);
  524. PageDrawList = XmlUtilHelper.DESerializer<List<Model_DrawData>>(PageDataXmlStr);
  525. }
  526. else
  527. {
  528. PageDrawList = new List<Model_DrawData>();
  529. }
  530. }
  531. catch (Exception ex)
  532. {
  533. LogHelper.WriteErrLog("【画板数据读取】(ReadDraw)读取失败:" + ex.Message, ex);
  534. }
  535. }
  536. #endregion 数据保存和读取
  537. #region 服务地址数据
  538. /// <summary>
  539. /// 服务地址存储到本地
  540. /// </summary>
  541. public static void SaveServiceAddressData()
  542. {
  543. try
  544. {
  545. if (ServiceAddress == null)
  546. {
  547. return;
  548. }
  549. string DateXmlStr = XmlUtilHelper.XmlSerialize(ServiceAddress);
  550. string SavePath = DataPath;
  551. FileToolsCommon.CreateDirectory(SavePath);
  552. string SaveName = SavePath + (isDebug ? "ServiceAddress_debug.xml" : "ServiceAddress_release.xml");
  553. Console.WriteLine("保存路径为:");
  554. FileToolsCommon.DeleteFile(SaveName);
  555. FileToolsCommon.WriteText(SaveName, DateXmlStr);
  556. }
  557. catch (Exception ex)
  558. {
  559. LogHelper.WriteErrLog("【服务地址存储】(SaveServiceAddressData)保存失败:" + ex.Message, ex);
  560. }
  561. }
  562. /// <summary>
  563. /// 从本地读取服务地址
  564. /// </summary>
  565. public static void ReadServiceAddressData()
  566. {
  567. try
  568. {
  569. string SavePath = DataPath;
  570. FileToolsCommon.CreateDirectory(SavePath);
  571. string SaveName = SavePath + (isDebug ? "ServiceAddress_debug.xml" : "ServiceAddress_release.xml");
  572. //文件若存在则读取
  573. if (FileToolsCommon.IsExistFile(SaveName))
  574. {
  575. string DateXmlStr = FileToolsCommon.FileToString(SaveName);
  576. ServiceAddress = XmlUtilHelper.DESerializer<Model_ServiceAddress>(DateXmlStr);
  577. SwitchAddress();
  578. }
  579. }
  580. catch (Exception ex)
  581. {
  582. LogHelper.WriteErrLog("【服务地址读取】(ReadServiceAddressData)读取失败:" + ex.Message, ex);
  583. }
  584. }
  585. /// <summary>
  586. /// 切换校内外网
  587. /// </summary>
  588. public static void SwitchAddress()
  589. {
  590. try
  591. {
  592. apiUrl = IsOutsideSchool ? ServiceAddress.wapi : ServiceAddress.napi;
  593. uploadUrl = (IsOutsideSchool ? ServiceAddress.wfile : ServiceAddress.nfile) + "/";
  594. showImageUrl = (IsOutsideSchool ? ServiceAddress.wdown : ServiceAddress.ndown) + "/static/";
  595. }
  596. catch (Exception ex)
  597. {
  598. LogHelper.WriteErrLog("【服务地址切换】(SwitchAddress)服务复制切换失败,请求地址为空!", ex);
  599. }
  600. }
  601. #endregion 服务地址数据
  602. /// <summary>
  603. /// 内存释放压缩
  604. /// </summary>
  605. /// <param name="e">
  606. /// </param>
  607. protected override void OnStartup(StartupEventArgs e)
  608. {
  609. RegisterEvents();
  610. base.OnStartup(e);
  611. }
  612. }
  613. }