| | |
| | | { |
| | | public class CommonPage : UIDrawerLayout |
| | | { |
| | | static CommonPage commonPage; |
| | | private static CommonPage commonPage; |
| | | public static CommonPage Instance |
| | | { |
| | | get |
| | | { |
| | | if (commonPage == null) |
| | | { |
| | | commonPage = new CommonPage { }; |
| | | commonPage = new CommonPage(); |
| | | } |
| | | return commonPage; |
| | | } |
| | |
| | | Application.SetStatusBarTextBlack(true); |
| | | } |
| | | |
| | | //public static string RequestHttpsHost = "https://global.hdlcontrol.com/HangZhouHdlCloudApi"; |
| | | public static string RequestHttpsHost = "https://global.hdlcontrol.com/ProposedProductionApi"; |
| | | /// <summary> |
| | | /// 版本号 |
| | | /// </summary> |
| | | public static string CodeIDString = "1.1.0120121401"; |
| | | /// <summary> |
| | | /// 注册来源(0:HDL On 1:Zigbee) |
| | | /// </summary> |
| | | public static int RegisterSoruceFromZIGBEE = 0; |
| | | /// <summary> |
| | | /// Company 用户公司用于公司App定制版 默认0 |
| | | /// </summary> |
| | | public static int Company = 0; |
| | | /// <summary> |
| | | /// 海外 注册时API调用和国内区分 |
| | | ///</summary> |
| | | public static int CompanyForINTERNETION = 4; |
| | | public static string CodeIDString = "1.1.0120101501"; |
| | | /// <summary> |
| | | /// 请求版本号 |
| | | /// </summary> |
| | | public static string RequestVersion = "2.0"; |
| | | /// <summary> |
| | | /// 左边距离屏幕的宽度--58 |
| | | /// </summary> |
| | | public static int XLeft = 58; |
| | | /// <summary> |
| | | /// 使用的语言 |
| | | /// </summary> |
| | | public static string ZigBeeLanguage |
| | | { |
| | | get |
| | | { |
| | | return Language.CurrentLanguage; |
| | | } |
| | | } |
| | | /// <summary> |
| | | /// 默认手机区号为86中国大陆 |
| | | /// </summary> |
| | | public static string PhoneZoneStr = "86"; |
| | | /// <summary> |
| | | /// APP宽度 -- 1080 |
| | | /// </summary> |
| | | public static int AppRealWidth = 1080; |
| | | |
| | | /// <summary> |
| | | /// 用于中国大陆验证手机号正则表达式 |
| | | /// </summary> |
| | | public static string PhoneRegexStr = "^[1][0-9]{10}$"; |
| | | /// <summary> |
| | | /// 用于验证非中国大陆手机号正则表达式 |
| | | /// </summary> |
| | | public static string PhoneForForeignRegexStr = "^[0-9]*$"; |
| | | /// <summary> |
| | | /// 用于验证邮箱正则表达式 |
| | | /// </summary> |
| | | public static string EmailRegexStr = "^\\s*([A-Za-z0-9_-]+(\\.\\w+)*@(\\w+\\.)+\\w{2,5})\\s*$"; |
| | | /// <summary>
|
| | | /// 安卓的系统返回按键能否按下(比如在备份还原时,不能按下返回键)
|
| | | /// </summary> |
| | | public static bool BackKeyCanClick = true; |
| | | |
| | | /// <summary> |
| | | /// 请求服务器方法 |
| | | /// 指定一个Url,和请求方法,数据,Cookie,得到响应的数据 |
| | | /// </summary> |
| | | /// <param name="requestUrl">请求Url</param> |
| | | /// <param name="byteData">请求的的数据</param> |
| | | /// <param name="timeout">超时,默认5秒</param> |
| | | /// <returns>得到响应的数据</returns> |
| | | public ResponseEntity.ResponsePack RequestHttpsZigbeeAsync(string requestUrl, byte[] byteData, int timeout = 5) |
| | | { |
| | | string result = this.RequestHttpsZigbeeResultAsync(requestUrl, byteData, timeout); |
| | | if (result != null) |
| | | { |
| | | try |
| | | { |
| | | var data = Newtonsoft.Json.JsonConvert.DeserializeObject<Shared.Common.ResponseEntity.ResponsePack>(result); |
| | | return data; |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | System.Console.WriteLine(ex.Message); |
| | | } |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 请求服务器方法 |
| | | /// 指定一个Url,和请求方法,数据,Cookie,得到响应的数据 |
| | | /// </summary> |
| | | /// <param name="requestUrl">请求Url</param> |
| | | /// <param name="byteData">请求的的数据</param> |
| | | /// <param name="timeout">超时,默认5秒</param> |
| | | /// <returns>得到响应的数据</returns> |
| | | public string RequestHttpsZigbeeResultAsync(string requestUrl, byte[] byteData, int timeout = 5) |
| | | { |
| | | var result = RequestHttpsZigbeeBytesResultAsync(requestUrl, byteData, "POST", timeout); |
| | | if (result != null)
|
| | | {
|
| | | return Encoding.UTF8.GetString(result);
|
| | | } |
| | | return null; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 请求服务器方法 |
| | | /// 指定一个Url,和请求方法,数据,Cookie,得到响应的数据 |
| | | /// </summary> |
| | | /// <param name="requestUrl">请求Url</param> |
| | | /// <param name="byteData">请求的的数据</param> |
| | | /// <param name="requestMethod">POST 或者 GET 等等</param> |
| | | /// <param name="timeout">超时,默认5秒</param> |
| | | /// <returns>得到响应的数据</returns> |
| | | public byte[] RequestHttpsZigbeeBytesResultAsync(string requestUrl, byte[] byteData, string requestMethod = "POST", int timeout = 5) |
| | | { |
| | | //请求Url的完成路径 |
| | | var fullUrl = $"{RequestHttpsHost}/{requestUrl}"; |
| | | return this.DoRequestZigbeeHttpsInterface(fullUrl, byteData, Config.Instance.Token, requestMethod, timeout); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// <pra>请求服务器方法,指定一个Url,和请求方法,数据,Cookie,得到响应的数据</pra> |
| | | /// <pra>注意!!此方法仅限【拥有管理员权限的成员】调用</pra> |
| | | /// </summary>
|
| | | /// <param name="requestUrl">请求Url</param> |
| | | /// <param name="byteData">请求的的数据</param> |
| | | /// <param name="requestMethod">POST 或者 GET 等等</param> |
| | | /// <param name="timeout">超时,默认5秒</param> |
| | | /// <returns>得到响应的数据</returns> |
| | | public byte[] RequestZigbeeHttpsByAdmin(string requestUrl, byte[] byteData, string requestMethod = "POST", int timeout = 5) |
| | | {
|
| | | //请求Url的完成路径 |
| | | var fullUrl = $"{Config.Instance.AdminRequestBaseUrl}/{requestUrl}";
|
| | | return this.DoRequestZigbeeHttpsInterface(fullUrl, byteData, Config.Instance.AdminRequestToken, requestMethod, timeout); |
| | | }
|
| | |
|
| | | /// <summary> |
| | | /// 请求服务器方法 |
| | | /// 指定一个Url,和请求方法,数据,Cookie,得到响应的数据 |
| | | /// </summary> |
| | | /// <param name="requestFullUrl">请求Url的完成路径</param> |
| | | /// <param name="byteData">请求的的数据</param> |
| | | /// <param name="token">token</param> |
| | | /// <param name="requestMethod">POST 或者 GET 等等</param> |
| | | /// <param name="timeout">超时,默认5秒</param> |
| | | /// <returns>得到响应的数据</returns> |
| | | public byte[] DoRequestZigbeeHttpsInterface(string requestFullUrl, byte[] byteData, string token, string requestMethod = "POST", int timeout = 5) |
| | | {
|
| | | try |
| | | { |
| | | //初始化新的webRequst |
| | | //1. 创建httpWebRequest对象 |
| | | HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri(requestFullUrl));
|
| | | |
| | | //2. 初始化HttpWebRequest对象 |
| | | webRequest.Method = requestMethod; |
| | | webRequest.Timeout = timeout * 1000; |
| | | //取消使用代理访问 |
| | | webRequest.Proxy = null; |
| | | webRequest.UseDefaultCredentials = false; |
| | | if (token != null)
|
| | | {
|
| | | //对应微信网址
|
| | | webRequest.Headers.Add(HttpRequestHeader.Authorization, token);
|
| | | } |
| | | if (requestMethod == "GET") |
| | | { |
| | | //webRequest.ContentType = "text/html"; |
| | | webRequest.ContentType = "application/json"; |
| | | } |
| | | else |
| | | { |
| | | webRequest.ContentType = "application/json"; |
| | | webRequest.ContentLength = byteData.Length; |
| | | |
| | | //3. 附加要POST给服务器的数据到HttpWebRequest对象(附加POST数据的过程比较特殊,它并没有提供一个属性给用户存取,需要写入HttpWebRequest对象提供的一个stream里面。) |
| | | var newStream = webRequest.GetRequestStream();//创建一个Stream,赋值是写入HttpWebRequest对象提供的一个stream里面 |
| | | newStream.Write(byteData, 0, byteData.Length); |
| | | newStream.Flush(); |
| | | newStream.Close(); |
| | | } |
| | | |
| | | //4. 读取服务器的返回信息 |
| | | var response = (HttpWebResponse)webRequest.GetResponse(); |
| | | using (var stream = response.GetResponseStream()) |
| | | { |
| | | if (stream == null)
|
| | | { |
| | | return null; |
| | | } |
| | | var ms = new MemoryStream(); |
| | | var bytes = new byte[1024]; |
| | | var len = int.MaxValue; |
| | | while (stream.CanRead && 0 < len) |
| | | { |
| | | len = stream.Read(bytes, 0, bytes.Length); |
| | | ms.Write(bytes, 0, len); |
| | | } |
| | | return ms.ToArray(); |
| | | } |
| | | } |
| | | catch (WebException e) |
| | | {
|
| | | if (e.Status == WebExceptionStatus.ConnectFailure//由有网络切换到无网络时触发
|
| | | || e.Status == WebExceptionStatus.Timeout//超时
|
| | | || e.Status == WebExceptionStatus.NameResolutionFailure)//本身就是无网络时启动时触发
|
| | | {
|
| | | Phone.UserCenter.HdlWifiLogic.Current.CanAccessHttp = false;
|
| | | }
|
| | | return null;
|
| | | } |
| | | catch (Exception e) |
| | | { |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 服务器获取数据失败提示 |
| | | /// </summary> |
| | | public void FailureToServer() |
| | | { |
| | | Application.RunOnMainThread(() => |
| | | { |
| | | var alert = new Alert(Language.StringByID(R.MyInternationalizationString.TIP), Language.StringByID(R.MyInternationalizationString.RequestServerFailed), Language.StringByID(R.MyInternationalizationString.Confrim)); |
| | | alert.Show(); |
| | | }); |
| | | } |
| | | /// <summary> |
| | | /// 显示错误提示 |
| | | /// </summary> |
| | | /// <param name="errorStringID">Error string identifier.</param> |
| | | public void ShowErrorInfoAlert(int errorStringID) |
| | | { |
| | | Application.RunOnMainThread(() => |
| | | { |
| | | var alert = new Alert(Language.StringByID(R.MyInternationalizationString.TIP), Language.StringByID(errorStringID), Language.StringByID(R.MyInternationalizationString.Confrim)); |
| | | alert.Show(); |
| | | }); |
| | | } |
| | | /// <summary> |
| | | /// Get服务器方法1 |
| | | /// </summary> |
| | | public string GetMethod(string requestUrl) |
| | | { |
| | | string requestMethod = "get"; |
| | | string urlHead = $"{RequestHttpsHost}/FeerView/"; |
| | | string url = urlHead + requestUrl; |
| | | HttpWebRequest httpWebRequestObj = WebRequest.CreateHttp(url); |
| | | httpWebRequestObj.Method = requestMethod; |
| | | using (HttpWebResponse httpWebResponseObj = httpWebRequestObj.GetResponse() as HttpWebResponse) |
| | | { |
| | | if (httpWebResponseObj == null) |
| | | { |
| | | return null; |
| | | } |
| | | else |
| | | { |
| | | Stream ResponseStream = httpWebResponseObj.GetResponseStream(); |
| | | using (StreamReader Sr = new StreamReader(ResponseStream)) |
| | | { |
| | | string responseString = Sr.ReadToEnd(); |
| | | return responseString; |
| | | } |
| | | } |
| | | } |
| | | }
|
| | |
|
| | | /// <summary>
|
| | | /// 登出
|
| | | /// </summary>
|
| | | public void SingOut()
|
| | | {
|
| | | try
|
| | | {
|
| | | Application.RunOnMainThread(() =>
|
| | | {
|
| | | //设定一个时间
|
| | | Config.Instance.LoginDateTime = new DateTime(1970, 1, 1);
|
| | | Config.Instance.Save();
|
| | | CommonPage.Instance.RemoveAll();
|
| | | CommonPage.Instance.Show();
|
| | | var login = new Phone.Login.AccountLoginForm();
|
| | | CommonPage.Instance.AddChidren(login);
|
| | | login.ShowForm();
|
| | | });
|
| | | }
|
| | | catch { }
|
| | | } |
| | | |
| | | /// <summary> |
| | | /// 退出登录时调用,清除推送数据 |
| | | /// </summary> |
| | | public void SignOutClearData() |
| | | { |
| | | try |
| | | { |
| | | System.Threading.Tasks.Task.Run(() => |
| | | { |
| | | using (var client = new System.Net.Http.HttpClient()) |
| | | { |
| | | client.DefaultRequestHeaders.Add("ContentType", "application/json"); |
| | | client.DefaultRequestHeaders.Add("Authorization", Shared.Common.Config.Instance.Token); |
| | | var responseString = client.GetStringAsync($"{CommonPage.RequestHttpsHost}/ZigbeeUsers/SignOut"); |
| | | } |
| | | }); |
| | | } |
| | | catch { } |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 登录成功后上报设备id等相关信息给后端 |
| | | /// </summary> |
| | | /// <returns>返回状态码</returns> |
| | | public string PushRegID() |
| | | { |
| | | #if DEBUG |
| | | //调试不需要发送这个东西 |
| | | return null; |
| | | #endif |
| | | try |
| | | { |
| | | string deviceAlias; |
| | | string deviceType; |
| | | #if iOS |
| | | deviceAlias = UIKit.UIDevice.CurrentDevice.Name; |
| | | deviceType = "iphone"; |
| | | #elif Android |
| | | deviceAlias = Android.OS.Build.Manufacturer; |
| | | deviceType = "android";
|
| | | #endif
|
| | | |
| | | var RegIDObj = new SendDataToServer.SignZigbeeNeedPushRegIDObj() |
| | | { |
| | | RegID = Shared.Common.Config.Instance.RegistrationID, |
| | | DeviceAlias = deviceAlias, |
| | | DeviceType = deviceType |
| | | }; |
| | | var requestJson = Newtonsoft.Json.JsonConvert.SerializeObject(RegIDObj); |
| | | var reverObj = CommonPage.Instance.RequestHttpsZigbeeAsync("ZigbeeUsers/SignZigbeeNeedPushRegID", System.Text.Encoding.UTF8.GetBytes(requestJson), 4); |
| | | if (reverObj == null) |
| | | {
|
| | | //调试:记录极光ID
|
| | | Phone.UserCenter.HdlLogLogic.Current.WriteOtherText(Shared.Phone.UserCenter.DirNameResourse.JiguangFile, "send:" + RegIDObj.RegID + " fail,receive obj is null", false, true); |
| | | return null; |
| | | }
|
| | | //调试:记录极光ID
|
| | | Phone.UserCenter.HdlLogLogic.Current.WriteOtherText(Shared.Phone.UserCenter.DirNameResourse.JiguangFile, "send:" + RegIDObj.RegID + " result:" + reverObj.StateCode, false, true); |
| | | |
| | | return reverObj.StateCode; |
| | | } |
| | | catch |
| | | { |
| | | return null; |
| | | } |
| | | } |
| | | } |
| | | } |