ChatGPTAIRewritingContentViewModel.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. using PDF_Master.CustomControl;
  2. using PDF_Master.Helper;
  3. using PDF_Master.Model;
  4. using PDF_Master.Properties;
  5. using Prism.Commands;
  6. using Prism.Mvvm;
  7. using Prism.Regions;
  8. using Prism.Services.Dialogs;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Threading.Tasks;
  13. using System.Windows;
  14. using System.Windows.Forms;
  15. using System.Windows.Threading;
  16. namespace PDF_Master.ViewModels.HomePanel.ChatGPTAI
  17. {
  18. public class ChatGPTAIRewritingContentViewModel : BindableBase, INavigationAware
  19. {
  20. #region 参数和属性
  21. public HomeContentViewModel homeContentViewModel = null;
  22. private string inputText;
  23. public string InputText
  24. {
  25. get { return inputText; }
  26. set
  27. {
  28. SetProperty(ref inputText, value);
  29. }
  30. }
  31. private string rewriteText;
  32. public string RewriteText
  33. {
  34. get { return rewriteText; }
  35. set
  36. {
  37. SetProperty(ref rewriteText, value);
  38. }
  39. }
  40. private Visibility showTip = Visibility.Collapsed;
  41. public Visibility ShowTip
  42. {
  43. get
  44. {
  45. return showTip;
  46. }
  47. set
  48. {
  49. SetProperty(ref showTip, value);
  50. }
  51. }
  52. private string errorTipText;
  53. public string ErrorTipText
  54. {
  55. get { return errorTipText; }
  56. set
  57. {
  58. SetProperty(ref errorTipText, value);
  59. }
  60. }
  61. private Visibility errorVisible = Visibility.Collapsed;
  62. public Visibility ErrorVisible
  63. {
  64. get
  65. {
  66. return errorVisible;
  67. }
  68. set
  69. {
  70. SetProperty(ref errorVisible, value);
  71. }
  72. }
  73. #endregion
  74. #region 委托声明
  75. public DelegateCommand CopyCommand { get; set; }
  76. public DelegateCommand RewriteCommand { get; set; }
  77. public DelegateCommand<object> textBoxEnterCharactersTextChangedCommad { get; set; }
  78. #endregion
  79. public ChatGPTAIRewritingContentViewModel()
  80. {
  81. CopyCommand = new DelegateCommand(copy);
  82. RewriteCommand = new DelegateCommand(rewrite);
  83. textBoxEnterCharactersTextChangedCommad = new DelegateCommand<object>(textBoxEnterCharactersTextChanged);
  84. }
  85. #region 逻辑函数
  86. /// <summary>
  87. /// 检查文字是否超出范围
  88. /// </summary>
  89. /// <param name="e"></param>
  90. private void textBoxEnterCharactersTextChanged(object e)
  91. {
  92. var ConverterPreview = e as TextBoxEx;
  93. if (ConverterPreview != null)
  94. {
  95. //结果置空
  96. RewriteText = "";
  97. if (ConverterPreview.Text.Length > 150)
  98. {
  99. ErrorTipText = "Limit 150 characters at a time";
  100. ErrorVisible = Visibility.Visible;
  101. }
  102. else
  103. {
  104. ErrorVisible = Visibility.Collapsed;
  105. }
  106. }
  107. }
  108. /// <summary>
  109. /// 复制到剪切板
  110. /// </summary>
  111. public void copy()
  112. {
  113. try
  114. {
  115. System.Windows.Forms.Clipboard.SetText(RewriteText);
  116. //Copy成功显示
  117. ShowTip=Visibility.Visible;
  118. }
  119. catch { }
  120. }
  121. /// <summary>
  122. /// 重写逻辑
  123. /// </summary>
  124. public async void rewrite()
  125. {
  126. //添加付费拦截锁
  127. if (!ServiceHelper.IAPBeforeFunction())
  128. {
  129. return;
  130. }
  131. if (Settings.Default.UserDate.subscribestatus != 1)
  132. {
  133. App.mainWindowViewModel.dialogs.ShowDialog(DialogNames.IAPCompareDialog);
  134. return;
  135. }
  136. ErrorVisible = Visibility.Collapsed;
  137. await Task.Run(async delegate
  138. {
  139. RewriteText = await ChatGTPAIHelper.Rewrite(InputText);
  140. });
  141. if (ChatGTPAIHelper.Code != "200")
  142. {
  143. //错误码文案,报错样式
  144. ErrorTipText = ChatGTPAIHelper.Code;
  145. ErrorVisible = Visibility.Visible;
  146. }
  147. }
  148. #endregion
  149. #region 构架行为
  150. public void OnNavigatedTo(NavigationContext navigationContext)
  151. {
  152. navigationContext.Parameters.TryGetValue<HomeContentViewModel>(ParameterNames.HomeContentViewModel, out homeContentViewModel);
  153. }
  154. public bool IsNavigationTarget(NavigationContext navigationContext)
  155. {
  156. return true;
  157. }
  158. public void OnNavigatedFrom(NavigationContext navigationContext)
  159. {
  160. }
  161. #endregion
  162. }
  163. }