ChatGPTAIRewritingContentViewModel.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. using PDF_Master.CustomControl;
  2. using PDF_Master.Helper;
  3. using PDF_Master.Model;
  4. using PDF_Master.Properties;
  5. using PDFReader_WPF.Helper;
  6. using Prism.Commands;
  7. using Prism.Mvvm;
  8. using Prism.Regions;
  9. using Prism.Services.Dialogs;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using System.Threading.Tasks;
  14. using System.Windows;
  15. using System.Windows.Forms;
  16. using System.Windows.Threading;
  17. namespace PDF_Master.ViewModels.HomePanel.ChatGPTAI
  18. {
  19. public class ChatGPTAIRewritingContentViewModel : BindableBase, INavigationAware
  20. {
  21. #region 文案
  22. private string T_title;
  23. public string T_Title
  24. {
  25. get { return T_title; }
  26. set
  27. {
  28. SetProperty(ref T_title, value);
  29. }
  30. }
  31. private string T_limitTip;
  32. public string T_LimitTip
  33. {
  34. get { return T_limitTip; }
  35. set
  36. {
  37. SetProperty(ref T_limitTip, value);
  38. }
  39. }
  40. private string T_textBoxWatermark;
  41. public string T_TextBoxWatermark
  42. {
  43. get { return T_textBoxWatermark; }
  44. set
  45. {
  46. SetProperty(ref T_textBoxWatermark, value);
  47. }
  48. }
  49. private string T_copy;
  50. public string T_Copy
  51. {
  52. get { return T_copy; }
  53. set
  54. {
  55. SetProperty(ref T_copy, value);
  56. }
  57. }
  58. private string T_result;
  59. public string T_Result
  60. {
  61. get { return T_result; }
  62. set
  63. {
  64. SetProperty(ref T_result, value);
  65. }
  66. }
  67. private string T_rewritingBtn;
  68. public string T_RewritingBtn
  69. {
  70. get { return T_rewritingBtn; }
  71. set
  72. {
  73. SetProperty(ref T_rewritingBtn, value);
  74. }
  75. }
  76. private string T_copied;
  77. public string T_Copied
  78. {
  79. get { return T_copied; }
  80. set
  81. {
  82. SetProperty(ref T_copied, value);
  83. }
  84. }
  85. private void InitString()
  86. {
  87. T_Title = App.HomePageLoader.GetString("AIRewriteBtn_Title");
  88. T_LimitTip = App.HomePageLoader.GetString("ChatGTPAI_LimitTip");
  89. T_TextBoxWatermark = App.HomePageLoader.GetString("ChatGTPAI_TextBoxWatermark");
  90. T_Copy = App.HomePageLoader.GetString("ChatGTPAI_Copy");
  91. T_Result = App.HomePageLoader.GetString("ChatGTPAI_Result");
  92. T_RewritingBtn = App.HomePageLoader.GetString("AIRewriteBtn_RewritingBtn");
  93. T_Copied = App.HomePageLoader.GetString("ChatGTPAI_Copied");
  94. }
  95. #endregion
  96. #region 参数和属性
  97. public HomeContentViewModel homeContentViewModel = null;
  98. public IDialogService dialogs;
  99. private string inputText;
  100. public string InputText
  101. {
  102. get { return inputText; }
  103. set
  104. {
  105. SetProperty(ref inputText, value);
  106. }
  107. }
  108. private string rewriteText;
  109. public string RewriteText
  110. {
  111. get { return rewriteText; }
  112. set
  113. {
  114. SetProperty(ref rewriteText, value);
  115. }
  116. }
  117. private Visibility showTip = Visibility.Collapsed;
  118. public Visibility ShowTip
  119. {
  120. get
  121. {
  122. return showTip;
  123. }
  124. set
  125. {
  126. SetProperty(ref showTip, value);
  127. }
  128. }
  129. private string errorTipText= App.HomePageLoader.GetString("ChatGTPLimit150");
  130. public string ErrorTipText
  131. {
  132. get { return errorTipText; }
  133. set
  134. {
  135. SetProperty(ref errorTipText, value);
  136. }
  137. }
  138. private Visibility errorVisible = Visibility.Collapsed;
  139. public Visibility ErrorVisible
  140. {
  141. get
  142. {
  143. return errorVisible;
  144. }
  145. set
  146. {
  147. SetProperty(ref errorVisible, value);
  148. }
  149. }
  150. #endregion
  151. #region 委托声明
  152. public DelegateCommand CopyCommand { get; set; }
  153. public DelegateCommand RewriteCommand { get; set; }
  154. public DelegateCommand<object> textBoxEnterCharactersTextChangedCommad { get; set; }
  155. #endregion
  156. public ChatGPTAIRewritingContentViewModel(IDialogService dialogService)
  157. {
  158. dialogs = dialogService;
  159. CopyCommand = new DelegateCommand(copy);
  160. RewriteCommand = new DelegateCommand(rewrite);
  161. textBoxEnterCharactersTextChangedCommad = new DelegateCommand<object>(textBoxEnterCharactersTextChanged);
  162. InitString();
  163. }
  164. #region 逻辑函数
  165. /// <summary>
  166. /// 检查文字是否超出范围
  167. /// </summary>
  168. /// <param name="e"></param>
  169. private void textBoxEnterCharactersTextChanged(object e)
  170. {
  171. var ConverterPreview = e as TextBoxEx;
  172. if (ConverterPreview != null)
  173. {
  174. //结果置空
  175. RewriteText = "";
  176. if (ConverterPreview.Text.Length > 150)
  177. {
  178. ErrorTipText = App.HomePageLoader.GetString("ChatGTPLimit150"); ;
  179. ErrorVisible = Visibility.Visible;
  180. }
  181. else
  182. {
  183. ErrorVisible = Visibility.Collapsed;
  184. }
  185. }
  186. }
  187. /// <summary>
  188. /// 复制到剪切板
  189. /// </summary>
  190. public void copy()
  191. {
  192. try
  193. {
  194. System.Windows.Forms.Clipboard.SetText(RewriteText);
  195. //Copy成功显示
  196. ShowTip=Visibility.Visible;
  197. }
  198. catch { }
  199. }
  200. /// <summary>
  201. /// 重写逻辑
  202. /// </summary>
  203. public async void rewrite()
  204. {
  205. DataTrackingHelper.SetSendInformation(DataTrackingHelper.EventType.Purchase_AI, DataTrackingHelper.EntryPathKeyType.Home_Tools);
  206. DataTrackingHelper.AddFirstAndSecondaryPath(DataTrackingHelper.FirstPath.Home, DataTrackingHelper.SecondaryPath.AIRewrite);
  207. bool flg = false;
  208. //权益弹窗
  209. if (!App.IsLogin || Settings.Default.UserDate.subscribestatus != 1)
  210. {
  211. DialogParameters value = new DialogParameters();
  212. value.Add(ParameterNames.Open, "AI");
  213. dialogs.ShowDialog(DialogNames.SubscriptionDialog, value, dialogResult =>
  214. {
  215. if (dialogResult.Result == ButtonResult.OK)
  216. {
  217. flg = true;
  218. }
  219. else
  220. {
  221. flg = false;
  222. }
  223. });
  224. if (flg == false)
  225. {
  226. return;
  227. }
  228. }
  229. ErrorVisible = Visibility.Collapsed;
  230. await Task.Run(async delegate
  231. {
  232. RewriteText = await ChatGTPAIHelper.Rewrite(InputText);
  233. });
  234. if (ChatGTPAIHelper.ChatGPTCode != "200")
  235. {
  236. //错误码文案,报错样式
  237. ErrorTipText = ChatGTPAIHelper.GetChatGPTCode(ChatGTPAIHelper.ChatGPTCode);
  238. ErrorVisible = Visibility.Visible;
  239. }
  240. }
  241. #endregion
  242. #region 构架行为
  243. public void OnNavigatedTo(NavigationContext navigationContext)
  244. {
  245. navigationContext.Parameters.TryGetValue<HomeContentViewModel>(ParameterNames.HomeContentViewModel, out homeContentViewModel);
  246. }
  247. public bool IsNavigationTarget(NavigationContext navigationContext)
  248. {
  249. return true;
  250. }
  251. public void OnNavigatedFrom(NavigationContext navigationContext)
  252. {
  253. }
  254. #endregion
  255. }
  256. }