Пример #1
0
        private async Task Login()
        {
            try
            {
                var saltResult = await CloudService.GetSalt(Email, CTSFactory.MakeCTS(100000).Token);

                saltResult.ParseAPIResult();
                if (!saltResult.IsSuccessful)
                {
                    throw new APIException(saltResult.ErrorMsg);
                }
                var saltObj = JsonObject.Parse(saltResult.JsonSrc);
                var salt    = JsonParser.GetStringFromJsonObj(saltObj, "Salt");
                if (string.IsNullOrEmpty(salt))
                {
                    throw new APIException();
                }
                var newPwd       = MD5.GetMd5String(Password);
                var newPwdInSalt = MD5.GetMd5String(newPwd + salt);
                var loginResult  = await CloudService.Login(Email, newPwdInSalt, CTSFactory.MakeCTS(100000).Token);

                loginResult.ParseAPIResult();
                if (!loginResult.IsSuccessful)
                {
                    throw new APIException(loginResult.ErrorMsg);
                }
                var loginObj    = JsonObject.Parse(loginResult.JsonSrc);
                var userObj     = loginObj["UserInfo"];
                var uid         = JsonParser.GetStringFromJsonObj(userObj, "uid");
                var accessToken = JsonParser.GetStringFromJsonObj(userObj, "access_token");
                if (uid.IsNotNullOrEmpty() && accessToken.IsNotNullOrEmpty())
                {
                    LocalSettingHelper.AddValue("uid", uid);
                    LocalSettingHelper.AddValue("access_token", accessToken);
                    LocalSettingHelper.AddValue("email", Email);
                    ShowLoginControl   = false;
                    MainVM.CurrentUser = new PlantSitterUser()
                    {
                        Email = Email
                    };
                    await MainVM.GetUserPlan();

                    await MainVM.RefreshAllSensor();
                }
            }
            catch (TaskCanceledException)
            {
                ToastService.SendToast("Connection time out");
            }
            catch (APIException e)
            {
                ToastService.SendToast(e.ErrorMessage.IsNotNullOrEmpty() ? e.ErrorMessage : "Fail to login");
            }
            catch (Exception e)
            {
                ToastService.SendToast("Fail to login");
                var task = ExceptionHelper.WriteRecordAsync(e, nameof(MainViewModel), nameof(Login));
            }
            finally
            {
                ShowLoading = Visibility.Collapsed;
            }
        }