ServiceHelper.cs 47 KB

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