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.Linq; using System.Threading.Tasks; using System.Windows; using System.Windows.Forms; using System.Windows.Threading; namespace PDF_Master.ViewModels.HomePanel.ChatGPTAI { public class ChatGPTAIRewritingContentViewModel : BindableBase, INavigationAware { #region 文案 private string T_title; public string T_Title { get { return T_title; } set { SetProperty(ref T_title, value); } } private string T_limitTip; public string T_LimitTip { get { return T_limitTip; } set { SetProperty(ref T_limitTip, value); } } private string T_textBoxWatermark; public string T_TextBoxWatermark { get { return T_textBoxWatermark; } set { SetProperty(ref T_textBoxWatermark, value); } } private string T_copy; public string T_Copy { get { return T_copy; } set { SetProperty(ref T_copy, value); } } private string T_result; public string T_Result { get { return T_result; } set { SetProperty(ref T_result, value); } } private string T_rewritingBtn; public string T_RewritingBtn { get { return T_rewritingBtn; } set { SetProperty(ref T_rewritingBtn, value); } } private string T_copied; public string T_Copied { get { return T_copied; } set { SetProperty(ref T_copied, value); } } private void InitString() { T_Title = App.HomePageLoader.GetString("AIRewriteBtn_Title"); T_LimitTip = App.HomePageLoader.GetString("ChatGTPAI_LimitTip"); T_TextBoxWatermark = App.HomePageLoader.GetString("ChatGTPAI_TextBoxWatermark"); T_Copy = App.HomePageLoader.GetString("ChatGTPAI_Copy"); T_Result = App.HomePageLoader.GetString("ChatGTPAI_Result"); T_RewritingBtn = App.HomePageLoader.GetString("AIRewriteBtn_RewritingBtn"); T_Copied = App.HomePageLoader.GetString("ChatGTPAI_Copied"); } #endregion #region 参数和属性 public HomeContentViewModel homeContentViewModel = null; public IDialogService dialogs; private string inputText; public string InputText { get { return inputText; } set { SetProperty(ref inputText, value); } } private string rewriteText; public string RewriteText { get { return rewriteText; } set { SetProperty(ref rewriteText, value); if ((RewriteText == null || RewriteText.Length == 0) && ErrorVisible == Visibility.Collapsed) { NullStateVisible = Visibility.Visible; } else { NullStateVisible = Visibility.Collapsed; } } } private Visibility showTip = Visibility.Collapsed; public Visibility ShowTip { get { return showTip; } set { SetProperty(ref showTip, value); } } private string errorTipText= App.HomePageLoader.GetString("ChatGTPLimit150"); public string ErrorTipText { get { return errorTipText; } set { SetProperty(ref errorTipText, value); } } private Visibility errorVisible = Visibility.Collapsed; public Visibility ErrorVisible { get { return errorVisible; } set { SetProperty(ref errorVisible, value); if ((RewriteText == null || RewriteText.Length == 0) && ErrorVisible == Visibility.Collapsed) { NullStateVisible = Visibility.Visible; } else { NullStateVisible = Visibility.Collapsed; } } } private Visibility nullStateVisible = Visibility.Visible; public Visibility NullStateVisible { get { return nullStateVisible; } set { SetProperty(ref nullStateVisible, value); } } #endregion #region 委托声明 public DelegateCommand CopyCommand { get; set; } public DelegateCommand RewriteCommand { get; set; } public DelegateCommand textBoxEnterCharactersTextChangedCommad { get; set; } #endregion public ChatGPTAIRewritingContentViewModel(IDialogService dialogService) { dialogs = dialogService; CopyCommand = new DelegateCommand(copy); RewriteCommand = new DelegateCommand(rewrite); textBoxEnterCharactersTextChangedCommad = new DelegateCommand(textBoxEnterCharactersTextChanged); InitString(); } #region 逻辑函数 /// /// 检查文字是否超出范围 /// /// private void textBoxEnterCharactersTextChanged(object e) { var ConverterPreview = e as TextBoxEx; if (ConverterPreview != null) { //结果置空 RewriteText = ""; if (ConverterPreview.Text.Length > 150) { ErrorTipText = App.HomePageLoader.GetString("ChatGTPLimit150"); ; ErrorVisible = Visibility.Visible; } else { ErrorVisible = Visibility.Collapsed; } } } /// /// 复制到剪切板 /// public void copy() { try { System.Windows.Forms.Clipboard.SetText(RewriteText); //Copy成功显示 ShowTip=Visibility.Visible; } catch { } } /// /// 重写逻辑 /// public async void rewrite() { DataTrackingHelper.SetSendInformation(DataTrackingHelper.EventType.Purchase_AI, DataTrackingHelper.EntryPathKeyType.Home_Tools); DataTrackingHelper.AddFirstAndSecondaryPath(DataTrackingHelper.FirstPath.Home, DataTrackingHelper.SecondaryPath.AIRewrite); //添加付费拦截锁 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 ErrorVisible = Visibility.Collapsed; await Task.Run(async delegate { RewriteText = await ChatGTPAIHelper.Rewrite(InputText); }); if (ChatGTPAIHelper.ChatGPTCode != "200") { //错误码文案,报错样式 ErrorTipText = ChatGTPAIHelper.GetChatGPTCode(ChatGTPAIHelper.ChatGPTCode); ErrorVisible = 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 } }