ChatGPTAITranslationContentViewModel.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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 = "File translation file size 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 = "Documents over 30 pages";
  245. ErrorTipVisible = Visibility.Visible;
  246. return;
  247. }
  248. }
  249. string newfile = "";
  250. await Task.Run(async delegate
  251. {
  252. newfile = await ChatGTPAIHelper.fileTranslate(dialog.FileName, fromlanguage, tolanguage);
  253. });
  254. if (!string.IsNullOrEmpty(newfile))
  255. {
  256. if (File.Exists(newfile))
  257. {
  258. if (homeContentViewModel != null)
  259. {
  260. homeContentViewModel.OpenFile(new string[] { newfile });
  261. return;
  262. }
  263. }
  264. }
  265. //newfile code
  266. if (newfile == "-1") { return; }
  267. ErrorTipText = ChatGTPAIHelper.GetBaiduTranslationCode(newfile);
  268. ErrorTipVisible = Visibility.Visible;
  269. }
  270. }
  271. #endregion
  272. #region 构架行为
  273. public void OnNavigatedTo(NavigationContext navigationContext)
  274. {
  275. navigationContext.Parameters.TryGetValue<HomeContentViewModel>(ParameterNames.HomeContentViewModel, out homeContentViewModel);
  276. }
  277. public bool IsNavigationTarget(NavigationContext navigationContext)
  278. {
  279. return true;
  280. }
  281. public void OnNavigatedFrom(NavigationContext navigationContext)
  282. {
  283. }
  284. #endregion
  285. }
  286. }