123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976 |
- using ComPDFKit_Conversion.Options;
- using DryIoc;
- using ImTools;
- using Microsoft.Office.Interop.Word;
- using Newtonsoft.Json;
- using Newtonsoft.Json.Linq;
- using PDF_Master.Properties;
- using PDF_Master.ViewModels.Dialog.ChatGPTAIDialogs;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Net.Http;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Web;
- using static Dropbox.Api.Files.SearchMatchType;
- using static Dropbox.Api.TeamLog.EventCategory;
- using static Google.Apis.Requests.BatchRequest;
- namespace PDF_Master.Helper
- {
- public static class ChatGTPAIHelper
- {
- #if DEBUG
- //测试环境
- //纠错重写
- private static string host = "https://ai.compdf.com";
- //百度翻译
- private static string translate = "http://101.132.103.13:8030";
- #else
- //纠错重写
- private static string host = "https://ai.compdf.com";
- //百度翻译
- private static string translate ="https://api-server.compdf.com";
- #endif
- /// <summary>
- /// 翻译文档Key接口
- /// </summary>
- private static string Uri_fileKeytranslate = translate + "/v1/translate/fileUpload";
- /// <summary>
- /// 翻译文档接口
- /// </summary>
- private static string Uri_filetranslate = translate + "/v1/translate/fileTranslateHandle";
- /// <summary>
- /// 翻译文本接口
- /// </summary>
- private static string Uri_texttranslate = translate + "/v1/translate/textTrans";
- /// <summary>
- /// 错别字纠正
- /// </summary>
- private static string Uri_Correction = host + "/api/correct-typos";
- /// <summary>
- /// 错别字纠正
- /// </summary>
- private static string Uri_Rewrite = host + "/api/rewrite";
- /// <summary>
- /// chatGPT错误码
- /// </summary>
- public static string ChatGPTCode = "";
- #region AI服务器对接接口
- /// <summary>
- /// 获取
- /// </summary>
- /// <param name="content"></param>
- /// <param name="filename"></param>
- /// <returns></returns>
- public static async Task<string> fileKeyTranslate(string content, string filename)
- {
- string Code = "Code" + "300";
- ServicePointManager.DefaultConnectionLimit = 200;
- HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(Uri_fileKeytranslate);
- //关闭请求
- //弹窗内进度条关闭
- DocumentaryTranslationDialogViewModel.CancelProgress = () =>
- {
- if (request != null)
- {
- Code = "Code" + "-1";
- request.Abort();
- }
- };
- //进度条关闭
- App.mainWindowViewModel.ProcessCloseAction = () =>
- {
- if (request != null)
- {
- Code = "Code" + "-1";
- request.Abort();
- }
- };
- request.Method = "Post";
- //request.Accept = "application/vnd.api+json;version=1";
- request.UserAgent = "Apifox/1.0.0 (https://www.apifox.cn)";
- request.Timeout = 20000;
- request.ServicePoint.Expect100Continue = false;
- request.KeepAlive = false;
- var formData = new MultipartFormDataContent();
- byte[] data = File.ReadAllBytes(@content);
- MemoryStream stream = new MemoryStream(data);
- formData.Add(new ByteArrayContent(data), "file", filename);
- formData.Add(new StringContent("2"), "projectId");
- formData.Add(new StringContent("1.0.1"), "version");
- if (!string.IsNullOrEmpty(Settings.Default.UserDate.id))
- {
- formData.Add(new StringContent(Settings.Default.UserDate.id), "userId");
- }
- else
- {
- formData.Add(new StringContent("1"), "userId");
- }
- // 设置请求体格式
- request.ContentType = formData.Headers.ContentType.ToString();
- request.ContentLength = formData.Headers.ContentLength.Value;
- // 将FormData写入请求体
-
- try
- {
- using (var requestStream = request.GetRequestStream())
- {
- await formData.CopyToAsync(requestStream);
- }
- // 获取HTTP响应
- HttpWebResponse response2 = (HttpWebResponse)request.GetResponse();
- using (StreamReader reader = new StreamReader(response2.GetResponseStream()))
- {
- string responseData = reader.ReadToEnd();
- Console.WriteLine(responseData);
- reader.Close();
- JObject jobject = (JObject)JsonConvert.DeserializeObject(responseData);
- if (response2 != null)
- {
- response2.Close();
- }
- if (request != null)
- {
- request.Abort();
- }
- string translation = "";
- if (jobject["code"].ToObject<string>().ToLower() == "200")
- {
- translation = jobject["data"]["fileKey"].ToObject<string>().ToLower();
- return translation;
- }
- return "Code" + jobject["code"].ToObject<string>().ToLower();
- }
- }
- catch (WebException ex)
- {
- int statusCode = 0;
- if (ex.Response is HttpWebResponse)
- {
- //从网络异常信息里解析错误,后面的逻辑根据错误码判断会更准确
- statusCode = (int)((HttpWebResponse)ex.Response).StatusCode;
- }
- Trace.WriteLine("HTTP异常:" + ex.Message);
- if (statusCode == 401)
- {
- return "Code" + "401";
- }
- else
- {
- //-1为自己取消,300为一般错误
- return Code;
- }
- }
- catch
- {
- return "300";
- }
- }
- public static string HttpDownloadFile(string url, string path)
- {
- // 设置参数
- HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
- //发送请求并获取相应回应数据
- HttpWebResponse response = request.GetResponse() as HttpWebResponse;
- //直到request.GetResponse()程序才开始向目标网页发送Post请求
- Stream responseStream = response.GetResponseStream();
- //创建本地文件写入流
- Stream stream = new FileStream(path, FileMode.Create);
- byte[] bArr = new byte[1024];
- int size = responseStream.Read(bArr, 0, (int)bArr.Length);
- while (size > 0)
- {
- stream.Write(bArr, 0, size);
- size = responseStream.Read(bArr, 0, (int)bArr.Length);
- }
- stream.Close();
- responseStream.Close();
- return path;
- }
- /// <summary>
- /// 翻译指定内容,可以指定语言
- /// </summary>
- /// <param name="content">文件</param>
- /// <param name="fromlanguage">文本语言</param>
- /// <param name="tolanguage">需要翻译的语言</param>
- /// <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);
- //判断word文档大小
- if (!GetDocumentPagesWord(content))
- {
- clossProcess();
- return "05011";
- }
- FileInfo file = new FileInfo(content);
- string fileKey = await fileKeyTranslate(content, file.Name);
- App.mainWindowViewModel.Value = 1;
- if (fileKey.Contains("Code"))
- {
- clossProcess();
- return fileKey.Replace("Code", "");
- }
- HttpWebResponse response = null;
- ServicePointManager.DefaultConnectionLimit = 200;
- HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(Uri_filetranslate);
- //关闭请求
- DocumentaryTranslationDialogViewModel.CancelProgress = () =>
- {
- if (request != null)
- {
- Code = "-1";
- request.Abort();
- }
- };
- App.mainWindowViewModel.ProcessCloseAction = () =>
- {
- if (request != null)
- {
- Code = "-1";
- request.Abort();
- }
- };
- request.Method = "Post";
- request.ContentType = "application/json";
- //request.Accept = "application/vnd.api+json;version=1";
- request.UserAgent = "Apifox/1.0.0 (https://www.apifox.cn)";
- //设置无限请求时间:由于文本翻译时间不确定且时间长
- request.Timeout = Timeout.Infinite;
- request.ServicePoint.Expect100Continue = false;
- request.KeepAlive = false;
- StringWriter sw = new StringWriter();
- using (JsonWriter writer = new JsonTextWriter(sw))
- {
- writer.WriteStartObject();
- writer.WritePropertyName("fileKey");
- writer.WriteValue(fileKey);
- writer.WritePropertyName("from");
- writer.WriteValue(fromlanguage);
- writer.WritePropertyName("to");
- writer.WriteValue(tolanguage);
- writer.WritePropertyName("projectId");
- writer.WriteValue("2");
- writer.WritePropertyName("version");
- writer.WriteValue("1.0.1");
- writer.WritePropertyName("userId");
- writer.WriteValue(Settings.Default.UserDate.id);
- writer.WriteEndObject();
- }
- try
- {
- App.mainWindowViewModel.Value = 2;
- string postBody = sw.ToString();
- using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
- {
- writer.Write(postBody);
- writer.Close();
- }
- App.mainWindowViewModel.Value = 3;
- response = (HttpWebResponse)request.GetResponse();
- using (StreamReader reader = new StreamReader(response.GetResponseStream()))
- {
- App.mainWindowViewModel.Value = 4;
- string responseData = reader.ReadToEnd();
- Console.WriteLine(responseData);
- reader.Close();
- JObject jobject = (JObject)JsonConvert.DeserializeObject(responseData);
- App.mainWindowViewModel.Value = 6;
- if (response != null)
- {
- response.Close();
- }
- if (request != null)
- {
- request.Abort();
- }
- if (jobject["code"].ToObject<string>().ToLower() == "200")
- {
- translate = jobject["data"]["ossDownUrl"].ToObject<string>().ToLower();
- //return jobject["code"].ToObject<string>().ToLower();
- using (var client = new WebClient())
- {
- App.mainWindowViewModel.Value = 8;
- string folderPath = file.FullName.Remove(file.FullName.LastIndexOf("."), file.FullName.Length - file.FullName.LastIndexOf(".")) + "_aiTranslation.pdf";
- folderPath = CommonHelper.CreateFilePath(folderPath);
- client.DownloadProgressChanged += (sender, e) =>
- {
- };
- client.DownloadFile(translate, folderPath);
- clossProcess();
- return folderPath;
- }
- }
- clossProcess();
- return jobject["code"].ToObject<string>().ToLower();
- }
- }
- catch (WebException ex)
- {
- clossProcess();
- int statusCode = 0;
- if (ex.Response is HttpWebResponse)
- {
- //从网络异常信息里解析错误,后面的逻辑根据错误码判断会更准确
- statusCode = (int)((HttpWebResponse)ex.Response).StatusCode;
- }
- Trace.WriteLine("HTTP异常:" + ex.Message);
- if (statusCode == 401)
- {
- return "401";
- }
- else
- {
- //-1为自己取消,300为一般错误
- return Code;
- }
- }
- catch
- {
- return "300";
- }
- }
- /// <summary>
- /// 翻译指定内容,可以指定语言
- /// </summary>
- /// <param name="content">翻译内容</param>
- /// <param name="fromlanguage">文本语言</param>
- /// <param name="tolanguage">需要翻译的语言</param>
- /// <returns></returns>
- public static String textTranslate(string content, string fromlanguage, string tolanguage, ref string translate)
- {
- HttpWebResponse response = null;
- ServicePointManager.DefaultConnectionLimit = 200;
- HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(Uri_texttranslate);
- request.Method = "Post";
- request.ContentType = "application/json";
- //request.Accept = "application/vnd.api+json;version=1";
- request.UserAgent = "Apifox/1.0.0 (https://www.apifox.cn)";
- request.Timeout = 60000;
- request.ServicePoint.Expect100Continue = false;
- StringWriter sw = new StringWriter();
- using (JsonWriter writer = new JsonTextWriter(sw))
- {
- writer.WriteStartObject();
- writer.WritePropertyName("q");
- writer.WriteValue(content);
- writer.WritePropertyName("from");
- writer.WriteValue(fromlanguage);
- writer.WritePropertyName("to");
- writer.WriteValue(tolanguage);
- writer.WritePropertyName("projectId");
- writer.WriteValue("2");
- writer.WritePropertyName("version");
- writer.WriteValue("1.0.1");
- writer.WritePropertyName("userId");
- writer.WriteValue(Settings.Default.UserDate.id);
- writer.WriteEndObject();
- }
- try
- {
- string postBody = sw.ToString();
- using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
- {
- writer.Write(postBody);
- writer.Close();
- }
- response = (HttpWebResponse)request.GetResponse();
- using (StreamReader reader = new StreamReader(response.GetResponseStream()))
- {
- string responseData = reader.ReadToEnd();
- Console.WriteLine(responseData);
- reader.Close();
- JObject jobject = (JObject)JsonConvert.DeserializeObject(responseData);
- if (response != null)
- {
- response.Close();
- }
- if (request != null)
- {
- request.Abort();
- }
- if (jobject["code"].ToObject<string>().ToLower() == "200")
- {
- translate = jobject["data"]["dst"].ToObject<string>().ToLower();
- }
- //return jobject["code"].ToObject<string>().ToLower();
- return jobject["code"].ToObject<string>().ToLower();
- }
- }
- catch (WebException ex)
- {
- int statusCode = 0;
- if (ex.Response is HttpWebResponse)
- {
- //从网络异常信息里解析错误,后面的逻辑根据错误码判断会更准确
- statusCode = (int)((HttpWebResponse)ex.Response).StatusCode;
- }
- if (statusCode == 401)
- {
- return "401";
- }
- else return "300";
- }
- catch
- {
- return "300";
- }
- }
- /// <summary>
- /// 纠错
- /// </summary>
- /// <param name="content"></param>
- /// <returns></returns>
- public static async Task<string> Correction(string content)
- {
- intProcess(true, "Correcting...");
- App.mainWindowViewModel.Value = 1;
- ServicePointManager.DefaultConnectionLimit = 200;
- HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(Uri_Correction);
- request.Method = "Post";
- //request.Accept = "application/vnd.api+json;version=1";
- request.UserAgent = "Apifox/1.0.0 (https://www.apifox.cn)";
- request.Timeout = 60000;
- request.ServicePoint.Expect100Continue = false;
- var formData = new MultipartFormDataContent();
- formData.Add(new StringContent("2"), "project_id");
- formData.Add(new StringContent("1.0.1"), "version");
- if (!string.IsNullOrEmpty(Settings.Default.UserDate.id))
- {
- formData.Add(new StringContent(Settings.Default.UserDate.id), "user_id");
- }
- else
- {
- formData.Add(new StringContent("1"), "user_id");
- }
- formData.Add(new StringContent(content), "content");
- // 设置请求体格式
- request.ContentType = formData.Headers.ContentType.ToString();
- request.ContentLength = formData.Headers.ContentLength.Value;
- App.mainWindowViewModel.Value = 3;
- return await PostStringHttpWeb(formData, request, "content");
- }
- /// <summary>
- /// 重写
- /// </summary>
- /// <param name="content"></param>
- /// <returns></returns>
- public static async Task<string> Rewrite(string content)
- {
- intProcess(true, "Rewriting...");
- App.mainWindowViewModel.Value = 1;
- ServicePointManager.DefaultConnectionLimit = 200;
- HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(Uri_Correction);
- request.Method = "Post";
- //request.Accept = "application/vnd.api+json;version=1";
- request.UserAgent = "Apifox/1.0.0 (https://www.apifox.cn)";
- request.Timeout = 60000;
- request.ServicePoint.Expect100Continue = false;
- var formData = new MultipartFormDataContent();
- formData.Add(new StringContent("2"), "project_id");
- formData.Add(new StringContent("1.0.1"), "version");
- if (!string.IsNullOrEmpty(Settings.Default.UserDate.id))
- {
- formData.Add(new StringContent(Settings.Default.UserDate.id), "user_id");
- }
- else
- {
- formData.Add(new StringContent("1"), "user_id");
- }
- formData.Add(new StringContent(content), "content");
- // 设置请求体格式
- request.ContentType = formData.Headers.ContentType.ToString();
- request.ContentLength = formData.Headers.ContentLength.Value;
- App.mainWindowViewModel.Value = 3;
- return await PostStringHttpWeb(formData, request, "content");
- }
- /// <summary>
- /// 从链接中返回存在的链接(仅返回第一个找到的链接)
- /// </summary>
- /// <param name="content"></param>
- /// <returns></returns>
- public static List<string> GetLinkFromString(string content)
- {
- if (string.IsNullOrEmpty(content))
- {
- return null;
- }
- // 创建正则表达式模式
- string pattern = @"(https?://[^\s]+)";
- // 获取所有匹配项
- MatchCollection matches = Regex.Matches(content, pattern);
- List<string> list = new List<string>();
- // 遍历并打印所有匹配项
- foreach (Match match in matches)
- {
- list.Add(match.Value);
- }
- return list;
- }
- /// <summary>
- /// 从接口获取信息,获取message对应value值
- /// </summary>
- /// <param name="formData">传入参数</param>
- /// <param name="request">请求体</param>
- /// /// <param name="key">值</param>
- /// <returns></returns>
- private static async Task<string> PostStringHttpWeb(MultipartFormDataContent formData, HttpWebRequest request, string key = "message")
- {
- App.mainWindowViewModel.Value = 6;
- string repsonseData = "";
-
- try
- {
- using (var requestStream = request.GetRequestStream())
- {
- await formData.CopyToAsync(requestStream);
- }
- // 获取HTTP响应
- HttpWebResponse response2 = (HttpWebResponse)request.GetResponse();
- using (StreamReader reader = new StreamReader(response2.GetResponseStream()))
- {
- string responseData = reader.ReadToEnd();
- Console.WriteLine(responseData);
- reader.Close();
- App.mainWindowViewModel.Value = 7;
- JObject jobject = (JObject)JsonConvert.DeserializeObject(responseData);
- App.mainWindowViewModel.Value = 8;
- if (response2 != null)
- {
- response2.Close();
- }
- if (request != null)
- {
- request.Abort();
- }
- string unicode = "";
- // 遍历字典对象输出键值对
- ChatGPTCode = jobject["code"].ToObject<string>().ToLower();
- if (ChatGPTCode == "200")
- {
- unicode = jobject["data"][key].ToObject<string>().ToLower();
- }
- //将Unicode格式转换成String
- //repsonseData = Regex.Unescape(unicode);
- repsonseData = unicode;
- clossProcess();
- }
- }
- catch (WebException ex)
- {
- App.mainWindowViewModel.Value = 8;
- int statusCode = 0;
- if (ex.Response is HttpWebResponse)
- {
- //从网络异常信息里解析错误,后面的逻辑根据错误码判断会更准确
- statusCode = (int)((HttpWebResponse)ex.Response).StatusCode;
- }
- if (statusCode == 401)
- {
- ChatGPTCode = "401";
- }
- else ChatGPTCode = "300";
- clossProcess();
- return null;
- }
- catch
- {
- ChatGPTCode = "300";
- clossProcess();
- }
- clossProcess();
- return repsonseData;
- }
- /// <summary>
- /// 从接口获取信息,获取message对应value值,WebClient没有设置请求时间参数换一种方案
- /// </summary>
- /// <param name="url"></param>
- /// <param name="namevalue"></param>
- /// <returns></returns>
- private static async Task<string> PostStringWebClient(string url, System.Collections.Specialized.NameValueCollection namevalue, string key = "message")
- {
- App.mainWindowViewModel.Value = 5;
- string repsonseData = "";
- try
- {
- using (var client = new WebClient())
- {
- byte[] bytes = await client.UploadValuesTaskAsync(url, namevalue);
- //转换成字符串类型
- var json = Encoding.Default.GetString(bytes);
- //将Json格式字符串转换成键值对
- //var values = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
- JObject jobject = (JObject)JsonConvert.DeserializeObject(json);
- App.mainWindowViewModel.Value = 8;
- string unicode = "";
- // 遍历字典对象输出键值对
- ChatGPTCode = jobject["code"].ToObject<string>().ToLower();
- if (ChatGPTCode == "200")
- {
- unicode = jobject["data"][key].ToObject<string>().ToLower();
- }
- //将Unicode格式转换成String
- //repsonseData = Regex.Unescape(unicode);
- repsonseData = unicode;
- }
- clossProcess();
- }
- catch (WebException ex)
- {
- int statusCode = 0;
- if (ex.Response is HttpWebResponse)
- {
- //从网络异常信息里解析错误,后面的逻辑根据错误码判断会更准确
- statusCode = (int)((HttpWebResponse)ex.Response).StatusCode;
- }
- if (statusCode == 401)
- {
- ChatGPTCode = "401";
- }
- else ChatGPTCode = "300";
- clossProcess();
- return null;
- }
- return repsonseData;
- }
- /// <summary>
- /// 初始进度条
- /// </summary>
- /// <param name="isprocess">是否需要进度条</param>
- /// <param name="progressTitle">文案</param>
- public static void intProcess(bool isprocess = true, string progressTitle = "Translating...", System.Windows.Visibility ProcessCloseBtnVisible = System.Windows.Visibility.Collapsed)
- {
- if (isprocess)
- {
- App.mainWindowViewModel.ProgressTitle = progressTitle;
- App.mainWindowViewModel.MaxValue = 10;
- App.mainWindowViewModel.Value = 0;
- App.mainWindowViewModel.IsProcessVisible = System.Windows.Visibility.Visible;
- App.mainWindowViewModel.ProcessCloseBtnVisible = ProcessCloseBtnVisible;
- }
- }
- /// <summary>
- /// 初始进度条
- /// </summary>
- /// <param name="isprocess">是否需要进度条</param>
- /// <param name="progressTitle">文案</param>
- /// <param name="maxValue">进度条最大值</param>
- /// <param name="ProcessCloseBtnVisible">进度条关闭按钮是否可见</param>
- public static void SetIntProcess(bool isprocess = true, string progressTitle = "Splitting...", int maxValue = 10, System.Windows.Visibility ProcessCloseBtnVisible = System.Windows.Visibility.Collapsed)
- {
- if (isprocess)
- {
- App.mainWindowViewModel.ProgressTitle = progressTitle;
- App.mainWindowViewModel.MaxValue = maxValue;
- App.mainWindowViewModel.Value = 0;
- App.mainWindowViewModel.IsProcessVisible = System.Windows.Visibility.Visible;
- App.mainWindowViewModel.ProcessCloseBtnVisible = ProcessCloseBtnVisible;
- }
- }
- /// <summary>
- /// 关闭进度条
- /// </summary>
- public static void clossProcess()
- {
- App.mainWindowViewModel.Value = 9;
- App.mainWindowViewModel.IsProcessVisible = System.Windows.Visibility.Collapsed;
- App.mainWindowViewModel.ProcessCloseBtnVisible = System.Windows.Visibility.Visible;
- }
- /// <summary>
- /// 关闭进度条
- /// </summary>
- /// <param name="value">进度条当前值</param>
- public static void clossProcess(int value = 9)
- {
- App.mainWindowViewModel.Value = value;
- 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 AI服务器对接接口
- #region 支持翻译的语言
- /// <summary>
- /// 当前语言列表
- /// </summary>
- public static List<string> FromlanguageFamily { set; get; } = new List<string>();
- public static List<string> SetFromlanguageOrigin()
- {
- FromlanguageFamily.Clear();
- FromlanguageFamily.Add("Automatic Identification");
- FromlanguageFamily.Add("English");
- FromlanguageFamily.Add("Simplified Chinese");
- FromlanguageFamily.Add("Traditional Chinese");
- FromlanguageFamily.Add("Japanese");
- FromlanguageFamily.Add("Korean");
- FromlanguageFamily.Add("French");
- FromlanguageFamily.Add("Spanish");
- FromlanguageFamily.Add("Italian");
- FromlanguageFamily.Add("German");
- FromlanguageFamily.Add("Portuguese");
- FromlanguageFamily.Add("Russian");
- FromlanguageFamily.Add("Vietnamese");
- FromlanguageFamily.Add("Thai");
- FromlanguageFamily.Add("Arabic");
- FromlanguageFamily.Add("Greek");
- FromlanguageFamily.Add("Bulgarian");
- FromlanguageFamily.Add("Finnish");
- FromlanguageFamily.Add("Slovene");
- FromlanguageFamily.Add("Dutch");
- FromlanguageFamily.Add("Czech");
- FromlanguageFamily.Add("Swedish");
- FromlanguageFamily.Add("Polish");
- FromlanguageFamily.Add("Danish");
- FromlanguageFamily.Add("Romanian");
- FromlanguageFamily.Add("Hungarian");
- return FromlanguageFamily;
- }
- /// <summary>
- /// 需要翻译成对应语言列表
- /// </summary>
- public static List<string> TolanguageFamily { set; get; } = new List<string>();
- public static List<string> SetTolanguageOrigin()
- {
- TolanguageFamily.Clear();
- TolanguageFamily.Add("English");
- TolanguageFamily.Add("Simplified Chinese");
- TolanguageFamily.Add("Traditional Chinese");
- TolanguageFamily.Add("Japanese");
- TolanguageFamily.Add("Korean");
- TolanguageFamily.Add("French");
- TolanguageFamily.Add("Spanish");
- TolanguageFamily.Add("Italian");
- TolanguageFamily.Add("German");
- TolanguageFamily.Add("Portuguese");
- TolanguageFamily.Add("Russian");
- TolanguageFamily.Add("Vietnamese");
- TolanguageFamily.Add("Thai");
- TolanguageFamily.Add("Arabic");
- TolanguageFamily.Add("Greek");
- TolanguageFamily.Add("Bulgarian");
- TolanguageFamily.Add("Finnish");
- TolanguageFamily.Add("Slovene");
- TolanguageFamily.Add("Dutch");
- TolanguageFamily.Add("Czech");
- TolanguageFamily.Add("Swedish");
- TolanguageFamily.Add("Polish");
- TolanguageFamily.Add("Danish");
- TolanguageFamily.Add("Romanian");
- TolanguageFamily.Add("Hungarian");
- return TolanguageFamily;
- }
- /// <summary>
- /// 接口传值对应字符
- /// </summary>
- /// <param name="index">控制form与to的区别,form为0开始,to为1开始,因为to没有auto</param>
- /// <returns></returns>
- public static string UpdateLanguagebType(int index)
- {
- switch (index)
- {
- case 0:
- return "auto";
- case 1:
- return "en";
- case 2:
- return "zh";
- case 3:
- return "cht";
- case 4:
- return "jp";
- case 5:
- return "kor";
- case 6:
- return "fra";
- case 7:
- return "spa";
- case 8:
- return "it";
- case 9:
- return "de";
- case 10:
- return "pt";
- case 11:
- return "ru";
- case 12:
- return "vie";
- case 13:
- return "th";
- case 14:
- return "ara";
- case 15:
- return "el";
- case 16:
- return "bul";
- case 17:
- return "fin";
- case 18:
- return "slo";
- case 19:
- return "nl";
- case 20:
- return "cs";
- case 21:
- return "swe";
- case 22:
- return "pl";
- case 23:
- return "dan";
- case 24:
- return "rom";
- case 25:
- return "hu";
- default:
- return "";
- }
- }
- #endregion 支持翻译的语言
- #region 错误码
- public static string GetBaiduTranslationCode(string code)
- {
- if (code == "300" || code == "401") { return App.HomePageLoader.GetString("ChatGTPNetworkError"); }
- if (!String.IsNullOrEmpty(App.HomePageLoader.GetString("BaiduTranslation" + code)))
- {
- return App.HomePageLoader.GetString("BaiduTranslation" + code);
- }
- return code;
- }
- public static string GetChatGPTCode(string code)
- {
- if (code == "300" || code == "401") { return App.HomePageLoader.GetString("ChatGTPNetworkError"); }
- if (!String.IsNullOrEmpty(App.HomePageLoader.GetString("ChatGTP" + code)))
- {
- return App.HomePageLoader.GetString("ChatGTP" + code);
- }
- return code;
- }
- #endregion 错误码
- }
- }
|