ChatGPTAIRewritingContentViewModel.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. public IDialogService dialogs;
  98. private string inputText;
  99. public string InputText
  100. {
  101. get { return inputText; }
  102. set
  103. {
  104. SetProperty(ref inputText, value);
  105. }
  106. }
  107. private string rewriteText;
  108. public string RewriteText
  109. {
  110. get { return rewriteText; }
  111. set
  112. {
  113. SetProperty(ref rewriteText, value);
  114. }
  115. }
  116. private Visibility showTip = Visibility.Collapsed;
  117. public Visibility ShowTip
  118. {
  119. get
  120. {
  121. return showTip;
  122. }
  123. set
  124. {
  125. SetProperty(ref showTip, value);
  126. }
  127. }
  128. private string errorTipText= App.HomePageLoader.GetString("ChatGTPLimit150");
  129. public string ErrorTipText
  130. {
  131. get { return errorTipText; }
  132. set
  133. {
  134. SetProperty(ref errorTipText, value);
  135. }
  136. }
  137. private Visibility errorVisible = Visibility.Collapsed;
  138. public Visibility ErrorVisible
  139. {
  140. get
  141. {
  142. return errorVisible;
  143. }
  144. set
  145. {
  146. SetProperty(ref errorVisible, value);
  147. }
  148. }
  149. #endregion
  150. #region 委托声明
  151. public DelegateCommand CopyCommand { get; set; }
  152. public DelegateCommand RewriteCommand { get; set; }
  153. public DelegateCommand<object> textBoxEnterCharactersTextChangedCommad { get; set; }
  154. #endregion
  155. public ChatGPTAIRewritingContentViewModel(IDialogService dialogService)
  156. {
  157. dialogs = dialogService;
  158. CopyCommand = new DelegateCommand(copy);
  159. RewriteCommand = new DelegateCommand(rewrite);
  160. textBoxEnterCharactersTextChangedCommad = new DelegateCommand<object>(textBoxEnterCharactersTextChanged);
  161. InitString();
  162. }
  163. #region 逻辑函数
  164. /// <summary>
  165. /// 检查文字是否超出范围
  166. /// </summary>
  167. /// <param name="e"></param>
  168. private void textBoxEnterCharactersTextChanged(object e)
  169. {
  170. var ConverterPreview = e as TextBoxEx;
  171. if (ConverterPreview != null)
  172. {
  173. //结果置空
  174. RewriteText = "";
  175. if (ConverterPreview.Text.Length > 150)
  176. {
  177. ErrorTipText = App.HomePageLoader.GetString("ChatGTPLimit150"); ;
  178. ErrorVisible = Visibility.Visible;
  179. }
  180. else
  181. {
  182. ErrorVisible = Visibility.Collapsed;
  183. }
  184. }
  185. }
  186. /// <summary>
  187. /// 复制到剪切板
  188. /// </summary>
  189. public void copy()
  190. {
  191. try
  192. {
  193. System.Windows.Forms.Clipboard.SetText(RewriteText);
  194. //Copy成功显示
  195. ShowTip=Visibility.Visible;
  196. }
  197. catch { }
  198. }
  199. /// <summary>
  200. /// 重写逻辑
  201. /// </summary>
  202. public async void rewrite()
  203. {
  204. bool flg = false;
  205. //权益弹窗
  206. if (!App.IsLogin || Settings.Default.UserDate.subscribestatus != 1)
  207. {
  208. DialogParameters value = new DialogParameters();
  209. value.Add(ParameterNames.Open, "AI");
  210. dialogs.ShowDialog(DialogNames.SubscriptionDialog, value, dialogResult =>
  211. {
  212. if (dialogResult.Result == ButtonResult.OK)
  213. {
  214. flg = true;
  215. }
  216. else
  217. {
  218. flg = false;
  219. }
  220. });
  221. if (flg == false)
  222. {
  223. return;
  224. }
  225. }
  226. ErrorVisible = Visibility.Collapsed;
  227. await Task.Run(async delegate
  228. {
  229. RewriteText = await ChatGTPAIHelper.Rewrite(InputText);
  230. });
  231. if (ChatGTPAIHelper.ChatGPTCode != "200")
  232. {
  233. //错误码文案,报错样式
  234. ErrorTipText = ChatGTPAIHelper.GetChatGPTCode(ChatGTPAIHelper.ChatGPTCode);
  235. ErrorVisible = Visibility.Visible;
  236. }
  237. }
  238. #endregion
  239. #region 构架行为
  240. public void OnNavigatedTo(NavigationContext navigationContext)
  241. {
  242. navigationContext.Parameters.TryGetValue<HomeContentViewModel>(ParameterNames.HomeContentViewModel, out homeContentViewModel);
  243. }
  244. public bool IsNavigationTarget(NavigationContext navigationContext)
  245. {
  246. return true;
  247. }
  248. public void OnNavigatedFrom(NavigationContext navigationContext)
  249. {
  250. }
  251. #endregion
  252. }
  253. }