ChatGPTAITranslationContentViewModel.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. using ComPDFKit.PDFDocument;
  2. using ComPDFKitViewer;
  3. using ComPDFKitViewer.PdfViewer;
  4. using Microsoft.Office.Core;
  5. using Microsoft.Office.Interop.Word;
  6. using Microsoft.Win32;
  7. using PDF_Master.CustomControl;
  8. using PDF_Master.Helper;
  9. using PDF_Master.Model;
  10. using PDF_Master.Properties;
  11. using Prism.Commands;
  12. using Prism.Mvvm;
  13. using Prism.Regions;
  14. using Prism.Services.Dialogs;
  15. using System;
  16. using System.Collections.Generic;
  17. using System.Diagnostics;
  18. using System.IO;
  19. using System.Linq;
  20. using System.Threading.Tasks;
  21. using System.Windows;
  22. using System.Windows.Threading;
  23. using Task = System.Threading.Tasks.Task;
  24. namespace PDF_Master.ViewModels.HomePanel.ChatGPTAI
  25. {
  26. public class ChatGPTAITranslationContentViewModel : BindableBase, INavigationAware
  27. {
  28. #region 参数和属性
  29. public HomeContentViewModel homeContentViewModel = null;
  30. /// <summary>
  31. /// 初始化需要翻译的内容语言
  32. /// </summary>
  33. public string fromlanguage = ChatGTPAIHelper.UpdateLanguagebType(0);
  34. /// <summary>
  35. /// 初始化翻译后的语言
  36. /// </summary>
  37. public string tolanguage = ChatGTPAIHelper.UpdateLanguagebType(1);
  38. private int fromlanguageIndex = 0;
  39. public int FromlanguageIndex
  40. {
  41. get { return fromlanguageIndex; }
  42. set
  43. {
  44. SetProperty(ref fromlanguageIndex, value);
  45. fromlanguage = ChatGTPAIHelper.UpdateLanguagebType(value);
  46. }
  47. }
  48. private int tolanguageIndex = 0;
  49. public int TolanguageIndex
  50. {
  51. get { return tolanguageIndex; }
  52. set
  53. {
  54. SetProperty(ref tolanguageIndex, value);
  55. tolanguage = ChatGTPAIHelper.UpdateLanguagebType(value + 1);
  56. }
  57. }
  58. private Visibility errorTipVisible = Visibility.Collapsed;
  59. public Visibility ErrorTipVisible
  60. {
  61. get
  62. {
  63. return errorTipVisible;
  64. }
  65. set
  66. {
  67. SetProperty(ref errorTipVisible, value);
  68. if (value == Visibility.Visible)
  69. {
  70. dispatcherTimer.Start();
  71. }
  72. }
  73. }
  74. private string errorTipText = "The uploaded file cannot exceed 10MB";
  75. public string ErrorTipText
  76. {
  77. get
  78. {
  79. return errorTipText;
  80. }
  81. set
  82. {
  83. SetProperty(ref errorTipText, value);
  84. }
  85. }
  86. /// <summary>
  87. /// 初始化翻译前语言列表
  88. /// </summary>
  89. public static List<string> FromlanguageFamily { set; get; } = new List<string>();
  90. private void GetFromlanguageOrigin()
  91. {
  92. FromlanguageFamily.Clear();
  93. FromlanguageFamily = ChatGTPAIHelper.SetFromlanguageOrigin();
  94. }
  95. /// <summary>
  96. /// 初始化翻译到语言列表
  97. /// </summary>
  98. public List<string> TolanguageFamily { set; get; } = new List<string>();
  99. private void GetTolanguageOrigin()
  100. {
  101. TolanguageFamily.Clear();
  102. TolanguageFamily = ChatGTPAIHelper.SetTolanguageOrigin();
  103. }
  104. /// <summary>
  105. /// 设置错误提示显示时间
  106. /// </summary>
  107. private DispatcherTimer dispatcherTimer = new DispatcherTimer();
  108. #endregion
  109. #region 委托声明
  110. public DelegateCommand SelectFilesCommand { get; set; }
  111. #endregion
  112. public ChatGPTAITranslationContentViewModel()
  113. {
  114. SelectFilesCommand = new DelegateCommand(selectFiles);
  115. dispatcherTimer.Interval = TimeSpan.FromSeconds(3);
  116. dispatcherTimer.Tick += Dispatchertimer_Tick;
  117. init();
  118. }
  119. #region 逻辑函数
  120. /// <summary>
  121. /// 初始化下拉列表,或其他
  122. /// </summary>
  123. private void init()
  124. {
  125. GetFromlanguageOrigin();
  126. GetTolanguageOrigin();
  127. }
  128. /// <summary>
  129. /// 控制错误提示
  130. /// </summary>
  131. /// <param name="sender"></param>
  132. /// <param name="e"></param>
  133. private void Dispatchertimer_Tick(object sender, EventArgs e)
  134. {
  135. ErrorTipVisible = Visibility.Collapsed;
  136. dispatcherTimer.Stop();
  137. }
  138. /// <summary>
  139. /// 选择文件逻辑翻译,word pdf
  140. /// </summary>
  141. public async void selectFiles()
  142. {
  143. //添加付费拦截锁
  144. if (!ServiceHelper.IAPBeforeFunction())
  145. {
  146. return;
  147. }
  148. if (Settings.Default.UserDate.subscribestatus != 1)
  149. {
  150. App.mainWindowViewModel.dialogs.ShowDialog(DialogNames.IAPCompareDialog);
  151. return;
  152. }
  153. string word = Properties.Resources.wordex;
  154. string pdf = Properties.Resources.pdf;
  155. string allfiles = pdf + word;
  156. OpenFileDialog dialog = new OpenFileDialog();
  157. //dialog.Multiselect = true;
  158. dialog.Filter = string.Format($"Files({allfiles.Replace(";", ",")}|{allfiles})|" +
  159. $"Pdf({pdf})|{pdf}|" +
  160. $"Microsoft Office Word({word})|{word}");
  161. if ((bool)dialog.ShowDialog())
  162. {
  163. ErrorTipVisible = Visibility.Collapsed;
  164. FileInfo fileInfo = new FileInfo(dialog.FileName);
  165. //判断文件大小
  166. if ((((float)fileInfo.Length) / 1024 / 1024) > 10)
  167. {
  168. ErrorTipText = "The uploaded file cannot exceed 10MB";
  169. ErrorTipVisible = Visibility.Visible;
  170. return;
  171. }
  172. //判断文件页数
  173. if (fileInfo.Extension.ToLower() == ".pdf")
  174. {
  175. CPDFDocument cPDFDocument = CPDFDocument.InitWithFilePath(dialog.FileName);
  176. if (cPDFDocument.PageCount > 30)
  177. {
  178. ErrorTipText = "The uploaded file cannot exceed 10MB";
  179. ErrorTipVisible = Visibility.Visible;
  180. return;
  181. }
  182. }
  183. else
  184. {
  185. try
  186. {
  187. Microsoft.Office.Interop.Word.Application myWordApp = new Microsoft.Office.Interop.Word.Application();
  188. object Nothing = System.Reflection.Missing.Value;
  189. object oMissing = System.Reflection.Missing.Value;
  190. object filePath = dialog.FileName; //这里是Word文件的路径
  191. //打开文件
  192. Document myWordDoc = myWordApp.Documents.Open(
  193. ref filePath, ref oMissing, ref oMissing, ref oMissing,
  194. ref oMissing, ref oMissing, ref oMissing, ref oMissing,
  195. ref oMissing, ref oMissing, ref oMissing, ref oMissing,
  196. ref oMissing, ref oMissing, ref oMissing, ref oMissing);
  197. //下面是取得打开文件的页数
  198. int pages = myWordDoc.ComputeStatistics(WdStatistic.wdStatisticPages, ref Nothing);
  199. //关闭文件
  200. myWordDoc.Close(ref oMissing, ref oMissing, ref oMissing);
  201. //退出Word程序
  202. myWordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
  203. if (pages > 30)
  204. {
  205. ErrorTipText = "The uploaded file cannot exceed 30pages";
  206. ErrorTipVisible = Visibility.Visible;
  207. return;
  208. }
  209. }
  210. catch
  211. {
  212. }
  213. }
  214. string newfile = "";
  215. await Task.Run(async delegate
  216. {
  217. newfile = await ChatGTPAIHelper.fileTranslate(dialog.FileName, fromlanguage, tolanguage);
  218. });
  219. if (!string.IsNullOrEmpty(newfile))
  220. {
  221. if (File.Exists(newfile))
  222. {
  223. if (homeContentViewModel != null)
  224. {
  225. homeContentViewModel.OpenFile(new string[] { newfile });
  226. return;
  227. }
  228. }
  229. }
  230. //newfile code
  231. ErrorTipText = newfile;
  232. ErrorTipVisible = Visibility.Visible;
  233. }
  234. }
  235. #endregion
  236. #region 构架行为
  237. public void OnNavigatedTo(NavigationContext navigationContext)
  238. {
  239. navigationContext.Parameters.TryGetValue<HomeContentViewModel>(ParameterNames.HomeContentViewModel, out homeContentViewModel);
  240. }
  241. public bool IsNavigationTarget(NavigationContext navigationContext)
  242. {
  243. return true;
  244. }
  245. public void OnNavigatedFrom(NavigationContext navigationContext)
  246. {
  247. }
  248. #endregion
  249. }
  250. }