Beispiel #1
0
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            SetContentView(Resource.Layout.activity_register);

            btn_register = FindViewById <Button>(Resource.Id.btn_register);
            s_first_name = FindViewById <EditText>(Resource.Id.s_first_name);
            s_last_name  = FindViewById <EditText>(Resource.Id.s_last_name);
            s_pass       = FindViewById <EditText>(Resource.Id.s_pass);
            s_pass_check = FindViewById <EditText>(Resource.Id.s_pass_check);
            s_email      = FindViewById <EditText>(Resource.Id.s_email);
            s_role_name  = FindViewById <Spinner>(Resource.Id.s_role_name);

            preloader = FindViewById <ProgressBar>(Resource.Id.loader);

            btn_back_a = FindViewById <ImageButton>(Resource.Id.btn_back_a);

            btn_back_a.Click += (s, e) =>
            {
                Finish();
            };

            s_role_name.Prompt        = "Выбор роли";
            s_role_name.ItemSelected += new EventHandler <AdapterView.ItemSelectedEventArgs>(Spinner_ItemSelected);
            var adapter = ArrayAdapter.CreateFromResource(this, Resource.Array.a_role, Android.Resource.Layout.SimpleSpinnerItem);

            adapter.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);
            s_role_name.Adapter = adapter;


            string dir_path = "/storage/emulated/0/Android/data/GeoGeometry.GeoGeometry/files/";

            btn_register.Click += async delegate
            {
                try
                {
                    preloader.Visibility = Android.Views.ViewStates.Visible;

                    RegisterModel register = new RegisterModel
                    {
                        FirstName       = s_first_name.Text,
                        LastName        = s_last_name.Text,
                        Email           = s_email.Text,
                        Password        = s_pass.Text,
                        PasswordConfirm = s_pass_check.Text,
                    };
                    if (s_section_role == "Водитель")
                    {
                        register.RoleName = "driver";
                    }
                    else if (s_section_role == "Пользователь")
                    {
                        register.RoleName = "user";
                    }
                    var myHttpClient = new HttpClient();
                    var uri          = new Uri(" http://iot-tmc-cen.1gb.ru/api/auth/register?email=" + register.Email + "&firstname=" + register.FirstName + "&lastname=" + register.LastName + "&password="******"&passwordconfirm=" + register.PasswordConfirm + "&rolename=" + register.RoleName);
                    //var uri = "../register?email="+register.Email+"&firstname="+register.FirstName+"&lastname="+register.LastName+"&password="******"&passwordconfirm=" + register.PasswordConfirm +"&rolename=" + register.RoleName;
                    var _authHeader = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.ASCII.GetBytes(string.Format("{0}:{1}", register.FirstName, register.LastName, register.Password, register.PasswordConfirm, register.Email, register.RoleName))));

                    /*
                     * //json структура.
                     * var formContent = new FormUrlEncodedContent(new Dictionary<string, string>
                     * {
                     *  { "FirstName", register.FirstName },
                     *  { "LastName", register.LastName },
                     *  { "Email", register.Email },
                     *  { "Password", register.Password },
                     *  { "PasswordConfirm", register.PasswordConfirm }
                     * });
                     */

                    HttpResponseMessage response = await myHttpClient.PostAsync(uri.ToString(), new StringContent(JsonConvert.SerializeObject(register), Encoding.UTF8, "application/json"));

                    string s_result;
                    using (HttpContent responseContent = response.Content)
                    {
                        s_result = await responseContent.ReadAsStringAsync();
                    }

                    AuthApiData <AuthResponseData> o_data = JsonConvert.DeserializeObject <AuthApiData <AuthResponseData> >(s_result);

                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        if (o_data.Status == "0")
                        {
                            Toast.MakeText(this, o_data.Message, ToastLength.Long).Show();

                            AuthResponseData o_user_data = new AuthResponseData();
                            o_user_data = o_data.ResponseData;

                            StaticUser.AddInfoAuth(o_user_data);

                            using (FileStream file = new FileStream(dir_path + "user_data.txt", FileMode.Create, FileAccess.Write))
                            {
                                // преобразуем строку в байты
                                byte[] array = Encoding.Default.GetBytes("0" + JsonConvert.SerializeObject(o_user_data));
                                // запись массива байтов в файл
                                file.Write(array, 0, array.Length);
                            }
                            if (register.RoleName == "driver")
                            {
                                Intent Driver = new Intent(this, typeof(Auth.DriverActivity));
                                StartActivity(Driver);
                                this.Finish();
                            }

                            /*else if(register.RoleName == "user")
                             *  {
                             *      Intent User = new Intent(this, typeof(Auth.UserActivity));
                             *      StartActivity(User);
                             *      this.Finish();
                             *  }
                             */
                            preloader.Visibility = Android.Views.ViewStates.Invisible;
                            // Переход на страницу выбора контейнера.
                            //Intent ContainerSelectionActivity = new Intent(this, typeof(Auth.ContainerSelection));
                            //StartActivity(ContainerSelectionActivity);
                            //this.Finish();
                        }
                        else
                        {
                            Toast.MakeText(this, o_data.Message, ToastLength.Long).Show();
                        }
                    }
                }
                catch (Exception ex)
                {
                    Toast.MakeText(this, "" + ex.Message, ToastLength.Long).Show();
                }
            };
        }
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            SetContentView(Resource.Layout.activity_register);

            btn_register = FindViewById <Button>(Resource.Id.btn_register);
            s_first_name = FindViewById <EditText>(Resource.Id.s_first_name);
            s_last_name  = FindViewById <EditText>(Resource.Id.s_last_name);
            s_pass       = FindViewById <EditText>(Resource.Id.s_pass);
            s_pass_check = FindViewById <EditText>(Resource.Id.s_pass_check);
            s_email      = FindViewById <EditText>(Resource.Id.s_email);

            preloader = FindViewById <ProgressBar>(Resource.Id.loader);

            btn_back_a = FindViewById <ImageButton>(Resource.Id.btn_back_a);

            btn_back_a.Click += (s, e) =>
            {
                Finish();
            };

            string dir_path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);

            btn_register.Click += async delegate
            {
                try
                {
                    preloader.Visibility = Android.Views.ViewStates.Visible;

                    RegisterModel register = new RegisterModel
                    {
                        FirstName       = s_first_name.Text,
                        LastName        = s_last_name.Text,
                        Email           = s_email.Text,
                        Password        = s_pass.Text,
                        PasswordConfirm = s_pass_check.Text,
                        RoleName        = "user",
                    };

                    var myHttpClient = new HttpClient();
                    var uri          = new Uri(" http://iot-tmc-cen.1gb.ru/api/auth/register?email=" + register.Email + "&firstname=" + register.FirstName + "&lastname=" + register.LastName + "&password="******"&passwordconfirm=" + register.PasswordConfirm + "&rolename=" + register.RoleName);
                    var _authHeader  = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.ASCII.GetBytes(string.Format("{0}:{1}", register.FirstName, register.LastName, register.Password, register.PasswordConfirm, register.Email, register.RoleName))));


                    HttpResponseMessage response = await myHttpClient.PostAsync(uri.ToString(), new StringContent(JsonConvert.SerializeObject(register), Encoding.UTF8, "application/json"));

                    string s_result;
                    using (HttpContent responseContent = response.Content)
                    {
                        s_result = await responseContent.ReadAsStringAsync();
                    }

                    AuthApiData <AuthResponseData> o_data = JsonConvert.DeserializeObject <AuthApiData <AuthResponseData> >(s_result);

                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        if (o_data.Status == "0")
                        {
                            Toast.MakeText(this, o_data.Message, ToastLength.Long).Show();

                            AuthResponseData o_user_data = new AuthResponseData();
                            o_user_data = o_data.ResponseData;

                            StaticUser.AddInfoAuth(o_user_data);

                            using (FileStream file = new FileStream(dir_path + "user_data.txt", FileMode.OpenOrCreate, FileAccess.Write))
                            {
                                // преобразуем строку в байты
                                byte[] array = Encoding.Default.GetBytes(JsonConvert.SerializeObject(o_user_data));
                                // запись массива байтов в файл
                                file.Write(array, 0, array.Length);
                            }


                            Intent Driver = new Intent(this, typeof(Auth.SensorParameters));
                            StartActivity(Driver);
                            this.Finish();

                            preloader.Visibility = Android.Views.ViewStates.Invisible;
                        }
                        else
                        {
                            Toast.MakeText(this, o_data.Message, ToastLength.Long).Show();
                        }
                    }
                }
                catch (Exception ex)
                {
                    Toast.MakeText(this, "" + ex.Message, ToastLength.Long).Show();
                }
            };
        }
Beispiel #3
0
        /// <summary>
        /// Mobile Box New
        /// </summary>
        /// <param name="savedInstanceState"></param>
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            SetContentView(Resource.Layout.activity_auth);

            btn_register = FindViewById <Button>(Resource.Id.btn_register);
            btn_auth     = FindViewById <Button>(Resource.Id.btn_auth);
            btn_back_a   = FindViewById <ImageButton>(Resource.Id.btn_back_a);

            s_login     = FindViewById <EditText>(Resource.Id.s_login);
            s_pass      = FindViewById <EditText>(Resource.Id.s_pass);
            is_remember = FindViewById <CheckBox>(Resource.Id.is_remember);
            preloader   = FindViewById <ProgressBar>(Resource.Id.loader);


            btn_back_a.Click += (s, e) =>
            {
                Finish();
            };



            string dir_path           = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
            string file_data_remember = "";
            // Проверяю запомнил ли пользователя.
            string check = CrossSettings.Current.GetValueOrDefault("check", "");

            if (check == "1")
            {
                s_login.Text = CrossSettings.Current.GetValueOrDefault("login", "");
                s_pass.Text  = CrossSettings.Current.GetValueOrDefault("password", "");
            }

            is_remember.Checked = true;



            //        if (file_data_remember.Substring(0, 1) == "1")
            //        {
            //preloader.Visibility = Android.Views.ViewStates.Visible;
            //file_data_remember = file_data_remember.Remove(0, 1);
            //            AuthResponseData o_data = JsonConvert.DeserializeObject<AuthResponseData>(file_data_remember);

            //            StaticUser.AddInfoAuth(o_data);

            //preloader.Visibility = Android.Views.ViewStates.Gone;

            //// Переход на главную страницу.
            //Intent homeActivity = new Intent(this, typeof(Home.HomeActivity));
            //            StartActivity(homeActivity);
            //            //this.Finish();
            //        }
            //        else
            //        {
            // Переход к форме регистрации.
            btn_register.Click += (s, e) =>
            {
                Intent registerActivity = new Intent(this, typeof(Auth.RegisterActivity));
                StartActivity(registerActivity);
            };

            btn_auth.Click += async delegate
            {
                try
                {
                    preloader.Visibility = Android.Views.ViewStates.Visible;
                    // Авторизируюсь клиентом.

                    AuthModel auth = new AuthModel
                    {
                        Email    = s_login.Text,
                        Password = s_pass.Text,
                    };



                    var myHttpClient = new HttpClient();
                    var _authHeader  = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.ASCII.GetBytes(string.Format("{0}:{1}", auth.Email, auth.Password))));

                    myHttpClient.DefaultRequestHeaders.Authorization = _authHeader;

                    var uri = new Uri("http://iot.tmc-centert.ru/api/auth/login?email=" + auth.Email + "&password="******"Email", auth.Email },
                     *  { "Password", auth.Password }
                     * });
                     */
                    // Поучаю ответ об авторизации [успех или нет]
                    HttpResponseMessage response = await myHttpClient.PostAsync(uri.ToString(), new StringContent(JsonConvert.SerializeObject(auth), Encoding.UTF8, "application/json"));

                    string s_result;
                    using (HttpContent responseContent = response.Content)
                    {
                        s_result = await responseContent.ReadAsStringAsync();
                    }

                    AuthApiData <AuthResponseData> o_data = JsonConvert.DeserializeObject <AuthApiData <AuthResponseData> >(s_result);


                    //ClearField();
                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        if (o_data.Status == "0")
                        {
                            Toast.MakeText(this, o_data.Message, ToastLength.Long).Show();

                            AuthResponseData o_user_data = new AuthResponseData();
                            o_user_data = o_data.ResponseData;

                            if (is_remember.Checked == true)
                            {
                                CrossSettings.Current.AddOrUpdateValue("check", "1");
                                CrossSettings.Current.AddOrUpdateValue("login", s_login.Text);
                                CrossSettings.Current.AddOrUpdateValue("password", s_pass.Text);
                            }
                            else
                            {
                                CrossSettings.Current.AddOrUpdateValue("check", "0");
                            }

                            StaticUser.Email = s_login.Text;
                            StaticUser.AddInfoAuth(o_user_data);

                            //пример ContainerSelection

                            //using (FileStream fs = new FileStream(dir_path + "user_data.txt", FileMode.OpenOrCreate))
                            //{
                            //    await System.Text.Json.JsonSerializer.SerializeAsync<AuthResponseData>(fs, o_user_data);
                            //}

                            using (FileStream file = new FileStream(dir_path + "user_data.txt", FileMode.OpenOrCreate, FileAccess.Write))
                            {
                                // преобразуем строку в байты
                                byte[] array = Encoding.Default.GetBytes(JsonConvert.SerializeObject(o_user_data));    // 0 связан с запоминанием
                                // запись массива байтов в файл
                                file.Write(array, 0, array.Length);
                            }

                            //var role = o_data.ResponseData.Role;
                            //Начинаю собирать информацию о клиенте
                            preloader.Visibility = Android.Views.ViewStates.Invisible;
                            // Переход на страницу водителя.
                            if (o_data.ResponseData.Role == "driver")
                            {
                                Intent Driver = new Intent(this, typeof(Auth.DriverActivity));
                                StartActivity(Driver);
                                this.Finish();
                            }
                            else if (o_data.ResponseData.Role == "user")
                            {
                                Intent UserActivity = new Intent(this, typeof(Auth.ActivityUserBox));
                                StartActivity(UserActivity);
                                this.Finish();
                            }
                        }
                        else
                        {
                            Toast.MakeText(this, o_data.Message, ToastLength.Long).Show();
                        }
                    }
                }
                catch (Exception ex)
                {
                    Toast.MakeText(this, "" + ex.Message, ToastLength.Long).Show();
                }
            };
        }