using System;
using Shared.Common;
namespace Shared.Phone.UserView
{
///
/// Home page.第二版呀
///
public class HomePage : PageLayout
{
static HomePage homePage;
///
/// Gets the instance.
///
/// The instance.
public static HomePage Instance
{
get
{
if (homePage == null)
{
homePage = new HomePage { IsShowPoint = false };
}
return homePage;
}
}
///
/// Inits the page.
///
public void InitPage()
{
if (0 < ChildrenCount)
{
return;
}
AddChidren(UserPage.Instance);
UserPage.Instance.InitPage();
this.PageChange += (sender, e) =>
{
while (e < this.ChildrenCount - 1)
{
this.GetChildren(this.ChildrenCount - 1).RemoveFromParent();
}
if (e == 0)
{
this.ScrollEnabled = true;
}
};
//*******待改*****
UserPage.Instance.Fresh();
if (Config.Instance.IsLogin)
{
CommonPage.Loading.Start();
Action action = async () =>
{
//CommonPage.Loading.Start(Language.StringByID(R.MyInternationalizationString.Logining));
var loginSuccess = await LoginByPWDResultAsync(Config.Instance.Account, Config.Instance.Password);
if (loginSuccess == 1)
{
new System.Threading.Thread(async () =>
{
//启动ZigBee
ZigBee.Common.Application.Init();
//登录成功
var homes = await House.GetHomeLists();
//刷新个人中心的内存及线程
await UserCenter.UserCenterLogic.InitUserCenterMenmoryAndThread();
Room.CanInitAllRoom = true;
Room.InitAllRoom();
Application.RunOnMainThread(() =>
{
UserPage.Instance.Fresh();
CommonPage.Loading.Hide();
});
})
{ IsBackground = true }.Start();
}
else
{
//未登录成功弹出登录界面 同时需要标记为未登录状态
//Config.Instance.LoginDateTime = new DateTime(1970, 1, 1);
//Config.Instance.Save();
//var accountLogin = new Device.Account.AccountLogin { };
//CommonPage.Instance.AddChidren(accountLogin);
//accountLogin.Show();
//CommonPage.Loading.Hide();
//登录失败,也直接进入主页
new System.Threading.Thread(async () =>
{
//启动ZigBee
ZigBee.Common.Application.Init();
//登录成功
var homes = await House.GetHomeLists();
//刷新个人中心的内存及线程
await UserCenter.UserCenterLogic.InitUserCenterMenmoryAndThread();
Room.CanInitAllRoom = true;
Room.InitAllRoom();
Application.RunOnMainThread(() =>
{
UserPage.Instance.Fresh();
CommonPage.Loading.Hide();
});
})
{ IsBackground = true }.Start();
}
};
//登录过直接后台登录
action();
}
else
{
//未登录弹出登录界面
var accountLogin = new Device.Account.AccountLogin { };
Shared.Common.CommonPage.Instance.AddChidren(accountLogin);
accountLogin.Show();
}
}
///
/// Logins the by PWDA sync.
///
/// The by PWDA sync.
/// Account.
/// Password.
/// Source.
/// Company.
public async System.Threading.Tasks.Task LoginByPWDAsync(string account, string password, string source = "", int company = 0)
{
var isLoginSuccess = -1;
try
{
var requestObj = new SendDataToServer.LoginObj
{
Account = account,
Password = password,
Source = source,
Company = 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)
{
isLoginSuccess = -1;
return isLoginSuccess;
}
var stateCodeStr = revertObj.StateCode.ToUpper();
//Error 不能直接从服务器取,只能根据状态码逐一判断
if (stateCodeStr == "SUCCESS")
{
if (revertObj.ResponseData == null)
{
return -1;
}
//初始化管理员权限信息(里面有特殊判断了,可以直接调用)
await UserCenter.UserCenterLogic.InitAdminConnectMqttInfo();
var responseDataObj = Newtonsoft.Json.JsonConvert.DeserializeObject(revertObj.ResponseData.ToString());
var revertData = responseDataObj;
Config.Instance.Account = revertData.Account;
Config.Instance.Password = password;
Config.Instance.MD5PWD = revertData.MD5PWD;
Config.Instance.Guid = revertData.Guid;
Config.Instance.MqttKey = revertData.MqttKey;
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);
}
isLoginSuccess = 1;
var resultRegID = await Shared.Common.CommonPage.Instance.PushRegID();
System.Console.WriteLine($"后台登录成功--{Config.Instance.LoginDateTime}");
}
else if (stateCodeStr == "YOUDATANOISLOCALREGION")
{
isLoginSuccess = -1;
//不在本区域,需要重定向区域后再次请求登录
if (revertObj.ResponseData == null)
{
return isLoginSuccess;
}
var responseDataObj = Newtonsoft.Json.JsonConvert.DeserializeObject(revertObj.ResponseData.ToString());
CommonPage.RequestHttpsHost = responseDataObj.RegionServer;
}
else
{
isLoginSuccess = -1;
}
}
catch
{
isLoginSuccess = -1;
}
return isLoginSuccess;
}
///
/// 后台登录
/// -1 失败 1 成功
///
/// The by PWDA sync.
/// Account.
/// Password.
/// Source.
/// Company.
public async System.Threading.Tasks.Task LoginByPWDResultAsync(string account, string password, string source = "", int company = 0)
{
var isLoginSuccess = await LoginByPWDAsync(account, password, source, company);
return isLoginSuccess;
}
}
}