ServiceHelper.cs 49 KB

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