123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290 |
- using Dragablz;
- using Newtonsoft.Json;
- using Newtonsoft.Json.Linq;
- using PDF_Master.Model;
- using PDF_Master.Properties;
- using PDF_Master.ViewModels;
- using PDF_Master.Views;
- using Prism.Services.Dialogs;
- using System;
- using System.Collections.Generic;
- using System.Globalization;
- using System.IO;
- using System.Linq;
- using System.Management;
- using System.Net;
- using System.Runtime.InteropServices;
- using System.Security.Cryptography;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Threading.Tasks;
- using System.Windows;
- namespace PDF_Master.Helper
- {
- /// <summary>
- /// 注册登陆相关的工具类
- /// </summary>
- ///
- public static class ServiceHelper
- {
- public static uint WakeId;
- public static bool IsLogin=false;
- public static string ALLPassword = "";
- public static string access_token = "";
- public static string outemail = "";
- public static string outcode = "";
- public static string outregister = "";
- public static string outlogin = "";
- public static string code = "";
- public static string okcode = "";
- public static string remsg = "";
- public static string state = "";
- public static string AppCode = "com.brother.pdfreaderprofree.windows";
- #if DEBUG
- //测试环境
- public static string RequestHost = "http://139.196.160.101:8081";
- public static string WebHost = "http://test-pdf-pro.kdan.cn:3021";
- #else
- //正式环境
- public static string RequestHost = "https://pdfmaster.pdfreaderpro.com";
- public static string WebHost = "https://www.pdfreaderpro.com";
- #endif
- /// <summary>
- /// 验证邮箱
- /// </summary>
- public static string OKemailUrl = "/pdf-office-sso/auth/validUser";
- /// <summary>
- /// 验证验证码
- /// </summary>
- public static string OKcodeUrl = "/pdf-office-sso/auth/isEmailCodeValid";
- /// <summary>
- /// 发送验证码
- /// </summary>
- public static string GetcodeUrl = "/pdf-office-sso/auth/getVerifyCode";
- /// <summary>
- /// 注册邮箱
- /// </summary>
- public static string registerUrl = "/pdf-office-sso/auth/emailRegister";
- /// <summary>
- /// /登录
- /// </summary>
- public static string LoginUrl = "/pdf-office-sso/auth/emailLogin";
- /// <summary>
- /// 登出
- /// </summary>
- public static string LogoutUrl = "/pdf-office-sso/user/logout";
- /// <summary>
- /// 注销
- /// </summary>
- public static string UseroutUrl = "/pdf-office-sso/user/logOffForUser";
- /// <summary>
- /// 重置密码
- /// </summary>
- public static string RebirthUrl = "/pdf-office-sso/auth/resetPassword";
- /// <summary>
- /// 获取用户信息
- /// </summary>
- public static string GetUserUrl = "/pdf-office-sso/user/me";
- /// <summary>
- /// 收集软件启动数据
- /// </summary>
- public static string DeviceLogUrl = "/pdf-office-account/deviceLog/create";
- /// <summary>
- /// 设备是否被自动登出
- /// </summary>
- public static string CheckDeviceStatusUrl = "/pdf-office-sso/auth/checkDeviceStatus";
- public static string AcceptdataUrl = "http://127.0.0.1:5544";
- /// <summary>
- /// 用于检查网络是否可以连接互联网,true表示连接成功,false表示连接失败
- /// </summary>
- /// <returns></returns>
- [DllImport("wininet.dll")]
- private extern static bool InternetGetConnectedState(int Description, int ReservedValue);
- public static bool IsConnectInternet()
- {
- int Description = 0;
- return InternetGetConnectedState(Description, 0);
- }
- private static string computerInfo;
- /// <summary>
- /// 电脑品牌信息
- /// </summary>
- public static string ComputerInfo
- {
- get
- {
- if (computerInfo == null)
- {
- computerInfo = GetComputerInfo();
- }
- return computerInfo;
- }
- set { computerInfo = value; }
- }
- private static string systemInfo;
- /// <summary>
- /// 系统信息
- /// </summary>
- public static string SystemInfo
- {
- get
- {
- if (systemInfo == null)
- {
- systemInfo = GetSystemInfo();
- }
- return systemInfo;
- }
- set { systemInfo = value; }
- }
- private static string GetSystemInfo()
- {
- try
- {
- string s = "";
- SelectQuery sq = new SelectQuery("Win32_OperatingSystem");
- ManagementObjectSearcher mos = new ManagementObjectSearcher(sq);
- ManagementObjectCollection moc = mos.Get();
- foreach (ManagementObject mo in moc)
- {
- s = mo.Properties["Version"].Value.ToString();
- }
- moc.Dispose();
- sq = null;
- return s;
- }
- catch { return ""; }
- }
- private static string GetComputerInfo()
- {
- try
- {
- string s = "";
- System.Windows.Controls.TextBox text = new System.Windows.Controls.TextBox();
- SelectQuery sq = new SelectQuery("Win32_ComputerSystem");
- ManagementObjectSearcher mos = new ManagementObjectSearcher(sq);
- ManagementObjectCollection moc = mos.Get();
- foreach (ManagementObject mo in moc)
- {
- s = mo.Properties["Manufacturer"].Value + " " + mo.Properties["Model"].Value;
- }
- moc.Dispose();
- sq = null;
- return s;
- }
- catch { return ""; }
- }
- /// <summary>
- /// 操作系统生成的UUID,用于获取虚拟机用户的设备识别码
- /// </summary>
- /// <returns></returns>
- public static string GetUUID()
- {
- string systemId = null;
- using (ManagementObjectSearcher mos = new ManagementObjectSearcher("select * from Win32_ComputerSystemProduct"))
- {
- foreach (var item in mos.Get())
- {
- systemId = item["UUID"].ToString();
- }
- }
- return systemId;
- }
- /// <summary>
- /// 主板编号
- /// </summary>
- /// <returns></returns>
- public static string GetBoardId()
- {
- var st = string.Empty;
- var mos = new ManagementObjectSearcher("Select * from Win32_BaseBoard");
- foreach (var o in mos.Get())
- {
- var mo = (ManagementObject)o;
- st = mo["SerialNumber"].ToString();
- }
- return st;
- }
- /// <summary>
- /// 获取设备唯一ID
- /// </summary>
- /// <returns></returns>
- public static string GetDeviceSerialNumber()
- {
- StringBuilder DeviceBuilder = new StringBuilder();
- bool IsVirtual = false;
- string boardId = GetBoardId();
- string uuid = GetUUID();
- //判断是否是虚拟机用户
- if (boardId.ToLower() == "" || boardId.ToLower() == "none")
- {
- if (string.IsNullOrEmpty(uuid) || uuid.ToLower() == "none")
- return string.Empty;
- else
- IsVirtual = true;
- }
- //真实机器用户 用主板id作为唯一识别码
- if (!IsVirtual)
- {
- DeviceBuilder.Append(boardId);
- }
- else
- {
- DeviceBuilder.Append(uuid);
- }
- MD5 md5 = MD5.Create();
- List<byte> charbytes = new List<byte>();
- foreach (char code in DeviceBuilder.ToString().ToArray())
- {
- charbytes.Add((byte)code);
- }
- byte[] md5Array = md5.ComputeHash(charbytes.ToArray());
- DeviceBuilder = new StringBuilder();
- foreach (var code in md5Array)
- {
- DeviceBuilder.Append(code.ToString("X2"));
- }
- return DeviceBuilder.ToString();
- }
- /// <summary>
- /// 获取设备系统版本
- /// </summary>
- /// <returns></returns>
- public static string GetSystemVersionNumber()
- {
- var st = string.Empty;
- try
- {
- var mc = new ManagementClass("Win32_OperatingSystem");
- var moc = mc.GetInstances();
- foreach (var o in moc)
- {
- var mo = (ManagementObject)o;
- st = mo.Properties["Version"].Value.ToString();
- if (st != string.Empty)
- {
- break;
- }
- }
- }
- catch (Exception ex)
- {
- }
- return st;
- }
- /// <summary>
- /// 高级功能前弹出比较表
- /// </summary>
- /// <param name="post">是否请求服务器,true为请求</param>
- public static bool IAPBeforeFunction(bool post = false)
- {
- if (!App.IsLogin)
- {
- if (Settings.Default.UserDate.Email == "")
- {
- App.mainWindowViewModel.OpenRegister();
- return false;
- }
- else
- {
- App.mainWindowViewModel.OpenLogin();
- return false;
- }
- }
-
- if (post)
- {
- //判断本地有没有token,没有显示登录,有再判断登录状态
- if (Settings.Default.AppProperties.LoginToken != "")
- {
- string code = ServiceHelper.GetUser();
- if (code == "330"|| code == "304")
- {
- App.mainWindowViewModel.UserVis = Visibility.Collapsed;
- App.mainWindowViewModel.LoginVis = Visibility.Visible;
- App.mainWindowViewModel.RegisterVis = Visibility.Collapsed;
- App.mainWindowViewModel.OphVis = Visibility.Visible;
- }
- }
- else
- {
- App.mainWindowViewModel.UserVis = Visibility.Collapsed;
- App.mainWindowViewModel.LoginVis = Visibility.Visible;
- App.mainWindowViewModel.RegisterVis = Visibility.Collapsed;
- App.mainWindowViewModel.OphVis = Visibility.Visible;
- }
- }
- (App.mainWindowViewModel.SelectedItem.DataContext as MainContentViewModel).IsUsedVIP = true;
- return true;
- }
- //超时同步为20秒
- /// <summary>
- /// 用户信息获取
- /// </summary>
- /// <returns></returns>
- public static String GetUser()
- {
- //判断是否有网络
- if (!IsConnectInternet())
- {
- return "300";
- }
- HttpWebResponse response = null;
- ServicePointManager.DefaultConnectionLimit = 200;
- HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(RequestHost + GetUserUrl + "");
- request.Method = "Get";
- request.Headers.Add("Authorization", $"Bearer {Settings.Default.AppProperties.LoginToken}");
- request.ContentType = "text/html;charset=UTF-8";
- //request.Accept = "application/vnd.api+json;version=1";
- request.UserAgent = "Apifox/1.0.0 (https://www.apifox.cn)";
- request.Timeout = 20000;
- request.ServicePoint.Expect100Continue = false;
- try
- {
- response = (HttpWebResponse)request.GetResponse();
- using (StreamReader reader = new StreamReader(response.GetResponseStream()))
- {
- string responseData = reader.ReadToEnd();
- Console.WriteLine(responseData);
- reader.Close();
- JObject jobject = (JObject)JsonConvert.DeserializeObject(responseData);
- if (response != null)
- {
- response.Close();
- }
- if (request != null)
- {
- request.Abort();
- };
- JToken jToken;
- if (jobject.TryGetValue("msg", out jToken) == false)
- {
- //ListenerSubscription();
- App.IsLogin = true;
- Settings.Default.UserDate.Email= jobject["email"].ToObject<string>().ToLower();
- Settings.Default.UserDate.id= jobject["id"].ToObject<string>().ToLower();
- JArray subscriptionInfoList = (JArray)jobject["subscriptionInfoList"];
- foreach (JToken info in subscriptionInfoList)
- {
- if ((int)info["platform"] == 2)
- {
- Settings.Default.UserDate.subscribeid = (string)info["id"];
- Settings.Default.UserDate.subscribeuserid = (string)info["userId"];
- Settings.Default.UserDate.subscribeplatform = (int)info["platform"];
- Settings.Default.UserDate.subscribestatus = (int)info["status"];
- if (Settings.Default.UserDate.subscribestatus > 0)
- {
- Settings.Default.UserDate.subscribeendDate = (string)info["endDate"];
- Settings.Default.UserDate.subscribepayType = (int)info["payType"];
- }
-
- }
- }
-
- //此处为主页UPgread按钮的Visible
- if (Settings.Default.UserDate.subscribestatus == 1)
- {
- for (int i = 0; i < App.Current.Windows.Count; i++)
- {
- MainWindow win = App.Current.Windows[i] as MainWindow;
- if (win != null&& win.DataContext!=null)
- {
- (win.DataContext as MainWindowViewModel).UpgradeVis = Visibility.Collapsed;
- (win.DataContext as MainWindowViewModel).Useremailchar = Settings.Default.UserDate.Email.Substring(0, 1);
- }
- }
- }
- else
- {
- for (int i = 0; i < App.Current.Windows.Count; i++)
- {
- MainWindow win = App.Current.Windows[i] as MainWindow;
- if (win != null&& win.DataContext!=null)
- {
- (win.DataContext as MainWindowViewModel).UpgradeVis = Visibility.Visible;
- (win.DataContext as MainWindowViewModel).Useremailchar = Settings.Default.UserDate.Email.Substring(0, 1);
- }
- }
- }
- #region 试用策略
- //Settings.Default.UserDate.isInFreeUseTime= jobject["isInFreeUseTime"].ToObject<bool>();
- //Settings.Default.UserDate.freeDate= jobject["freeDate"].ToObject<string>().ToLower();
- //if(DateTime.Parse(Settings.Default.UserDate.freeDate)<=DateTime.Now)
- //{
- // Settings.Default.UserDate.isInFreeUseTime = false;
- //}
- #endregion
- App.mainWindowViewModel.Useremailchar = Settings.Default.UserDate.Email.Substring(0, 1);
- Settings.Default.Save();
- return "ture";
- }
- else
- {
- return "false";
- }
- }
- }
- catch (System.Net.WebException e)
- {
- int statusCode = 0;
- if (e.Response is HttpWebResponse)
- {
- //从网络异常信息里解析错误,后面的逻辑根据错误码判断会更准确
- statusCode = (int)((HttpWebResponse)e.Response).StatusCode;
- }
- //试用策略注释
- //if (DateTime.Parse(Settings.Default.UserDate.freeDate) <= DateTime.Now)
- //{
- // Settings.Default.UserDate.isInFreeUseTime = false;
- //}
- Console.WriteLine(e.Message);
- //无权限
- if (Settings.Default.AppProperties.LoginToken == "")
- return "No Token";
- else if(statusCode == 401)
- {
- if(CheckDeviceStatusout()=="330")
- {
- App.IsLogin = false;
- return "330";
- }
- else
- {
- return "304";
- }
-
- }
- else return "300";
- }
- catch
- {
- //试用策略注释
- //if (DateTime.Parse(Settings.Default.UserDate.freeDate) <= DateTime.Now)
- //{
- // Settings.Default.UserDate.isInFreeUseTime = false;
- //}
- return "300";
- }
-
- }
- /// <summary>
- /// 验证email格式
- /// </summary>
- /// <param name="intemail"></param>
- /// <param name="validType"></param>
- /// <returns></returns>
- public static String Ok_email(string intemail, string validType)
- {
- HttpWebResponse response = null;
- ServicePointManager.DefaultConnectionLimit = 200;
- HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(RequestHost + OKemailUrl);
- request.Method = "Post";
- request.ContentType = "application/json";
- //request.Accept = "application/vnd.api+json;version=1";
- request.UserAgent = "Apifox/1.0.0 (https://www.apifox.cn)";
- request.Timeout = 20000;
- request.ServicePoint.Expect100Continue = false;
- StringWriter sw = new StringWriter();
- using (JsonWriter writer = new JsonTextWriter(sw))
- {
- writer.WriteStartObject();
- writer.WritePropertyName("appId");
- writer.WriteValue(16);
- writer.WritePropertyName("platformType");
- writer.WriteValue(0);
- writer.WritePropertyName("username");
- writer.WriteValue(intemail);
- writer.WritePropertyName("validType");
- writer.WriteValue(validType);
- writer.WriteEndObject();
- }
- try
- {
- string postBody = sw.ToString();
- using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
- {
- writer.Write(postBody);
- writer.Close();
- }
- response = (HttpWebResponse)request.GetResponse();
- using (StreamReader reader = new StreamReader(response.GetResponseStream()))
- {
- string responseData = reader.ReadToEnd();
- Console.WriteLine(responseData);
- reader.Close();
- JObject jobject = (JObject)JsonConvert.DeserializeObject(responseData);
- //outemail = jobject["msg"].ToObject<string>().ToLower();
- if (response != null)
- {
- response.Close();
- }
- if (request != null)
- {
- request.Abort();
- }
- return jobject["code"].ToObject<string>().ToLower();
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex);
- return "300";
- }
- }
- /// <summary>
- /// 获取验证码
- /// </summary>
- /// <param name="action"></param>
- /// <param name="email"></param>
- /// <returns></returns>
- public static string Get_code(string action, string email)
- {
- string post = $"?action={action}&appId=16&receiver={email}&type=0";
- HttpWebResponse response = null;
- ServicePointManager.DefaultConnectionLimit = 200;
- HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(RequestHost + GetcodeUrl + post);
- request.Method = "Get";
- request.ContentType = "text/html;charset=UTF-8";
- //request.Accept = "application/vnd.api+json;version=1";
- request.UserAgent = "Apifox/1.0.0 (https://www.apifox.cn)";
- request.Timeout = 20000;
- request.ServicePoint.Expect100Continue = false;
- try
- {
- response = (HttpWebResponse)request.GetResponse();
- using (StreamReader reader = new StreamReader(response.GetResponseStream()))
- {
- string responseData = reader.ReadToEnd();
- Console.WriteLine(responseData);
- reader.Close();
- JObject jobject = (JObject)JsonConvert.DeserializeObject(responseData);
- if (response != null)
- {
- response.Close();
- }
- if (request != null)
- {
- request.Abort();
- }
- return jobject["code"].ToObject<string>().ToLower();
- }
- }
- catch
- {
- return "300";
- }
- }
- /// <summary>
- /// 验证验证码是否正确
- /// </summary>
- /// <param name="email"></param>
- /// <param name="code"></param>
- /// <param name="type"></param>
- /// <returns></returns>
- public static String Ok_code(string email, string code, string type)
- {
- string post = $"?account={email}&code={code}&type={type}&appId=16";
- HttpWebResponse response = null;
- ServicePointManager.DefaultConnectionLimit = 200;
- HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(RequestHost + OKcodeUrl + post);
- request.Method = "Get";
- request.ContentType = "text/html;charset=UTF-8";
- //request.Accept = "application/vnd.api+json;version=1";
- request.UserAgent = "Apifox/1.0.0 (https://www.apifox.cn)";
- request.Timeout = 20000;
- request.ServicePoint.Expect100Continue = false;
- try
- {
- response = (HttpWebResponse)request.GetResponse();
- using (StreamReader reader = new StreamReader(response.GetResponseStream()))
- {
- string responseData = reader.ReadToEnd();
- Console.WriteLine(responseData);
- reader.Close();
- JObject jobject = (JObject)JsonConvert.DeserializeObject(responseData);
- if (response != null)
- {
- response.Close();
- }
- if (request != null)
- {
- request.Abort();
- }
- return jobject["code"].ToObject<string>().ToLower();
- }
- }
- catch
- {
- return "300";
- }
- }
- /// <summary>
- /// 注册账号
- /// </summary>
- /// <param name="intemail"></param>
- /// <param name="intpassword"></param>
- /// <param name="intcode"></param>
- /// <param name="uuid"></param>
- /// <returns></returns>
- public static String Register_email(string intemail, string intpassword, string intcode, string uuid)
- {
- HttpWebResponse response = null;
- ServicePointManager.DefaultConnectionLimit = 200;
- HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(RequestHost + registerUrl);
- request.Method = "Post";
- request.ContentType = "application/json";
- //request.Accept = "application/vnd.api+json;version=1";
- request.UserAgent = "Apifox/1.0.0 (https://www.apifox.cn)";
- request.Timeout = 20000;
- request.ServicePoint.Expect100Continue = false;
- StringWriter sw = new StringWriter();
- using (JsonWriter writer = new JsonTextWriter(sw))
- {
- writer.WriteStartObject();
- writer.WritePropertyName("appId");
- writer.WriteValue(16);
- writer.WritePropertyName("deviceSign");
- writer.WriteValue(uuid);
- writer.WritePropertyName("password");
- writer.WriteValue(intpassword);
- writer.WritePropertyName("platformType");
- writer.WriteValue(0);
- writer.WritePropertyName("username");
- writer.WriteValue(intemail);
- writer.WritePropertyName("verifyCode");
- writer.WriteValue(intcode);
- writer.WritePropertyName("model");
- writer.WriteValue("windows");
- writer.WriteEndObject();
- }
- try
- {
- string postBody = sw.ToString();
- using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
- {
- writer.Write(postBody);
- writer.Close();
- }
- response = (HttpWebResponse)request.GetResponse();
- using (StreamReader reader = new StreamReader(response.GetResponseStream()))
- {
- string responseData = reader.ReadToEnd();
- Console.WriteLine(responseData);
- reader.Close();
- JObject jobject = (JObject)JsonConvert.DeserializeObject(responseData);
- if (response != null)
- {
- response.Close();
- }
- if (request != null)
- {
- request.Abort();
- }
- if (jobject["code"].ToObject<string>().ToLower() == "200")
- {
- App.IsLogin =true;
- for (int i = 0; i < App.Current.Windows.Count; i++)
- {
- MainWindow win = App.Current.Windows[i] as MainWindow;
- if (win != null && win.DataContext != null)
- {
- (win.DataContext as MainWindowViewModel).UserVis = Visibility.Visible;
- (win.DataContext as MainWindowViewModel).LoginVis = Visibility.Collapsed;
- (win.DataContext as MainWindowViewModel).RegisterVis = Visibility.Collapsed;
- }
- }
- Settings.Default.UserDate.Email = intemail;
- Settings.Default.UserDate.IsLoginoff = false;
- access_token = jobject["result"]["access_token"].ToObject<string>().ToLower();
- Settings.Default.AppProperties.LoginToken = jobject["result"]["access_token"].ToObject<string>().ToLower();
- Settings.Default.Save();
- GetUser();
- }
- //return jobject["code"].ToObject<string>().ToLower();
- return "200";
- }
- }
- catch
- {
- return "300";
- }
- }
- /// <summary>
- /// 登录
- /// </summary>
- /// <param name="intemail"></param>
- /// <param name="intpassword"></param>
- /// <param name="uuid"></param>
- /// <returns></returns>
- public static String Login(string intemail, string intpassword, string uuid)
- {
- string postBody = $"?appId=16&deviceSign={uuid}&email={intemail}&password={intpassword}&platformType=0&model=windows";
- HttpWebResponse response = null;
- ServicePointManager.DefaultConnectionLimit = 200;
- HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(RequestHost + LoginUrl + postBody);
- request.Method = "Post";
- request.ContentType = "application/x-www-form-urlencoded";
- //request.Accept = "application/vnd.api+json;version=1";
- request.UserAgent = "Apifox/1.0.0 (https://www.apifox.cn)";
- request.Timeout = 20000;
- request.ServicePoint.Expect100Continue = false;
- try
- {
- response = (HttpWebResponse)request.GetResponse();
- using (StreamReader reader = new StreamReader(response.GetResponseStream()))
- {
- string responseData = reader.ReadToEnd();
- Console.WriteLine(responseData);
- reader.Close();
- JObject jobject = (JObject)JsonConvert.DeserializeObject(responseData);
- if (response != null)
- {
- response.Close();
- }
- if (request != null)
- {
- request.Abort();
- }
- if (jobject["code"].ToObject<string>().ToLower() == "200")
- {
- //ListenerSubscription();
- for (int i = 0; i < App.Current.Windows.Count; i++)
- {
- MainWindow win = App.Current.Windows[i] as MainWindow;
- if (win != null && win.DataContext != null)
- {
- (win.DataContext as MainWindowViewModel).UserVis = Visibility.Visible;
- (win.DataContext as MainWindowViewModel).LoginVis = Visibility.Collapsed;
- (win.DataContext as MainWindowViewModel).RegisterVis = Visibility.Collapsed;
- }
- }
- App.IsLogin = true;
- Settings.Default.UserDate.Email = intemail;
- Settings.Default.UserDate.IsLoginoff = false;
- access_token = jobject["result"]["access_token"].ToObject<string>().ToLower();
- Settings.Default.AppProperties.LoginToken = jobject["result"]["access_token"].ToObject<string>().ToLower();
- Settings.Default.Save();
- GetUser();
- }
- return jobject["code"].ToObject<string>().ToLower();
- }
- }
- catch
- {
- return "300";
- }
- }
- /// <summary>
- /// 登出
- /// </summary>
- /// <param name="uuid"></param>
- public static void Logout(string uuid)
- {
- string postBody = $"?deviceSign={uuid}&appId=16";
- HttpWebResponse response = null;
- ServicePointManager.DefaultConnectionLimit = 200;
- HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(RequestHost + LogoutUrl + postBody);
- request.Method = "Post";
- request.Headers.Add("Authorization", $"Bearer {Settings.Default.AppProperties.LoginToken}");
- request.ContentType = "application/x-www-form-urlencoded";
- //request.Accept = "application/vnd.api+json;version=1";
- request.UserAgent = "Apifox/1.0.0 (https://www.apifox.cn)";
- request.Timeout = 20000;
- request.ServicePoint.Expect100Continue = false;
- try
- {
- response = (HttpWebResponse)request.GetResponse();
- using (StreamReader reader = new StreamReader(response.GetResponseStream()))
- {
- string responseData = reader.ReadToEnd();
- Console.WriteLine(responseData);
- reader.Close();
- JObject jobject = (JObject)JsonConvert.DeserializeObject(responseData);
- App.IsLogin = false;
- Settings.Default.UserDate.subscribestatus = -1;
- Settings.Default.UserDate.subscribepayType = 0;
- App.mainWindowViewModel.UpgradeVis = Visibility.Visible;
- for (int i = 0; i < App.Current.Windows.Count; i++)
- {
- MainWindow win = App.Current.Windows[i] as MainWindow;
- if (win != null && win.DataContext != null)
- {
- (win.DataContext as MainWindowViewModel).UserVis = Visibility.Collapsed;
- (win.DataContext as MainWindowViewModel).LoginVis = Visibility.Visible;
- (win.DataContext as MainWindowViewModel).RegisterVis = Visibility.Collapsed;
- (win.DataContext as MainWindowViewModel).UpgradeVis = Visibility.Visible;
- }
- }
- if (response != null)
- {
- response.Close();
- }
- if (request != null)
- {
- request.Abort();
- }
- }
- }
- catch
- {
- }
- }
- /// <summary>
- /// 注销
- /// </summary>
- /// <param name="code"></param>
- /// <returns></returns>
- public static String Usergout(string code)
- {
- string postBody = $"?code={code}&appId=16";
- HttpWebResponse response = null;
- ServicePointManager.DefaultConnectionLimit = 200;
- HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(RequestHost + UseroutUrl + postBody);
- request.Method = "Post";
- request.Headers.Add("Authorization", $"Bearer {Settings.Default.AppProperties.LoginToken}");
- request.ContentType = "application/x-www-form-urlencoded";
- //request.Accept = "application/vnd.api+json;version=1";
- request.UserAgent = "Apifox/1.0.0 (https://www.apifox.cn)";
- request.Timeout = 20000;
- request.ServicePoint.Expect100Continue = false;
- try
- {
- response = (HttpWebResponse)request.GetResponse();
- using (StreamReader reader = new StreamReader(response.GetResponseStream()))
- {
- string responseData = reader.ReadToEnd();
- Console.WriteLine(responseData);
- reader.Close();
- JObject jobject = (JObject)JsonConvert.DeserializeObject(responseData);
- App.IsLogin = false;
- Settings.Default.UserDate.subscribestatus = -1;
- Settings.Default.UserDate.subscribepayType = 0;
- for (int i = 0; i < App.Current.Windows.Count; i++)
- {
- MainWindow win = App.Current.Windows[i] as MainWindow;
- if (win != null && win.DataContext != null)
- {
- (win.DataContext as MainWindowViewModel).UserVis = Visibility.Collapsed;
- (win.DataContext as MainWindowViewModel).LoginVis = Visibility.Visible;
- (win.DataContext as MainWindowViewModel).RegisterVis = Visibility.Collapsed;
- (win.DataContext as MainWindowViewModel).UpgradeVis = Visibility.Visible;
- }
- }
- App.mainWindowViewModel.UpgradeVis = Visibility.Visible;
- if (response != null)
- {
- response.Close();
- }
- if (request != null)
- {
- request.Abort();
- }
- return jobject["code"].ToObject<string>().ToLower();
- }
- }
- catch
- {
- return "300";
- }
- }
- /// <summary>
- /// 重置密码
- /// </summary>
- /// <param name="intemail"></param>
- /// <param name="firstpassword"></param>
- /// <param name="secondPassword"></param>
- /// <param name="intcode"></param>
- /// <returns></returns>
- public static String Rebirth(string intemail, string firstpassword, string secondPassword, string intcode)
- {
- HttpWebResponse response = null;
- ServicePointManager.DefaultConnectionLimit = 200;
- HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(RequestHost + RebirthUrl);
- request.Method = "Post";
- request.ContentType = "application/json";
- //request.Accept = "application/vnd.api+json;version=1";
- request.UserAgent = "Apifox/1.0.0 (https://www.apifox.cn)";
- request.Timeout = 20000;
- request.ServicePoint.Expect100Continue = false;
- StringWriter sw = new StringWriter();
- using (JsonWriter writer = new JsonTextWriter(sw))
- {
- writer.WriteStartObject();
- writer.WritePropertyName("account");
- writer.WriteValue(intemail);
- writer.WritePropertyName("appId");
- writer.WriteValue(16);
- writer.WritePropertyName("firstPassword");
- writer.WriteValue(firstpassword);
- writer.WritePropertyName("platformType");
- writer.WriteValue(0);
- writer.WritePropertyName("secondPassword");
- writer.WriteValue(secondPassword);
- writer.WritePropertyName("verifyCode");
- writer.WriteValue(intcode);
- writer.WriteEndObject();
- }
- try
- {
- string postBody = sw.ToString();
- using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
- {
- writer.Write(postBody);
- writer.Close();
- }
- response = (HttpWebResponse)request.GetResponse();
- using (StreamReader reader = new StreamReader(response.GetResponseStream()))
- {
- string responseData = reader.ReadToEnd();
- Console.WriteLine(responseData);
- reader.Close();
- JObject jobject = (JObject)JsonConvert.DeserializeObject(responseData);
- if (response != null)
- {
- response.Close();
- }
- if (request != null)
- {
- request.Abort();
- }
- for (int i = 0; i < App.Current.Windows.Count; i++)
- {
- MainWindow win = App.Current.Windows[i] as MainWindow;
- if (win != null)
- {
- (win.DataContext as MainWindowViewModel).UserVis = Visibility.Collapsed;
- (win.DataContext as MainWindowViewModel).LoginVis = Visibility.Visible;
- (win.DataContext as MainWindowViewModel).RegisterVis = Visibility.Collapsed;
- }
- }
- return jobject["code"].ToObject<string>().ToLower();
- }
- }
- catch
- {
- return "300";
- }
-
- }
- /// <summary>
- /// 设备是否被自动登出
- /// </summary>
- public static String CheckDeviceStatusout()
- {
- string postBody = $"?appId=16&deviceSign={GetDeviceSerialNumber()}";
- HttpWebResponse response = null;
- ServicePointManager.DefaultConnectionLimit = 200;
- HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(RequestHost + CheckDeviceStatusUrl + postBody);
- request.Method = "Post";
- request.ContentType = "application/x-www-form-urlencoded";
- //request.Accept = "application/vnd.api+json;version=1";
- request.UserAgent = "Apifox/1.0.0 (https://www.apifox.cn)";
- request.Timeout = 20000;
- request.ServicePoint.Expect100Continue = false;
- try
- {
- response = (HttpWebResponse)request.GetResponse();
- using (StreamReader reader = new StreamReader(response.GetResponseStream()))
- {
- string responseData = reader.ReadToEnd();
- Console.WriteLine(responseData);
- reader.Close();
- JObject jobject = (JObject)JsonConvert.DeserializeObject(responseData);
- App.IsLogin = false;
- Settings.Default.UserDate.subscribestatus = -1;
- Settings.Default.UserDate.subscribepayType = 0;
- App.mainWindowViewModel.UpgradeVis = Visibility.Visible;
- if(App.Current.Windows!=null&&App.Current.Windows.Count!=0)
- {
- for (int i = 0; i < App.Current.Windows.Count; i++)
- {
- MainWindow win = App.Current.Windows[i] as MainWindow;
- if (win != null&& win.DataContext!=null)
- {
- (win.DataContext as MainWindowViewModel).UserVis = Visibility.Collapsed;
- (win.DataContext as MainWindowViewModel).LoginVis = Visibility.Visible;
- (win.DataContext as MainWindowViewModel).RegisterVis = Visibility.Collapsed;
- (win.DataContext as MainWindowViewModel).UpgradeVis = Visibility.Visible;
- }
- }
- }
- if (response != null)
- {
- response.Close();
- }
- if (request != null)
- {
- request.Abort();
- }
- return jobject["code"].ToObject<string>().ToLower();
- }
- }
- catch
- {
- return "300";
- }
- }
- /// <summary>
- ///上传设备启动信息到服务端
- /// </summary>
- public static void CreateDeviceLog()
- {
- string url = RequestHost + DeviceLogUrl;
- try
- {
- HttpWebResponse response = null;
- ServicePointManager.DefaultConnectionLimit = 200;
- HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(RequestHost + DeviceLogUrl);
- request.Method = "Post";
- request.ContentType = "application/json";
- request.UserAgent = "Apifox/1.0.0 (https://www.apifox.cn)";
- request.Timeout = 20000;
- request.ServicePoint.Expect100Continue = false;
- StringWriter sw = new StringWriter();
- using (JsonWriter writer = new JsonTextWriter(sw))
- {
- writer.WriteStartObject();
- writer.WritePropertyName("appVersion");
- writer.WriteValue(App.Version);
- //产品ID Mac21 Windows 22
- writer.WritePropertyName("productId");
- writer.WriteValue(22);
- writer.WritePropertyName("os");
- writer.WriteValue(GetSystemVersionNumber());
- writer.WritePropertyName("timeZone");
- writer.WriteValue(TimeZoneInfo.Local.DisplayName);
- writer.WritePropertyName("language");
- writer.WriteValue(CultureInfo.CurrentCulture.Name);
- //平台 Mac 0 Windows 1
- writer.WritePropertyName("platform");
- writer.WriteValue(1);
- writer.WritePropertyName("createdAt");
- writer.WriteValue(DateTime.Now.ToFileTime());
- writer.WritePropertyName("appId");
- writer.WriteValue(16);
- writer.WritePropertyName("model");
- writer.WriteValue("Windows");
- writer.WritePropertyName("uniqueSn");
- writer.WriteValue(GetDeviceSerialNumber());
- writer.WritePropertyName("updatedAt");
- writer.WriteValue(DateTime.Now.ToFileTime());
- writer.WriteEndObject();
- }
- string postBody = sw.ToString();
- using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
- {
- writer.Write(postBody);
- writer.Close();
- }
- response = (HttpWebResponse)request.GetResponse();
- using (StreamReader reader = new StreamReader(response.GetResponseStream()))
- {
- string responseData = reader.ReadToEnd();
- Console.WriteLine(responseData);
- reader.Close();
- JObject jobject = (JObject)JsonConvert.DeserializeObject(responseData);
- if (response != null)
- {
- response.Close();
- }
- if (request != null)
- {
- request.Abort();
- }
- if (jobject["code"].ToObject<string>().ToLower() == "200")
- {
- Console.WriteLine("软件启动信息上传成功");
- }
- }
- }
- catch { }
- }
- //监听代码,暂时用不到,注释
- //监听订阅行为
- //public static HttpListener httpListener;
- //public static bool isListening = false;
- //public async static void ServerBoot()
- //{
- // if (!isListening)
- // {
- // httpListener = new HttpListener();
- // httpListener.Prefixes.Add("http://127.0.0.1:5544/");
- // // 启动监听器
- // httpListener.Start();
- // Console.WriteLine($"开始喽");
- // isListening = true;
- // // 在后台线程中等待请求并处理
- // await Task.Run(() => ListenForRequests());
-
- // }
- //}
- //private static async Task ListenForRequests()
- //{
- // WakeId = Win32Helper.RegisterWindowMessage("msgWake_PDFMaster");
- // Console.WriteLine("等待请求...");
- // // 持续监听请求
- // while (true)
- // {
-
- // // 等待接收请求
- // HttpListenerContext context = await httpListener.GetContextAsync();
- // // 允许跨域请求
- // context.Response.Headers.Add("Access-Control-Allow-Origin", "*");
- // context.Response.Headers.Add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
- // context.Response.Headers.Add("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
- // // 处理请求
- // if (context.Request.HttpMethod == "GET")
- // {
- // //返回 true 并结束监听
- // if (App.IsLogin == true)
- // {
- // Win32Helper.PostMessage((IntPtr)0xffff, WakeId, IntPtr.Zero, IntPtr.Zero);
- // IntPtr waitPtr = Win32Helper.CreateEvent(IntPtr.Zero, true, false, "Global\\shareWaitNotify" + Guid.NewGuid());
- // Win32Helper.WaitForSingleObject(waitPtr, 5000);
- // Win32Helper.CloseHandle(waitPtr);
- // }
- // byte[] responseBytes = System.Text.Encoding.UTF8.GetBytes("true");
- // context.Response.StatusCode = (int)HttpStatusCode.OK;
- // context.Response.ContentLength64 = responseBytes.Length;
- // context.Response.OutputStream.Write(responseBytes, 0, responseBytes.Length);
- // context.Response.OutputStream.Close();
- // httpListener.Stop();
- // isListening = false;
- // break;
- // }
- // }
- //}
- }
-
- }
|