using System; using System.Collections.Generic; using System.Net; using System.Text; using uPLibrary.Networking.M2Mqtt; using Shared.Common; using Shared.IO; using Shared.Phone.UserView; using System.Net.Http; namespace Shared.Phone.Device.Account { /// /// 登录界面 /// public class AccountLogin : FrameLayout { #region ◆ 变量____________________________ /// /// 背景视图 /// private FrameLayout midFrameLayout; /// /// 注册按钮 /// private Button registerBtn; /// /// 账号输入控件 /// private EditText accountET; /// /// 密码输入控件 /// private EditText passwrodET; /// /// 登录按钮 /// private Button loginBtn; /// /// 隐藏密码 /// private Button hidenPWDbtn; /// /// 登录错误提示按钮 /// private Button loginErrorBtn; /// /// 忘记密码按钮 /// private Button forgotPasswordBtn; /// /// 验证码登录按钮 /// private Button loginByCodeBtn; #endregion #region ◆ 构造方法_________________________ /// /// 构造方法 /// public AccountLogin() { BackgroundColor = ZigbeeColor.Current.GXCBackgroundColor; } #endregion #region ◆ 显示界面_________________________ /// /// 显示界面 /// /// Account. /// Password. public void Show(string account = "", string password = "") { if (Config.Instance.IsLogin) { } else { LoginViewShow(account, password); } } /// /// Logins the view show. /// /// Account. /// Password. public void LoginViewShow(string account = "", string password = "") { #region midFrameLayout midFrameLayout = new FrameLayout() { Width = LayoutParams.MatchParent, Height = Application.GetRealHeight(CommonPage.AppRealHeight), //Y = Application.GetRealHeight(36), BackgroundColor =ZigbeeColor.Current.GXCBackgroundColor }; AddChidren(midFrameLayout); #endregion #region 布局账户,密码,忘记密码,注册 手机验证码登录 registerBtn = new Button() { Width = Application.GetRealWidth(250), Height = Application.GetRealHeight(80), X = Application.GetRealWidth(CommonPage.AppRealWidth-250-50), Y = Application.GetRealHeight(100), TextID = R.MyInternationalizationString.Register, TextSize = 15, TextColor = ZigbeeColor.Current.GXCTextBlueColor, TextAlignment = TextAlignment.CenterRight }; midFrameLayout.AddChidren(registerBtn); var logoBtn = new Button() { Width = Application.GetMinRealAverage(270), Height = Application.GetMinRealAverage(270), Y = Application.GetRealHeight(150)+registerBtn.Bottom, UnSelectedImagePath = "Logo/Logo.png", Gravity=Gravity.CenterHorizontal }; midFrameLayout.AddChidren(logoBtn); var accountFL = new FrameLayout() { Width = Application.GetRealWidth(700), Height = Application.GetRealHeight(110), Y = Application.GetRealHeight(200) + logoBtn.Bottom, Radius = (uint)Application.GetRealHeight(55), BorderColor = ZigbeeColor.Current.GXCBorderColor, BorderWidth = 1, Gravity = Gravity.CenterHorizontal }; midFrameLayout.AddChidren(accountFL); accountET = new EditText() { X=Application.GetRealWidth(20), Width = Application.GetRealWidth(660), PlaceholderText = Language.StringByID(R.MyInternationalizationString.PleaseInputPhoneNumberOrEmail), TextAlignment = TextAlignment.Center, PlaceholderTextColor = ZigbeeColor.Current.GXCPlaceHolderTextColor, TextColor = ZigbeeColor.Current.GXCTextBlackColor, Text=account }; accountFL.AddChidren(accountET); var passwordFL = new FrameLayout() { Width = Application.GetRealWidth(700), Height = Application.GetRealHeight(110), Y = accountFL.Bottom + Application.GetRealHeight(100), Radius = (uint)Application.GetRealHeight(55), BorderColor = ZigbeeColor.Current.GXCBorderColor, BorderWidth = 1, Gravity = Gravity.CenterHorizontal }; midFrameLayout.AddChidren(passwordFL); passwrodET = new EditText() { Width = Application.GetRealWidth(450), PlaceholderText = Language.StringByID(R.MyInternationalizationString.PleaseInputPWD), TextAlignment = TextAlignment.Center, SecureTextEntry = true, PlaceholderTextColor = ZigbeeColor.Current.GXCPlaceHolderTextColor, TextColor = ZigbeeColor.Current.GXCTextBlackColor, Text=password, Gravity=Gravity.CenterHorizontal }; passwordFL.AddChidren(passwrodET); hidenPWDbtn = new Button() { X = passwrodET.Right + Application.GetRealWidth(20), Width = Application.GetMinReal(72), Height = Application.GetMinReal(72), UnSelectedImagePath = "Account/HidenPWD.png", SelectedImagePath = "Account/UnHidenPWD.png", Gravity=Gravity.CenterVertical }; passwordFL.AddChidren(hidenPWDbtn); forgotPasswordBtn = new Button() { Width = Application.GetRealWidth(400), Height = Application.GetRealHeight(110), Y = passwordFL.Bottom + Application.GetRealHeight(100), TextID = R.MyInternationalizationString.ForgotPassword, TextSize = 15, TextColor = Shared.Common.ZigbeeColor.Current.GXCTextBlackColor, TextAlignment = TextAlignment.Center, Gravity=Gravity.CenterHorizontal }; midFrameLayout.AddChidren(forgotPasswordBtn); loginBtn = new Button() { Width = Application.GetRealWidth(700), Height = Application.GetRealHeight(110), Y = Application.GetRealHeight(100) + forgotPasswordBtn.Bottom, TextID = R.MyInternationalizationString.Login, TextSize = 15, TextColor = Shared.Common.ZigbeeColor.Current.GXCTextWhiteColor, Radius = (uint)Application.GetRealHeight(55), BorderWidth = 0, BackgroundColor = ZigbeeColor.Current.GXCButtonUnselectedColor, SelectedBackgroundColor = ZigbeeColor.Current.GXCButtonBlueColor, Gravity = Gravity.CenterHorizontal, Enable = !string.IsNullOrEmpty(passwrodET.Text), IsSelected = !string.IsNullOrEmpty(passwrodET.Text) }; midFrameLayout.AddChidren(loginBtn); //错误提示Btn loginErrorBtn = new Button() { Y=loginBtn.Bottom+Application.GetRealHeight(50), Width=Application.GetRealWidth(1080-100), Height=Application.GetRealHeight(110), TextColor=ZigbeeColor.Current.GXCRedColor, Gravity=Gravity.CenterHorizontal }; midFrameLayout.AddChidren(loginErrorBtn); loginByCodeBtn = new Button() { Width = Application.GetRealWidth(300), Height = Application.GetRealHeight(110), Y = midFrameLayout.Bottom - Application.GetRealHeight(100+110), TextID = R.MyInternationalizationString.LoginByCode, TextColor = ZigbeeColor.Current.GXCTextBlueColor, TextAlignment = TextAlignment.Center, TextSize = 15, Gravity = Gravity.CenterHorizontal }; midFrameLayout.AddChidren(loginByCodeBtn); #endregion #region event passwrodET.EditorEnterAction = (ae) => { Application.HideSoftInput(); }; BindEvent(); #endregion } #endregion #region ◆ 绑定事件_________________________ /// /// 绑定按钮事件 /// private void BindEvent() { //登录事件 loginBtn.MouseUpEventHandler += LoginBtnEvent; //短信登录 loginByCodeBtn.MouseUpEventHandler += LoginByCode; //忘记密码 forgotPasswordBtn.MouseUpEventHandler += ForgetPWD; //注册事件 registerBtn.MouseUpEventHandler += Register; //账号监听 accountET.TextChangeEventHandler += Account_TextChange; //密码监听事件 passwrodET.TextChangeEventHandler += Pwd_TextChange; //隐藏密码 hidenPWDbtn.MouseUpEventHandler += HidePWD; } #endregion #region ◆ 登录____________________________ /// /// 登录事件 /// /// Sender. /// The ${ParameterType} instance containing the event data. private void LoginBtnEvent(object sender,MouseEventArgs mouseEventArgs) { Login(accountET.Text.Trim(), passwrodET.Text.Trim()); } /// /// 登录 /// /// Account. /// Password. private void Login(string account, string password) { //Application.RunOnMainThread( () => //{ Action action = async () => { try { CommonPage.Loading.Start(Language.StringByID(R.MyInternationalizationString.Logining)); var requestObj = new SendDataToServer.LoginObj { Account = account, Password = password, Source = CommonPage.Source, Company = CommonPage.Company }; var requestJson = Newtonsoft.Json.JsonConvert.SerializeObject(requestObj); var revertObj = await CommonPage.Instance.RequestHttpsZigbeeAsync("ZigbeeUsers/Login", System.Text.Encoding.UTF8.GetBytes(requestJson)); if (revertObj == null) { CommonPage.Instance.FailureToServer(); return; } var stateCodeStr = revertObj.StateCode.ToUpper(); //Error 不能直接从服务器取,只能根据状态码逐一判断 if (stateCodeStr == "SUCCESS") { if (revertObj.ResponseData == null) { return; } this.RemoveFromParent(); UserPage.Instance.Fresh(); new System.Threading.Thread(async () => { //启动ZigBee ZigBee.Common.Application.Init(); //存储数据 var responseDataObj = Newtonsoft.Json.JsonConvert.DeserializeObject(revertObj.ResponseData.ToString()); var revertData = responseDataObj; Config.ReFresh(); Config.Instance.UserID = revertData.UserID; Config.Instance.MasterID = revertData.MainUserID; Config.Instance.AccountType = revertData.UserType; Config.Instance.Account = revertData.Account; Config.Instance.MqttKey = revertData.MqttKey; Config.Instance.Password = password; Config.Instance.Guid = revertData.Guid; Config.Instance.LoginDateTime = DateTime.Now; Config.Instance.ConnectZigbeeMqttBrokerPwd = revertData.ConnectZigbeeMqttBrokerPwd; Config.Instance.ConnectZigbeeMqttClientId = revertData.ConnectZigbeeMqttClientId; Config.Instance.ZigbeeMqttBrokerLoadSubDomain = revertData.ZigbeeMqttBrokerLoadSubDomain; if (Config.Instance.AccountList.Find((obj) => obj == revertData.Account) == null) { Config.Instance.AccountList.Add(revertData.Account); } Config.Instance.Save(); var resultRegID = await Shared.Common.CommonPage.Instance.PushRegID(); var homes = await House.GetHomeLists(); //刷新个人中心的内存及线程 await UserCenter.UserCenterLogic.InitUserCenterMenmoryAndThread(); Shared.Common.Room.canInitAllRoom = true; Shared.Common.Room.InitAllRoom(); Application.RunOnMainThread(() => { UserPage.Instance.Fresh(); CommonPage.Loading.Hide(); }); }) { IsBackground = true }.Start(); } else if (stateCodeStr == "NOTVERIFY") { CommonPage.Loading.Hide(); //未激活 loginErrorBtn.TextID = R.MyInternationalizationString.NOTVERIFY; CommonPage.Instance.ShowErrorInfoAlert(R.MyInternationalizationString.NOTVERIFY); } else if (stateCodeStr == "NOTENABLE") { CommonPage.Loading.Hide(); //该用户属于调试账号,并未启用 loginErrorBtn.TextID = R.MyInternationalizationString.NOTENABLE; CommonPage.Instance.ShowErrorInfoAlert(R.MyInternationalizationString.NOTENABLE); } else if (stateCodeStr == "USERNAMEORPWDERROR") { CommonPage.Loading.Hide(); //账号或密码错误 loginErrorBtn.TextID = R.MyInternationalizationString.USERNAMEORPWDERROR; CommonPage.Instance.ShowErrorInfoAlert(R.MyInternationalizationString.USERNAMEORPWDERROR); } else if (stateCodeStr == "ACCOUNTNOEXISTS") { CommonPage.Loading.Hide(); //账号不存在 loginErrorBtn.TextID = R.MyInternationalizationString.ACCOUNTNOEXISTS; CommonPage.Instance.ShowErrorInfoAlert(R.MyInternationalizationString.ACCOUNTNOEXISTS); } else if (stateCodeStr == "YOUDATANOISLOCALREGION") { CommonPage.Loading.Hide(); //不在本区域,需要重定向区域后再次请求登录 if (revertObj.ResponseData == null) { return; } var responseDataObj = Newtonsoft.Json.JsonConvert.DeserializeObject(revertObj.ResponseData.ToString()); CommonPage.RequestHttpsHost = responseDataObj.RegionServer; //再次登录 Login(account, password); } else { loginErrorBtn.TextID = R.MyInternationalizationString.RequestServerFailed; CommonPage.Instance.ShowErrorInfoAlert(R.MyInternationalizationString.RequestServerFailed); } } catch (Exception ex) { System.Console.WriteLine($"登录失败--{ex.Message}"); CommonPage.Loading.Hide(); CommonPage.Instance.FailureToServer(); } finally { //CommonPage.Loading.Hide(); } //}); }; action(); } /// /// 通过验证码登录 /// /// Sender. /// The ${ParameterType} instance containing the event data. private void LoginByCode(object sender,MouseEventArgs mouseEventArgs) { this.RemoveFromParent(); var loginByCodePage = new AccountLoginByCode(); CommonPage.Instance.AddChidren(loginByCodePage); loginByCodePage.Show(); } #endregion #region ◆ 忘记密码________________________ /// /// 忘记密码 /// private void ForgetPWD(object sender, MouseEventArgs mouseEventArgs) { var forgetWPDDialog = new Dialog() { BackgroundColor = ZigbeeColor.Current.GXCDailogBackGroundColor }; forgetWPDDialog.Show(); var forgetSelectedView = new FrameLayout() { BackgroundColor = ZigbeeColor.Current.GXCTextWhiteColor, Gravity = Gravity.Center, Width = Application.GetRealWidth(700), Height = Application.GetRealHeight(700), Radius = (uint)Application.GetRealHeight(30), }; forgetWPDDialog.AddChidren(forgetSelectedView); var forgetByPhone = new Button() { Y = Application.GetRealHeight(100), Width = Application.GetRealWidth(600), Height = Application.GetRealHeight(100), TextID = R.MyInternationalizationString.FindPWDByPhone, TextColor = ZigbeeColor.Current.GXCTextWhiteColor, TextAlignment = TextAlignment.Center, BackgroundColor = ZigbeeColor.Current.GXCButtonBlueColor, Radius = (uint)Application.GetRealHeight(50), Gravity = Gravity.CenterHorizontal }; forgetSelectedView.AddChidren(forgetByPhone); var forgetByEmail = new Button() { Y = Application.GetRealHeight(100) + forgetByPhone.Bottom, Width = Application.GetRealWidth(600), Height = Application.GetRealHeight(100), TextID = R.MyInternationalizationString.FindPWDByEmail, TextColor = ZigbeeColor.Current.GXCTextWhiteColor, TextAlignment = TextAlignment.Center, BackgroundColor = ZigbeeColor.Current.GXCButtonBlueColor, Radius = (uint)Application.GetRealHeight(50), Gravity = Gravity.CenterHorizontal }; forgetSelectedView.AddChidren(forgetByEmail); var cancleBtn = new Button() { Y = Application.GetRealHeight(100) + forgetByEmail.Bottom, Width = Application.GetRealWidth(300), Height = Application.GetRealHeight(100), TextID = R.MyInternationalizationString.Cancel, TextColor = ZigbeeColor.Current.GXCTextWhiteColor, TextAlignment = TextAlignment.Center, BackgroundColor = ZigbeeColor.Current.GXCButtonBlueColor, Radius = (uint)Application.GetRealHeight(50), Gravity = Gravity.CenterHorizontal }; forgetSelectedView.AddChidren(cancleBtn); forgetByPhone.MouseUpEventHandler += (sender1, e1) => { forgetWPDDialog.Close(); this.RemoveFromParent(); var forgot = new Account.ForgotPasswordByPhone(); CommonPage.Instance.AddChidren(forgot); forgot.Show(); }; forgetByEmail.MouseUpEventHandler += (sender1, e1) => { forgetWPDDialog.Close(); this.RemoveFromParent(); var forgot = new Account.ForgotPasswordByEmail(); CommonPage.Instance.AddChidren(forgot); forgot.Show(); }; cancleBtn.MouseUpEventHandler += (sender1, e1) => { forgetWPDDialog.Close(); }; } #endregion #region ◆ 注册_____________________________ /// /// 注册 /// /// Sender. /// The ${ParameterType} instance containing the event data. private void Register(object sender,MouseEventArgs mouseEventArgs) { this.RemoveFromParent(); var registerPage = new RegistrationPhoneOREmail(); Shared.Common.CommonPage.Instance.AddChidren(registerPage); registerPage.Show(); } #endregion #region ◆ 账号密码监听______________________ /// /// 账号监听 /// /// Sender. /// The ${ParameterType} instance containing the event data. private void Account_TextChange(object sender,string mouseEventArgs) { loginErrorBtn.Text = ""; } /// /// 密码监听事件 /// /// Sender. /// The ${ParameterType} instance containing the event data. private void Pwd_TextChange(object sender,string mouseEventArgs) { loginErrorBtn.Text = ""; if (1 <= passwrodET.Text.Trim().Length && passwrodET.Text.Trim().Length <= 16 && accountET.Text.Trim().Length > 0) { loginBtn.Enable = loginBtn.IsSelected = true; } else if (passwrodET.Text.Trim().Length > 16) { passwrodET.Text = mouseEventArgs.Remove(16); } else if (passwrodET.Text.Trim().Length > 16 && accountET.Text.Trim().Length > 0) { loginBtn.Enable = loginBtn.IsSelected = true; passwrodET.Text = mouseEventArgs.Remove(16); } else { loginBtn.Enable = loginBtn.IsSelected = false; } } #endregion #region ◆ 密码隐藏__________________________ private void HidePWD(object sender, MouseEventArgs mouseEventArgs) { passwrodET.SecureTextEntry = !passwrodET.SecureTextEntry; hidenPWDbtn.IsSelected = !hidenPWDbtn.IsSelected; } #endregion } }