Login.xaml.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using DeviceCenter.model;
  2. using DeviceCenter.utils;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Windows;
  6. namespace DeviceCenter
  7. {
  8. /// <summary>
  9. /// Login.xaml 的交互逻辑
  10. /// </summary>
  11. public partial class Login : Window
  12. {
  13. private Config config = Config.getInstance();
  14. public Login()
  15. {
  16. InitializeComponent();
  17. this.MouseDown += delegate { DragMove(); };
  18. if (!string.IsNullOrEmpty(config.url))
  19. {
  20. tb_server_url.Text = config.url;
  21. }
  22. if (!string.IsNullOrEmpty(config.url))
  23. {
  24. tb_username.Text = config.username;
  25. }
  26. if (!string.IsNullOrEmpty(config.url))
  27. {
  28. tb_passowrd.Password = config.password;
  29. }
  30. }
  31. private void btn_login_Click(object sender, RoutedEventArgs e)
  32. {
  33. if (string.IsNullOrWhiteSpace(tb_server_url.Text))
  34. {
  35. MessageBox.Show("请输入服务器地址");
  36. return;
  37. }
  38. if (string.IsNullOrWhiteSpace(tb_username.Text))
  39. {
  40. MessageBox.Show("请输入用户名");
  41. return;
  42. }
  43. if (string.IsNullOrWhiteSpace(tb_passowrd.Password))
  44. {
  45. MessageBox.Show("请输入密码");
  46. return;
  47. }
  48. btns.Visibility = Visibility.Collapsed;
  49. loading.Visibility = Visibility.Visible;
  50. login();
  51. }
  52. private void btn_quit_Click(object sender, RoutedEventArgs e)
  53. {
  54. Environment.Exit(0);
  55. }
  56. async void login()
  57. {
  58. config.username = tb_username.Text;
  59. config.password = tb_passowrd.Password;
  60. config.url = tb_server_url.Text;
  61. config.save();
  62. http.post<string>("auth/login", new Dictionary<string, string>
  63. {
  64. ["username"] = tb_username.Text,
  65. ["password"] = tb_passowrd.Password
  66. })
  67. .Then(token =>
  68. {
  69. config.token = token;
  70. MainWindow mainWindow = new MainWindow();
  71. mainWindow.Show();
  72. Close();
  73. })
  74. .Catch(e =>
  75. {
  76. btns.Visibility = Visibility.Visible;
  77. loading.Visibility = Visibility.Collapsed;
  78. MessageBox.Show(e.Message);
  79. });
  80. }
  81. private void Window_Loaded(object sender, RoutedEventArgs e)
  82. {
  83. if (!string.IsNullOrWhiteSpace(config.username) && !string.IsNullOrWhiteSpace(config.password))
  84. {
  85. btn_login_Click(null, null);
  86. }
  87. }
  88. }
  89. }