ChatGPTAITranslationContentViewModel.cs 11 KB

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