Beispiel #1
0
        // 一个提交表单的http请求
        private async void signup_Handle()
        {
            string _nickname = nickname.Text;
            string _username = username.Text;
            string _password = password.Password;

            if (_nickname.Equals("") || _username.Equals("") || _password.Equals(""))
            {
                await new MessageDialog("不能为空").ShowAsync();
                return;
            }

            if (_nickname.Length > 10)
            {
                await new MessageDialog("昵称过长了哦,最长了10个字符").ShowAsync();
                return;
            }

            HttpClient client = new HttpClient();

            try
            {
                string url  = Config.config.prefix + "/user";
                var    user = new Model.user(_username, _nickname, "http://img1.famulei.com/b/929239/p/162/1000380952218.jpg", _password);
                var    temp = JsonConvert.SerializeObject(user);

                var content = new StringContent(temp);
                content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                var res = await client.PostAsync(url, content);

                string result = await res.Content.ReadAsStringAsync();

                var resJson = JObject.Parse(result);
                if (resJson["status"].ToString().Equals("200"))
                {
                    await new MessageDialog(resJson["message"].ToString()).ShowAsync();
                    Frame.Navigate(typeof(login));
                }
                else
                {
                    await new MessageDialog(resJson["message"].ToString()).ShowAsync();
                }
            }
            catch (Exception ex)
            {
                var i = new MessageDialog("怕是你没有连网噢").ShowAsync();
            }
        }
Beispiel #2
0
        // 登录的http请求
        private async void login_Handle()
        {
            string name = username.Text;
            string psd  = password.Password;

            if (name.Equals("") || psd.Equals(""))
            {
                var i = new MessageDialog("不能留空噢~").ShowAsync();
                return;
            }
            HttpClient client = new HttpClient();
            string     url    = Config.config.prefix + "/session";
            var        user   = new Model.user(name, "", null, psd);
            var        temp   = JsonConvert.SerializeObject(user);

            var content = new StringContent(temp);

            content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            HttpResponseMessage res;

            try
            {
                res = await client.PostAsync(url, content);
            }
            catch (Exception ex)
            {
                var i = new MessageDialog("怕是你没连网哦").ShowAsync();
                return;
            }
            string result = await res.Content.ReadAsStringAsync();

            var resJson = JObject.Parse(result);

            if (resJson["status"].ToString().Equals("200"))
            {
                await new MessageDialog(resJson["message"].ToString()).ShowAsync();

                success_Handle();
            }
            else
            {
                await new MessageDialog(resJson["message"].ToString()).ShowAsync();
            }
        }
        private async void getUser()
        {
            try
            {
                HttpClient client  = new HttpClient();
                var        _result = await client.GetAsync(Config.config.prefix + "/user");

                string _content = await _result.Content.ReadAsStringAsync();

                var obj = JObject.Parse(_content);
                VM = new Model.user(obj["username"].ToString(), obj["nickname"].ToString(), obj["avatar"].ToString(), "");
                avatar.ImageSource = new BitmapImage(new Uri(obj["avatar"].ToString()));
                name.Text          = obj["nickname"].ToString();
            }
            catch (Exception ex)
            {
                await new MessageDialog("怕是你没联网哦").ShowAsync();
            }
        }
        // 这里是修改昵称的http请求,昵称不能为空不能超过10个字符
        private async void edit_Click(object sender, RoutedEventArgs e)
        {
            string str = name.Text;

            if (str.Equals(""))
            {
                var i = new MessageDialog("不能为空啊~").ShowAsync();
            }
            else if (str.Length > 10)
            {
                var i = new MessageDialog("昵称别要太长,10个字符就好").ShowAsync();
            }
            else
            {
                try
                {
                    HttpClient client = new HttpClient();
                    string     url    = Config.config.prefix + "/editNickname";
                    var        user   = new Model.user(VM.username, str, "", "");
                    var        temp   = JsonConvert.SerializeObject(user);

                    var content = new StringContent(temp);
                    content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                    var res = await client.PostAsync(url, content);

                    string result = await res.Content.ReadAsStringAsync();

                    var obj = JObject.Parse(result);
                    if (obj["status"].ToString().Equals("200"))
                    {
                        await new MessageDialog("修改成功").ShowAsync();
                    }
                }
                catch (Exception ex)
                {
                    await new MessageDialog("怕是你没联网哦").ShowAsync();
                }
            }
        }
Beispiel #5
0
        // 登录成功的处理事件,将登录者信息存在用户的本地数据库
        private async void success_Handle()
        {
            var client  = new HttpClient();
            var _result = await client.GetAsync(Config.config.prefix + "/user");

            string _content = await _result.Content.ReadAsStringAsync();

            var obj = JObject.Parse(_content);



            using (var custstmt = App.conn.Prepare("INSERT INTO user (username, nickname, avatar, anchor) VALUES (?, ?, ?, ?)"))
            {
                custstmt.Bind(1, obj["username"].ToString());
                custstmt.Bind(2, obj["nickname"].ToString());
                custstmt.Bind(3, obj["avatar"].ToString());
                custstmt.Bind(4, "zhulifeng");
                custstmt.Step();
            }

            Model.user _user = new Model.user(obj["username"].ToString(), obj["nickname"].ToString(), obj["avatar"].ToString(), "");
            Frame.Navigate(typeof(MainPage));
        }
        // 这里是修改服务器数据库的头像url的http请求
        private async void edit_Avatar(string imageSrc)
        {
            try
            {
                HttpClient client  = new HttpClient();
                string     url     = Config.config.prefix + "/editAvatar";
                var        user    = new Model.user(VM.username, "", imageSrc, "");
                var        temp    = JsonConvert.SerializeObject(user);
                var        content = new StringContent(temp);
                content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                var res = await client.PostAsync(url, content);

                string result = await res.Content.ReadAsStringAsync();

                var obj = JObject.Parse(result);
                if (obj["status"].ToString().Equals("200"))
                {
                }
            }
            catch (Exception ex)
            {
                await new MessageDialog("怕是你没联网哦").ShowAsync();
            }
        }