ChatGPTAIRewritingContentViewModel.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. if ((RewriteText == null || RewriteText.Length == 0) && ErrorVisible == Visibility.Collapsed)
  116. {
  117. NullStateVisible = Visibility.Visible;
  118. }
  119. else
  120. {
  121. NullStateVisible = Visibility.Collapsed;
  122. }
  123. }
  124. }
  125. private Visibility showTip = Visibility.Collapsed;
  126. public Visibility ShowTip
  127. {
  128. get
  129. {
  130. return showTip;
  131. }
  132. set
  133. {
  134. SetProperty(ref showTip, value);
  135. }
  136. }
  137. private string errorTipText= App.HomePageLoader.GetString("ChatGTPLimit150");
  138. public string ErrorTipText
  139. {
  140. get { return errorTipText; }
  141. set
  142. {
  143. SetProperty(ref errorTipText, value);
  144. }
  145. }
  146. private Visibility errorVisible = Visibility.Collapsed;
  147. public Visibility ErrorVisible
  148. {
  149. get
  150. {
  151. return errorVisible;
  152. }
  153. set
  154. {
  155. SetProperty(ref errorVisible, value);
  156. if ((RewriteText == null || RewriteText.Length == 0) && ErrorVisible == Visibility.Collapsed)
  157. {
  158. NullStateVisible = Visibility.Visible;
  159. }
  160. else
  161. {
  162. NullStateVisible = Visibility.Collapsed;
  163. }
  164. }
  165. }
  166. private Visibility nullStateVisible = Visibility.Visible;
  167. public Visibility NullStateVisible
  168. {
  169. get
  170. {
  171. return nullStateVisible;
  172. }
  173. set
  174. {
  175. SetProperty(ref nullStateVisible, value);
  176. }
  177. }
  178. #endregion
  179. #region 委托声明
  180. public DelegateCommand CopyCommand { get; set; }
  181. public DelegateCommand RewriteCommand { get; set; }
  182. public DelegateCommand<object> textBoxEnterCharactersTextChangedCommad { get; set; }
  183. #endregion
  184. public ChatGPTAIRewritingContentViewModel(IDialogService dialogService)
  185. {
  186. dialogs = dialogService;
  187. CopyCommand = new DelegateCommand(copy);
  188. RewriteCommand = new DelegateCommand(rewrite);
  189. textBoxEnterCharactersTextChangedCommad = new DelegateCommand<object>(textBoxEnterCharactersTextChanged);
  190. InitString();
  191. }
  192. #region 逻辑函数
  193. /// <summary>
  194. /// 检查文字是否超出范围
  195. /// </summary>
  196. /// <param name="e"></param>
  197. private void textBoxEnterCharactersTextChanged(object e)
  198. {
  199. var ConverterPreview = e as TextBoxEx;
  200. if (ConverterPreview != null)
  201. {
  202. //结果置空
  203. RewriteText = "";
  204. if (ConverterPreview.Text.Length > 150)
  205. {
  206. ErrorTipText = App.HomePageLoader.GetString("ChatGTPLimit150"); ;
  207. ErrorVisible = Visibility.Visible;
  208. }
  209. else
  210. {
  211. ErrorVisible = Visibility.Collapsed;
  212. }
  213. }
  214. }
  215. /// <summary>
  216. /// 复制到剪切板
  217. /// </summary>
  218. public void copy()
  219. {
  220. try
  221. {
  222. System.Windows.Forms.Clipboard.SetText(RewriteText);
  223. //Copy成功显示
  224. ShowTip=Visibility.Visible;
  225. }
  226. catch { }
  227. }
  228. /// <summary>
  229. /// 重写逻辑
  230. /// </summary>
  231. public async void rewrite()
  232. {
  233. DataTrackingHelper.SetSendInformation(DataTrackingHelper.EventType.Purchase_AI, DataTrackingHelper.EntryPathKeyType.Home_Tools);
  234. DataTrackingHelper.AddFirstAndSecondaryPath(DataTrackingHelper.FirstPath.Home, DataTrackingHelper.SecondaryPath.AIRewrite);
  235. //添加付费拦截锁
  236. if (!ServiceHelper.IAPBeforeFunction())
  237. {
  238. return;
  239. }
  240. #region 暂时关闭权益弹窗
  241. //bool flg = false;
  242. ////权益弹窗
  243. //if (!App.IsLogin || Settings.Default.UserDate.subscribestatus != 1)
  244. //{
  245. // DialogParameters value = new DialogParameters();
  246. // value.Add(ParameterNames.Open, "AI");
  247. // dialogs.ShowDialog(DialogNames.SubscriptionDialog, value, dialogResult =>
  248. // {
  249. // if (dialogResult.Result == ButtonResult.OK)
  250. // {
  251. // flg = true;
  252. // }
  253. // else
  254. // {
  255. // flg = false;
  256. // }
  257. // });
  258. // if (flg == false)
  259. // {
  260. // return;
  261. // }
  262. //}
  263. #endregion
  264. ErrorVisible = Visibility.Collapsed;
  265. await Task.Run(async delegate
  266. {
  267. RewriteText = await ChatGTPAIHelper.Rewrite(InputText);
  268. });
  269. if (ChatGTPAIHelper.ChatGPTCode != "200")
  270. {
  271. //错误码文案,报错样式
  272. ErrorTipText = ChatGTPAIHelper.GetChatGPTCode(ChatGTPAIHelper.ChatGPTCode);
  273. ErrorVisible = Visibility.Visible;
  274. }
  275. }
  276. #endregion
  277. #region 构架行为
  278. public void OnNavigatedTo(NavigationContext navigationContext)
  279. {
  280. navigationContext.Parameters.TryGetValue<HomeContentViewModel>(ParameterNames.HomeContentViewModel, out homeContentViewModel);
  281. }
  282. public bool IsNavigationTarget(NavigationContext navigationContext)
  283. {
  284. return true;
  285. }
  286. public void OnNavigatedFrom(NavigationContext navigationContext)
  287. {
  288. }
  289. #endregion
  290. }
  291. }