using ComPDFKit.PDFDocument; using ComPDFKitViewer; using ComPDFKitViewer.PdfViewer; using Microsoft.Office.Core; using Microsoft.Office.Interop.Word; using Microsoft.Win32; using PDF_Master.CustomControl; using PDF_Master.Helper; using PDF_Master.Model; using PDF_Master.Properties; using PDFReader_WPF.Helper; using Prism.Commands; using Prism.Mvvm; using Prism.Regions; using Prism.Services.Dialogs; using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Threading.Tasks; using System.Windows; using System.Windows.Threading; using Task = System.Threading.Tasks.Task; namespace PDF_Master.ViewModels.HomePanel.ChatGPTAI { public class ChatGPTAITranslationContentViewModel : BindableBase, INavigationAware { #region 文案 private string T_title; public string T_Title { get { return T_title; } set { SetProperty(ref T_title, value); } } private string T_supportTip; public string T_SupportTip { get { return T_supportTip; } set { SetProperty(ref T_supportTip, value); } } private string T_limitTip; public string T_LimitTip { get { return T_limitTip; } set { SetProperty(ref T_limitTip, value); } } private string T_translationLanguage; public string T_TranslationLanguage { get { return T_translationLanguage; } set { SetProperty(ref T_translationLanguage, value); } } private string T_selectFileBtn; public string T_SelectFileBtn { get { return T_selectFileBtn; } set { SetProperty(ref T_selectFileBtn, value); } } private string T_dropTip; public string T_DropTip { get { return T_dropTip; } set { SetProperty(ref T_dropTip, value); } } private void InitString() { T_Title = App.HomePageLoader.GetString("AITranslationBtn_Title"); T_SupportTip = App.HomePageLoader.GetString("AITranslationBtn_SupportTip"); T_LimitTip = App.HomePageLoader.GetString("AITranslationBtn_LimitTip"); T_TranslationLanguage = App.HomePageLoader.GetString("AITranslationBtn_TranslationLanguage"); T_SelectFileBtn = App.HomePageLoader.GetString("AITranslationBtn_SelectFileBtn"); T_DropTip = App.HomePageLoader.GetString("AITranslationBtn_DropTip"); } #endregion #region 参数和属性 public HomeContentViewModel homeContentViewModel = null; public IDialogService dialogs; /// /// 初始化需要翻译的内容语言 /// public string fromlanguage = ChatGTPAIHelper.UpdateLanguagebType(0); /// /// 初始化翻译后的语言 /// public string tolanguage = ChatGTPAIHelper.UpdateLanguagebType(1); private int fromlanguageIndex = 0; public int FromlanguageIndex { get { return fromlanguageIndex; } set { SetProperty(ref fromlanguageIndex, value); fromlanguage = ChatGTPAIHelper.UpdateLanguagebType(value); } } private int tolanguageIndex = 0; public int TolanguageIndex { get { return tolanguageIndex; } set { SetProperty(ref tolanguageIndex, value); tolanguage = ChatGTPAIHelper.UpdateLanguagebType(value + 1); } } private Visibility errorTipVisible = Visibility.Collapsed; public Visibility ErrorTipVisible { get { return errorTipVisible; } set { SetProperty(ref errorTipVisible, value); if (value == Visibility.Visible) { dispatcherTimer.Start(); } } } private string errorTipText = "The uploaded file cannot exceed 10MB"; public string ErrorTipText { get { return errorTipText; } set { SetProperty(ref errorTipText, value); } } /// /// 初始化翻译前语言列表 /// public static List FromlanguageFamily { set; get; } = new List(); private void GetFromlanguageOrigin() { FromlanguageFamily.Clear(); FromlanguageFamily = ChatGTPAIHelper.SetFromlanguageOrigin(); } /// /// 初始化翻译到语言列表 /// public List TolanguageFamily { set; get; } = new List(); private void GetTolanguageOrigin() { TolanguageFamily.Clear(); TolanguageFamily = ChatGTPAIHelper.SetTolanguageOrigin(); } /// /// 设置错误提示显示时间 /// private DispatcherTimer dispatcherTimer = new DispatcherTimer(); #endregion #region 委托声明 public DelegateCommand SelectFilesCommand { get; set; } #endregion public ChatGPTAITranslationContentViewModel(IDialogService dialogService) { dialogs = dialogService; SelectFilesCommand = new DelegateCommand(selectFiles); dispatcherTimer.Interval = TimeSpan.FromSeconds(3); dispatcherTimer.Tick += Dispatchertimer_Tick; init(); InitString(); } #region 逻辑函数 /// /// 初始化下拉列表,或其他 /// private void init() { GetFromlanguageOrigin(); GetTolanguageOrigin(); } /// /// 控制错误提示 /// /// /// private void Dispatchertimer_Tick(object sender, EventArgs e) { ErrorTipVisible = Visibility.Collapsed; dispatcherTimer.Stop(); } /// /// 选择文件逻辑翻译,word pdf /// public async void selectFiles() { DataTrackingHelper.SetSendInformation(DataTrackingHelper.EventType.Purchase_AI, DataTrackingHelper.EntryPathKeyType.Home_Tools); DataTrackingHelper.AddFirstAndSecondaryPath(DataTrackingHelper.FirstPath.Home, DataTrackingHelper.SecondaryPath.AITranslate); //添加付费拦截锁 if (!ServiceHelper.IAPBeforeFunction()) { return; } #region 暂时关闭权益弹窗 //bool flg = false; ////权益弹窗 //if (!App.IsLogin || Settings.Default.UserDate.subscribestatus != 1) //{ // DialogParameters value = new DialogParameters(); // value.Add(ParameterNames.Open, "AI"); // dialogs.ShowDialog(DialogNames.SubscriptionDialog, value, dialogResult => // { // if (dialogResult.Result == ButtonResult.OK) // { // flg = true; // } // else // { // flg = false; // } // }); // if (flg == false) // { // return; // } //} #endregion string word = Properties.Resources.wordex; string pdf = Properties.Resources.pdf; string allfiles = pdf + word; OpenFileDialog dialog = new OpenFileDialog(); //dialog.Multiselect = true; dialog.Filter = string.Format($"Files({allfiles.Replace(";", ",")}|{allfiles})|" + $"Pdf({pdf})|{pdf}|" + $"Microsoft Office Word({word})|{word}"); if ((bool)dialog.ShowDialog()) { ErrorTipVisible = Visibility.Collapsed; FileInfo fileInfo = new FileInfo(dialog.FileName); //判断文件大小 if ((((float)fileInfo.Length) / 1024 / 1024) > 10) { ErrorTipText = "File translation file size cannot exceed 10MB"; ErrorTipVisible = Visibility.Visible; return; } //判断文件页数 if (fileInfo.Extension.ToLower() == ".pdf") { CPDFDocument cPDFDocument = CPDFDocument.InitWithFilePath(dialog.FileName); if (cPDFDocument.PageCount > 30) { ErrorTipText = "Documents over 30 pages"; ErrorTipVisible = Visibility.Visible; return; } } string newfile = ""; await Task.Run(async delegate { newfile = await ChatGTPAIHelper.fileTranslate(dialog.FileName, fromlanguage, tolanguage); }); if (!string.IsNullOrEmpty(newfile)) { if (File.Exists(newfile)) { if (homeContentViewModel != null) { homeContentViewModel.OpenFile(new string[] { newfile }); return; } } } //newfile code if (newfile == "-1") { return; } ErrorTipText = ChatGTPAIHelper.GetBaiduTranslationCode(newfile); ErrorTipVisible = Visibility.Visible; } } #endregion #region 构架行为 public void OnNavigatedTo(NavigationContext navigationContext) { navigationContext.Parameters.TryGetValue(ParameterNames.HomeContentViewModel, out homeContentViewModel); } public bool IsNavigationTarget(NavigationContext navigationContext) { return true; } public void OnNavigatedFrom(NavigationContext navigationContext) { } #endregion } }