gxc
2020-02-28 66a9965c44ecc32a6696abca876ab9d1cd091584
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
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)
            {
                ShowLoginLoadView();
                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 () =>
                        {
                            //登录成功
                            var homes = await House.GetHomeLists();
                            //刷新个人中心的内存及线程
                            await UserCenter.UserCenterLogic.InitUserCenterMenmoryAndThread();
                            //启动ZigBee
                            ZigBee.Common.Application.Init();
 
                            Room.InitAllRoom();
 
                            Application.RunOnMainThread(() =>
                            {
                                CommonPage.Instance.RemoveViewByTag("Login");
                                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 () =>
                        {
                            //登录成功
                            var homes = await House.GetHomeLists();
                            //刷新个人中心的内存及线程
                            await UserCenter.UserCenterLogic.InitUserCenterMenmoryAndThread();
                            //启动ZigBee
                            ZigBee.Common.Application.Init();
 
                            Room.InitAllRoom();
 
                            Application.RunOnMainThread(() =>
                            {
                                CommonPage.Instance.RemoveViewByTag("Login");
                                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();
            }
        }
 
 
        public void ShowLoginLoadView()
        {
            var loginLoad = new LoginLoading { };
            CommonPage.Instance.AddChidren(loginLoad);
            loginLoad.Show();
        }
 
        /// <summary>
        /// Logins the by PWDA sync.
        /// </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 async System.Threading.Tasks.Task<int> 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;
                    }
 
                    var responseDataObj = Newtonsoft.Json.JsonConvert.DeserializeObject<Shared.Common.ResponseEntity.UserLoginRes>(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.LoginDateTime = DateTime.Now;
                    Config.Instance.LoginToken = revertData.Token;
                    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<Shared.Common.ResponseEntity.UserLoginLocalRegionRes>(revertObj.ResponseData.ToString());
                    CommonPage.RequestHttpsHost = responseDataObj.RegionServer;
                }
                else
                {
                    isLoginSuccess = -1;
                }
            }
            catch
            {
                isLoginSuccess = -1;
            }
            return isLoginSuccess;
        }
 
        /// <summary>
        /// 后台登录
        /// -1 失败  1 成功
        /// </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 async System.Threading.Tasks.Task<int> LoginByPWDResultAsync(string account, string password, string source = "", int company = 0)
        {
            var isLoginSuccess = await LoginByPWDAsync(account, password, source, company);
            return isLoginSuccess;
        }
    }
}