ServiceHelper.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  1. using Newtonsoft.Json;
  2. using Newtonsoft.Json.Linq;
  3. using PDF_Office.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_Office.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. return jobject["email"].ToObject<string>().ToLower();
  227. }
  228. else
  229. {
  230. return "false";
  231. }
  232. }
  233. }
  234. catch
  235. {
  236. return "false";
  237. }
  238. }
  239. public static String Ok_email(string intemail,string validType)
  240. {
  241. HttpWebResponse response = null;
  242. ServicePointManager.DefaultConnectionLimit = 200;
  243. HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(RequestHost + OKemailUrl);
  244. request.Method = "Post";
  245. request.ContentType = "application/json";
  246. //request.Accept = "application/vnd.api+json;version=1";
  247. request.UserAgent = "Apifox/1.0.0 (https://www.apifox.cn)";
  248. request.Timeout = 20000;
  249. request.ServicePoint.Expect100Continue = false;
  250. StringWriter sw = new StringWriter();
  251. using (JsonWriter writer = new JsonTextWriter(sw))
  252. {
  253. writer.WriteStartObject();
  254. writer.WritePropertyName("appId");
  255. writer.WriteValue(16);
  256. writer.WritePropertyName("platformType");
  257. writer.WriteValue(0);
  258. writer.WritePropertyName("username");
  259. writer.WriteValue(intemail);
  260. writer.WritePropertyName("validType");
  261. writer.WriteValue(validType);
  262. writer.WriteEndObject();
  263. }
  264. string postBody = sw.ToString();
  265. using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
  266. {
  267. writer.Write(postBody);
  268. writer.Close();
  269. }
  270. try
  271. {
  272. response = (HttpWebResponse)request.GetResponse();
  273. using (StreamReader reader = new StreamReader(response.GetResponseStream()))
  274. {
  275. string responseData = reader.ReadToEnd();
  276. Console.WriteLine(responseData);
  277. reader.Close();
  278. JObject jobject = (JObject)JsonConvert.DeserializeObject(responseData);
  279. //outemail = jobject["msg"].ToObject<string>().ToLower();
  280. if (response != null)
  281. {
  282. response.Close();
  283. }
  284. if (request != null)
  285. {
  286. request.Abort();
  287. }
  288. return jobject["msg"].ToObject<string>().ToLower();
  289. }
  290. }
  291. catch
  292. {
  293. return "网络不稳定请重试";
  294. }
  295. }
  296. //⑤报错
  297. public static void Get_code(string action, string email)
  298. {
  299. string post = $"?action={action}&appId=16&receiver={email}&type=0";
  300. HttpWebResponse response = null;
  301. ServicePointManager.DefaultConnectionLimit = 200;
  302. HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(RequestHost + GetcodeUrl + post);
  303. request.Method = "Get";
  304. request.ContentType = "text/html;charset=UTF-8";
  305. //request.Accept = "application/vnd.api+json;version=1";
  306. request.UserAgent = "Apifox/1.0.0 (https://www.apifox.cn)";
  307. request.Timeout = 20000;
  308. request.ServicePoint.Expect100Continue = false;
  309. try
  310. {
  311. response = (HttpWebResponse)request.GetResponse();
  312. using (StreamReader reader = new StreamReader(response.GetResponseStream()))
  313. {
  314. string responseData = reader.ReadToEnd();
  315. Console.WriteLine(responseData);
  316. reader.Close();
  317. JObject jobject = (JObject)JsonConvert.DeserializeObject(responseData);
  318. if (response != null)
  319. {
  320. response.Close();
  321. }
  322. if (request != null)
  323. {
  324. request.Abort();
  325. }
  326. }
  327. }
  328. catch
  329. {
  330. }
  331. }
  332. public static String Ok_code(string email, string code, string type)
  333. {
  334. string post = $"?account={email}&code={code}&type={type}&appId=16";
  335. HttpWebResponse response = null;
  336. ServicePointManager.DefaultConnectionLimit = 200;
  337. HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(RequestHost + OKcodeUrl + post);
  338. request.Method = "Get";
  339. request.ContentType = "text/html;charset=UTF-8";
  340. //request.Accept = "application/vnd.api+json;version=1";
  341. request.UserAgent = "Apifox/1.0.0 (https://www.apifox.cn)";
  342. request.Timeout = 20000;
  343. request.ServicePoint.Expect100Continue = false;
  344. try
  345. {
  346. response = (HttpWebResponse)request.GetResponse();
  347. using (StreamReader reader = new StreamReader(response.GetResponseStream()))
  348. {
  349. string responseData = reader.ReadToEnd();
  350. Console.WriteLine(responseData);
  351. reader.Close();
  352. JObject jobject = (JObject)JsonConvert.DeserializeObject(responseData);
  353. if (response != null)
  354. {
  355. response.Close();
  356. }
  357. if (request != null)
  358. {
  359. request.Abort();
  360. }
  361. return jobject["msg"].ToObject<string>().ToLower();
  362. }
  363. }
  364. catch
  365. {
  366. return "网络不稳定请重试";
  367. }
  368. }
  369. public static String Register_email(string intemail, string intpassword, string intcode, string uuid)
  370. {
  371. HttpWebResponse response = null;
  372. ServicePointManager.DefaultConnectionLimit = 200;
  373. HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(RequestHost + registerUrl);
  374. request.Method = "Post";
  375. request.ContentType = "application/json";
  376. //request.Accept = "application/vnd.api+json;version=1";
  377. request.UserAgent = "Apifox/1.0.0 (https://www.apifox.cn)";
  378. request.Timeout = 20000;
  379. request.ServicePoint.Expect100Continue = false;
  380. StringWriter sw = new StringWriter();
  381. using (JsonWriter writer = new JsonTextWriter(sw))
  382. {
  383. writer.WriteStartObject();
  384. writer.WritePropertyName("appId");
  385. writer.WriteValue(16);
  386. writer.WritePropertyName("deviceSign");
  387. writer.WriteValue(uuid);
  388. writer.WritePropertyName("password");
  389. writer.WriteValue(intpassword);
  390. writer.WritePropertyName("platformType");
  391. writer.WriteValue(0);
  392. writer.WritePropertyName("username");
  393. writer.WriteValue(intemail);
  394. writer.WritePropertyName("verifyCode");
  395. writer.WriteValue(intcode);
  396. writer.WriteEndObject();
  397. }
  398. string postBody = sw.ToString();
  399. using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
  400. {
  401. writer.Write(postBody);
  402. writer.Close();
  403. }
  404. try
  405. {
  406. response = (HttpWebResponse)request.GetResponse();
  407. using (StreamReader reader = new StreamReader(response.GetResponseStream()))
  408. {
  409. string responseData = reader.ReadToEnd();
  410. Console.WriteLine(responseData);
  411. reader.Close();
  412. JObject jobject = (JObject)JsonConvert.DeserializeObject(responseData);
  413. if (response != null)
  414. {
  415. response.Close();
  416. }
  417. if (request != null)
  418. {
  419. request.Abort();
  420. }
  421. if (jobject["msg"].ToObject<string>().ToLower() == "success")
  422. {
  423. App.IsLogin =true;
  424. access_token = jobject["result"]["access_token"].ToObject<string>().ToLower();
  425. Settings.Default.AppProperties.LoginToken = jobject["result"]["access_token"].ToObject<string>().ToLower();
  426. Settings.Default.Save();
  427. }
  428. return jobject["msg"].ToObject<string>().ToLower();
  429. }
  430. }
  431. catch
  432. {
  433. return "网络不稳定请重试";
  434. }
  435. }
  436. public static String Login(string intemail, string intpassword, string uuid)
  437. {
  438. string postBody = $"?appId=16&deviceSign={uuid}&email={intemail}&password={intpassword}&platformType=0";
  439. HttpWebResponse response = null;
  440. ServicePointManager.DefaultConnectionLimit = 200;
  441. HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(RequestHost + LoginUrl + postBody);
  442. request.Method = "Post";
  443. request.ContentType = "application/x-www-form-urlencoded";
  444. //request.Accept = "application/vnd.api+json;version=1";
  445. request.UserAgent = "Apifox/1.0.0 (https://www.apifox.cn)";
  446. request.Timeout = 20000;
  447. request.ServicePoint.Expect100Continue = false;
  448. try
  449. {
  450. response = (HttpWebResponse)request.GetResponse();
  451. using (StreamReader reader = new StreamReader(response.GetResponseStream()))
  452. {
  453. string responseData = reader.ReadToEnd();
  454. Console.WriteLine(responseData);
  455. reader.Close();
  456. JObject jobject = (JObject)JsonConvert.DeserializeObject(responseData);
  457. outlogin = jobject["msg"].ToObject<string>().ToLower();
  458. if (response != null)
  459. {
  460. response.Close();
  461. }
  462. if (request != null)
  463. {
  464. request.Abort();
  465. }
  466. if (jobject["msg"].ToObject<string>().ToLower() == "success")
  467. {
  468. App.IsLogin = true;
  469. access_token = jobject["result"]["access_token"].ToObject<string>().ToLower();
  470. Settings.Default.AppProperties.LoginToken = jobject["result"]["access_token"].ToObject<string>().ToLower();
  471. Settings.Default.Save();
  472. }
  473. return jobject["msg"].ToObject<string>().ToLower();
  474. }
  475. }
  476. catch
  477. {
  478. return "网络不稳定请重试";
  479. }
  480. }
  481. //⑤报错
  482. public static void Logout(string uuid)
  483. {
  484. string postBody = $"?deviceSign={uuid}&appId=16";
  485. HttpWebResponse response = null;
  486. ServicePointManager.DefaultConnectionLimit = 200;
  487. HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(RequestHost + LogoutUrl + postBody);
  488. request.Method = "Post";
  489. request.Headers.Add("Authorization", $"Bearer {Settings.Default.AppProperties.LoginToken}");
  490. request.ContentType = "application/x-www-form-urlencoded";
  491. //request.Accept = "application/vnd.api+json;version=1";
  492. request.UserAgent = "Apifox/1.0.0 (https://www.apifox.cn)";
  493. request.Timeout = 20000;
  494. request.ServicePoint.Expect100Continue = false;
  495. try
  496. {
  497. response = (HttpWebResponse)request.GetResponse();
  498. using (StreamReader reader = new StreamReader(response.GetResponseStream()))
  499. {
  500. string responseData = reader.ReadToEnd();
  501. Console.WriteLine(responseData);
  502. reader.Close();
  503. JObject jobject = (JObject)JsonConvert.DeserializeObject(responseData);
  504. App.IsLogin = false;
  505. if (response != null)
  506. {
  507. response.Close();
  508. }
  509. if (request != null)
  510. {
  511. request.Abort();
  512. }
  513. }
  514. }
  515. catch
  516. {
  517. }
  518. }
  519. public static String Usergout(string code)
  520. {
  521. string postBody = $"?code={code}";
  522. HttpWebResponse response = null;
  523. ServicePointManager.DefaultConnectionLimit = 200;
  524. HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(RequestHost + UseroutUrl + postBody);
  525. request.Method = "Post";
  526. request.Headers.Add("Authorization", $"Bearer {Settings.Default.AppProperties.LoginToken}");
  527. request.ContentType = "application/x-www-form-urlencoded";
  528. //request.Accept = "application/vnd.api+json;version=1";
  529. request.UserAgent = "Apifox/1.0.0 (https://www.apifox.cn)";
  530. request.Timeout = 20000;
  531. request.ServicePoint.Expect100Continue = false;
  532. try
  533. {
  534. response = (HttpWebResponse)request.GetResponse();
  535. using (StreamReader reader = new StreamReader(response.GetResponseStream()))
  536. {
  537. string responseData = reader.ReadToEnd();
  538. Console.WriteLine(responseData);
  539. reader.Close();
  540. JObject jobject = (JObject)JsonConvert.DeserializeObject(responseData);
  541. if (response != null)
  542. {
  543. response.Close();
  544. }
  545. if (request != null)
  546. {
  547. request.Abort();
  548. }
  549. return jobject["msg"].ToObject<string>().ToLower();
  550. }
  551. }
  552. catch
  553. {
  554. return "网络不稳定请重试";
  555. }
  556. }
  557. public static String Rebirth(string intemail, string firstpassword, string secondPassword, string intcode)
  558. {
  559. HttpWebResponse response = null;
  560. ServicePointManager.DefaultConnectionLimit = 200;
  561. HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(RequestHost + RebirthUrl);
  562. request.Method = "Post";
  563. request.ContentType = "application/json";
  564. //request.Accept = "application/vnd.api+json;version=1";
  565. request.UserAgent = "Apifox/1.0.0 (https://www.apifox.cn)";
  566. request.Timeout = 20000;
  567. request.ServicePoint.Expect100Continue = false;
  568. StringWriter sw = new StringWriter();
  569. using (JsonWriter writer = new JsonTextWriter(sw))
  570. {
  571. writer.WriteStartObject();
  572. writer.WritePropertyName("account");
  573. writer.WriteValue(intemail);
  574. writer.WritePropertyName("appId");
  575. writer.WriteValue(16);
  576. writer.WritePropertyName("firstPassword");
  577. writer.WriteValue(firstpassword);
  578. writer.WritePropertyName("platformType");
  579. writer.WriteValue(0);
  580. writer.WritePropertyName("secondPassword");
  581. writer.WriteValue(secondPassword);
  582. writer.WritePropertyName("verifyCode");
  583. writer.WriteValue(intcode);
  584. writer.WriteEndObject();
  585. }
  586. string postBody = sw.ToString();
  587. using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
  588. {
  589. writer.Write(postBody);
  590. writer.Close();
  591. }
  592. try
  593. {
  594. response = (HttpWebResponse)request.GetResponse();
  595. using (StreamReader reader = new StreamReader(response.GetResponseStream()))
  596. {
  597. string responseData = reader.ReadToEnd();
  598. Console.WriteLine(responseData);
  599. reader.Close();
  600. JObject jobject = (JObject)JsonConvert.DeserializeObject(responseData);
  601. if (response != null)
  602. {
  603. response.Close();
  604. }
  605. if (request != null)
  606. {
  607. request.Abort();
  608. }
  609. return jobject["msg"].ToObject<string>().ToLower();
  610. }
  611. }
  612. catch
  613. {
  614. return "网络不稳定请重试";
  615. }
  616. }
  617. }
  618. }