ServiceHelper.cs 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290
  1. using Dragablz;
  2. using Newtonsoft.Json;
  3. using Newtonsoft.Json.Linq;
  4. using PDF_Master.Model;
  5. using PDF_Master.Properties;
  6. using PDF_Master.ViewModels;
  7. using PDF_Master.Views;
  8. using Prism.Services.Dialogs;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Globalization;
  12. using System.IO;
  13. using System.Linq;
  14. using System.Management;
  15. using System.Net;
  16. using System.Runtime.InteropServices;
  17. using System.Security.Cryptography;
  18. using System.Text;
  19. using System.Text.RegularExpressions;
  20. using System.Threading.Tasks;
  21. using System.Windows;
  22. namespace PDF_Master.Helper
  23. {
  24. /// <summary>
  25. /// 注册登陆相关的工具类
  26. /// </summary>
  27. ///
  28. public static class ServiceHelper
  29. {
  30. public static uint WakeId;
  31. public static bool IsLogin=false;
  32. public static string ALLPassword = "";
  33. public static string access_token = "";
  34. public static string outemail = "";
  35. public static string outcode = "";
  36. public static string outregister = "";
  37. public static string outlogin = "";
  38. public static string code = "";
  39. public static string okcode = "";
  40. public static string remsg = "";
  41. public static string state = "";
  42. public static string AppCode = "com.brother.pdfreaderprofree.windows";
  43. #if DEBUG
  44. //测试环境
  45. public static string RequestHost = "http://139.196.160.101:8081";
  46. public static string WebHost = "http://test-pdf-pro.kdan.cn:3021";
  47. #else
  48. //正式环境
  49. public static string RequestHost = "https://pdfmaster.pdfreaderpro.com";
  50. public static string WebHost = "https://www.pdfreaderpro.com";
  51. #endif
  52. /// <summary>
  53. /// 验证邮箱
  54. /// </summary>
  55. public static string OKemailUrl = "/pdf-office-sso/auth/validUser";
  56. /// <summary>
  57. /// 验证验证码
  58. /// </summary>
  59. public static string OKcodeUrl = "/pdf-office-sso/auth/isEmailCodeValid";
  60. /// <summary>
  61. /// 发送验证码
  62. /// </summary>
  63. public static string GetcodeUrl = "/pdf-office-sso/auth/getVerifyCode";
  64. /// <summary>
  65. /// 注册邮箱
  66. /// </summary>
  67. public static string registerUrl = "/pdf-office-sso/auth/emailRegister";
  68. /// <summary>
  69. /// /登录
  70. /// </summary>
  71. public static string LoginUrl = "/pdf-office-sso/auth/emailLogin";
  72. /// <summary>
  73. /// 登出
  74. /// </summary>
  75. public static string LogoutUrl = "/pdf-office-sso/user/logout";
  76. /// <summary>
  77. /// 注销
  78. /// </summary>
  79. public static string UseroutUrl = "/pdf-office-sso/user/logOffForUser";
  80. /// <summary>
  81. /// 重置密码
  82. /// </summary>
  83. public static string RebirthUrl = "/pdf-office-sso/auth/resetPassword";
  84. /// <summary>
  85. /// 获取用户信息
  86. /// </summary>
  87. public static string GetUserUrl = "/pdf-office-sso/user/me";
  88. /// <summary>
  89. /// 收集软件启动数据
  90. /// </summary>
  91. public static string DeviceLogUrl = "/pdf-office-account/deviceLog/create";
  92. /// <summary>
  93. /// 设备是否被自动登出
  94. /// </summary>
  95. public static string CheckDeviceStatusUrl = "/pdf-office-sso/auth/checkDeviceStatus";
  96. public static string AcceptdataUrl = "http://127.0.0.1:5544";
  97. /// <summary>
  98. /// 用于检查网络是否可以连接互联网,true表示连接成功,false表示连接失败
  99. /// </summary>
  100. /// <returns></returns>
  101. [DllImport("wininet.dll")]
  102. private extern static bool InternetGetConnectedState(int Description, int ReservedValue);
  103. public static bool IsConnectInternet()
  104. {
  105. int Description = 0;
  106. return InternetGetConnectedState(Description, 0);
  107. }
  108. private static string computerInfo;
  109. /// <summary>
  110. /// 电脑品牌信息
  111. /// </summary>
  112. public static string ComputerInfo
  113. {
  114. get
  115. {
  116. if (computerInfo == null)
  117. {
  118. computerInfo = GetComputerInfo();
  119. }
  120. return computerInfo;
  121. }
  122. set { computerInfo = value; }
  123. }
  124. private static string systemInfo;
  125. /// <summary>
  126. /// 系统信息
  127. /// </summary>
  128. public static string SystemInfo
  129. {
  130. get
  131. {
  132. if (systemInfo == null)
  133. {
  134. systemInfo = GetSystemInfo();
  135. }
  136. return systemInfo;
  137. }
  138. set { systemInfo = value; }
  139. }
  140. private static string GetSystemInfo()
  141. {
  142. try
  143. {
  144. string s = "";
  145. SelectQuery sq = new SelectQuery("Win32_OperatingSystem");
  146. ManagementObjectSearcher mos = new ManagementObjectSearcher(sq);
  147. ManagementObjectCollection moc = mos.Get();
  148. foreach (ManagementObject mo in moc)
  149. {
  150. s = mo.Properties["Version"].Value.ToString();
  151. }
  152. moc.Dispose();
  153. sq = null;
  154. return s;
  155. }
  156. catch { return ""; }
  157. }
  158. private static string GetComputerInfo()
  159. {
  160. try
  161. {
  162. string s = "";
  163. System.Windows.Controls.TextBox text = new System.Windows.Controls.TextBox();
  164. SelectQuery sq = new SelectQuery("Win32_ComputerSystem");
  165. ManagementObjectSearcher mos = new ManagementObjectSearcher(sq);
  166. ManagementObjectCollection moc = mos.Get();
  167. foreach (ManagementObject mo in moc)
  168. {
  169. s = mo.Properties["Manufacturer"].Value + " " + mo.Properties["Model"].Value;
  170. }
  171. moc.Dispose();
  172. sq = null;
  173. return s;
  174. }
  175. catch { return ""; }
  176. }
  177. /// <summary>
  178. /// 操作系统生成的UUID,用于获取虚拟机用户的设备识别码
  179. /// </summary>
  180. /// <returns></returns>
  181. public static string GetUUID()
  182. {
  183. string systemId = null;
  184. using (ManagementObjectSearcher mos = new ManagementObjectSearcher("select * from Win32_ComputerSystemProduct"))
  185. {
  186. foreach (var item in mos.Get())
  187. {
  188. systemId = item["UUID"].ToString();
  189. }
  190. }
  191. return systemId;
  192. }
  193. /// <summary>
  194. /// 主板编号
  195. /// </summary>
  196. /// <returns></returns>
  197. public static string GetBoardId()
  198. {
  199. var st = string.Empty;
  200. var mos = new ManagementObjectSearcher("Select * from Win32_BaseBoard");
  201. foreach (var o in mos.Get())
  202. {
  203. var mo = (ManagementObject)o;
  204. st = mo["SerialNumber"].ToString();
  205. }
  206. return st;
  207. }
  208. /// <summary>
  209. /// 获取设备唯一ID
  210. /// </summary>
  211. /// <returns></returns>
  212. public static string GetDeviceSerialNumber()
  213. {
  214. StringBuilder DeviceBuilder = new StringBuilder();
  215. bool IsVirtual = false;
  216. string boardId = GetBoardId();
  217. string uuid = GetUUID();
  218. //判断是否是虚拟机用户
  219. if (boardId.ToLower() == "" || boardId.ToLower() == "none")
  220. {
  221. if (string.IsNullOrEmpty(uuid) || uuid.ToLower() == "none")
  222. return string.Empty;
  223. else
  224. IsVirtual = true;
  225. }
  226. //真实机器用户 用主板id作为唯一识别码
  227. if (!IsVirtual)
  228. {
  229. DeviceBuilder.Append(boardId);
  230. }
  231. else
  232. {
  233. DeviceBuilder.Append(uuid);
  234. }
  235. MD5 md5 = MD5.Create();
  236. List<byte> charbytes = new List<byte>();
  237. foreach (char code in DeviceBuilder.ToString().ToArray())
  238. {
  239. charbytes.Add((byte)code);
  240. }
  241. byte[] md5Array = md5.ComputeHash(charbytes.ToArray());
  242. DeviceBuilder = new StringBuilder();
  243. foreach (var code in md5Array)
  244. {
  245. DeviceBuilder.Append(code.ToString("X2"));
  246. }
  247. return DeviceBuilder.ToString();
  248. }
  249. /// <summary>
  250. /// 获取设备系统版本
  251. /// </summary>
  252. /// <returns></returns>
  253. public static string GetSystemVersionNumber()
  254. {
  255. var st = string.Empty;
  256. try
  257. {
  258. var mc = new ManagementClass("Win32_OperatingSystem");
  259. var moc = mc.GetInstances();
  260. foreach (var o in moc)
  261. {
  262. var mo = (ManagementObject)o;
  263. st = mo.Properties["Version"].Value.ToString();
  264. if (st != string.Empty)
  265. {
  266. break;
  267. }
  268. }
  269. }
  270. catch (Exception ex)
  271. {
  272. }
  273. return st;
  274. }
  275. /// <summary>
  276. /// 高级功能前弹出比较表
  277. /// </summary>
  278. /// <param name="post">是否请求服务器,true为请求</param>
  279. public static bool IAPBeforeFunction(bool post = false)
  280. {
  281. if (!App.IsLogin)
  282. {
  283. if (Settings.Default.UserDate.Email == "")
  284. {
  285. App.mainWindowViewModel.OpenRegister();
  286. return false;
  287. }
  288. else
  289. {
  290. App.mainWindowViewModel.OpenLogin();
  291. return false;
  292. }
  293. }
  294. if (post)
  295. {
  296. //判断本地有没有token,没有显示登录,有再判断登录状态
  297. if (Settings.Default.AppProperties.LoginToken != "")
  298. {
  299. string code = ServiceHelper.GetUser();
  300. if (code == "330"|| code == "304")
  301. {
  302. App.mainWindowViewModel.UserVis = Visibility.Collapsed;
  303. App.mainWindowViewModel.LoginVis = Visibility.Visible;
  304. App.mainWindowViewModel.RegisterVis = Visibility.Collapsed;
  305. App.mainWindowViewModel.OphVis = Visibility.Visible;
  306. }
  307. }
  308. else
  309. {
  310. App.mainWindowViewModel.UserVis = Visibility.Collapsed;
  311. App.mainWindowViewModel.LoginVis = Visibility.Visible;
  312. App.mainWindowViewModel.RegisterVis = Visibility.Collapsed;
  313. App.mainWindowViewModel.OphVis = Visibility.Visible;
  314. }
  315. }
  316. (App.mainWindowViewModel.SelectedItem.DataContext as MainContentViewModel).IsUsedVIP = true;
  317. return true;
  318. }
  319. //超时同步为20秒
  320. /// <summary>
  321. /// 用户信息获取
  322. /// </summary>
  323. /// <returns></returns>
  324. public static String GetUser()
  325. {
  326. //判断是否有网络
  327. if (!IsConnectInternet())
  328. {
  329. return "300";
  330. }
  331. HttpWebResponse response = null;
  332. ServicePointManager.DefaultConnectionLimit = 200;
  333. HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(RequestHost + GetUserUrl + "");
  334. request.Method = "Get";
  335. request.Headers.Add("Authorization", $"Bearer {Settings.Default.AppProperties.LoginToken}");
  336. request.ContentType = "text/html;charset=UTF-8";
  337. //request.Accept = "application/vnd.api+json;version=1";
  338. request.UserAgent = "Apifox/1.0.0 (https://www.apifox.cn)";
  339. request.Timeout = 20000;
  340. request.ServicePoint.Expect100Continue = false;
  341. try
  342. {
  343. response = (HttpWebResponse)request.GetResponse();
  344. using (StreamReader reader = new StreamReader(response.GetResponseStream()))
  345. {
  346. string responseData = reader.ReadToEnd();
  347. Console.WriteLine(responseData);
  348. reader.Close();
  349. JObject jobject = (JObject)JsonConvert.DeserializeObject(responseData);
  350. if (response != null)
  351. {
  352. response.Close();
  353. }
  354. if (request != null)
  355. {
  356. request.Abort();
  357. };
  358. JToken jToken;
  359. if (jobject.TryGetValue("msg", out jToken) == false)
  360. {
  361. //ListenerSubscription();
  362. App.IsLogin = true;
  363. Settings.Default.UserDate.Email= jobject["email"].ToObject<string>().ToLower();
  364. Settings.Default.UserDate.id= jobject["id"].ToObject<string>().ToLower();
  365. JArray subscriptionInfoList = (JArray)jobject["subscriptionInfoList"];
  366. foreach (JToken info in subscriptionInfoList)
  367. {
  368. if ((int)info["platform"] == 2)
  369. {
  370. Settings.Default.UserDate.subscribeid = (string)info["id"];
  371. Settings.Default.UserDate.subscribeuserid = (string)info["userId"];
  372. Settings.Default.UserDate.subscribeplatform = (int)info["platform"];
  373. Settings.Default.UserDate.subscribestatus = (int)info["status"];
  374. if (Settings.Default.UserDate.subscribestatus > 0)
  375. {
  376. Settings.Default.UserDate.subscribeendDate = (string)info["endDate"];
  377. Settings.Default.UserDate.subscribepayType = (int)info["payType"];
  378. }
  379. }
  380. }
  381. //此处为主页UPgread按钮的Visible
  382. if (Settings.Default.UserDate.subscribestatus == 1)
  383. {
  384. for (int i = 0; i < App.Current.Windows.Count; i++)
  385. {
  386. MainWindow win = App.Current.Windows[i] as MainWindow;
  387. if (win != null&& win.DataContext!=null)
  388. {
  389. (win.DataContext as MainWindowViewModel).UpgradeVis = Visibility.Collapsed;
  390. (win.DataContext as MainWindowViewModel).Useremailchar = Settings.Default.UserDate.Email.Substring(0, 1);
  391. }
  392. }
  393. }
  394. else
  395. {
  396. for (int i = 0; i < App.Current.Windows.Count; i++)
  397. {
  398. MainWindow win = App.Current.Windows[i] as MainWindow;
  399. if (win != null&& win.DataContext!=null)
  400. {
  401. (win.DataContext as MainWindowViewModel).UpgradeVis = Visibility.Visible;
  402. (win.DataContext as MainWindowViewModel).Useremailchar = Settings.Default.UserDate.Email.Substring(0, 1);
  403. }
  404. }
  405. }
  406. #region 试用策略
  407. //Settings.Default.UserDate.isInFreeUseTime= jobject["isInFreeUseTime"].ToObject<bool>();
  408. //Settings.Default.UserDate.freeDate= jobject["freeDate"].ToObject<string>().ToLower();
  409. //if(DateTime.Parse(Settings.Default.UserDate.freeDate)<=DateTime.Now)
  410. //{
  411. // Settings.Default.UserDate.isInFreeUseTime = false;
  412. //}
  413. #endregion
  414. App.mainWindowViewModel.Useremailchar = Settings.Default.UserDate.Email.Substring(0, 1);
  415. Settings.Default.Save();
  416. return "ture";
  417. }
  418. else
  419. {
  420. return "false";
  421. }
  422. }
  423. }
  424. catch (System.Net.WebException e)
  425. {
  426. int statusCode = 0;
  427. if (e.Response is HttpWebResponse)
  428. {
  429. //从网络异常信息里解析错误,后面的逻辑根据错误码判断会更准确
  430. statusCode = (int)((HttpWebResponse)e.Response).StatusCode;
  431. }
  432. //试用策略注释
  433. //if (DateTime.Parse(Settings.Default.UserDate.freeDate) <= DateTime.Now)
  434. //{
  435. // Settings.Default.UserDate.isInFreeUseTime = false;
  436. //}
  437. Console.WriteLine(e.Message);
  438. //无权限
  439. if (Settings.Default.AppProperties.LoginToken == "")
  440. return "No Token";
  441. else if(statusCode == 401)
  442. {
  443. if(CheckDeviceStatusout()=="330")
  444. {
  445. App.IsLogin = false;
  446. return "330";
  447. }
  448. else
  449. {
  450. return "304";
  451. }
  452. }
  453. else return "300";
  454. }
  455. catch
  456. {
  457. //试用策略注释
  458. //if (DateTime.Parse(Settings.Default.UserDate.freeDate) <= DateTime.Now)
  459. //{
  460. // Settings.Default.UserDate.isInFreeUseTime = false;
  461. //}
  462. return "300";
  463. }
  464. }
  465. /// <summary>
  466. /// 验证email格式
  467. /// </summary>
  468. /// <param name="intemail"></param>
  469. /// <param name="validType"></param>
  470. /// <returns></returns>
  471. public static String Ok_email(string intemail, string validType)
  472. {
  473. HttpWebResponse response = null;
  474. ServicePointManager.DefaultConnectionLimit = 200;
  475. HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(RequestHost + OKemailUrl);
  476. request.Method = "Post";
  477. request.ContentType = "application/json";
  478. //request.Accept = "application/vnd.api+json;version=1";
  479. request.UserAgent = "Apifox/1.0.0 (https://www.apifox.cn)";
  480. request.Timeout = 20000;
  481. request.ServicePoint.Expect100Continue = false;
  482. StringWriter sw = new StringWriter();
  483. using (JsonWriter writer = new JsonTextWriter(sw))
  484. {
  485. writer.WriteStartObject();
  486. writer.WritePropertyName("appId");
  487. writer.WriteValue(16);
  488. writer.WritePropertyName("platformType");
  489. writer.WriteValue(0);
  490. writer.WritePropertyName("username");
  491. writer.WriteValue(intemail);
  492. writer.WritePropertyName("validType");
  493. writer.WriteValue(validType);
  494. writer.WriteEndObject();
  495. }
  496. try
  497. {
  498. string postBody = sw.ToString();
  499. using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
  500. {
  501. writer.Write(postBody);
  502. writer.Close();
  503. }
  504. response = (HttpWebResponse)request.GetResponse();
  505. using (StreamReader reader = new StreamReader(response.GetResponseStream()))
  506. {
  507. string responseData = reader.ReadToEnd();
  508. Console.WriteLine(responseData);
  509. reader.Close();
  510. JObject jobject = (JObject)JsonConvert.DeserializeObject(responseData);
  511. //outemail = jobject["msg"].ToObject<string>().ToLower();
  512. if (response != null)
  513. {
  514. response.Close();
  515. }
  516. if (request != null)
  517. {
  518. request.Abort();
  519. }
  520. return jobject["code"].ToObject<string>().ToLower();
  521. }
  522. }
  523. catch (Exception ex)
  524. {
  525. Console.WriteLine(ex);
  526. return "300";
  527. }
  528. }
  529. /// <summary>
  530. /// 获取验证码
  531. /// </summary>
  532. /// <param name="action"></param>
  533. /// <param name="email"></param>
  534. /// <returns></returns>
  535. public static string Get_code(string action, string email)
  536. {
  537. string post = $"?action={action}&appId=16&receiver={email}&type=0";
  538. HttpWebResponse response = null;
  539. ServicePointManager.DefaultConnectionLimit = 200;
  540. HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(RequestHost + GetcodeUrl + post);
  541. request.Method = "Get";
  542. request.ContentType = "text/html;charset=UTF-8";
  543. //request.Accept = "application/vnd.api+json;version=1";
  544. request.UserAgent = "Apifox/1.0.0 (https://www.apifox.cn)";
  545. request.Timeout = 20000;
  546. request.ServicePoint.Expect100Continue = false;
  547. try
  548. {
  549. response = (HttpWebResponse)request.GetResponse();
  550. using (StreamReader reader = new StreamReader(response.GetResponseStream()))
  551. {
  552. string responseData = reader.ReadToEnd();
  553. Console.WriteLine(responseData);
  554. reader.Close();
  555. JObject jobject = (JObject)JsonConvert.DeserializeObject(responseData);
  556. if (response != null)
  557. {
  558. response.Close();
  559. }
  560. if (request != null)
  561. {
  562. request.Abort();
  563. }
  564. return jobject["code"].ToObject<string>().ToLower();
  565. }
  566. }
  567. catch
  568. {
  569. return "300";
  570. }
  571. }
  572. /// <summary>
  573. /// 验证验证码是否正确
  574. /// </summary>
  575. /// <param name="email"></param>
  576. /// <param name="code"></param>
  577. /// <param name="type"></param>
  578. /// <returns></returns>
  579. public static String Ok_code(string email, string code, string type)
  580. {
  581. string post = $"?account={email}&code={code}&type={type}&appId=16";
  582. HttpWebResponse response = null;
  583. ServicePointManager.DefaultConnectionLimit = 200;
  584. HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(RequestHost + OKcodeUrl + post);
  585. request.Method = "Get";
  586. request.ContentType = "text/html;charset=UTF-8";
  587. //request.Accept = "application/vnd.api+json;version=1";
  588. request.UserAgent = "Apifox/1.0.0 (https://www.apifox.cn)";
  589. request.Timeout = 20000;
  590. request.ServicePoint.Expect100Continue = false;
  591. try
  592. {
  593. response = (HttpWebResponse)request.GetResponse();
  594. using (StreamReader reader = new StreamReader(response.GetResponseStream()))
  595. {
  596. string responseData = reader.ReadToEnd();
  597. Console.WriteLine(responseData);
  598. reader.Close();
  599. JObject jobject = (JObject)JsonConvert.DeserializeObject(responseData);
  600. if (response != null)
  601. {
  602. response.Close();
  603. }
  604. if (request != null)
  605. {
  606. request.Abort();
  607. }
  608. return jobject["code"].ToObject<string>().ToLower();
  609. }
  610. }
  611. catch
  612. {
  613. return "300";
  614. }
  615. }
  616. /// <summary>
  617. /// 注册账号
  618. /// </summary>
  619. /// <param name="intemail"></param>
  620. /// <param name="intpassword"></param>
  621. /// <param name="intcode"></param>
  622. /// <param name="uuid"></param>
  623. /// <returns></returns>
  624. public static String Register_email(string intemail, string intpassword, string intcode, string uuid)
  625. {
  626. HttpWebResponse response = null;
  627. ServicePointManager.DefaultConnectionLimit = 200;
  628. HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(RequestHost + registerUrl);
  629. request.Method = "Post";
  630. request.ContentType = "application/json";
  631. //request.Accept = "application/vnd.api+json;version=1";
  632. request.UserAgent = "Apifox/1.0.0 (https://www.apifox.cn)";
  633. request.Timeout = 20000;
  634. request.ServicePoint.Expect100Continue = false;
  635. StringWriter sw = new StringWriter();
  636. using (JsonWriter writer = new JsonTextWriter(sw))
  637. {
  638. writer.WriteStartObject();
  639. writer.WritePropertyName("appId");
  640. writer.WriteValue(16);
  641. writer.WritePropertyName("deviceSign");
  642. writer.WriteValue(uuid);
  643. writer.WritePropertyName("password");
  644. writer.WriteValue(intpassword);
  645. writer.WritePropertyName("platformType");
  646. writer.WriteValue(0);
  647. writer.WritePropertyName("username");
  648. writer.WriteValue(intemail);
  649. writer.WritePropertyName("verifyCode");
  650. writer.WriteValue(intcode);
  651. writer.WritePropertyName("model");
  652. writer.WriteValue("windows");
  653. writer.WriteEndObject();
  654. }
  655. try
  656. {
  657. string postBody = sw.ToString();
  658. using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
  659. {
  660. writer.Write(postBody);
  661. writer.Close();
  662. }
  663. response = (HttpWebResponse)request.GetResponse();
  664. using (StreamReader reader = new StreamReader(response.GetResponseStream()))
  665. {
  666. string responseData = reader.ReadToEnd();
  667. Console.WriteLine(responseData);
  668. reader.Close();
  669. JObject jobject = (JObject)JsonConvert.DeserializeObject(responseData);
  670. if (response != null)
  671. {
  672. response.Close();
  673. }
  674. if (request != null)
  675. {
  676. request.Abort();
  677. }
  678. if (jobject["code"].ToObject<string>().ToLower() == "200")
  679. {
  680. App.IsLogin =true;
  681. for (int i = 0; i < App.Current.Windows.Count; i++)
  682. {
  683. MainWindow win = App.Current.Windows[i] as MainWindow;
  684. if (win != null && win.DataContext != null)
  685. {
  686. (win.DataContext as MainWindowViewModel).UserVis = Visibility.Visible;
  687. (win.DataContext as MainWindowViewModel).LoginVis = Visibility.Collapsed;
  688. (win.DataContext as MainWindowViewModel).RegisterVis = Visibility.Collapsed;
  689. }
  690. }
  691. Settings.Default.UserDate.Email = intemail;
  692. Settings.Default.UserDate.IsLoginoff = false;
  693. access_token = jobject["result"]["access_token"].ToObject<string>().ToLower();
  694. Settings.Default.AppProperties.LoginToken = jobject["result"]["access_token"].ToObject<string>().ToLower();
  695. Settings.Default.Save();
  696. GetUser();
  697. }
  698. //return jobject["code"].ToObject<string>().ToLower();
  699. return "200";
  700. }
  701. }
  702. catch
  703. {
  704. return "300";
  705. }
  706. }
  707. /// <summary>
  708. /// 登录
  709. /// </summary>
  710. /// <param name="intemail"></param>
  711. /// <param name="intpassword"></param>
  712. /// <param name="uuid"></param>
  713. /// <returns></returns>
  714. public static String Login(string intemail, string intpassword, string uuid)
  715. {
  716. string postBody = $"?appId=16&deviceSign={uuid}&email={intemail}&password={intpassword}&platformType=0&model=windows";
  717. HttpWebResponse response = null;
  718. ServicePointManager.DefaultConnectionLimit = 200;
  719. HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(RequestHost + LoginUrl + postBody);
  720. request.Method = "Post";
  721. request.ContentType = "application/x-www-form-urlencoded";
  722. //request.Accept = "application/vnd.api+json;version=1";
  723. request.UserAgent = "Apifox/1.0.0 (https://www.apifox.cn)";
  724. request.Timeout = 20000;
  725. request.ServicePoint.Expect100Continue = false;
  726. try
  727. {
  728. response = (HttpWebResponse)request.GetResponse();
  729. using (StreamReader reader = new StreamReader(response.GetResponseStream()))
  730. {
  731. string responseData = reader.ReadToEnd();
  732. Console.WriteLine(responseData);
  733. reader.Close();
  734. JObject jobject = (JObject)JsonConvert.DeserializeObject(responseData);
  735. if (response != null)
  736. {
  737. response.Close();
  738. }
  739. if (request != null)
  740. {
  741. request.Abort();
  742. }
  743. if (jobject["code"].ToObject<string>().ToLower() == "200")
  744. {
  745. //ListenerSubscription();
  746. for (int i = 0; i < App.Current.Windows.Count; i++)
  747. {
  748. MainWindow win = App.Current.Windows[i] as MainWindow;
  749. if (win != null && win.DataContext != null)
  750. {
  751. (win.DataContext as MainWindowViewModel).UserVis = Visibility.Visible;
  752. (win.DataContext as MainWindowViewModel).LoginVis = Visibility.Collapsed;
  753. (win.DataContext as MainWindowViewModel).RegisterVis = Visibility.Collapsed;
  754. }
  755. }
  756. App.IsLogin = true;
  757. Settings.Default.UserDate.Email = intemail;
  758. Settings.Default.UserDate.IsLoginoff = false;
  759. access_token = jobject["result"]["access_token"].ToObject<string>().ToLower();
  760. Settings.Default.AppProperties.LoginToken = jobject["result"]["access_token"].ToObject<string>().ToLower();
  761. Settings.Default.Save();
  762. GetUser();
  763. }
  764. return jobject["code"].ToObject<string>().ToLower();
  765. }
  766. }
  767. catch
  768. {
  769. return "300";
  770. }
  771. }
  772. /// <summary>
  773. /// 登出
  774. /// </summary>
  775. /// <param name="uuid"></param>
  776. public static void Logout(string uuid)
  777. {
  778. string postBody = $"?deviceSign={uuid}&appId=16";
  779. HttpWebResponse response = null;
  780. ServicePointManager.DefaultConnectionLimit = 200;
  781. HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(RequestHost + LogoutUrl + postBody);
  782. request.Method = "Post";
  783. request.Headers.Add("Authorization", $"Bearer {Settings.Default.AppProperties.LoginToken}");
  784. request.ContentType = "application/x-www-form-urlencoded";
  785. //request.Accept = "application/vnd.api+json;version=1";
  786. request.UserAgent = "Apifox/1.0.0 (https://www.apifox.cn)";
  787. request.Timeout = 20000;
  788. request.ServicePoint.Expect100Continue = false;
  789. try
  790. {
  791. response = (HttpWebResponse)request.GetResponse();
  792. using (StreamReader reader = new StreamReader(response.GetResponseStream()))
  793. {
  794. string responseData = reader.ReadToEnd();
  795. Console.WriteLine(responseData);
  796. reader.Close();
  797. JObject jobject = (JObject)JsonConvert.DeserializeObject(responseData);
  798. App.IsLogin = false;
  799. Settings.Default.UserDate.subscribestatus = -1;
  800. Settings.Default.UserDate.subscribepayType = 0;
  801. App.mainWindowViewModel.UpgradeVis = Visibility.Visible;
  802. for (int i = 0; i < App.Current.Windows.Count; i++)
  803. {
  804. MainWindow win = App.Current.Windows[i] as MainWindow;
  805. if (win != null && win.DataContext != null)
  806. {
  807. (win.DataContext as MainWindowViewModel).UserVis = Visibility.Collapsed;
  808. (win.DataContext as MainWindowViewModel).LoginVis = Visibility.Visible;
  809. (win.DataContext as MainWindowViewModel).RegisterVis = Visibility.Collapsed;
  810. (win.DataContext as MainWindowViewModel).UpgradeVis = Visibility.Visible;
  811. }
  812. }
  813. if (response != null)
  814. {
  815. response.Close();
  816. }
  817. if (request != null)
  818. {
  819. request.Abort();
  820. }
  821. }
  822. }
  823. catch
  824. {
  825. }
  826. }
  827. /// <summary>
  828. /// 注销
  829. /// </summary>
  830. /// <param name="code"></param>
  831. /// <returns></returns>
  832. public static String Usergout(string code)
  833. {
  834. string postBody = $"?code={code}&appId=16";
  835. HttpWebResponse response = null;
  836. ServicePointManager.DefaultConnectionLimit = 200;
  837. HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(RequestHost + UseroutUrl + postBody);
  838. request.Method = "Post";
  839. request.Headers.Add("Authorization", $"Bearer {Settings.Default.AppProperties.LoginToken}");
  840. request.ContentType = "application/x-www-form-urlencoded";
  841. //request.Accept = "application/vnd.api+json;version=1";
  842. request.UserAgent = "Apifox/1.0.0 (https://www.apifox.cn)";
  843. request.Timeout = 20000;
  844. request.ServicePoint.Expect100Continue = false;
  845. try
  846. {
  847. response = (HttpWebResponse)request.GetResponse();
  848. using (StreamReader reader = new StreamReader(response.GetResponseStream()))
  849. {
  850. string responseData = reader.ReadToEnd();
  851. Console.WriteLine(responseData);
  852. reader.Close();
  853. JObject jobject = (JObject)JsonConvert.DeserializeObject(responseData);
  854. App.IsLogin = false;
  855. Settings.Default.UserDate.subscribestatus = -1;
  856. Settings.Default.UserDate.subscribepayType = 0;
  857. for (int i = 0; i < App.Current.Windows.Count; i++)
  858. {
  859. MainWindow win = App.Current.Windows[i] as MainWindow;
  860. if (win != null && win.DataContext != null)
  861. {
  862. (win.DataContext as MainWindowViewModel).UserVis = Visibility.Collapsed;
  863. (win.DataContext as MainWindowViewModel).LoginVis = Visibility.Visible;
  864. (win.DataContext as MainWindowViewModel).RegisterVis = Visibility.Collapsed;
  865. (win.DataContext as MainWindowViewModel).UpgradeVis = Visibility.Visible;
  866. }
  867. }
  868. App.mainWindowViewModel.UpgradeVis = Visibility.Visible;
  869. if (response != null)
  870. {
  871. response.Close();
  872. }
  873. if (request != null)
  874. {
  875. request.Abort();
  876. }
  877. return jobject["code"].ToObject<string>().ToLower();
  878. }
  879. }
  880. catch
  881. {
  882. return "300";
  883. }
  884. }
  885. /// <summary>
  886. /// 重置密码
  887. /// </summary>
  888. /// <param name="intemail"></param>
  889. /// <param name="firstpassword"></param>
  890. /// <param name="secondPassword"></param>
  891. /// <param name="intcode"></param>
  892. /// <returns></returns>
  893. public static String Rebirth(string intemail, string firstpassword, string secondPassword, string intcode)
  894. {
  895. HttpWebResponse response = null;
  896. ServicePointManager.DefaultConnectionLimit = 200;
  897. HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(RequestHost + RebirthUrl);
  898. request.Method = "Post";
  899. request.ContentType = "application/json";
  900. //request.Accept = "application/vnd.api+json;version=1";
  901. request.UserAgent = "Apifox/1.0.0 (https://www.apifox.cn)";
  902. request.Timeout = 20000;
  903. request.ServicePoint.Expect100Continue = false;
  904. StringWriter sw = new StringWriter();
  905. using (JsonWriter writer = new JsonTextWriter(sw))
  906. {
  907. writer.WriteStartObject();
  908. writer.WritePropertyName("account");
  909. writer.WriteValue(intemail);
  910. writer.WritePropertyName("appId");
  911. writer.WriteValue(16);
  912. writer.WritePropertyName("firstPassword");
  913. writer.WriteValue(firstpassword);
  914. writer.WritePropertyName("platformType");
  915. writer.WriteValue(0);
  916. writer.WritePropertyName("secondPassword");
  917. writer.WriteValue(secondPassword);
  918. writer.WritePropertyName("verifyCode");
  919. writer.WriteValue(intcode);
  920. writer.WriteEndObject();
  921. }
  922. try
  923. {
  924. string postBody = sw.ToString();
  925. using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
  926. {
  927. writer.Write(postBody);
  928. writer.Close();
  929. }
  930. response = (HttpWebResponse)request.GetResponse();
  931. using (StreamReader reader = new StreamReader(response.GetResponseStream()))
  932. {
  933. string responseData = reader.ReadToEnd();
  934. Console.WriteLine(responseData);
  935. reader.Close();
  936. JObject jobject = (JObject)JsonConvert.DeserializeObject(responseData);
  937. if (response != null)
  938. {
  939. response.Close();
  940. }
  941. if (request != null)
  942. {
  943. request.Abort();
  944. }
  945. for (int i = 0; i < App.Current.Windows.Count; i++)
  946. {
  947. MainWindow win = App.Current.Windows[i] as MainWindow;
  948. if (win != null)
  949. {
  950. (win.DataContext as MainWindowViewModel).UserVis = Visibility.Collapsed;
  951. (win.DataContext as MainWindowViewModel).LoginVis = Visibility.Visible;
  952. (win.DataContext as MainWindowViewModel).RegisterVis = Visibility.Collapsed;
  953. }
  954. }
  955. return jobject["code"].ToObject<string>().ToLower();
  956. }
  957. }
  958. catch
  959. {
  960. return "300";
  961. }
  962. }
  963. /// <summary>
  964. /// 设备是否被自动登出
  965. /// </summary>
  966. public static String CheckDeviceStatusout()
  967. {
  968. string postBody = $"?appId=16&deviceSign={GetDeviceSerialNumber()}";
  969. HttpWebResponse response = null;
  970. ServicePointManager.DefaultConnectionLimit = 200;
  971. HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(RequestHost + CheckDeviceStatusUrl + postBody);
  972. request.Method = "Post";
  973. request.ContentType = "application/x-www-form-urlencoded";
  974. //request.Accept = "application/vnd.api+json;version=1";
  975. request.UserAgent = "Apifox/1.0.0 (https://www.apifox.cn)";
  976. request.Timeout = 20000;
  977. request.ServicePoint.Expect100Continue = false;
  978. try
  979. {
  980. response = (HttpWebResponse)request.GetResponse();
  981. using (StreamReader reader = new StreamReader(response.GetResponseStream()))
  982. {
  983. string responseData = reader.ReadToEnd();
  984. Console.WriteLine(responseData);
  985. reader.Close();
  986. JObject jobject = (JObject)JsonConvert.DeserializeObject(responseData);
  987. App.IsLogin = false;
  988. Settings.Default.UserDate.subscribestatus = -1;
  989. Settings.Default.UserDate.subscribepayType = 0;
  990. App.mainWindowViewModel.UpgradeVis = Visibility.Visible;
  991. if(App.Current.Windows!=null&&App.Current.Windows.Count!=0)
  992. {
  993. for (int i = 0; i < App.Current.Windows.Count; i++)
  994. {
  995. MainWindow win = App.Current.Windows[i] as MainWindow;
  996. if (win != null&& win.DataContext!=null)
  997. {
  998. (win.DataContext as MainWindowViewModel).UserVis = Visibility.Collapsed;
  999. (win.DataContext as MainWindowViewModel).LoginVis = Visibility.Visible;
  1000. (win.DataContext as MainWindowViewModel).RegisterVis = Visibility.Collapsed;
  1001. (win.DataContext as MainWindowViewModel).UpgradeVis = Visibility.Visible;
  1002. }
  1003. }
  1004. }
  1005. if (response != null)
  1006. {
  1007. response.Close();
  1008. }
  1009. if (request != null)
  1010. {
  1011. request.Abort();
  1012. }
  1013. return jobject["code"].ToObject<string>().ToLower();
  1014. }
  1015. }
  1016. catch
  1017. {
  1018. return "300";
  1019. }
  1020. }
  1021. /// <summary>
  1022. ///上传设备启动信息到服务端
  1023. /// </summary>
  1024. public static void CreateDeviceLog()
  1025. {
  1026. string url = RequestHost + DeviceLogUrl;
  1027. try
  1028. {
  1029. HttpWebResponse response = null;
  1030. ServicePointManager.DefaultConnectionLimit = 200;
  1031. HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(RequestHost + DeviceLogUrl);
  1032. request.Method = "Post";
  1033. request.ContentType = "application/json";
  1034. request.UserAgent = "Apifox/1.0.0 (https://www.apifox.cn)";
  1035. request.Timeout = 20000;
  1036. request.ServicePoint.Expect100Continue = false;
  1037. StringWriter sw = new StringWriter();
  1038. using (JsonWriter writer = new JsonTextWriter(sw))
  1039. {
  1040. writer.WriteStartObject();
  1041. writer.WritePropertyName("appVersion");
  1042. writer.WriteValue(App.Version);
  1043. //产品ID Mac21 Windows 22
  1044. writer.WritePropertyName("productId");
  1045. writer.WriteValue(22);
  1046. writer.WritePropertyName("os");
  1047. writer.WriteValue(GetSystemVersionNumber());
  1048. writer.WritePropertyName("timeZone");
  1049. writer.WriteValue(TimeZoneInfo.Local.DisplayName);
  1050. writer.WritePropertyName("language");
  1051. writer.WriteValue(CultureInfo.CurrentCulture.Name);
  1052. //平台 Mac 0 Windows 1
  1053. writer.WritePropertyName("platform");
  1054. writer.WriteValue(1);
  1055. writer.WritePropertyName("createdAt");
  1056. writer.WriteValue(DateTime.Now.ToFileTime());
  1057. writer.WritePropertyName("appId");
  1058. writer.WriteValue(16);
  1059. writer.WritePropertyName("model");
  1060. writer.WriteValue("Windows");
  1061. writer.WritePropertyName("uniqueSn");
  1062. writer.WriteValue(GetDeviceSerialNumber());
  1063. writer.WritePropertyName("updatedAt");
  1064. writer.WriteValue(DateTime.Now.ToFileTime());
  1065. writer.WriteEndObject();
  1066. }
  1067. string postBody = sw.ToString();
  1068. using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
  1069. {
  1070. writer.Write(postBody);
  1071. writer.Close();
  1072. }
  1073. response = (HttpWebResponse)request.GetResponse();
  1074. using (StreamReader reader = new StreamReader(response.GetResponseStream()))
  1075. {
  1076. string responseData = reader.ReadToEnd();
  1077. Console.WriteLine(responseData);
  1078. reader.Close();
  1079. JObject jobject = (JObject)JsonConvert.DeserializeObject(responseData);
  1080. if (response != null)
  1081. {
  1082. response.Close();
  1083. }
  1084. if (request != null)
  1085. {
  1086. request.Abort();
  1087. }
  1088. if (jobject["code"].ToObject<string>().ToLower() == "200")
  1089. {
  1090. Console.WriteLine("软件启动信息上传成功");
  1091. }
  1092. }
  1093. }
  1094. catch { }
  1095. }
  1096. //监听代码,暂时用不到,注释
  1097. //监听订阅行为
  1098. //public static HttpListener httpListener;
  1099. //public static bool isListening = false;
  1100. //public async static void ServerBoot()
  1101. //{
  1102. // if (!isListening)
  1103. // {
  1104. // httpListener = new HttpListener();
  1105. // httpListener.Prefixes.Add("http://127.0.0.1:5544/");
  1106. // // 启动监听器
  1107. // httpListener.Start();
  1108. // Console.WriteLine($"开始喽");
  1109. // isListening = true;
  1110. // // 在后台线程中等待请求并处理
  1111. // await Task.Run(() => ListenForRequests());
  1112. // }
  1113. //}
  1114. //private static async Task ListenForRequests()
  1115. //{
  1116. // WakeId = Win32Helper.RegisterWindowMessage("msgWake_PDFMaster");
  1117. // Console.WriteLine("等待请求...");
  1118. // // 持续监听请求
  1119. // while (true)
  1120. // {
  1121. // // 等待接收请求
  1122. // HttpListenerContext context = await httpListener.GetContextAsync();
  1123. // // 允许跨域请求
  1124. // context.Response.Headers.Add("Access-Control-Allow-Origin", "*");
  1125. // context.Response.Headers.Add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
  1126. // context.Response.Headers.Add("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  1127. // // 处理请求
  1128. // if (context.Request.HttpMethod == "GET")
  1129. // {
  1130. // //返回 true 并结束监听
  1131. // if (App.IsLogin == true)
  1132. // {
  1133. // Win32Helper.PostMessage((IntPtr)0xffff, WakeId, IntPtr.Zero, IntPtr.Zero);
  1134. // IntPtr waitPtr = Win32Helper.CreateEvent(IntPtr.Zero, true, false, "Global\\shareWaitNotify" + Guid.NewGuid());
  1135. // Win32Helper.WaitForSingleObject(waitPtr, 5000);
  1136. // Win32Helper.CloseHandle(waitPtr);
  1137. // }
  1138. // byte[] responseBytes = System.Text.Encoding.UTF8.GetBytes("true");
  1139. // context.Response.StatusCode = (int)HttpStatusCode.OK;
  1140. // context.Response.ContentLength64 = responseBytes.Length;
  1141. // context.Response.OutputStream.Write(responseBytes, 0, responseBytes.Length);
  1142. // context.Response.OutputStream.Close();
  1143. // httpListener.Stop();
  1144. // isListening = false;
  1145. // break;
  1146. // }
  1147. // }
  1148. //}
  1149. }
  1150. }