Frm_Login.xaml.cs 2.85 KB
using HHWCS.Model;
using HHWCS.Model.ClientModel;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace HHWCSHost.View
{
    /// <summary>
    /// Frm_Login.xaml 的交互逻辑
    /// </summary>
    public partial class Frm_Login : Window
    {
        public Frm_Login()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //HttpClient httpClient = new HttpClient();
            //HttpContent content = new 
            //httpClient.PostAsync();
            AppCommon.User.UserName = txt_UserName.Text;
            Frm_Main frm_Main = new Frm_Main
            {
                WindowState = WindowState.Maximized
            };
            frm_Main.Show();
            this.Hide();
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown();
        }

        private async void Btn_Login_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                LoginModel loginModel = new LoginModel()
                {
                    Code = txt_UserName.Text,
                    Password = Password.Password
                };
                HttpContent content = new StringContent(JsonConvert.SerializeObject(loginModel));
                content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
                var ret = await AppCommon.Client.PostAsync("/mobile/login", content);
                if (ret.IsSuccessStatusCode)
                {
                    ret.EnsureSuccessStatusCode();
                    string temp = await ret.Content.ReadAsStringAsync();
                    var b = JsonConvert.DeserializeObject<ReturnModel<List<WarehouseModel>>>(temp);
                    if (b.Code == "200")
                    {
                        AppCommon.User.UserName = txt_UserName.Text;
                        Frm_Main frm_Main = new Frm_Main
                        {
                            WindowState = WindowState.Maximized
                        };
                        frm_Main.Show();
                        this.Hide();
                    }
                    else
                    {
                        MessageBox.Show(b.Msg);
                    }
                    
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("登录失败:" + ex.Message);
            }
        }
    }
}