Добавлен вывод QR яндекса
This commit is contained in:
@@ -0,0 +1,145 @@
|
||||
@using System.Threading
|
||||
@using PlaylistShared.Shared.DTO
|
||||
@using PlaylistShared.Shared.Yandex
|
||||
@inject HttpClient Http
|
||||
@inject ISnackbar Snackbar
|
||||
@inject IJSRuntime JsRuntime
|
||||
|
||||
<MudDialog>
|
||||
<TitleContent>
|
||||
<MudText Typo="Typo.h6">Авторизация Яндекс.Музыки по QR</MudText>
|
||||
</TitleContent>
|
||||
<DialogContent>
|
||||
@if (_qrUrl != null)
|
||||
{
|
||||
<div style="text-align: center;">
|
||||
<MudText Typo="Typo.body2" Class="mb-2">Отсканируйте QR-код приложением Яндекс</MudText>
|
||||
<MudImage Src="@_qrUrl" Style="max-width: 250px; border-radius: 12px; background-color: white;" />
|
||||
<MudText Typo="Typo.body2" Class="mt-2" Color="Color.Secondary">
|
||||
Статус: @_statusText
|
||||
</MudText>
|
||||
@if (_isWaiting)
|
||||
{
|
||||
<MudProgressCircular Indeterminate Class="mt-2" Size="Size.Small" />
|
||||
}
|
||||
@if (_isError)
|
||||
{
|
||||
<MudAlert Severity="Severity.Error" Class="mt-4">
|
||||
@_errorMessage
|
||||
</MudAlert>
|
||||
}
|
||||
</div>
|
||||
}
|
||||
else
|
||||
{
|
||||
<MudProgressCircular Indeterminate />
|
||||
}
|
||||
</DialogContent>
|
||||
<DialogActions>
|
||||
<MudButton Variant="Variant.Text" OnClick="Cancel">Отмена</MudButton>
|
||||
</DialogActions>
|
||||
</MudDialog>
|
||||
|
||||
@code {
|
||||
[CascadingParameter] IMudDialogInstance MudDialog { get; set; }
|
||||
|
||||
private string _qrUrl;
|
||||
private string _sessionId;
|
||||
private string _statusText = "Ожидание сканирования";
|
||||
private bool _isWaiting = true;
|
||||
private bool _isError = false;
|
||||
private string _errorMessage = "";
|
||||
private CancellationTokenSource _cts;
|
||||
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
await StartQrFlow();
|
||||
}
|
||||
|
||||
private async Task StartQrFlow()
|
||||
{
|
||||
try
|
||||
{
|
||||
// 1. Получить QR и sessionId
|
||||
var response = await Http.GetFromJsonAsync<ApiResponse<YandexAuthQr>>("/api/yandexaccount/qr");
|
||||
if (!response.Success || response.Data == null)
|
||||
{
|
||||
ShowError("Не удалось получить QR-код");
|
||||
return;
|
||||
}
|
||||
|
||||
_qrUrl = response.Data.QrLink;
|
||||
_sessionId = response.Data.SessionId;
|
||||
|
||||
// 2. Начать опрос статуса
|
||||
_cts = new CancellationTokenSource();
|
||||
_ = PollStatus(_cts.Token);
|
||||
StateHasChanged();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
ShowError(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task PollStatus(CancellationToken token)
|
||||
{
|
||||
while (!token.IsCancellationRequested)
|
||||
{
|
||||
try
|
||||
{
|
||||
await Task.Delay(2000, token);
|
||||
var statusResponse = await Http.GetFromJsonAsync<ApiResponse<YandexAuthQrCheck>>($"/api/yandexaccount/qr/{_sessionId}", token);
|
||||
if (statusResponse?.Data != null)
|
||||
{
|
||||
switch (statusResponse.Data.Status)
|
||||
{
|
||||
case Shared.Enums.YandexAuthQrStatus.Pending:
|
||||
_statusText = "Ожидание подтверждения...";
|
||||
break;
|
||||
case Shared.Enums.YandexAuthQrStatus.Authorized:
|
||||
_statusText = "Авторизация успешна!";
|
||||
_isWaiting = false;
|
||||
Snackbar.Add("Авторизация выполнена", Severity.Success);
|
||||
MudDialog.Close(DialogResult.Ok(true));
|
||||
_cts?.Cancel();
|
||||
return;
|
||||
case Shared.Enums.YandexAuthQrStatus.Expired:
|
||||
ShowError("Срок действия QR-кода истёк");
|
||||
return;
|
||||
case Shared.Enums.YandexAuthQrStatus.Error:
|
||||
ShowError("Ошибка авторизации");
|
||||
return;
|
||||
}
|
||||
StateHasChanged();
|
||||
}
|
||||
}
|
||||
catch (TaskCanceledException) { break; }
|
||||
catch (Exception ex)
|
||||
{
|
||||
ShowError(ex.Message);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void ShowError(string message)
|
||||
{
|
||||
_isError = true;
|
||||
_errorMessage = message;
|
||||
_isWaiting = false;
|
||||
StateHasChanged();
|
||||
}
|
||||
|
||||
private void Cancel()
|
||||
{
|
||||
_cts?.Cancel();
|
||||
MudDialog.Close(DialogResult.Cancel());
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_cts?.Cancel();
|
||||
_cts?.Dispose();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
@using System.Text.RegularExpressions
|
||||
@using PlaylistShared.Shared.DTO
|
||||
@inject HttpClient Http
|
||||
@inject ISnackbar Snackbar
|
||||
|
||||
<MudDialog>
|
||||
<TitleContent>
|
||||
<MudText Typo="Typo.h6">Подключение Яндекс.Музыки</MudText>
|
||||
</TitleContent>
|
||||
<DialogContent>
|
||||
<MudStepper @bind-ActiveIndex="_index" Vertical CenterLabels CompletedStepColor="Color.Success" @onwheel="HandleWheel">
|
||||
<ChildContent>
|
||||
<MudStep Title="Вход" Skippable>
|
||||
<MudText Typo="Typo.body2" Class="mb-4">Нажмите на кнопку и разрешите доступ приложению.</MudText>
|
||||
<MudButton Variant="Variant.Filled"
|
||||
Color="Color.Primary"
|
||||
StartIcon="@PlaylistShared.Pwa.CustomIcons.Yandex"
|
||||
Href="https://oauth.yandex.ru/authorize?response_type=token&client_id=23cabbbdc6cd418abb4b39c32c41195d"
|
||||
Target="_blank"
|
||||
@onclick="() => {_index++;}"
|
||||
FullWidth>
|
||||
Войти в Яндекс
|
||||
</MudButton>
|
||||
</MudStep>
|
||||
|
||||
<MudStep Title="Добавление токена">
|
||||
Скопируйте значение <code>access_token</code> или <code>весь URL</code> из адресной строки после перенаправления.
|
||||
<MudAlert Severity="Severity.Info" Class="mt-4">
|
||||
https://music.yandex.ru/#access_token=<code>ВАШ_ТОКЕН</code>&...
|
||||
</MudAlert>
|
||||
<MudTextField @bind-Value="_rawInput" @bind-Value:after="Submit" Label="Вставьте скопированную ссылку или токен" Variant="Variant.Outlined" Margin="Margin.Dense" Error="_tokenErr" />
|
||||
<MudButton Variant="Variant.Filled" Color="Color.Success" OnClick="Submit" FullWidth Class="mt-4">
|
||||
Сохранить
|
||||
</MudButton>
|
||||
</MudStep>
|
||||
</ChildContent>
|
||||
<ActionContent Context="stepper">
|
||||
<MudIconButton OnClick="@(() => stepper.PreviousStepAsync())" Icon="@Icons.Material.Filled.ArrowBack" Color="Color.Primary" Disabled="@(_index <= 0)" />
|
||||
<MudSpacer />
|
||||
<MudIconButton OnClick="@(() => stepper.NextStepAsync())" Icon="@Icons.Material.Filled.ArrowForward" Color="Color.Primary" Disabled="@(_index >= 1)" />
|
||||
</ActionContent>
|
||||
</MudStepper>
|
||||
</DialogContent>
|
||||
</MudDialog>
|
||||
|
||||
@code {
|
||||
[CascadingParameter] IMudDialogInstance MudDialog { get; set; }
|
||||
private string _rawInput = "";
|
||||
private int _index;
|
||||
private bool _tokenErr = false;
|
||||
|
||||
private async Task HandleWheel(WheelEventArgs e)
|
||||
{
|
||||
|
||||
if (e.DeltaY > 0 && _index < 1) // Прокрутка вниз -> Вперед
|
||||
{
|
||||
_index++;
|
||||
}
|
||||
else if (e.DeltaY < 0 && _index > 0) // Прокрутка вверх -> Назад
|
||||
{
|
||||
_index--;
|
||||
}
|
||||
}
|
||||
|
||||
private async Task Submit()
|
||||
{
|
||||
var token = ExtractToken(_rawInput);
|
||||
if (string.IsNullOrWhiteSpace(token))
|
||||
{
|
||||
_tokenErr = true;
|
||||
Snackbar.Add("Токен не найден", Severity.Error);
|
||||
return;
|
||||
}
|
||||
|
||||
_tokenErr = false;
|
||||
|
||||
var response = await Http.PostAsJsonAsync("/api/yandexaccount/token", new SetYandexTokenRequest { Token = token });
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
Snackbar.Add("Токен успешно обновлен", Severity.Success);
|
||||
MudDialog.Close(DialogResult.Ok(true));
|
||||
}
|
||||
else
|
||||
{
|
||||
Snackbar.Add("Ошибка обновления токена. Повторите позже.", Severity.Error);
|
||||
}
|
||||
}
|
||||
|
||||
private string ExtractToken(string input) =>
|
||||
input.Contains("access_token=") ? Regex.Match(input, @"access_token=([^&]+)").Groups[1].Value : input.Trim();
|
||||
|
||||
public class SetYandexTokenRequest { public string Token { get; set; } }
|
||||
}
|
||||
Reference in New Issue
Block a user