示例#1
0
        private async void WebView_Navigating(object sender, WebNavigatedEventArgs e)
        {
            gridLoading.IsVisible = false;
            //if (e.Url.StartsWith(Configuration.AppConfig.API_IP, StringComparison.Ordinal))
            {
                try
                {
                    string queryString = e.Url.Replace(CallBackUrl, "");
                    Dictionary <string, string> keyValues = ParseQueryString(queryString);
                    if (keyValues.ContainsKey("code") == false)
                    {
                        return;
                    }

                    string code = keyValues["code"];
                    using (HttpClient client = new HttpClient())
                    {
                        HttpResponseMessage response = await client.GetAsync($"https://oauth.zaloapp.com/v3/access_token?app_id={ZALOAPPID}&app_secret={AppScret}&code={code}");

                        if (response.IsSuccessStatusCode)
                        {
                            HttpContent auContent = response.Content;
                            string      authBody  = await auContent.ReadAsStringAsync();

                            ZaloAuthReponse zaloAuthReponse = JsonConvert.DeserializeObject <ZaloAuthReponse>(authBody);


                            var profileResponse = await client.GetAsync($"https://graph.zalo.me/v2.0/me?access_token={zaloAuthReponse.access_token}&fields=id,birthday,name,gender,picture");

                            var profileBody = await profileResponse.Content.ReadAsStringAsync();

                            ZaloUser zaloUser = JsonConvert.DeserializeObject <ZaloUser>(profileBody);

                            MessagingCenter.Send <ZaloLogin, ZaloUser>(this, "ZaloLoginCallback", zaloUser);
                        }
                        else
                        {
                            await DisplayAlert("", "Lỗi hệ thống, vui lòng thử lại.", "Đóng");
                        }
                    }
                }
                catch (Exception ex)
                {
                    await DisplayAlert("", "Lỗi hệ thống, vui lòng thử lại." + Environment.NewLine + ex.Message, "Đóng");
                }
            }
        }
        private async void WebView_NavigatingSocialLinked(object sender, WebNavigatedEventArgs e)
        {
            //gridLoading.IsVisible = false;
            if (e.Url.StartsWith("https://bsdinsight.net", StringComparison.Ordinal))
            {
                try
                {
                    string queryString = e.Url.Replace(CallBackUrl, "");
                    Dictionary <string, string> keyValues = ParseQueryString(queryString);
                    if (keyValues.ContainsKey("code") == false)
                    {
                        return;
                    }

                    string code = keyValues["code"];
                    using (HttpClient client = new HttpClient())
                    {
                        HttpResponseMessage response = await client.GetAsync($"https://oauth.zaloapp.com/v3/access_token?app_id={ZALOAPPID}&app_secret={AppScret}&code={code}");

                        if (response.IsSuccessStatusCode)
                        {
                            HttpContent auContent = response.Content;
                            string      authBody  = await auContent.ReadAsStringAsync();

                            ZaloAuthReponse zaloAuthReponse = JsonConvert.DeserializeObject <ZaloAuthReponse>(authBody);


                            var profileResponse = await client.GetAsync($"https://graph.zalo.me/v2.0/me?access_token={zaloAuthReponse.access_token}&fields=id,birthday,name,gender,picture");

                            var profileBody = await profileResponse.Content.ReadAsStringAsync();

                            ZaloUser zaloUser = JsonConvert.DeserializeObject <ZaloUser>(profileBody);

                            var zlUser = new User()
                            {
                                ZaloId    = long.Parse(zaloUser.id),
                                FullName  = zaloUser.name,
                                Sex       = zaloUser.gender == "male" ? (short)0 : zaloUser.gender == "female" ? (short)1 : (short)-1,
                                Birthday  = DateTime.ParseExact(zaloUser.birthday, "dd/MM/yyyy", null),
                                AvatarUrl = zaloUser.picture.data.url
                            };

                            try
                            {
                                ApiResponse apiResponse = await ApiHelper.Put("api/auth/sociallinked", zlUser, true);

                                if (apiResponse.IsSuccess)
                                {
                                    var user = JsonConvert.DeserializeObject <User>(apiResponse.Content.ToString());
                                    UserLogged.SaveProfile(user);
                                    viewModel.IsZaloLoginVisible = false;
                                    await Application.Current.MainPage.DisplayAlert("", Language.lien_ket_zalo_thanh_cong, Language.dong);

                                    MessagingCenter.Send <SocialLinkedPage, bool>(this, "UpdateSocialLinked", true);
                                }
                                else
                                {
                                    throw new Exception(apiResponse.Message);
                                }
                            }
                            catch (Exception ex)
                            {
                                await Application.Current.MainPage.DisplayAlert("", ex.Message, Language.dong);

                                await Navigation.PopAsync();
                            }
                        }
                        else
                        {
                            await DisplayAlert("", Language.loi_he_thong_vui_long_thu_lai, Language.dong);

                            await this.Navigation.PopToRootAsync();
                        }
                    }
                }
                catch (Exception ex)
                {
                    await DisplayAlert("", Language.loi_he_thong_vui_long_thu_lai, Language.dong);

                    await this.Navigation.PopToRootAsync();
                }
            }
        }