PageLoginMs.xaml.vb 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. Public Class PageLoginMs
  2. ''' <summary>
  3. ''' 刷新页面显示的所有信息。
  4. ''' </summary>
  5. Public Sub Reload(KeepInput As Boolean)
  6. Dim IndexBefore = ComboAccounts.SelectedIndex
  7. '刷新下拉框列表
  8. ComboAccounts.Items.Clear()
  9. ComboAccounts.Items.Add(New MyComboBoxItem With {.Content = "添加新账号"})
  10. Try
  11. Dim MsJson As JObject = Settings.Get(Of String)("LoginMsJson").DeserializeJson()
  12. For Each Account In MsJson
  13. Dim Item As MyListItem = CType(FindResource("ComboBoxItemTemplateWithDelete"), DataTemplate).LoadContent()
  14. Item.Tag = Account.Value.ToString
  15. Item.Title = Account.Key
  16. CType(Item.Buttons(0), MyIconButton).Tag = Account.Key
  17. ComboAccounts.Items.Add(Item)
  18. Next
  19. Catch ex As Exception
  20. Logger.Error(ex, $"微软登录信息出错,登录信息已被重置", LogBehavior.Toast)
  21. Settings.Set("LoginMsJson", "{}")
  22. End Try
  23. '如果不保留输入,刷新列表后自动选择第一项
  24. ComboAccounts.SelectedIndex = If(KeepInput, Math.Max(0, IndexBefore), 0)
  25. End Sub
  26. Private Sub ComboAccounts_SelectionChanged(sender As Object, e As SelectionChangedEventArgs) Handles ComboAccounts.SelectionChanged
  27. If AniControlEnabled <> 0 OrElse ComboAccounts.SelectedItem Is Nothing OrElse ComboAccounts.ContentPresenter Is Nothing Then Return
  28. If TypeOf ComboAccounts.SelectedItem Is MyListItem Then
  29. ComboAccounts.ContentPresenter.Content = CType(ComboAccounts.SelectedItem, MyListItem).Title
  30. ElseIf TypeOf ComboAccounts.SelectedItem Is MyComboBoxItem Then
  31. ComboAccounts.ContentPresenter.Content = CType(ComboAccounts.SelectedItem, MyComboBoxItem).Content
  32. End If
  33. End Sub
  34. ''' <summary>
  35. ''' 获取当前页面的登录信息。
  36. ''' </summary>
  37. Public Shared Function GetLoginData() As McLoginMs
  38. If FrmLoginMs Is Nothing Then Return New McLoginMs With {.OAuthRefreshToken = Settings.Get(Of String)("CacheMsV2OAuthRefresh"), .UserName = Settings.Get(Of String)("CacheMsV2Name")}
  39. Dim Result As McLoginMs = Nothing
  40. RunInUiWait(
  41. Sub()
  42. If FrmLoginMs.ComboAccounts.SelectedIndex = 0 Then
  43. Result = New McLoginMs
  44. Else
  45. Dim Item As MyListItem = FrmLoginMs.ComboAccounts.SelectedItem
  46. Result = New McLoginMs With {.OAuthRefreshToken = Item.Tag, .UserName = Item.Title}
  47. End If
  48. End Sub)
  49. Return Result
  50. End Function
  51. ''' <summary>
  52. ''' 当前页面的登录信息是否有效。
  53. ''' </summary>
  54. Public Shared Function IsVaild(LoginData As McLoginMs) As String
  55. If LoginData.OAuthRefreshToken = "" Then
  56. Return "请在登录账号后再启动游戏!"
  57. Else
  58. Return ""
  59. End If
  60. End Function
  61. Public Function IsVaild() As String
  62. Return IsVaild(GetLoginData())
  63. End Function
  64. Private Sub BtnLogin_Click(sender As Object, e As EventArgs) Handles BtnLogin.Click
  65. ComboAccounts.IsEnabled = False
  66. BtnLogin.IsEnabled = False
  67. BtnLogin.Text = "0%"
  68. RunInNewThread(
  69. Sub()
  70. Try
  71. McLoginMsLoader.Start(GetLoginData(), IsForceRestart:=True)
  72. Do While McLoginMsLoader.State = LoadState.Loading
  73. RunInUi(Sub() BtnLogin.Text = Math.Round(McLoginMsLoader.Progress * 100) & "%")
  74. Thread.Sleep(50)
  75. Loop
  76. If McLoginMsLoader.State = LoadState.Finished Then
  77. RunInUi(Sub() FrmLaunchLeft.RefreshPage(False, True))
  78. ElseIf McLoginMsLoader.State = LoadState.Canceled Then
  79. Throw New OperationCanceledException
  80. ElseIf McLoginMsLoader.Error Is Nothing Then
  81. Throw New Exception("未知错误!")
  82. Else
  83. Throw New Exception(McLoginMsLoader.Error.Message, McLoginMsLoader.Error)
  84. End If
  85. Catch ex As Exception
  86. If ex.IsCanceled Then
  87. ElseIf ex.Message.StartsWithF("$") Then
  88. Hint(ex.Message.TrimStart("$"), HintType.Red)
  89. ElseIf TypeOf ex Is Security.Authentication.AuthenticationException AndAlso ex.Message.Contains("SSL/TLS") Then
  90. Logger.Error(ex, $"正版登录验证失败,请考虑在 [设置 → 其他] 中关闭 [在正版登录时验证 SSL 证书],然后再试。{vbCrLf}{vbCrLf}原始错误信息:", LogBehavior.Alert)
  91. Else
  92. Logger.Error(ex, "正版登录尝试失败", LogBehavior.Alert)
  93. End If
  94. Finally
  95. RunInUi(
  96. Sub()
  97. ComboAccounts.IsEnabled = True
  98. BtnLogin.IsEnabled = True
  99. BtnLogin.Text = "登录"
  100. End Sub)
  101. End Try
  102. End Sub, "Ms Login")
  103. End Sub
  104. End Class