ServiceHelper.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  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.Text;
  11. using System.Threading.Tasks;
  12. namespace PDF_Office.Helper
  13. {
  14. /// <summary>
  15. /// 注册登陆相关的工具类
  16. /// </summary>
  17. ///
  18. public static class ServiceHelper
  19. {
  20. public static bool IsLogin=false;
  21. public static string ALLEmail = "";
  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. //超时同步为20秒
  121. public static String GetUser()
  122. {
  123. HttpWebResponse response = null;
  124. ServicePointManager.DefaultConnectionLimit = 200;
  125. HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(RequestHost + GetUserUrl+"");
  126. request.Method = "Get";
  127. request.Headers.Add("Authorization", $"Bearer {Settings.Default.AppProperties.LoginToken}");
  128. request.ContentType = "text/html;charset=UTF-8";
  129. //request.Accept = "application/vnd.api+json;version=1";
  130. request.UserAgent = "Apifox/1.0.0 (https://www.apifox.cn)";
  131. request.Timeout = 20000;
  132. request.ServicePoint.Expect100Continue = false;
  133. try
  134. {
  135. response = (HttpWebResponse)request.GetResponse();
  136. using (StreamReader reader = new StreamReader(response.GetResponseStream()))
  137. {
  138. string responseData = reader.ReadToEnd();
  139. Console.WriteLine(responseData);
  140. reader.Close();
  141. JObject jobject = (JObject)JsonConvert.DeserializeObject(responseData);
  142. if (response != null)
  143. {
  144. response.Close();
  145. }
  146. if (request != null)
  147. {
  148. request.Abort();
  149. }
  150. JToken jToken;
  151. if(jobject.TryGetValue("msg" ,out jToken)==false)
  152. {
  153. App.IsLogin = true;
  154. return jobject["email"].ToObject<string>().ToLower();
  155. }
  156. else
  157. {
  158. return "false";
  159. }
  160. }
  161. }
  162. catch
  163. {
  164. return "网络不稳定请重试";
  165. }
  166. }
  167. public static String Ok_email(string intemail,string validType)
  168. {
  169. HttpWebResponse response = null;
  170. ServicePointManager.DefaultConnectionLimit = 200;
  171. HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(RequestHost + OKemailUrl);
  172. request.Method = "Post";
  173. request.ContentType = "application/json";
  174. //request.Accept = "application/vnd.api+json;version=1";
  175. request.UserAgent = "Apifox/1.0.0 (https://www.apifox.cn)";
  176. request.Timeout = 20000;
  177. request.ServicePoint.Expect100Continue = false;
  178. StringWriter sw = new StringWriter();
  179. using (JsonWriter writer = new JsonTextWriter(sw))
  180. {
  181. writer.WriteStartObject();
  182. writer.WritePropertyName("appId");
  183. writer.WriteValue(16);
  184. writer.WritePropertyName("platformType");
  185. writer.WriteValue(0);
  186. writer.WritePropertyName("username");
  187. writer.WriteValue(intemail);
  188. writer.WritePropertyName("validType");
  189. writer.WriteValue(validType);
  190. writer.WriteEndObject();
  191. }
  192. string postBody = sw.ToString();
  193. using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
  194. {
  195. writer.Write(postBody);
  196. writer.Close();
  197. }
  198. try
  199. {
  200. response = (HttpWebResponse)request.GetResponse();
  201. using (StreamReader reader = new StreamReader(response.GetResponseStream()))
  202. {
  203. string responseData = reader.ReadToEnd();
  204. Console.WriteLine(responseData);
  205. reader.Close();
  206. JObject jobject = (JObject)JsonConvert.DeserializeObject(responseData);
  207. //outemail = jobject["msg"].ToObject<string>().ToLower();
  208. if (response != null)
  209. {
  210. response.Close();
  211. }
  212. if (request != null)
  213. {
  214. request.Abort();
  215. }
  216. return jobject["msg"].ToObject<string>().ToLower();
  217. }
  218. }
  219. catch
  220. {
  221. return "网络不稳定请重试";
  222. }
  223. }
  224. //⑤报错
  225. public static void Get_code(string action, string email)
  226. {
  227. string post = $"?action={action}&appId=16&receiver={email}&type=0";
  228. HttpWebResponse response = null;
  229. ServicePointManager.DefaultConnectionLimit = 200;
  230. HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(RequestHost + GetcodeUrl + post);
  231. request.Method = "Get";
  232. request.ContentType = "text/html;charset=UTF-8";
  233. //request.Accept = "application/vnd.api+json;version=1";
  234. request.UserAgent = "Apifox/1.0.0 (https://www.apifox.cn)";
  235. request.Timeout = 20000;
  236. request.ServicePoint.Expect100Continue = false;
  237. try
  238. {
  239. response = (HttpWebResponse)request.GetResponse();
  240. using (StreamReader reader = new StreamReader(response.GetResponseStream()))
  241. {
  242. string responseData = reader.ReadToEnd();
  243. Console.WriteLine(responseData);
  244. reader.Close();
  245. JObject jobject = (JObject)JsonConvert.DeserializeObject(responseData);
  246. if (response != null)
  247. {
  248. response.Close();
  249. }
  250. if (request != null)
  251. {
  252. request.Abort();
  253. }
  254. }
  255. }
  256. catch
  257. {
  258. }
  259. }
  260. public static String Ok_code(string email, string code, string type)
  261. {
  262. string post = $"?account={email}&code={code}&type={type}&appId=16";
  263. HttpWebResponse response = null;
  264. ServicePointManager.DefaultConnectionLimit = 200;
  265. HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(RequestHost + OKcodeUrl + post);
  266. request.Method = "Get";
  267. request.ContentType = "text/html;charset=UTF-8";
  268. //request.Accept = "application/vnd.api+json;version=1";
  269. request.UserAgent = "Apifox/1.0.0 (https://www.apifox.cn)";
  270. request.Timeout = 20000;
  271. request.ServicePoint.Expect100Continue = false;
  272. try
  273. {
  274. response = (HttpWebResponse)request.GetResponse();
  275. using (StreamReader reader = new StreamReader(response.GetResponseStream()))
  276. {
  277. string responseData = reader.ReadToEnd();
  278. Console.WriteLine(responseData);
  279. reader.Close();
  280. JObject jobject = (JObject)JsonConvert.DeserializeObject(responseData);
  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. public static String Register_email(string intemail, string intpassword, string intcode, string uuid)
  298. {
  299. HttpWebResponse response = null;
  300. ServicePointManager.DefaultConnectionLimit = 200;
  301. HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(RequestHost + registerUrl);
  302. request.Method = "Post";
  303. request.ContentType = "application/json";
  304. //request.Accept = "application/vnd.api+json;version=1";
  305. request.UserAgent = "Apifox/1.0.0 (https://www.apifox.cn)";
  306. request.Timeout = 20000;
  307. request.ServicePoint.Expect100Continue = false;
  308. StringWriter sw = new StringWriter();
  309. using (JsonWriter writer = new JsonTextWriter(sw))
  310. {
  311. writer.WriteStartObject();
  312. writer.WritePropertyName("appId");
  313. writer.WriteValue(16);
  314. writer.WritePropertyName("deviceSign");
  315. writer.WriteValue(uuid);
  316. writer.WritePropertyName("password");
  317. writer.WriteValue(intpassword);
  318. writer.WritePropertyName("platformType");
  319. writer.WriteValue(0);
  320. writer.WritePropertyName("username");
  321. writer.WriteValue(intemail);
  322. writer.WritePropertyName("verifyCode");
  323. writer.WriteValue(intcode);
  324. writer.WriteEndObject();
  325. }
  326. string postBody = sw.ToString();
  327. using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
  328. {
  329. writer.Write(postBody);
  330. writer.Close();
  331. }
  332. try
  333. {
  334. response = (HttpWebResponse)request.GetResponse();
  335. using (StreamReader reader = new StreamReader(response.GetResponseStream()))
  336. {
  337. string responseData = reader.ReadToEnd();
  338. Console.WriteLine(responseData);
  339. reader.Close();
  340. JObject jobject = (JObject)JsonConvert.DeserializeObject(responseData);
  341. if (response != null)
  342. {
  343. response.Close();
  344. }
  345. if (request != null)
  346. {
  347. request.Abort();
  348. }
  349. if (jobject["msg"].ToObject<string>().ToLower() == "success")
  350. {
  351. App.IsLogin =true;
  352. access_token = jobject["result"]["access_token"].ToObject<string>().ToLower();
  353. Settings.Default.AppProperties.LoginToken = jobject["result"]["access_token"].ToObject<string>().ToLower();
  354. Settings.Default.Save();
  355. }
  356. return jobject["msg"].ToObject<string>().ToLower();
  357. }
  358. }
  359. catch
  360. {
  361. return "网络不稳定请重试";
  362. }
  363. }
  364. public static String Login(string intemail, string intpassword, string uuid)
  365. {
  366. string postBody = $"?appId=16&deviceSign={uuid}&email={intemail}&password={intpassword}&platformType=0";
  367. HttpWebResponse response = null;
  368. ServicePointManager.DefaultConnectionLimit = 200;
  369. HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(RequestHost + LoginUrl + postBody);
  370. request.Method = "Post";
  371. request.ContentType = "application/x-www-form-urlencoded";
  372. //request.Accept = "application/vnd.api+json;version=1";
  373. request.UserAgent = "Apifox/1.0.0 (https://www.apifox.cn)";
  374. request.Timeout = 20000;
  375. request.ServicePoint.Expect100Continue = false;
  376. try
  377. {
  378. response = (HttpWebResponse)request.GetResponse();
  379. using (StreamReader reader = new StreamReader(response.GetResponseStream()))
  380. {
  381. string responseData = reader.ReadToEnd();
  382. Console.WriteLine(responseData);
  383. reader.Close();
  384. JObject jobject = (JObject)JsonConvert.DeserializeObject(responseData);
  385. outlogin = jobject["msg"].ToObject<string>().ToLower();
  386. if (response != null)
  387. {
  388. response.Close();
  389. }
  390. if (request != null)
  391. {
  392. request.Abort();
  393. }
  394. if (jobject["msg"].ToObject<string>().ToLower() == "success")
  395. {
  396. App.IsLogin = true;
  397. access_token = jobject["result"]["access_token"].ToObject<string>().ToLower();
  398. Settings.Default.AppProperties.LoginToken = jobject["result"]["access_token"].ToObject<string>().ToLower();
  399. Settings.Default.Save();
  400. }
  401. return jobject["msg"].ToObject<string>().ToLower();
  402. }
  403. }
  404. catch
  405. {
  406. return "网络不稳定请重试";
  407. }
  408. }
  409. //⑤报错
  410. public static void Logout(string uuid)
  411. {
  412. string postBody = $"?deviceSign={uuid}&appId=16";
  413. HttpWebResponse response = null;
  414. ServicePointManager.DefaultConnectionLimit = 200;
  415. HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(RequestHost + LogoutUrl + postBody);
  416. request.Method = "Post";
  417. request.Headers.Add("Authorization", $"Bearer {Settings.Default.AppProperties.LoginToken}");
  418. request.ContentType = "application/x-www-form-urlencoded";
  419. //request.Accept = "application/vnd.api+json;version=1";
  420. request.UserAgent = "Apifox/1.0.0 (https://www.apifox.cn)";
  421. request.Timeout = 20000;
  422. request.ServicePoint.Expect100Continue = false;
  423. try
  424. {
  425. response = (HttpWebResponse)request.GetResponse();
  426. using (StreamReader reader = new StreamReader(response.GetResponseStream()))
  427. {
  428. string responseData = reader.ReadToEnd();
  429. Console.WriteLine(responseData);
  430. reader.Close();
  431. JObject jobject = (JObject)JsonConvert.DeserializeObject(responseData);
  432. App.IsLogin = false;
  433. if (response != null)
  434. {
  435. response.Close();
  436. }
  437. if (request != null)
  438. {
  439. request.Abort();
  440. }
  441. }
  442. }
  443. catch
  444. {
  445. }
  446. }
  447. public static String Usergout(string code)
  448. {
  449. string postBody = $"?code={code}";
  450. HttpWebResponse response = null;
  451. ServicePointManager.DefaultConnectionLimit = 200;
  452. HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(RequestHost + UseroutUrl + postBody);
  453. request.Method = "Post";
  454. request.Headers.Add("Authorization", $"Bearer {Settings.Default.AppProperties.LoginToken}");
  455. request.ContentType = "application/x-www-form-urlencoded";
  456. //request.Accept = "application/vnd.api+json;version=1";
  457. request.UserAgent = "Apifox/1.0.0 (https://www.apifox.cn)";
  458. request.Timeout = 20000;
  459. request.ServicePoint.Expect100Continue = false;
  460. try
  461. {
  462. response = (HttpWebResponse)request.GetResponse();
  463. using (StreamReader reader = new StreamReader(response.GetResponseStream()))
  464. {
  465. string responseData = reader.ReadToEnd();
  466. Console.WriteLine(responseData);
  467. reader.Close();
  468. JObject jobject = (JObject)JsonConvert.DeserializeObject(responseData);
  469. if (response != null)
  470. {
  471. response.Close();
  472. }
  473. if (request != null)
  474. {
  475. request.Abort();
  476. }
  477. return jobject["msg"].ToObject<string>().ToLower();
  478. }
  479. }
  480. catch
  481. {
  482. return "网络不稳定请重试";
  483. }
  484. }
  485. public static String Rebirth(string intemail, string firstpassword, string secondPassword, string intcode)
  486. {
  487. HttpWebResponse response = null;
  488. ServicePointManager.DefaultConnectionLimit = 200;
  489. HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(RequestHost + RebirthUrl);
  490. request.Method = "Post";
  491. request.ContentType = "application/json";
  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. StringWriter sw = new StringWriter();
  497. using (JsonWriter writer = new JsonTextWriter(sw))
  498. {
  499. writer.WriteStartObject();
  500. writer.WritePropertyName("account");
  501. writer.WriteValue(intemail);
  502. writer.WritePropertyName("appId");
  503. writer.WriteValue(16);
  504. writer.WritePropertyName("firstPassword");
  505. writer.WriteValue(firstpassword);
  506. writer.WritePropertyName("platformType");
  507. writer.WriteValue(0);
  508. writer.WritePropertyName("secondPassword");
  509. writer.WriteValue(secondPassword);
  510. writer.WritePropertyName("verifyCode");
  511. writer.WriteValue(intcode);
  512. writer.WriteEndObject();
  513. }
  514. string postBody = sw.ToString();
  515. using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
  516. {
  517. writer.Write(postBody);
  518. writer.Close();
  519. }
  520. try
  521. {
  522. response = (HttpWebResponse)request.GetResponse();
  523. using (StreamReader reader = new StreamReader(response.GetResponseStream()))
  524. {
  525. string responseData = reader.ReadToEnd();
  526. Console.WriteLine(responseData);
  527. reader.Close();
  528. JObject jobject = (JObject)JsonConvert.DeserializeObject(responseData);
  529. if (response != null)
  530. {
  531. response.Close();
  532. }
  533. if (request != null)
  534. {
  535. request.Abort();
  536. }
  537. return jobject["msg"].ToObject<string>().ToLower();
  538. }
  539. }
  540. catch
  541. {
  542. return "网络不稳定请重试";
  543. }
  544. }
  545. }
  546. }