星火微课系统客户端
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

App.xaml.cs 22KB

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