ServiceHelper.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  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 string ALLEmail = "";
  21. public static string ALLPassword = "";
  22. public static string access_token = "";
  23. public static string outemail = "";
  24. public static string outcode = "";
  25. public static string outregister = "";
  26. public static string outlogin = "";
  27. public static string code = "";
  28. public static string okcode = "";
  29. public static string remsg = "";
  30. public static string state = "";
  31. public static string AppCode = "com.brother.pdfreaderprofree.windows";
  32. public static string RequestHost = "http://139.196.160.101:8081";
  33. //验证邮箱
  34. public static string OKemailUrl = "/pdf-office-sso/auth/validUser";
  35. //验证验证码
  36. public static string OKcodeUrl = "/pdf-office-sso/auth/isEmailCodeValid";
  37. //发送验证码
  38. public static string GetcodeUrl = "/pdf-office-sso/auth/getVerifyCode";
  39. //注册邮箱
  40. public static string registerUrl = "/pdf-office-sso/auth/emailRegister";
  41. //登录
  42. public static string LoginUrl = "/pdf-office-sso/auth/emailLogin";
  43. //登出
  44. public static string LogoutUrl = "/pdf-office-sso/user/logout";
  45. //注销
  46. public static string UseroutUrl = "/pdf-office-sso/user/logOffForUser";
  47. //重置密码
  48. public static string RebirthUrl = "/pdf-office-sso/auth/resetPassword";
  49. //获取用户信息
  50. public static string GetUserUrl = "/pdf-office-sso/user/me";
  51. private static string computerInfo;
  52. /// <summary>
  53. /// 电脑品牌信息
  54. /// </summary>
  55. public static string ComputerInfo
  56. {
  57. get {
  58. if (computerInfo == null)
  59. {
  60. computerInfo = GetComputerInfo();
  61. }
  62. return computerInfo;
  63. }
  64. set { computerInfo = value; }
  65. }
  66. private static string systemInfo;
  67. /// <summary>
  68. /// 系统信息
  69. /// </summary>
  70. public static string SystemInfo
  71. {
  72. get
  73. {
  74. if (systemInfo == null)
  75. {
  76. systemInfo = GetSystemInfo();
  77. }
  78. return systemInfo;
  79. }
  80. set { systemInfo = value; }
  81. }
  82. private static string GetSystemInfo()
  83. {
  84. try
  85. {
  86. string s = "";
  87. SelectQuery sq = new SelectQuery("Win32_OperatingSystem");
  88. ManagementObjectSearcher mos = new ManagementObjectSearcher(sq);
  89. ManagementObjectCollection moc = mos.Get();
  90. foreach (ManagementObject mo in moc)
  91. {
  92. s = mo.Properties["Version"].Value.ToString();
  93. }
  94. moc.Dispose();
  95. sq = null;
  96. return s;
  97. }
  98. catch { return ""; }
  99. }
  100. private static string GetComputerInfo()
  101. {
  102. try
  103. {
  104. string s = "";
  105. System.Windows.Controls.TextBox text = new System.Windows.Controls.TextBox();
  106. SelectQuery sq = new SelectQuery("Win32_ComputerSystem");
  107. ManagementObjectSearcher mos = new ManagementObjectSearcher(sq);
  108. ManagementObjectCollection moc = mos.Get();
  109. foreach (ManagementObject mo in moc)
  110. {
  111. s = mo.Properties["Manufacturer"].Value + " " + mo.Properties["Model"].Value;
  112. }
  113. moc.Dispose();
  114. sq = null;
  115. return s;
  116. }
  117. catch { return ""; }
  118. }
  119. public static String GetUser()
  120. {
  121. HttpWebResponse response = null;
  122. ServicePointManager.DefaultConnectionLimit = 200;
  123. HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(RequestHost + GetUserUrl+"");
  124. request.Method = "Get";
  125. request.Headers.Add("Authorization", $"Bearer {Settings.Default.AppProperties.LoginToken}");
  126. request.ContentType = "text/html;charset=UTF-8";
  127. //request.Accept = "application/vnd.api+json;version=1";
  128. request.UserAgent = "Apifox/1.0.0 (https://www.apifox.cn)";
  129. request.Timeout = 20000;
  130. request.ServicePoint.Expect100Continue = false;
  131. try
  132. {
  133. response = (HttpWebResponse)request.GetResponse();
  134. using (StreamReader reader = new StreamReader(response.GetResponseStream()))
  135. {
  136. string responseData = reader.ReadToEnd();
  137. Console.WriteLine(responseData);
  138. reader.Close();
  139. JObject jobject = (JObject)JsonConvert.DeserializeObject(responseData);
  140. if (response != null)
  141. {
  142. response.Close();
  143. }
  144. if (request != null)
  145. {
  146. request.Abort();
  147. }
  148. return jobject["email"].ToObject<string>().ToLower();
  149. }
  150. }
  151. catch
  152. {
  153. return "网络不稳定请重试";
  154. }
  155. }
  156. public static String Ok_email(string intemail,string validType)
  157. {
  158. HttpWebResponse response = null;
  159. ServicePointManager.DefaultConnectionLimit = 200;
  160. HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(RequestHost + OKemailUrl);
  161. request.Method = "Post";
  162. request.ContentType = "application/json";
  163. //request.Accept = "application/vnd.api+json;version=1";
  164. request.UserAgent = "Apifox/1.0.0 (https://www.apifox.cn)";
  165. request.Timeout = 20000;
  166. request.ServicePoint.Expect100Continue = false;
  167. StringWriter sw = new StringWriter();
  168. using (JsonWriter writer = new JsonTextWriter(sw))
  169. {
  170. writer.WriteStartObject();
  171. writer.WritePropertyName("appId");
  172. writer.WriteValue(16);
  173. writer.WritePropertyName("platformType");
  174. writer.WriteValue(0);
  175. writer.WritePropertyName("username");
  176. writer.WriteValue(intemail);
  177. writer.WritePropertyName("validType");
  178. writer.WriteValue(validType);
  179. writer.WriteEndObject();
  180. }
  181. string postBody = sw.ToString();
  182. using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
  183. {
  184. writer.Write(postBody);
  185. writer.Close();
  186. }
  187. try
  188. {
  189. response = (HttpWebResponse)request.GetResponse();
  190. using (StreamReader reader = new StreamReader(response.GetResponseStream()))
  191. {
  192. string responseData = reader.ReadToEnd();
  193. Console.WriteLine(responseData);
  194. reader.Close();
  195. JObject jobject = (JObject)JsonConvert.DeserializeObject(responseData);
  196. //outemail = jobject["msg"].ToObject<string>().ToLower();
  197. if (response != null)
  198. {
  199. response.Close();
  200. }
  201. if (request != null)
  202. {
  203. request.Abort();
  204. }
  205. return jobject["msg"].ToObject<string>().ToLower();
  206. }
  207. }
  208. catch
  209. {
  210. return "网络不稳定请重试";
  211. }
  212. }
  213. //⑤报错
  214. public static void Get_code(string action, string email)
  215. {
  216. string post = $"?action={action}&appId=16&receiver={email}&type=0";
  217. HttpWebResponse response = null;
  218. ServicePointManager.DefaultConnectionLimit = 200;
  219. HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(RequestHost + GetcodeUrl + post);
  220. request.Method = "Get";
  221. request.ContentType = "text/html;charset=UTF-8";
  222. //request.Accept = "application/vnd.api+json;version=1";
  223. request.UserAgent = "Apifox/1.0.0 (https://www.apifox.cn)";
  224. request.Timeout = 20000;
  225. request.ServicePoint.Expect100Continue = false;
  226. try
  227. {
  228. response = (HttpWebResponse)request.GetResponse();
  229. using (StreamReader reader = new StreamReader(response.GetResponseStream()))
  230. {
  231. string responseData = reader.ReadToEnd();
  232. Console.WriteLine(responseData);
  233. reader.Close();
  234. JObject jobject = (JObject)JsonConvert.DeserializeObject(responseData);
  235. if (response != null)
  236. {
  237. response.Close();
  238. }
  239. if (request != null)
  240. {
  241. request.Abort();
  242. }
  243. }
  244. }
  245. catch
  246. {
  247. }
  248. }
  249. public static String Ok_code(string email, string code, string type)
  250. {
  251. string post = $"?account={email}&code={code}&type={type}&appId=16";
  252. HttpWebResponse response = null;
  253. ServicePointManager.DefaultConnectionLimit = 200;
  254. HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(RequestHost + OKcodeUrl + post);
  255. request.Method = "Get";
  256. request.ContentType = "text/html;charset=UTF-8";
  257. //request.Accept = "application/vnd.api+json;version=1";
  258. request.UserAgent = "Apifox/1.0.0 (https://www.apifox.cn)";
  259. request.Timeout = 20000;
  260. request.ServicePoint.Expect100Continue = false;
  261. try
  262. {
  263. response = (HttpWebResponse)request.GetResponse();
  264. using (StreamReader reader = new StreamReader(response.GetResponseStream()))
  265. {
  266. string responseData = reader.ReadToEnd();
  267. Console.WriteLine(responseData);
  268. reader.Close();
  269. JObject jobject = (JObject)JsonConvert.DeserializeObject(responseData);
  270. if (response != null)
  271. {
  272. response.Close();
  273. }
  274. if (request != null)
  275. {
  276. request.Abort();
  277. }
  278. return jobject["msg"].ToObject<string>().ToLower();
  279. }
  280. }
  281. catch
  282. {
  283. return "网络不稳定请重试";
  284. }
  285. }
  286. public static String Register_email(string intemail, string intpassword, string intcode, string uuid)
  287. {
  288. HttpWebResponse response = null;
  289. ServicePointManager.DefaultConnectionLimit = 200;
  290. HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(RequestHost + registerUrl);
  291. request.Method = "Post";
  292. request.ContentType = "application/json";
  293. //request.Accept = "application/vnd.api+json;version=1";
  294. request.UserAgent = "Apifox/1.0.0 (https://www.apifox.cn)";
  295. request.Timeout = 20000;
  296. request.ServicePoint.Expect100Continue = false;
  297. StringWriter sw = new StringWriter();
  298. using (JsonWriter writer = new JsonTextWriter(sw))
  299. {
  300. writer.WriteStartObject();
  301. writer.WritePropertyName("appId");
  302. writer.WriteValue(16);
  303. writer.WritePropertyName("deviceSign");
  304. writer.WriteValue(uuid);
  305. writer.WritePropertyName("password");
  306. writer.WriteValue(intpassword);
  307. writer.WritePropertyName("platformType");
  308. writer.WriteValue(0);
  309. writer.WritePropertyName("username");
  310. writer.WriteValue(intemail);
  311. writer.WritePropertyName("verifyCode");
  312. writer.WriteValue(intcode);
  313. writer.WriteEndObject();
  314. }
  315. string postBody = sw.ToString();
  316. using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
  317. {
  318. writer.Write(postBody);
  319. writer.Close();
  320. }
  321. try
  322. {
  323. response = (HttpWebResponse)request.GetResponse();
  324. using (StreamReader reader = new StreamReader(response.GetResponseStream()))
  325. {
  326. string responseData = reader.ReadToEnd();
  327. Console.WriteLine(responseData);
  328. reader.Close();
  329. JObject jobject = (JObject)JsonConvert.DeserializeObject(responseData);
  330. if (response != null)
  331. {
  332. response.Close();
  333. }
  334. if (request != null)
  335. {
  336. request.Abort();
  337. }
  338. if (jobject["msg"].ToObject<string>().ToLower() == "success")
  339. {
  340. access_token = jobject["result"]["access_token"].ToObject<string>().ToLower();
  341. Settings.Default.AppProperties.LoginToken = jobject["result"]["access_token"].ToObject<string>().ToLower();
  342. Settings.Default.Save();
  343. }
  344. return jobject["msg"].ToObject<string>().ToLower();
  345. }
  346. }
  347. catch
  348. {
  349. return "网络不稳定请重试";
  350. }
  351. }
  352. public static String Login(string intemail, string intpassword, string uuid)
  353. {
  354. string postBody = $"?appId=16&deviceSign={uuid}&email={intemail}&password={intpassword}&platformType=0";
  355. HttpWebResponse response = null;
  356. ServicePointManager.DefaultConnectionLimit = 200;
  357. HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(RequestHost + LoginUrl + postBody);
  358. request.Method = "Post";
  359. request.ContentType = "application/x-www-form-urlencoded";
  360. //request.Accept = "application/vnd.api+json;version=1";
  361. request.UserAgent = "Apifox/1.0.0 (https://www.apifox.cn)";
  362. request.Timeout = 20000;
  363. request.ServicePoint.Expect100Continue = false;
  364. try
  365. {
  366. response = (HttpWebResponse)request.GetResponse();
  367. using (StreamReader reader = new StreamReader(response.GetResponseStream()))
  368. {
  369. string responseData = reader.ReadToEnd();
  370. Console.WriteLine(responseData);
  371. reader.Close();
  372. JObject jobject = (JObject)JsonConvert.DeserializeObject(responseData);
  373. outlogin = jobject["msg"].ToObject<string>().ToLower();
  374. if (response != null)
  375. {
  376. response.Close();
  377. }
  378. if (request != null)
  379. {
  380. request.Abort();
  381. }
  382. if (jobject["msg"].ToObject<string>().ToLower() == "success")
  383. {
  384. access_token = jobject["result"]["access_token"].ToObject<string>().ToLower();
  385. Settings.Default.AppProperties.LoginToken = jobject["result"]["access_token"].ToObject<string>().ToLower();
  386. Settings.Default.Save();
  387. }
  388. return jobject["msg"].ToObject<string>().ToLower();
  389. }
  390. }
  391. catch
  392. {
  393. return "网络不稳定请重试";
  394. }
  395. }
  396. //⑤报错
  397. public static void Logout(string uuid)
  398. {
  399. string postBody = $"?deviceSign={uuid}&appId=16";
  400. HttpWebResponse response = null;
  401. ServicePointManager.DefaultConnectionLimit = 200;
  402. HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(RequestHost + LogoutUrl + postBody);
  403. request.Method = "Post";
  404. request.Headers.Add("Authorization", $"Bearer {Settings.Default.AppProperties.LoginToken}");
  405. request.ContentType = "application/x-www-form-urlencoded";
  406. //request.Accept = "application/vnd.api+json;version=1";
  407. request.UserAgent = "Apifox/1.0.0 (https://www.apifox.cn)";
  408. request.Timeout = 20000;
  409. request.ServicePoint.Expect100Continue = false;
  410. try
  411. {
  412. response = (HttpWebResponse)request.GetResponse();
  413. using (StreamReader reader = new StreamReader(response.GetResponseStream()))
  414. {
  415. string responseData = reader.ReadToEnd();
  416. Console.WriteLine(responseData);
  417. reader.Close();
  418. JObject jobject = (JObject)JsonConvert.DeserializeObject(responseData);
  419. if (response != null)
  420. {
  421. response.Close();
  422. }
  423. if (request != null)
  424. {
  425. request.Abort();
  426. }
  427. }
  428. }
  429. catch
  430. {
  431. }
  432. }
  433. public static String Usergout(string code)
  434. {
  435. string postBody = $"?code={code}";
  436. HttpWebResponse response = null;
  437. ServicePointManager.DefaultConnectionLimit = 200;
  438. HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(RequestHost + UseroutUrl + postBody);
  439. request.Method = "Post";
  440. request.Headers.Add("Authorization", $"Bearer {Settings.Default.AppProperties.LoginToken}");
  441. request.ContentType = "application/x-www-form-urlencoded";
  442. //request.Accept = "application/vnd.api+json;version=1";
  443. request.UserAgent = "Apifox/1.0.0 (https://www.apifox.cn)";
  444. request.Timeout = 20000;
  445. request.ServicePoint.Expect100Continue = false;
  446. try
  447. {
  448. response = (HttpWebResponse)request.GetResponse();
  449. using (StreamReader reader = new StreamReader(response.GetResponseStream()))
  450. {
  451. string responseData = reader.ReadToEnd();
  452. Console.WriteLine(responseData);
  453. reader.Close();
  454. JObject jobject = (JObject)JsonConvert.DeserializeObject(responseData);
  455. if (response != null)
  456. {
  457. response.Close();
  458. }
  459. if (request != null)
  460. {
  461. request.Abort();
  462. }
  463. return jobject["msg"].ToObject<string>().ToLower();
  464. }
  465. }
  466. catch
  467. {
  468. return "网络不稳定请重试";
  469. }
  470. }
  471. public static String Rebirth(string intemail, string firstpassword, string secondPassword, string intcode)
  472. {
  473. HttpWebResponse response = null;
  474. ServicePointManager.DefaultConnectionLimit = 200;
  475. HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(RequestHost + RebirthUrl);
  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("account");
  487. writer.WriteValue(intemail);
  488. writer.WritePropertyName("appId");
  489. writer.WriteValue(16);
  490. writer.WritePropertyName("firstPassword");
  491. writer.WriteValue(firstpassword);
  492. writer.WritePropertyName("platformType");
  493. writer.WriteValue(0);
  494. writer.WritePropertyName("secondPassword");
  495. writer.WriteValue(secondPassword);
  496. writer.WritePropertyName("verifyCode");
  497. writer.WriteValue(intcode);
  498. writer.WriteEndObject();
  499. }
  500. string postBody = sw.ToString();
  501. using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
  502. {
  503. writer.Write(postBody);
  504. writer.Close();
  505. }
  506. try
  507. {
  508. response = (HttpWebResponse)request.GetResponse();
  509. using (StreamReader reader = new StreamReader(response.GetResponseStream()))
  510. {
  511. string responseData = reader.ReadToEnd();
  512. Console.WriteLine(responseData);
  513. reader.Close();
  514. JObject jobject = (JObject)JsonConvert.DeserializeObject(responseData);
  515. if (response != null)
  516. {
  517. response.Close();
  518. }
  519. if (request != null)
  520. {
  521. request.Abort();
  522. }
  523. return jobject["msg"].ToObject<string>().ToLower();
  524. }
  525. }
  526. catch
  527. {
  528. return "网络不稳定请重试";
  529. }
  530. }
  531. }
  532. }