ChatGPTAITranslationContentViewModel.cs 7.0 KB

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