ChatGPTAITranslationContentViewModel.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. private string T_title;
  30. public string T_Title
  31. {
  32. get { return T_title; }
  33. set
  34. {
  35. SetProperty(ref T_title, value);
  36. }
  37. }
  38. private string T_supportTip;
  39. public string T_SupportTip
  40. {
  41. get { return T_supportTip; }
  42. set
  43. {
  44. SetProperty(ref T_supportTip, value);
  45. }
  46. }
  47. private string T_limitTip;
  48. public string T_LimitTip
  49. {
  50. get { return T_limitTip; }
  51. set
  52. {
  53. SetProperty(ref T_limitTip, value);
  54. }
  55. }
  56. private string T_translationLanguage;
  57. public string T_TranslationLanguage
  58. {
  59. get { return T_translationLanguage; }
  60. set
  61. {
  62. SetProperty(ref T_translationLanguage, value);
  63. }
  64. }
  65. private string T_selectFileBtn;
  66. public string T_SelectFileBtn
  67. {
  68. get { return T_selectFileBtn; }
  69. set
  70. {
  71. SetProperty(ref T_selectFileBtn, value);
  72. }
  73. }
  74. private string T_dropTip;
  75. public string T_DropTip
  76. {
  77. get { return T_dropTip; }
  78. set
  79. {
  80. SetProperty(ref T_dropTip, value);
  81. }
  82. }
  83. private void InitString()
  84. {
  85. T_Title = App.HomePageLoader.GetString("AITranslationBtn_Title");
  86. T_SupportTip = App.HomePageLoader.GetString("AITranslationBtn_SupportTip");
  87. T_LimitTip = App.HomePageLoader.GetString("AITranslationBtn_LimitTip");
  88. T_TranslationLanguage = App.HomePageLoader.GetString("AITranslationBtn_TranslationLanguage");
  89. T_SelectFileBtn = App.HomePageLoader.GetString("AITranslationBtn_SelectFileBtn");
  90. T_DropTip = App.HomePageLoader.GetString("AITranslationBtn_DropTip");
  91. }
  92. #endregion
  93. #region 参数和属性
  94. public HomeContentViewModel homeContentViewModel = null;
  95. /// <summary>
  96. /// 初始化需要翻译的内容语言
  97. /// </summary>
  98. public string fromlanguage = ChatGTPAIHelper.UpdateLanguagebType(0);
  99. /// <summary>
  100. /// 初始化翻译后的语言
  101. /// </summary>
  102. public string tolanguage = ChatGTPAIHelper.UpdateLanguagebType(1);
  103. private int fromlanguageIndex = 0;
  104. public int FromlanguageIndex
  105. {
  106. get { return fromlanguageIndex; }
  107. set
  108. {
  109. SetProperty(ref fromlanguageIndex, value);
  110. fromlanguage = ChatGTPAIHelper.UpdateLanguagebType(value);
  111. }
  112. }
  113. private int tolanguageIndex = 0;
  114. public int TolanguageIndex
  115. {
  116. get { return tolanguageIndex; }
  117. set
  118. {
  119. SetProperty(ref tolanguageIndex, value);
  120. tolanguage = ChatGTPAIHelper.UpdateLanguagebType(value + 1);
  121. }
  122. }
  123. private Visibility errorTipVisible = Visibility.Collapsed;
  124. public Visibility ErrorTipVisible
  125. {
  126. get
  127. {
  128. return errorTipVisible;
  129. }
  130. set
  131. {
  132. SetProperty(ref errorTipVisible, value);
  133. if (value == Visibility.Visible)
  134. {
  135. dispatcherTimer.Start();
  136. }
  137. }
  138. }
  139. private string errorTipText = "The uploaded file cannot exceed 10MB";
  140. public string ErrorTipText
  141. {
  142. get
  143. {
  144. return errorTipText;
  145. }
  146. set
  147. {
  148. SetProperty(ref errorTipText, value);
  149. }
  150. }
  151. /// <summary>
  152. /// 初始化翻译前语言列表
  153. /// </summary>
  154. public static List<string> FromlanguageFamily { set; get; } = new List<string>();
  155. private void GetFromlanguageOrigin()
  156. {
  157. FromlanguageFamily.Clear();
  158. FromlanguageFamily = ChatGTPAIHelper.SetFromlanguageOrigin();
  159. }
  160. /// <summary>
  161. /// 初始化翻译到语言列表
  162. /// </summary>
  163. public List<string> TolanguageFamily { set; get; } = new List<string>();
  164. private void GetTolanguageOrigin()
  165. {
  166. TolanguageFamily.Clear();
  167. TolanguageFamily = ChatGTPAIHelper.SetTolanguageOrigin();
  168. }
  169. /// <summary>
  170. /// 设置错误提示显示时间
  171. /// </summary>
  172. private DispatcherTimer dispatcherTimer = new DispatcherTimer();
  173. #endregion
  174. #region 委托声明
  175. public DelegateCommand SelectFilesCommand { get; set; }
  176. #endregion
  177. public ChatGPTAITranslationContentViewModel()
  178. {
  179. SelectFilesCommand = new DelegateCommand(selectFiles);
  180. dispatcherTimer.Interval = TimeSpan.FromSeconds(3);
  181. dispatcherTimer.Tick += Dispatchertimer_Tick;
  182. init();
  183. InitString();
  184. }
  185. #region 逻辑函数
  186. /// <summary>
  187. /// 初始化下拉列表,或其他
  188. /// </summary>
  189. private void init()
  190. {
  191. GetFromlanguageOrigin();
  192. GetTolanguageOrigin();
  193. }
  194. /// <summary>
  195. /// 控制错误提示
  196. /// </summary>
  197. /// <param name="sender"></param>
  198. /// <param name="e"></param>
  199. private void Dispatchertimer_Tick(object sender, EventArgs e)
  200. {
  201. ErrorTipVisible = Visibility.Collapsed;
  202. dispatcherTimer.Stop();
  203. }
  204. /// <summary>
  205. /// 选择文件逻辑翻译,word pdf
  206. /// </summary>
  207. public async void selectFiles()
  208. {
  209. //添加付费拦截锁
  210. if (!ServiceHelper.IAPBeforeFunction())
  211. {
  212. return;
  213. }
  214. if (Settings.Default.UserDate.subscribestatus != 1)
  215. {
  216. App.mainWindowViewModel.dialogs.ShowDialog(DialogNames.IAPCompareDialog);
  217. return;
  218. }
  219. string word = Properties.Resources.wordex;
  220. string pdf = Properties.Resources.pdf;
  221. string allfiles = pdf + word;
  222. OpenFileDialog dialog = new OpenFileDialog();
  223. //dialog.Multiselect = true;
  224. dialog.Filter = string.Format($"Files({allfiles.Replace(";", ",")}|{allfiles})|" +
  225. $"Pdf({pdf})|{pdf}|" +
  226. $"Microsoft Office Word({word})|{word}");
  227. if ((bool)dialog.ShowDialog())
  228. {
  229. ErrorTipVisible = Visibility.Collapsed;
  230. FileInfo fileInfo = new FileInfo(dialog.FileName);
  231. //判断文件大小
  232. if ((((float)fileInfo.Length) / 1024 / 1024) > 10)
  233. {
  234. ErrorTipText = "The uploaded file cannot exceed 10MB";
  235. ErrorTipVisible = Visibility.Visible;
  236. return;
  237. }
  238. //判断文件页数
  239. if (fileInfo.Extension.ToLower() == ".pdf")
  240. {
  241. CPDFDocument cPDFDocument = CPDFDocument.InitWithFilePath(dialog.FileName);
  242. if (cPDFDocument.PageCount > 30)
  243. {
  244. ErrorTipText = "The uploaded file cannot exceed 10MB";
  245. ErrorTipVisible = Visibility.Visible;
  246. return;
  247. }
  248. }
  249. else
  250. {
  251. try
  252. {
  253. Microsoft.Office.Interop.Word.Application myWordApp = new Microsoft.Office.Interop.Word.Application();
  254. object Nothing = System.Reflection.Missing.Value;
  255. object oMissing = System.Reflection.Missing.Value;
  256. object filePath = dialog.FileName; //这里是Word文件的路径
  257. //打开文件
  258. Document myWordDoc = myWordApp.Documents.Open(
  259. ref filePath, ref oMissing, ref oMissing, ref oMissing,
  260. ref oMissing, ref oMissing, ref oMissing, ref oMissing,
  261. ref oMissing, ref oMissing, ref oMissing, ref oMissing,
  262. ref oMissing, ref oMissing, ref oMissing, ref oMissing);
  263. //下面是取得打开文件的页数
  264. int pages = myWordDoc.ComputeStatistics(WdStatistic.wdStatisticPages, ref Nothing);
  265. //关闭文件
  266. myWordDoc.Close(ref oMissing, ref oMissing, ref oMissing);
  267. //退出Word程序
  268. myWordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
  269. if (pages > 30)
  270. {
  271. ErrorTipText = "The uploaded file cannot exceed 30pages";
  272. ErrorTipVisible = Visibility.Visible;
  273. return;
  274. }
  275. }
  276. catch
  277. {
  278. }
  279. }
  280. string newfile = "";
  281. await Task.Run(async delegate
  282. {
  283. newfile = await ChatGTPAIHelper.fileTranslate(dialog.FileName, fromlanguage, tolanguage);
  284. });
  285. if (!string.IsNullOrEmpty(newfile))
  286. {
  287. if (File.Exists(newfile))
  288. {
  289. if (homeContentViewModel != null)
  290. {
  291. homeContentViewModel.OpenFile(new string[] { newfile });
  292. return;
  293. }
  294. }
  295. }
  296. //newfile code
  297. if (newfile == "-1") { return; }
  298. ErrorTipText = ChatGTPAIHelper.GetBaiduTranslationCode(newfile);
  299. ErrorTipVisible = Visibility.Visible;
  300. }
  301. }
  302. #endregion
  303. #region 构架行为
  304. public void OnNavigatedTo(NavigationContext navigationContext)
  305. {
  306. navigationContext.Parameters.TryGetValue<HomeContentViewModel>(ParameterNames.HomeContentViewModel, out homeContentViewModel);
  307. }
  308. public bool IsNavigationTarget(NavigationContext navigationContext)
  309. {
  310. return true;
  311. }
  312. public void OnNavigatedFrom(NavigationContext navigationContext)
  313. {
  314. }
  315. #endregion
  316. }
  317. }