ServiceHelper.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  1. using Newtonsoft.Json;
  2. using Newtonsoft.Json.Linq;
  3. using PDF_Master.Properties;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Management;
  9. using System.Net;
  10. using System.Security.Cryptography;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. namespace PDF_Master.Helper
  14. {
  15. /// <summary>
  16. /// 注册登陆相关的工具类
  17. /// </summary>
  18. ///
  19. public static class ServiceHelper
  20. {
  21. public static bool IsLogin=false;
  22. public static string ALLPassword = "";
  23. public static string access_token = "";
  24. public static string outemail = "";
  25. public static string outcode = "";
  26. public static string outregister = "";
  27. public static string outlogin = "";
  28. public static string code = "";
  29. public static string okcode = "";
  30. public static string remsg = "";
  31. public static string state = "";
  32. public static string AppCode = "com.brother.pdfreaderprofree.windows";
  33. public static string RequestHost = "http://139.196.160.101:8081";
  34. //验证邮箱
  35. public static string OKemailUrl = "/pdf-office-sso/auth/validUser";
  36. //验证验证码
  37. public static string OKcodeUrl = "/pdf-office-sso/auth/isEmailCodeValid";
  38. //发送验证码
  39. public static string GetcodeUrl = "/pdf-office-sso/auth/getVerifyCode";
  40. //注册邮箱
  41. public static string registerUrl = "/pdf-office-sso/auth/emailRegister";
  42. //登录
  43. public static string LoginUrl = "/pdf-office-sso/auth/emailLogin";
  44. //登出
  45. public static string LogoutUrl = "/pdf-office-sso/user/logout";
  46. //注销
  47. public static string UseroutUrl = "/pdf-office-sso/user/logOffForUser";
  48. //重置密码
  49. public static string RebirthUrl = "/pdf-office-sso/auth/resetPassword";
  50. //获取用户信息
  51. public static string GetUserUrl = "/pdf-office-sso/user/me";
  52. private static string computerInfo;
  53. /// <summary>
  54. /// 电脑品牌信息
  55. /// </summary>
  56. public static string ComputerInfo
  57. {
  58. get {
  59. if (computerInfo == null)
  60. {
  61. computerInfo = GetComputerInfo();
  62. }
  63. return computerInfo;
  64. }
  65. set { computerInfo = value; }
  66. }
  67. private static string systemInfo;
  68. /// <summary>
  69. /// 系统信息
  70. /// </summary>
  71. public static string SystemInfo
  72. {
  73. get
  74. {
  75. if (systemInfo == null)
  76. {
  77. systemInfo = GetSystemInfo();
  78. }
  79. return systemInfo;
  80. }
  81. set { systemInfo = value; }
  82. }
  83. private static string GetSystemInfo()
  84. {
  85. try
  86. {
  87. string s = "";
  88. SelectQuery sq = new SelectQuery("Win32_OperatingSystem");
  89. ManagementObjectSearcher mos = new ManagementObjectSearcher(sq);
  90. ManagementObjectCollection moc = mos.Get();
  91. foreach (ManagementObject mo in moc)
  92. {
  93. s = mo.Properties["Version"].Value.ToString();
  94. }
  95. moc.Dispose();
  96. sq = null;
  97. return s;
  98. }
  99. catch { return ""; }
  100. }
  101. private static string GetComputerInfo()
  102. {
  103. try
  104. {
  105. string s = "";
  106. System.Windows.Controls.TextBox text = new System.Windows.Controls.TextBox();
  107. SelectQuery sq = new SelectQuery("Win32_ComputerSystem");
  108. ManagementObjectSearcher mos = new ManagementObjectSearcher(sq);
  109. ManagementObjectCollection moc = mos.Get();
  110. foreach (ManagementObject mo in moc)
  111. {
  112. s = mo.Properties["Manufacturer"].Value + " " + mo.Properties["Model"].Value;
  113. }
  114. moc.Dispose();
  115. sq = null;
  116. return s;
  117. }
  118. catch { return ""; }
  119. }
  120. /// <summary>
  121. /// 操作系统生成的UUID,用于获取虚拟机用户的设备识别码
  122. /// </summary>
  123. /// <returns></returns>
  124. public static string GetUUID()
  125. {
  126. string systemId = null;
  127. using (ManagementObjectSearcher mos = new ManagementObjectSearcher("select * from Win32_ComputerSystemProduct"))
  128. {
  129. foreach (var item in mos.Get())
  130. {
  131. systemId = item["UUID"].ToString();
  132. }
  133. }
  134. return systemId;
  135. }
  136. /// <summary>
  137. /// 主板编号
  138. /// </summary>
  139. /// <returns></returns>
  140. public static string GetBoardId()
  141. {
  142. var st = string.Empty;
  143. var mos = new ManagementObjectSearcher("Select * from Win32_BaseBoard");
  144. foreach (var o in mos.Get())
  145. {
  146. var mo = (ManagementObject)o;
  147. st = mo["SerialNumber"].ToString();
  148. }
  149. return st;
  150. }
  151. /// <summary>
  152. /// 获取设备唯一ID
  153. /// </summary>
  154. /// <returns></returns>
  155. public static string GetDeviceSerialNumber()
  156. {
  157. StringBuilder DeviceBuilder = new StringBuilder();
  158. bool IsVirtual = false;
  159. string boardId = GetBoardId();
  160. string uuid = GetUUID();
  161. //判断是否是虚拟机用户
  162. if (boardId.ToLower() == "" || boardId.ToLower() == "none")
  163. {
  164. if (string.IsNullOrEmpty(uuid) || uuid.ToLower() == "none")
  165. return string.Empty;
  166. else
  167. IsVirtual = true;
  168. }
  169. //真实机器用户 用主板id作为唯一识别码
  170. if (!IsVirtual)
  171. {
  172. DeviceBuilder.Append(boardId);
  173. }
  174. else
  175. {
  176. DeviceBuilder.Append(uuid);
  177. }
  178. MD5 md5 = MD5.Create();
  179. List<byte> charbytes = new List<byte>();
  180. foreach (char code in DeviceBuilder.ToString().ToArray())
  181. {
  182. charbytes.Add((byte)code);
  183. }
  184. byte[] md5Array = md5.ComputeHash(charbytes.ToArray());
  185. DeviceBuilder = new StringBuilder();
  186. foreach (var code in md5Array)
  187. {
  188. DeviceBuilder.Append(code.ToString("X2"));
  189. }
  190. return DeviceBuilder.ToString();
  191. }
  192. //超时同步为20秒
  193. public static String GetUser()
  194. {
  195. HttpWebResponse response = null;
  196. ServicePointManager.DefaultConnectionLimit = 200;
  197. HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(RequestHost + GetUserUrl+"");
  198. request.Method = "Get";
  199. request.Headers.Add("Authorization", $"Bearer {Settings.Default.AppProperties.LoginToken}");
  200. request.ContentType = "text/html;charset=UTF-8";
  201. //request.Accept = "application/vnd.api+json;version=1";
  202. request.UserAgent = "Apifox/1.0.0 (https://www.apifox.cn)";
  203. request.Timeout = 20000;
  204. request.ServicePoint.Expect100Continue = false;
  205. try
  206. {
  207. response = (HttpWebResponse)request.GetResponse();
  208. using (StreamReader reader = new StreamReader(response.GetResponseStream()))
  209. {
  210. string responseData = reader.ReadToEnd();
  211. Console.WriteLine(responseData);
  212. reader.Close();
  213. JObject jobject = (JObject)JsonConvert.DeserializeObject(responseData);
  214. if (response != null)
  215. {
  216. response.Close();
  217. }
  218. if (request != null)
  219. {
  220. request.Abort();
  221. }
  222. JToken jToken;
  223. if(jobject.TryGetValue("msg" ,out jToken)==false)
  224. {
  225. App.IsLogin = true;
  226. Settings.Default.UserDate.Email= jobject["email"].ToObject<string>().ToLower();
  227. return "ture";
  228. }
  229. else
  230. {
  231. return "false";
  232. }
  233. }
  234. }
  235. catch
  236. {
  237. return "NOINT";
  238. }
  239. }
  240. public static String Ok_email(string intemail,string validType)
  241. {
  242. HttpWebResponse response = null;
  243. ServicePointManager.DefaultConnectionLimit = 200;
  244. HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(RequestHost + OKemailUrl);
  245. request.Method = "Post";
  246. request.ContentType = "application/json";
  247. //request.Accept = "application/vnd.api+json;version=1";
  248. request.UserAgent = "Apifox/1.0.0 (https://www.apifox.cn)";
  249. request.Timeout = 20000;
  250. request.ServicePoint.Expect100Continue = false;
  251. StringWriter sw = new StringWriter();
  252. using (JsonWriter writer = new JsonTextWriter(sw))
  253. {
  254. writer.WriteStartObject();
  255. writer.WritePropertyName("appId");
  256. writer.WriteValue(16);
  257. writer.WritePropertyName("platformType");
  258. writer.WriteValue(0);
  259. writer.WritePropertyName("username");
  260. writer.WriteValue(intemail);
  261. writer.WritePropertyName("validType");
  262. writer.WriteValue(validType);
  263. writer.WriteEndObject();
  264. }
  265. try
  266. {
  267. string postBody = sw.ToString();
  268. using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
  269. {
  270. writer.Write(postBody);
  271. writer.Close();
  272. }
  273. response = (HttpWebResponse)request.GetResponse();
  274. using (StreamReader reader = new StreamReader(response.GetResponseStream()))
  275. {
  276. string responseData = reader.ReadToEnd();
  277. Console.WriteLine(responseData);
  278. reader.Close();
  279. JObject jobject = (JObject)JsonConvert.DeserializeObject(responseData);
  280. //outemail = jobject["msg"].ToObject<string>().ToLower();
  281. if (response != null)
  282. {
  283. response.Close();
  284. }
  285. if (request != null)
  286. {
  287. request.Abort();
  288. }
  289. return jobject["msg"].ToObject<string>().ToLower();
  290. }
  291. }
  292. catch
  293. {
  294. return "网络不稳定请重试";
  295. }
  296. }
  297. //⑤报错
  298. public static void Get_code(string action, string email)
  299. {
  300. string post = $"?action={action}&appId=16&receiver={email}&type=0";
  301. HttpWebResponse response = null;
  302. ServicePointManager.DefaultConnectionLimit = 200;
  303. HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(RequestHost + GetcodeUrl + post);
  304. request.Method = "Get";
  305. request.ContentType = "text/html;charset=UTF-8";
  306. //request.Accept = "application/vnd.api+json;version=1";
  307. request.UserAgent = "Apifox/1.0.0 (https://www.apifox.cn)";
  308. request.Timeout = 20000;
  309. request.ServicePoint.Expect100Continue = false;
  310. try
  311. {
  312. response = (HttpWebResponse)request.GetResponse();
  313. using (StreamReader reader = new StreamReader(response.GetResponseStream()))
  314. {
  315. string responseData = reader.ReadToEnd();
  316. Console.WriteLine(responseData);
  317. reader.Close();
  318. JObject jobject = (JObject)JsonConvert.DeserializeObject(responseData);
  319. if (response != null)
  320. {
  321. response.Close();
  322. }
  323. if (request != null)
  324. {
  325. request.Abort();
  326. }
  327. }
  328. }
  329. catch
  330. {
  331. }
  332. }
  333. public static String Ok_code(string email, string code, string type)
  334. {
  335. string post = $"?account={email}&code={code}&type={type}&appId=16";
  336. HttpWebResponse response = null;
  337. ServicePointManager.DefaultConnectionLimit = 200;
  338. HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(RequestHost + OKcodeUrl + post);
  339. request.Method = "Get";
  340. request.ContentType = "text/html;charset=UTF-8";
  341. //request.Accept = "application/vnd.api+json;version=1";
  342. request.UserAgent = "Apifox/1.0.0 (https://www.apifox.cn)";
  343. request.Timeout = 20000;
  344. request.ServicePoint.Expect100Continue = false;
  345. try
  346. {
  347. response = (HttpWebResponse)request.GetResponse();
  348. using (StreamReader reader = new StreamReader(response.GetResponseStream()))
  349. {
  350. string responseData = reader.ReadToEnd();
  351. Console.WriteLine(responseData);
  352. reader.Close();
  353. JObject jobject = (JObject)JsonConvert.DeserializeObject(responseData);
  354. if (response != null)
  355. {
  356. response.Close();
  357. }
  358. if (request != null)
  359. {
  360. request.Abort();
  361. }
  362. return jobject["msg"].ToObject<string>().ToLower();
  363. }
  364. }
  365. catch
  366. {
  367. return "网络不稳定请重试";
  368. }
  369. }
  370. public static String Register_email(string intemail, string intpassword, string intcode, string uuid)
  371. {
  372. HttpWebResponse response = null;
  373. ServicePointManager.DefaultConnectionLimit = 200;
  374. HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(RequestHost + registerUrl);
  375. request.Method = "Post";
  376. request.ContentType = "application/json";
  377. //request.Accept = "application/vnd.api+json;version=1";
  378. request.UserAgent = "Apifox/1.0.0 (https://www.apifox.cn)";
  379. request.Timeout = 20000;
  380. request.ServicePoint.Expect100Continue = false;
  381. StringWriter sw = new StringWriter();
  382. using (JsonWriter writer = new JsonTextWriter(sw))
  383. {
  384. writer.WriteStartObject();
  385. writer.WritePropertyName("appId");
  386. writer.WriteValue(16);
  387. writer.WritePropertyName("deviceSign");
  388. writer.WriteValue(uuid);
  389. writer.WritePropertyName("password");
  390. writer.WriteValue(intpassword);
  391. writer.WritePropertyName("platformType");
  392. writer.WriteValue(0);
  393. writer.WritePropertyName("username");
  394. writer.WriteValue(intemail);
  395. writer.WritePropertyName("verifyCode");
  396. writer.WriteValue(intcode);
  397. writer.WriteEndObject();
  398. }
  399. try
  400. {
  401. string postBody = sw.ToString();
  402. using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
  403. {
  404. writer.Write(postBody);
  405. writer.Close();
  406. }
  407. response = (HttpWebResponse)request.GetResponse();
  408. using (StreamReader reader = new StreamReader(response.GetResponseStream()))
  409. {
  410. string responseData = reader.ReadToEnd();
  411. Console.WriteLine(responseData);
  412. reader.Close();
  413. JObject jobject = (JObject)JsonConvert.DeserializeObject(responseData);
  414. if (response != null)
  415. {
  416. response.Close();
  417. }
  418. if (request != null)
  419. {
  420. request.Abort();
  421. }
  422. if (jobject["msg"].ToObject<string>().ToLower() == "success")
  423. {
  424. App.IsLogin =true;
  425. access_token = jobject["result"]["access_token"].ToObject<string>().ToLower();
  426. Settings.Default.AppProperties.LoginToken = jobject["result"]["access_token"].ToObject<string>().ToLower();
  427. Settings.Default.Save();
  428. }
  429. return jobject["msg"].ToObject<string>().ToLower();
  430. }
  431. }
  432. catch
  433. {
  434. return "网络不稳定请重试";
  435. }
  436. }
  437. public static String Login(string intemail, string intpassword, string uuid)
  438. {
  439. string postBody = $"?appId=16&deviceSign={uuid}&email={intemail}&password={intpassword}&platformType=0";
  440. HttpWebResponse response = null;
  441. ServicePointManager.DefaultConnectionLimit = 200;
  442. HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(RequestHost + LoginUrl + postBody);
  443. request.Method = "Post";
  444. request.ContentType = "application/x-www-form-urlencoded";
  445. //request.Accept = "application/vnd.api+json;version=1";
  446. request.UserAgent = "Apifox/1.0.0 (https://www.apifox.cn)";
  447. request.Timeout = 20000;
  448. request.ServicePoint.Expect100Continue = false;
  449. try
  450. {
  451. response = (HttpWebResponse)request.GetResponse();
  452. using (StreamReader reader = new StreamReader(response.GetResponseStream()))
  453. {
  454. string responseData = reader.ReadToEnd();
  455. Console.WriteLine(responseData);
  456. reader.Close();
  457. JObject jobject = (JObject)JsonConvert.DeserializeObject(responseData);
  458. outlogin = jobject["msg"].ToObject<string>().ToLower();
  459. if (response != null)
  460. {
  461. response.Close();
  462. }
  463. if (request != null)
  464. {
  465. request.Abort();
  466. }
  467. if (jobject["msg"].ToObject<string>().ToLower() == "success")
  468. {
  469. App.IsLogin = true;
  470. access_token = jobject["result"]["access_token"].ToObject<string>().ToLower();
  471. Settings.Default.AppProperties.LoginToken = jobject["result"]["access_token"].ToObject<string>().ToLower();
  472. Settings.Default.Save();
  473. }
  474. return jobject["msg"].ToObject<string>().ToLower();
  475. }
  476. }
  477. catch
  478. {
  479. return "网络不稳定请重试";
  480. }
  481. }
  482. //⑤报错
  483. public static void Logout(string uuid)
  484. {
  485. string postBody = $"?deviceSign={uuid}&appId=16";
  486. HttpWebResponse response = null;
  487. ServicePointManager.DefaultConnectionLimit = 200;
  488. HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(RequestHost + LogoutUrl + postBody);
  489. request.Method = "Post";
  490. request.Headers.Add("Authorization", $"Bearer {Settings.Default.AppProperties.LoginToken}");
  491. request.ContentType = "application/x-www-form-urlencoded";
  492. //request.Accept = "application/vnd.api+json;version=1";
  493. request.UserAgent = "Apifox/1.0.0 (https://www.apifox.cn)";
  494. request.Timeout = 20000;
  495. request.ServicePoint.Expect100Continue = false;
  496. try
  497. {
  498. response = (HttpWebResponse)request.GetResponse();
  499. using (StreamReader reader = new StreamReader(response.GetResponseStream()))
  500. {
  501. string responseData = reader.ReadToEnd();
  502. Console.WriteLine(responseData);
  503. reader.Close();
  504. JObject jobject = (JObject)JsonConvert.DeserializeObject(responseData);
  505. App.IsLogin = false;
  506. if (response != null)
  507. {
  508. response.Close();
  509. }
  510. if (request != null)
  511. {
  512. request.Abort();
  513. }
  514. }
  515. }
  516. catch
  517. {
  518. }
  519. }
  520. public static String Usergout(string code)
  521. {
  522. string postBody = $"?code={code}";
  523. HttpWebResponse response = null;
  524. ServicePointManager.DefaultConnectionLimit = 200;
  525. HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(RequestHost + UseroutUrl + postBody);
  526. request.Method = "Post";
  527. request.Headers.Add("Authorization", $"Bearer {Settings.Default.AppProperties.LoginToken}");
  528. request.ContentType = "application/x-www-form-urlencoded";
  529. //request.Accept = "application/vnd.api+json;version=1";
  530. request.UserAgent = "Apifox/1.0.0 (https://www.apifox.cn)";
  531. request.Timeout = 20000;
  532. request.ServicePoint.Expect100Continue = false;
  533. try
  534. {
  535. response = (HttpWebResponse)request.GetResponse();
  536. using (StreamReader reader = new StreamReader(response.GetResponseStream()))
  537. {
  538. string responseData = reader.ReadToEnd();
  539. Console.WriteLine(responseData);
  540. reader.Close();
  541. JObject jobject = (JObject)JsonConvert.DeserializeObject(responseData);
  542. if (response != null)
  543. {
  544. response.Close();
  545. }
  546. if (request != null)
  547. {
  548. request.Abort();
  549. }
  550. return jobject["msg"].ToObject<string>().ToLower();
  551. }
  552. }
  553. catch
  554. {
  555. return "网络不稳定请重试";
  556. }
  557. }
  558. public static String Rebirth(string intemail, string firstpassword, string secondPassword, string intcode)
  559. {
  560. HttpWebResponse response = null;
  561. ServicePointManager.DefaultConnectionLimit = 200;
  562. HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(RequestHost + RebirthUrl);
  563. request.Method = "Post";
  564. request.ContentType = "application/json";
  565. //request.Accept = "application/vnd.api+json;version=1";
  566. request.UserAgent = "Apifox/1.0.0 (https://www.apifox.cn)";
  567. request.Timeout = 20000;
  568. request.ServicePoint.Expect100Continue = false;
  569. StringWriter sw = new StringWriter();
  570. using (JsonWriter writer = new JsonTextWriter(sw))
  571. {
  572. writer.WriteStartObject();
  573. writer.WritePropertyName("account");
  574. writer.WriteValue(intemail);
  575. writer.WritePropertyName("appId");
  576. writer.WriteValue(16);
  577. writer.WritePropertyName("firstPassword");
  578. writer.WriteValue(firstpassword);
  579. writer.WritePropertyName("platformType");
  580. writer.WriteValue(0);
  581. writer.WritePropertyName("secondPassword");
  582. writer.WriteValue(secondPassword);
  583. writer.WritePropertyName("verifyCode");
  584. writer.WriteValue(intcode);
  585. writer.WriteEndObject();
  586. }
  587. try
  588. {
  589. string postBody = sw.ToString();
  590. using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
  591. {
  592. writer.Write(postBody);
  593. writer.Close();
  594. }
  595. response = (HttpWebResponse)request.GetResponse();
  596. using (StreamReader reader = new StreamReader(response.GetResponseStream()))
  597. {
  598. string responseData = reader.ReadToEnd();
  599. Console.WriteLine(responseData);
  600. reader.Close();
  601. JObject jobject = (JObject)JsonConvert.DeserializeObject(responseData);
  602. if (response != null)
  603. {
  604. response.Close();
  605. }
  606. if (request != null)
  607. {
  608. request.Abort();
  609. }
  610. return jobject["msg"].ToObject<string>().ToLower();
  611. }
  612. }
  613. catch
  614. {
  615. return "网络不稳定请重试";
  616. }
  617. }
  618. }
  619. }