ChatGPTAITranslationContentViewModel.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. public IDialogService dialogs;
  96. /// <summary>
  97. /// 初始化需要翻译的内容语言
  98. /// </summary>
  99. public string fromlanguage = ChatGTPAIHelper.UpdateLanguagebType(0);
  100. /// <summary>
  101. /// 初始化翻译后的语言
  102. /// </summary>
  103. public string tolanguage = ChatGTPAIHelper.UpdateLanguagebType(1);
  104. private int fromlanguageIndex = 0;
  105. public int FromlanguageIndex
  106. {
  107. get { return fromlanguageIndex; }
  108. set
  109. {
  110. SetProperty(ref fromlanguageIndex, value);
  111. fromlanguage = ChatGTPAIHelper.UpdateLanguagebType(value);
  112. }
  113. }
  114. private int tolanguageIndex = 0;
  115. public int TolanguageIndex
  116. {
  117. get { return tolanguageIndex; }
  118. set
  119. {
  120. SetProperty(ref tolanguageIndex, value);
  121. tolanguage = ChatGTPAIHelper.UpdateLanguagebType(value + 1);
  122. }
  123. }
  124. private Visibility errorTipVisible = Visibility.Collapsed;
  125. public Visibility ErrorTipVisible
  126. {
  127. get
  128. {
  129. return errorTipVisible;
  130. }
  131. set
  132. {
  133. SetProperty(ref errorTipVisible, value);
  134. if (value == Visibility.Visible)
  135. {
  136. dispatcherTimer.Start();
  137. }
  138. }
  139. }
  140. private string errorTipText = "The uploaded file cannot exceed 10MB";
  141. public string ErrorTipText
  142. {
  143. get
  144. {
  145. return errorTipText;
  146. }
  147. set
  148. {
  149. SetProperty(ref errorTipText, value);
  150. }
  151. }
  152. /// <summary>
  153. /// 初始化翻译前语言列表
  154. /// </summary>
  155. public static List<string> FromlanguageFamily { set; get; } = new List<string>();
  156. private void GetFromlanguageOrigin()
  157. {
  158. FromlanguageFamily.Clear();
  159. FromlanguageFamily = ChatGTPAIHelper.SetFromlanguageOrigin();
  160. }
  161. /// <summary>
  162. /// 初始化翻译到语言列表
  163. /// </summary>
  164. public List<string> TolanguageFamily { set; get; } = new List<string>();
  165. private void GetTolanguageOrigin()
  166. {
  167. TolanguageFamily.Clear();
  168. TolanguageFamily = ChatGTPAIHelper.SetTolanguageOrigin();
  169. }
  170. /// <summary>
  171. /// 设置错误提示显示时间
  172. /// </summary>
  173. private DispatcherTimer dispatcherTimer = new DispatcherTimer();
  174. #endregion
  175. #region 委托声明
  176. public DelegateCommand SelectFilesCommand { get; set; }
  177. #endregion
  178. public ChatGPTAITranslationContentViewModel(IDialogService dialogService)
  179. {
  180. dialogs = dialogService;
  181. SelectFilesCommand = new DelegateCommand(selectFiles);
  182. dispatcherTimer.Interval = TimeSpan.FromSeconds(3);
  183. dispatcherTimer.Tick += Dispatchertimer_Tick;
  184. init();
  185. InitString();
  186. }
  187. #region 逻辑函数
  188. /// <summary>
  189. /// 初始化下拉列表,或其他
  190. /// </summary>
  191. private void init()
  192. {
  193. GetFromlanguageOrigin();
  194. GetTolanguageOrigin();
  195. }
  196. /// <summary>
  197. /// 控制错误提示
  198. /// </summary>
  199. /// <param name="sender"></param>
  200. /// <param name="e"></param>
  201. private void Dispatchertimer_Tick(object sender, EventArgs e)
  202. {
  203. ErrorTipVisible = Visibility.Collapsed;
  204. dispatcherTimer.Stop();
  205. }
  206. /// <summary>
  207. /// 选择文件逻辑翻译,word pdf
  208. /// </summary>
  209. public async void selectFiles()
  210. {
  211. bool flg = false;
  212. //权益弹窗
  213. if (!App.IsLogin || Settings.Default.UserDate.subscribestatus != 1)
  214. {
  215. DialogParameters value = new DialogParameters();
  216. value.Add(ParameterNames.Open, "AI");
  217. dialogs.ShowDialog(DialogNames.SubscriptionDialog, value, dialogResult =>
  218. {
  219. if (dialogResult.Result == ButtonResult.OK)
  220. {
  221. flg = true;
  222. }
  223. else
  224. {
  225. flg = false;
  226. }
  227. });
  228. if (flg == false)
  229. {
  230. return;
  231. }
  232. }
  233. string word = Properties.Resources.wordex;
  234. string pdf = Properties.Resources.pdf;
  235. string allfiles = pdf + word;
  236. OpenFileDialog dialog = new OpenFileDialog();
  237. //dialog.Multiselect = true;
  238. dialog.Filter = string.Format($"Files({allfiles.Replace(";", ",")}|{allfiles})|" +
  239. $"Pdf({pdf})|{pdf}|" +
  240. $"Microsoft Office Word({word})|{word}");
  241. if ((bool)dialog.ShowDialog())
  242. {
  243. ErrorTipVisible = Visibility.Collapsed;
  244. FileInfo fileInfo = new FileInfo(dialog.FileName);
  245. //判断文件大小
  246. if ((((float)fileInfo.Length) / 1024 / 1024) > 10)
  247. {
  248. ErrorTipText = "File translation file size cannot exceed 10MB";
  249. ErrorTipVisible = Visibility.Visible;
  250. return;
  251. }
  252. //判断文件页数
  253. if (fileInfo.Extension.ToLower() == ".pdf")
  254. {
  255. CPDFDocument cPDFDocument = CPDFDocument.InitWithFilePath(dialog.FileName);
  256. if (cPDFDocument.PageCount > 30)
  257. {
  258. ErrorTipText = "Documents over 30 pages";
  259. ErrorTipVisible = Visibility.Visible;
  260. return;
  261. }
  262. }
  263. string newfile = "";
  264. await Task.Run(async delegate
  265. {
  266. newfile = await ChatGTPAIHelper.fileTranslate(dialog.FileName, fromlanguage, tolanguage);
  267. });
  268. if (!string.IsNullOrEmpty(newfile))
  269. {
  270. if (File.Exists(newfile))
  271. {
  272. if (homeContentViewModel != null)
  273. {
  274. homeContentViewModel.OpenFile(new string[] { newfile });
  275. return;
  276. }
  277. }
  278. }
  279. //newfile code
  280. if (newfile == "-1") { return; }
  281. ErrorTipText = ChatGTPAIHelper.GetBaiduTranslationCode(newfile);
  282. ErrorTipVisible = Visibility.Visible;
  283. }
  284. }
  285. #endregion
  286. #region 构架行为
  287. public void OnNavigatedTo(NavigationContext navigationContext)
  288. {
  289. navigationContext.Parameters.TryGetValue<HomeContentViewModel>(ParameterNames.HomeContentViewModel, out homeContentViewModel);
  290. }
  291. public bool IsNavigationTarget(NavigationContext navigationContext)
  292. {
  293. return true;
  294. }
  295. public void OnNavigatedFrom(NavigationContext navigationContext)
  296. {
  297. }
  298. #endregion
  299. }
  300. }