Ver código fonte

Merge branch 'dev' of http://git.kdan.cc:8865/Windows/PDFOffice_Windows_exe into dev

OYXH\oyxh 1 ano atrás
pai
commit
97670b7325

+ 73 - 21
PDF Office/Helper/ChatGTPAIHelper.cs

@@ -1,6 +1,7 @@
 using ComPDFKit_Conversion.Options;
 using DryIoc;
 using ImTools;
+using Microsoft.Office.Interop.Word;
 using Newtonsoft.Json;
 using Newtonsoft.Json.Linq;
 using PDF_Master.Properties;
@@ -66,7 +67,7 @@ namespace PDF_Master.Helper
 
         #region AI服务器对接接口
         /// <summary>
-        /// 翻译指定内容,可以指定语言
+        /// 获取文件Key用来发送给服务端获取翻译文件
         /// </summary>
         /// <param name="content"></param>
         /// <param name="filename"></param>
@@ -162,12 +163,12 @@ namespace PDF_Master.Helper
                 {
                     //从网络异常信息里解析错误,后面的逻辑根据错误码判断会更准确
                     statusCode = (int)((HttpWebResponse)ex.Response).StatusCode;
-                   
+
                 }
                 Trace.WriteLine("HTTP异常:" + ex.Message);
                 if (statusCode == 401)
                 {
-                    return "Code"+"401";
+                    return "Code" + "401";
                 }
                 else
                 {
@@ -201,20 +202,25 @@ namespace PDF_Master.Helper
             return path;
         }
 
-        
+
 
         /// <summary>
         /// 翻译指定内容,可以指定语言
         /// </summary>
-        /// <param name="fileKey">文本Key</param>
+        /// <param name="content">文件</param>
         /// <param name="fromlanguage">文本语言</param>
         /// <param name="tolanguage">需要翻译的语言</param>
-        /// <returns></returns>
+        /// <param name="isprocess">是否需要阅读也进度条</param>
+        /// <returns>code与文件路径</returns>
         public static async Task<string> fileTranslate(string content, string fromlanguage, string tolanguage, bool isprocess = true)
         {
             string Code = "300";
-            intProcess(isprocess,"Translating", System.Windows.Visibility.Visible);
-            
+            intProcess(isprocess, "Translating", System.Windows.Visibility.Visible);
+            //判断word文档大小
+            if (!GetDocumentPagesWord(content)) {
+                clossProcess();
+                return "05011";
+            }
             FileInfo file = new FileInfo(content);
             string fileKey = await fileKeyTranslate(content, file.Name);
             App.mainWindowViewModel.Value = 1;
@@ -224,14 +230,14 @@ namespace PDF_Master.Helper
                 return fileKey.Replace("Code", "");
             }
             HttpWebResponse response = null;
-            ServicePointManager.DefaultConnectionLimit =200;
+            ServicePointManager.DefaultConnectionLimit = 200;
             HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(Uri_filetranslate);
             //关闭请求
-            DocumentaryTranslationDialogViewModel.CancelProgress=()=> 
+            DocumentaryTranslationDialogViewModel.CancelProgress = () =>
             {
                 if (request != null)
                 {
-                    Code =  "-1";
+                    Code = "-1";
                     request.Abort();
                 }
             };
@@ -240,10 +246,10 @@ namespace PDF_Master.Helper
             {
                 if (request != null)
                 {
-                    Code =  "-1";
-                    request.Abort(); 
+                    Code = "-1";
+                    request.Abort();
                 }
-            }; 
+            };
             request.Method = "Post";
             request.ContentType = "application/json";
             //request.Accept = "application/vnd.api+json;version=1";
@@ -321,7 +327,7 @@ namespace PDF_Master.Helper
                     return jobject["code"].ToObject<string>().ToLower();
                 }
             }
-            catch(WebException ex)
+            catch (WebException ex)
             {
                 clossProcess();
                 int statusCode = 0;
@@ -335,7 +341,8 @@ namespace PDF_Master.Helper
                 {
                     return "401";
                 }
-                else {
+                else
+                {
                     //-1为自己取消,300为一般错误
                     return Code;
                 }
@@ -415,7 +422,7 @@ namespace PDF_Master.Helper
                     return jobject["code"].ToObject<string>().ToLower();
                 }
             }
-            catch(WebException ex)
+            catch (WebException ex)
             {
                 int statusCode = 0;
                 if (ex.Response is HttpWebResponse)
@@ -559,7 +566,7 @@ namespace PDF_Master.Helper
                 }
                 if (statusCode == 401)
                 {
-                    ChatGPTCode= "401";
+                    ChatGPTCode = "401";
                 }
                 else ChatGPTCode = "300";
                 clossProcess();
@@ -594,6 +601,49 @@ namespace PDF_Master.Helper
             App.mainWindowViewModel.IsProcessVisible = System.Windows.Visibility.Collapsed;
             App.mainWindowViewModel.ProcessCloseBtnVisible = System.Windows.Visibility.Visible;
         }
+
+        /// <summary>
+        /// 判断word文档页面大小,如果接口有问题则交给服务器判断
+        /// </summary>
+        /// <param name="filepath"></param>
+        /// <returns>返回值为是否拿到页码没拿到则为true,页面过小为true</returns>
+        public static bool GetDocumentPagesWord(string filepath)
+        {
+            try
+            {
+                FileInfo fileInfo = new FileInfo(filepath);
+                if (fileInfo.Extension.ToLower()!=".pdf")
+                {
+                    Microsoft.Office.Interop.Word.Application myWordApp = new Microsoft.Office.Interop.Word.Application();
+                    object Nothing = System.Reflection.Missing.Value;
+                    object oMissing = System.Reflection.Missing.Value;
+                    //这里是Word文件的路径
+                    object filePath = filepath;
+                    //打开文件
+                    Document myWordDoc = myWordApp.Documents.Open(
+                        ref filePath, ref oMissing, ref oMissing, ref oMissing,
+                        ref oMissing, ref oMissing, ref oMissing, ref oMissing,
+                        ref oMissing, ref oMissing, ref oMissing, ref oMissing,
+                        ref oMissing, ref oMissing, ref oMissing, ref oMissing);
+                    //下面是取得打开文件的页数
+                    int pages = myWordDoc.ComputeStatistics(WdStatistic.wdStatisticPages, ref Nothing);
+                    //关闭文件
+                    myWordDoc.Close(ref oMissing, ref oMissing, ref oMissing);
+                    //退出Word程序
+                    myWordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
+                    if (pages > 30)
+                    {
+                        return false;
+                    }
+                }
+                return true;
+
+            }
+            catch
+            {
+                return true;
+            }
+        }
         #endregion
 
         #region 支持翻译的语言
@@ -739,9 +789,11 @@ namespace PDF_Master.Helper
 
         #region 错误码
 
-        public static string GetBaiduTranslationCode(string code) {
-            
-            if (!String.IsNullOrEmpty(App.HomePageLoader.GetString("BaiduTranslation" + code))) {
+        public static string GetBaiduTranslationCode(string code)
+        {
+
+            if (!String.IsNullOrEmpty(App.HomePageLoader.GetString("BaiduTranslation" + code)))
+            {
                 return App.HomePageLoader.GetString("BaiduTranslation" + code);
             }
             return code;

+ 5 - 1
PDF Office/ViewModels/Dialog/ChatGPTAIDialogs/DocumentaryTranslationDialogViewModel.cs

@@ -320,7 +320,11 @@ namespace PDF_Master.ViewModels.Dialog.ChatGPTAIDialogs
                 else
                 {
                     //newfile code
-                    if (newfile == "-1") { return; }
+                    if (newfile == "-1")
+                    {
+                        Value = 9;
+                        ProgressVisible = Visibility.Collapsed; return; 
+                    }
                     AlertsMessage alertsMessage = new AlertsMessage();
                     alertsMessage.ShowDialog("",  ChatGTPAIHelper.GetBaiduTranslationCode(newfile), App.ServiceLoader.GetString("Text_ok"));
                 }

+ 7 - 4
PDF Office/ViewModels/Dialog/ServiceDialog/SubscriptionDialogViewModel.cs

@@ -203,18 +203,21 @@ namespace PDF_Master.ViewModels.Dialog.ServiceDialog
         /// </summary>
         private void WarermarkSaving()
         {
+            
             String IsFormSave = "FormSave";
            
             if(open!=null)
             {
-                Close();
+                RequestClose?.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.OK));
             }
             else
             {
-                viewContentViewModel.saveAsFile(null, IsFormSave);
-                Close();
+               if( viewContentViewModel.saveAsFile(null, IsFormSave)==false)
+                {
+                    RequestClose?.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.Cancel));
+                }
+              else  RequestClose?.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.OK));
             }
-            
         }
 
         /// <summary>

+ 5 - 34
PDF Office/ViewModels/HomePanel/ChatGPTAI/ChatGPTAITranslationContentViewModel.cs

@@ -230,6 +230,8 @@ namespace PDF_Master.ViewModels.HomePanel.ChatGPTAI
             dispatcherTimer.Stop();
         }
 
+       
+
         /// <summary>
         /// 选择文件逻辑翻译,word pdf
         /// </summary>
@@ -260,7 +262,7 @@ namespace PDF_Master.ViewModels.HomePanel.ChatGPTAI
                 //判断文件大小
                 if ((((float)fileInfo.Length) / 1024 / 1024) > 10)
                 {
-                    ErrorTipText = "The uploaded file cannot exceed 10MB";
+                    ErrorTipText = "File translation file size cannot exceed 10MB";
                     ErrorTipVisible = Visibility.Visible;
                     return;
                 }
@@ -270,43 +272,12 @@ namespace PDF_Master.ViewModels.HomePanel.ChatGPTAI
                     CPDFDocument cPDFDocument = CPDFDocument.InitWithFilePath(dialog.FileName);
                     if (cPDFDocument.PageCount > 30)
                     {
-                        ErrorTipText = "The uploaded file cannot exceed 10MB";
+                        ErrorTipText = "Documents over 30 pages";
                         ErrorTipVisible = Visibility.Visible;
                         return;
                     }
                 }
-                else
-                {
-                    try
-                    {
-                        Microsoft.Office.Interop.Word.Application myWordApp = new Microsoft.Office.Interop.Word.Application();
-                        object Nothing = System.Reflection.Missing.Value;
-                        object oMissing = System.Reflection.Missing.Value;
-                        object filePath = dialog.FileName; //这里是Word文件的路径
-                                                           //打开文件
-                        Document myWordDoc = myWordApp.Documents.Open(
-                            ref filePath, ref oMissing, ref oMissing, ref oMissing,
-                            ref oMissing, ref oMissing, ref oMissing, ref oMissing,
-                            ref oMissing, ref oMissing, ref oMissing, ref oMissing,
-                            ref oMissing, ref oMissing, ref oMissing, ref oMissing);
-                        //下面是取得打开文件的页数
-                        int pages = myWordDoc.ComputeStatistics(WdStatistic.wdStatisticPages, ref Nothing);
-                        //关闭文件
-                        myWordDoc.Close(ref oMissing, ref oMissing, ref oMissing);
-                        //退出Word程序
-                        myWordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
-                        if (pages > 30)
-                        {
-                            ErrorTipText = "The uploaded file cannot exceed 30pages";
-                            ErrorTipVisible = Visibility.Visible;
-                            return;
-                        }
-                    }
-                    catch
-                    {
-
-                    }
-                }
+                
                 string newfile = "";
                 await Task.Run(async delegate
                 {

+ 20 - 6
PDF Office/ViewModels/ViewContentViewModel.cs

@@ -2757,6 +2757,8 @@ namespace PDF_Master.ViewModels
         {
             try
             {
+                //是否取消了水印保存步骤
+                bool flg = false;
                 //删掉Settings.Default.UserDate.isInFreeUseTime == false,没有试用期策略了
                 if ((!App.IsLogin || Settings.Default.UserDate.subscribestatus != 1) &&   (App.mainWindowViewModel.SelectedItem.DataContext as MainContentViewModel).IsUsedVIP== true)
                 {
@@ -2764,11 +2766,16 @@ namespace PDF_Master.ViewModels
                     value.Add(ParameterNames.ViewContentViewModel, this);
                     dialogs.ShowDialog(DialogNames.SubscriptionDialog, value, dialogResult =>
                     {
-
+                        if (dialogResult.Result == ButtonResult.OK)
+                        {
+                             flg = true;
+                        }
+                        else
+                        {
+                            flg= false;
+                        }
                     });
-
-
-                    return false;
+                        return flg;
 
                 }
 
@@ -3019,6 +3026,7 @@ namespace PDF_Master.ViewModels
         /// </summary>
         public bool saveAsFile(Action RedactionAction = null, String IsFormSave=null)
         {
+            //是否来自savefile方法
             if(IsFormSave=="FormSave")
             {
                 var dlg = new Microsoft.Win32.SaveFileDialog();
@@ -3026,6 +3034,7 @@ namespace PDF_Master.ViewModels
                 dlg.FileName = PDFViewer.Document.FileName;
                 if (dlg.ShowDialog() == true && !string.IsNullOrEmpty(dlg.FileName))
                 {
+                  
                     bool result = false;
                     if (RedactionAction != null)
                     {
@@ -3135,17 +3144,21 @@ namespace PDF_Master.ViewModels
                     value.Add(ParameterNames.Open, "saveAs");
                     dialogs.ShowDialog(DialogNames.SubscriptionDialog, value, dialogResult =>
                     {
-                        if (dialogResult.Result == ButtonResult.Cancel)
+                        if (dialogResult.Result == ButtonResult.OK)
                         {
                             flg = true;
                         }
+                        else
+                        {
+                            flg = false;
+                        }
                     });
 
                     if (flg == false)
                     {
                         return false;
                     }
-                }
+                  }
                 var dlg = new Microsoft.Win32.SaveFileDialog();
                 dlg.Filter = Properties.Resources.OpenDialogFilter;
                 dlg.FileName = PDFViewer.Document.FileName;
@@ -3183,6 +3196,7 @@ namespace PDF_Master.ViewModels
                         }
 
                         PDFViewer.Document.Encrypt(openPassword, permissionsPassword, cPDFPermissionsInfo);
+                        //是否需要加水印
                         if(flg==true)
                         {
                             Uri resourceUri = new Uri("pack://application:,,,/PDF Master;component/Resources/Service/Warermark.png");

+ 0 - 33
PDF Office/Views/HomePanel/ChatGPTAI/ChatGPTAITranslationContent.xaml.cs

@@ -94,39 +94,6 @@ namespace PDF_Master.Views.HomePanel.ChatGPTAI
                                     return;
                                 }
                             }
-                            else
-                            {
-                                try
-                                {
-                                    Microsoft.Office.Interop.Word.Application myWordApp = new Microsoft.Office.Interop.Word.Application();
-                                    object Nothing = System.Reflection.Missing.Value;
-                                    object oMissing = System.Reflection.Missing.Value;
-                                    object filePath = dropFile; //这里是Word文件的路径
-                                                                //打开文件
-                                    Document myWordDoc = myWordApp.Documents.Open(
-                                        ref filePath, ref oMissing, ref oMissing, ref oMissing,
-                                        ref oMissing, ref oMissing, ref oMissing, ref oMissing,
-                                        ref oMissing, ref oMissing, ref oMissing, ref oMissing,
-                                        ref oMissing, ref oMissing, ref oMissing, ref oMissing);
-                                    //下面是取得打开文件的页数
-                                    int pages = myWordDoc.ComputeStatistics(WdStatistic.wdStatisticPages, ref Nothing);
-                                    //关闭文件
-                                    myWordDoc.Close(ref oMissing, ref oMissing, ref oMissing);
-                                    //退出Word程序
-                                    myWordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
-                                    if (pages > 30)
-                                    {
-                                        viewModel.ErrorTipText = "The uploaded file cannot exceed 30Pages";
-                                        viewModel.ErrorTipVisible = Visibility.Visible;
-                                        isFirst = false;
-                                        return;
-                                    }
-                                }
-                                catch
-                                {
-
-                                }
-                            }
                             string newfile = "";
                             await System.Threading.Tasks.Task.Run(async delegate
                             {