ChatGPTAIRewritingContentViewModel.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. private string T_title;
  22. public string T_Title
  23. {
  24. get { return T_title; }
  25. set
  26. {
  27. SetProperty(ref T_title, value);
  28. }
  29. }
  30. private string T_limitTip;
  31. public string T_LimitTip
  32. {
  33. get { return T_limitTip; }
  34. set
  35. {
  36. SetProperty(ref T_limitTip, value);
  37. }
  38. }
  39. private string T_textBoxWatermark;
  40. public string T_TextBoxWatermark
  41. {
  42. get { return T_textBoxWatermark; }
  43. set
  44. {
  45. SetProperty(ref T_textBoxWatermark, value);
  46. }
  47. }
  48. private string T_copy;
  49. public string T_Copy
  50. {
  51. get { return T_copy; }
  52. set
  53. {
  54. SetProperty(ref T_copy, value);
  55. }
  56. }
  57. private string T_result;
  58. public string T_Result
  59. {
  60. get { return T_result; }
  61. set
  62. {
  63. SetProperty(ref T_result, value);
  64. }
  65. }
  66. private string T_rewritingBtn;
  67. public string T_RewritingBtn
  68. {
  69. get { return T_rewritingBtn; }
  70. set
  71. {
  72. SetProperty(ref T_rewritingBtn, value);
  73. }
  74. }
  75. private string T_copied;
  76. public string T_Copied
  77. {
  78. get { return T_copied; }
  79. set
  80. {
  81. SetProperty(ref T_copied, value);
  82. }
  83. }
  84. private void InitString()
  85. {
  86. T_Title = App.HomePageLoader.GetString("AIRewriteBtn_Title");
  87. T_LimitTip = App.HomePageLoader.GetString("ChatGTPAI_LimitTip");
  88. T_TextBoxWatermark = App.HomePageLoader.GetString("ChatGTPAI_TextBoxWatermark");
  89. T_Copy = App.HomePageLoader.GetString("ChatGTPAI_Copy");
  90. T_Result = App.HomePageLoader.GetString("ChatGTPAI_Result");
  91. T_RewritingBtn = App.HomePageLoader.GetString("AIRewriteBtn_RewritingBtn");
  92. T_Copied = App.HomePageLoader.GetString("ChatGTPAI_Copied");
  93. }
  94. #endregion
  95. #region 参数和属性
  96. public HomeContentViewModel homeContentViewModel = null;
  97. private string inputText;
  98. public string InputText
  99. {
  100. get { return inputText; }
  101. set
  102. {
  103. SetProperty(ref inputText, value);
  104. }
  105. }
  106. private string rewriteText;
  107. public string RewriteText
  108. {
  109. get { return rewriteText; }
  110. set
  111. {
  112. SetProperty(ref rewriteText, value);
  113. }
  114. }
  115. private Visibility showTip = Visibility.Collapsed;
  116. public Visibility ShowTip
  117. {
  118. get
  119. {
  120. return showTip;
  121. }
  122. set
  123. {
  124. SetProperty(ref showTip, value);
  125. }
  126. }
  127. private string errorTipText= App.HomePageLoader.GetString("ChatGTPLimit150");
  128. public string ErrorTipText
  129. {
  130. get { return errorTipText; }
  131. set
  132. {
  133. SetProperty(ref errorTipText, value);
  134. }
  135. }
  136. private Visibility errorVisible = Visibility.Collapsed;
  137. public Visibility ErrorVisible
  138. {
  139. get
  140. {
  141. return errorVisible;
  142. }
  143. set
  144. {
  145. SetProperty(ref errorVisible, value);
  146. }
  147. }
  148. #endregion
  149. #region 委托声明
  150. public DelegateCommand CopyCommand { get; set; }
  151. public DelegateCommand RewriteCommand { get; set; }
  152. public DelegateCommand<object> textBoxEnterCharactersTextChangedCommad { get; set; }
  153. #endregion
  154. public ChatGPTAIRewritingContentViewModel()
  155. {
  156. CopyCommand = new DelegateCommand(copy);
  157. RewriteCommand = new DelegateCommand(rewrite);
  158. textBoxEnterCharactersTextChangedCommad = new DelegateCommand<object>(textBoxEnterCharactersTextChanged);
  159. InitString();
  160. }
  161. #region 逻辑函数
  162. /// <summary>
  163. /// 检查文字是否超出范围
  164. /// </summary>
  165. /// <param name="e"></param>
  166. private void textBoxEnterCharactersTextChanged(object e)
  167. {
  168. var ConverterPreview = e as TextBoxEx;
  169. if (ConverterPreview != null)
  170. {
  171. //结果置空
  172. RewriteText = "";
  173. if (ConverterPreview.Text.Length > 150)
  174. {
  175. ErrorTipText = App.HomePageLoader.GetString("ChatGTPLimit150"); ;
  176. ErrorVisible = Visibility.Visible;
  177. }
  178. else
  179. {
  180. ErrorVisible = Visibility.Collapsed;
  181. }
  182. }
  183. }
  184. /// <summary>
  185. /// 复制到剪切板
  186. /// </summary>
  187. public void copy()
  188. {
  189. try
  190. {
  191. System.Windows.Forms.Clipboard.SetText(RewriteText);
  192. //Copy成功显示
  193. ShowTip=Visibility.Visible;
  194. }
  195. catch { }
  196. }
  197. /// <summary>
  198. /// 重写逻辑
  199. /// </summary>
  200. public async void rewrite()
  201. {
  202. //添加付费拦截锁
  203. if (!ServiceHelper.IAPBeforeFunction())
  204. {
  205. return;
  206. }
  207. if (Settings.Default.UserDate.subscribestatus != 1)
  208. {
  209. App.mainWindowViewModel.dialogs.ShowDialog(DialogNames.IAPCompareDialog);
  210. return;
  211. }
  212. ErrorVisible = Visibility.Collapsed;
  213. await Task.Run(async delegate
  214. {
  215. RewriteText = await ChatGTPAIHelper.Rewrite(InputText);
  216. });
  217. if (ChatGTPAIHelper.ChatGPTCode != "200")
  218. {
  219. //错误码文案,报错样式
  220. ErrorTipText = ChatGTPAIHelper.GetChatGPTCode(ChatGTPAIHelper.ChatGPTCode);
  221. ErrorVisible = Visibility.Visible;
  222. }
  223. }
  224. #endregion
  225. #region 构架行为
  226. public void OnNavigatedTo(NavigationContext navigationContext)
  227. {
  228. navigationContext.Parameters.TryGetValue<HomeContentViewModel>(ParameterNames.HomeContentViewModel, out homeContentViewModel);
  229. }
  230. public bool IsNavigationTarget(NavigationContext navigationContext)
  231. {
  232. return true;
  233. }
  234. public void OnNavigatedFrom(NavigationContext navigationContext)
  235. {
  236. }
  237. #endregion
  238. }
  239. }