HDL Home App 第二版本 旧平台金堂用 正在使用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
using System;
using Shared.Common;
 
namespace Shared.Phone.UserView
{
    /// <summary>
    /// Home page.第二版呀
    /// </summary>
    public class HomePage : PageLayout
    {
        static HomePage homePage;
        /// <summary>
        /// Gets the instance.
        /// </summary>
        /// <value>The instance.</value>
        public static HomePage Instance
        {
            get
            {
                if (homePage == null)
                {
                    homePage = new HomePage { IsShowPoint = false };
                }
                return homePage;
            }
        }
        /// <summary>
        /// Inits the page.
        /// </summary>
        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;
                }
            };
 
            //直接登录
            if (Config.Instance.IsLogin)
            {
                //显示启动页
                CommonPage.Loading.Start();
 
                UserCenter.HdlThreadLogic.Current.RunThread(() =>
                {
                    //登录
                    var loginSuccess = LoginByPWDAsync(Config.Instance.Account, Config.Instance.Password);
                    //登录成功,或者没有网络都可以登录
                    if (loginSuccess == 1 || loginSuccess == 2)
                    {
                        var homes = UserCenter.HdlResidenceLogic.Current.GetHomeListsFromDb(true);
                        //刷新个人中心的内存及线程
                        bool result = UserCenter.UserCenterLogic.InitUserCenterMenmoryAndThread();
                        //启动ZigBee
                        ZigBee.Common.Application.Init();
 
                        UserCenter.HdlThreadLogic.Current.RunMain(() =>
                        {
                            CommonPage.Instance.RemoveViewByTag("Login");
                            CommonPage.Loading.Hide();
                            if (result == true)
                            {
                                //false:开启了调试功能
                                UserPage.Instance.ReFreshControl();
                            }
                        });
                    }
                    else
                    {
                        //设定一个时间
                        Config.Instance.LoginDateTime = new DateTime(1970, 1, 1);
                        Config.Instance.Save();
 
                        UserCenter.HdlThreadLogic.Current.RunMain(() =>
                        {
                            CommonPage.Loading.Hide();
                            CommonPage.Instance.RemoveViewByTag("Login");
                            //登录弹出登录界面
                            var accountLogin = new Login.AccountLoginForm { };
                            Shared.Common.CommonPage.Instance.AddChidren(accountLogin);
                            accountLogin.ShowForm();
                        });
                    }
                });
            }
            else
            {
                //未登录弹出登录界面
                var accountLogin = new Login.AccountLoginForm { };
                Shared.Common.CommonPage.Instance.AddChidren(accountLogin);
                accountLogin.ShowForm();
            }
        }
 
        /// <summary>
        /// 1:登录成功 0:密码错误 -1:其他异常 2:无法联网
        /// </summary>
        /// <returns>The by PWDA sync.</returns>
        /// <param name="account">Account.</param>
        /// <param name="password">Password.</param>
        /// <param name="source">Source.</param>
        /// <param name="company">Company.</param>
        public int LoginByPWDAsync(string account, string password, string source = "", int company = 0)
        {
            var isLoginSuccess = -1;
            var requestObj = new SendDataToServer.LoginObj
            {
                Account = account,
                Password = password
            };
            var requestJson = Newtonsoft.Json.JsonConvert.SerializeObject(requestObj);
            //登陆接口特殊,需要快一点访问,设置3秒超时
            var revertObj = CommonPage.Instance.RequestHttpsZigbeeAsync("ZigbeeUsers/Login", System.Text.Encoding.UTF8.GetBytes(requestJson), 3);
            if (revertObj == null)
            {
                return 2;
            }
            var stateCodeStr = revertObj.StateCode.ToUpper();
            //Error 不能直接从服务器取,只能根据状态码逐一判断
            if (stateCodeStr == "SUCCESS")
            {
                var responseDataObj = Newtonsoft.Json.JsonConvert.DeserializeObject<Shared.Common.ResponseEntity.UserLoginRes>(revertObj.ResponseData.ToString());
                var revertData = responseDataObj;
                //标记上一次是不是同一个账号登陆
                Config.Instance.TheSameLoginAccount = Config.Instance.Guid == revertData.Guid;
                Config.Instance.Account = revertData.Account;
                Config.Instance.Password = password;
                Config.Instance.MD5PWD = revertData.MD5PWD;
                Config.Instance.Guid = revertData.Guid;
                Config.Instance.LoginDateTime = DateTime.Now;
                Config.Instance.LoginToken = revertData.Token;
                //上报设备ID
                CommonPage.Instance.PushRegID();
                isLoginSuccess = 1;
 
//#if iOS
//                //初始化全视通
//                Shared.IOS.HDLFVSDK.Video.Init("",revertData.UserID.ToString());
//#endif
            }
            else if (stateCodeStr == "YOUDATANOISLOCALREGION")
            {
                //不在本区域,需要重定向区域后再次请求登录
                var responseDataObj = Newtonsoft.Json.JsonConvert.DeserializeObject<Shared.Common.ResponseEntity.UserLoginLocalRegionRes>(revertObj.ResponseData.ToString());
                CommonPage.RequestHttpsHost = responseDataObj.RegionServer;
                //再次登录
                return this.LoginByPWDAsync(account, password, source, company);
            }
            if (stateCodeStr == "NOTVERIFY")
            {
                //未激活
            }
            else if (stateCodeStr == "NOTENABLE")
            {
                //该用户属于调试账号,并未启用
            }
            else if (stateCodeStr == "PARAMETEROREMPTY")
            {
                //提供的参数错误
            }
            else if (stateCodeStr == "USERNAMEORPWDERROR")
            {
                //账号或密码错误
                isLoginSuccess = 0;
            }
            else if (stateCodeStr == "ACCOUNTNOEXISTS")
            {
                //账号不存在
                isLoginSuccess = 0;
            }
            else if (stateCodeStr == "BERESTRICT")
            {
                //此账号无权登录
                isLoginSuccess = 0;
            }
            return isLoginSuccess;
        }
    }
}