Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 98a024ba10 |
@ -3,7 +3,7 @@
|
||||
|
||||
#define MyAppName "Электронная Фармация"
|
||||
#define MyAppBrand "ЭльФиСА"
|
||||
#define MyAppVersion "2.0.3"
|
||||
#define MyAppVersion "2.0.1"
|
||||
#define MyAppPublisher "PharmData"
|
||||
#define MyAppURL "https://cdn.24pharmdata.ru"
|
||||
#define MyAppSupportURL "https://24pharmdata.ru"
|
||||
|
||||
@ -5,9 +5,9 @@
|
||||
<Nullable>enable</Nullable>
|
||||
<ApplicationManifest>app.manifest</ApplicationManifest>
|
||||
<AvaloniaUseCompiledBindingsByDefault>true</AvaloniaUseCompiledBindingsByDefault>
|
||||
<Version>2.0.3</Version>
|
||||
<AssemblyVersion>2.0.3.0</AssemblyVersion>
|
||||
<FileVersion>2.0.3.0</FileVersion>
|
||||
<Version>2.0.1</Version>
|
||||
<AssemblyVersion>2.0.1.0</AssemblyVersion>
|
||||
<FileVersion>2.0.1.0</FileVersion>
|
||||
<Product>ЭльФиСА — Электронная Фармация</Product>
|
||||
<Company>PharmData</Company>
|
||||
<!-- Кросс-платформенная публикация задаётся флагами CLI (RID + self-contained). -->
|
||||
|
||||
@ -61,9 +61,7 @@ public partial class LoadPriceViewModel : ViewModelBase
|
||||
ProgressText = "Соединение с сервером…";
|
||||
|
||||
var all = new List<PriceItem>();
|
||||
// Большая страница: сервер на каждый запрос заново прогоняет тяжёлый
|
||||
// сводный запрос, поэтому одна большая страница в разы быстрее десятков мелких.
|
||||
const int page = 20000;
|
||||
const int page = 500;
|
||||
int offset = 0, total = 0;
|
||||
|
||||
while (true)
|
||||
@ -79,7 +77,7 @@ public partial class LoadPriceViewModel : ViewModelBase
|
||||
: $"Загружено {all.Count} позиций…";
|
||||
|
||||
offset += batch.Count;
|
||||
if (batch.Count < page || (total > 0 && all.Count >= total) || offset > 500000)
|
||||
if (batch.Count < page || (total > 0 && all.Count >= total) || offset > 30000)
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
@ -15,12 +15,6 @@ public partial class RootViewModel : ViewModelBase
|
||||
|
||||
public RootViewModel()
|
||||
{
|
||||
// Запомнили вход в прошлый раз и токен ещё живой — сразу в программу.
|
||||
if (Session.TryRestore())
|
||||
{
|
||||
Current = new ShellViewModel(Logout);
|
||||
return;
|
||||
}
|
||||
Current = new LoginViewModel(OnLoggedIn);
|
||||
TryDevAutoLogin();
|
||||
}
|
||||
@ -44,11 +38,7 @@ public partial class RootViewModel : ViewModelBase
|
||||
catch { /* остаёмся на экране входа */ }
|
||||
}
|
||||
|
||||
private void OnLoggedIn()
|
||||
{
|
||||
Session.Current.Save(); // запомнить вход между запусками
|
||||
Current = new ShellViewModel(Logout);
|
||||
}
|
||||
private void OnLoggedIn() => Current = new ShellViewModel(Logout);
|
||||
|
||||
private void Logout()
|
||||
{
|
||||
|
||||
@ -9,119 +9,98 @@ using Elfisa.Core;
|
||||
namespace Elfisa.Avalonia.ViewModels;
|
||||
|
||||
/// <summary>
|
||||
/// Отправить — отправка НЕОТПРАВЛЕННЫХ накладных (черновиков заказов из раздела
|
||||
/// «Заказы»). Список готовых накладных, кнопка «Отправить» (создаёт реальный заказ
|
||||
/// у поставщика). Без поиска по прайсу и наименований — только сами накладные.
|
||||
/// Отправить: поиск по прайсу → корзина (общая с Прайс-листом) → создание заказа.
|
||||
/// ВНИМАНИЕ: отправка создаёт реальный заказ у поставщика.
|
||||
/// </summary>
|
||||
public partial class SendOrderViewModel : ViewModelBase
|
||||
{
|
||||
private readonly ApiClient _api = new();
|
||||
private readonly CartService _cart = CartService.Instance;
|
||||
|
||||
/// <summary>Неотправленные накладные (черновики).</summary>
|
||||
public ObservableCollection<DraftOrder> Drafts { get; } = new();
|
||||
public ObservableCollection<PriceItem> Results { get; } = new();
|
||||
public ObservableCollection<CartItem> Cart => _cart.Items;
|
||||
public ObservableCollection<BuyerLocationDto> Locations { get; } = new();
|
||||
|
||||
[ObservableProperty] private DraftOrder? _selected;
|
||||
[ObservableProperty] private string _search = "";
|
||||
[ObservableProperty] private PriceItem? _selectedResult;
|
||||
[ObservableProperty] private BuyerLocationDto? _selectedLocation;
|
||||
[ObservableProperty] private string _comment = "";
|
||||
[ObservableProperty] private bool _busy;
|
||||
[ObservableProperty] private string? _status;
|
||||
[ObservableProperty] private decimal _totalSum;
|
||||
[ObservableProperty] private bool _isEmpty = true;
|
||||
[ObservableProperty] private int _count;
|
||||
[ObservableProperty] private decimal _cartTotal;
|
||||
|
||||
public SendOrderViewModel()
|
||||
{
|
||||
_api.SetToken(Session.Current.Token);
|
||||
Load();
|
||||
_cart.Changed += Recalc;
|
||||
Recalc();
|
||||
if (Session.Current.IsAuthenticated) _ = InitAsync();
|
||||
}
|
||||
|
||||
private async Task InitAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
var locs = await _api.GetBuyerLocationsAsync();
|
||||
Locations.Clear();
|
||||
foreach (var l in locs) Locations.Add(l);
|
||||
SelectedLocation = Locations.FirstOrDefault(l => l.IsDefault) ?? Locations.FirstOrDefault();
|
||||
await SearchAsync();
|
||||
}
|
||||
catch (Exception ex) { Status = ex.Message; }
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
private void Refresh() => Load();
|
||||
|
||||
private void Load()
|
||||
private async Task SearchAsync()
|
||||
{
|
||||
Drafts.Clear();
|
||||
foreach (var d in DraftStore.All.OrderByDescending(x => x.CreatedAt))
|
||||
Drafts.Add(d);
|
||||
TotalSum = Drafts.Sum(d => d.Total);
|
||||
Count = Drafts.Count;
|
||||
IsEmpty = Drafts.Count == 0;
|
||||
if (Selected is null || !Drafts.Contains(Selected))
|
||||
Selected = Drafts.FirstOrDefault();
|
||||
try
|
||||
{
|
||||
Busy = true; Status = null;
|
||||
var resp = await _api.GetPriceSummaryAsync(Search, limit: 200);
|
||||
Results.Clear();
|
||||
foreach (var it in resp.Summary ?? new()) Results.Add(it);
|
||||
}
|
||||
catch (Exception ex) { Status = ex.Message; }
|
||||
finally { Busy = false; }
|
||||
}
|
||||
|
||||
/// <summary>Отправить одну накладную.</summary>
|
||||
[RelayCommand]
|
||||
private async Task SendAsync(DraftOrder? draft)
|
||||
private void AddToCart()
|
||||
{
|
||||
if (SelectedResult is null) { Status = "Выберите позицию в списке слева."; return; }
|
||||
_cart.Add(SelectedResult);
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
private void RemoveFromCart(CartItem? item) => _cart.Remove(item!);
|
||||
|
||||
private void Recalc() => CartTotal = _cart.Total;
|
||||
|
||||
[RelayCommand]
|
||||
private async Task SendAsync()
|
||||
{
|
||||
var d = draft ?? Selected;
|
||||
if (d is null) { Status = "Выберите накладную для отправки."; return; }
|
||||
Status = null;
|
||||
if (_cart.Count == 0) { Status = "Корзина пуста."; return; }
|
||||
try
|
||||
{
|
||||
Busy = true;
|
||||
var resp = await CreateFromDraft(d);
|
||||
DraftStore.Remove(d.Id);
|
||||
Status = $"✓ Накладная {resp.GlobalSign ?? resp.OrderId} отправлена ({resp.ItemsCount} поз., {resp.TotalAmount:N2}).";
|
||||
var req = new BuyerOrderCreateRequest
|
||||
{
|
||||
LocationId = SelectedLocation?.BuyerLocationId,
|
||||
Comment = string.IsNullOrWhiteSpace(Comment) ? null : Comment.Trim(),
|
||||
Items = _cart.Items.Select(c => new BuyerOrderItemReq
|
||||
{
|
||||
SupplierPriceId = c.Source.SupplierPriceId ?? "",
|
||||
Qty = (double)c.Qty,
|
||||
ItemName = c.Source.DrugName,
|
||||
Barcode = c.Source.Barcode
|
||||
}).ToList()
|
||||
};
|
||||
var resp = await _api.CreateOrderAsync(req);
|
||||
Status = $"✓ Заказ {resp.GlobalSign ?? resp.OrderId} создан: {resp.ItemsCount} поз., {resp.TotalAmount:N2}.";
|
||||
_cart.Clear();
|
||||
}
|
||||
catch (Exception ex) { Status = "Ошибка отправки: " + ex.Message; }
|
||||
finally { Busy = false; Load(); }
|
||||
}
|
||||
|
||||
/// <summary>Отправить все неотправленные накладные.</summary>
|
||||
[RelayCommand]
|
||||
private async Task SendAllAsync()
|
||||
{
|
||||
if (Drafts.Count == 0) { Status = "Нет неотправленных накладных."; return; }
|
||||
Status = null;
|
||||
int ok = 0;
|
||||
string? lastErr = null;
|
||||
try
|
||||
{
|
||||
Busy = true;
|
||||
foreach (var d in DraftStore.All.ToList())
|
||||
{
|
||||
try
|
||||
{
|
||||
await CreateFromDraft(d);
|
||||
DraftStore.Remove(d.Id);
|
||||
ok++;
|
||||
}
|
||||
catch (Exception ex) { lastErr = ex.Message; }
|
||||
}
|
||||
}
|
||||
finally { Busy = false; Load(); }
|
||||
|
||||
Status = lastErr is null
|
||||
? $"✓ Отправлено накладных: {ok}."
|
||||
: $"Отправлено: {ok}. Не удалось часть — {lastErr}";
|
||||
}
|
||||
|
||||
/// <summary>Удалить неотправленную накладную (не отправляя).</summary>
|
||||
[RelayCommand]
|
||||
private void Delete(DraftOrder? draft)
|
||||
{
|
||||
var d = draft ?? Selected;
|
||||
if (d is null) return;
|
||||
DraftStore.Remove(d.Id);
|
||||
Load();
|
||||
Status = "Накладная удалена.";
|
||||
}
|
||||
|
||||
private async Task<BuyerOrderResponse> CreateFromDraft(DraftOrder d)
|
||||
{
|
||||
var req = new BuyerOrderCreateRequest
|
||||
{
|
||||
// Номер EX резервировался при сохранении черновика — используем его.
|
||||
GlobalSign = d.Number.StartsWith("EX-", StringComparison.OrdinalIgnoreCase) ? d.Number : null,
|
||||
LocationId = d.LocationId,
|
||||
Comment = d.Comment,
|
||||
Items = d.Items.Select(i => new BuyerOrderItemReq
|
||||
{
|
||||
SupplierPriceId = i.SupplierPriceId,
|
||||
Qty = (double)i.Qty,
|
||||
ItemName = i.Name,
|
||||
Barcode = i.Barcode
|
||||
}).ToList()
|
||||
};
|
||||
return await _api.CreateOrderAsync(req);
|
||||
finally { Busy = false; }
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,19 +1,16 @@
|
||||
<UserControl xmlns="https://github.com/avaloniaui"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:vm="using:Elfisa.Avalonia.ViewModels"
|
||||
xmlns:core="using:Elfisa.Core"
|
||||
xmlns:conv="using:Avalonia.Data.Converters"
|
||||
x:Class="Elfisa.Avalonia.Views.SendOrderView"
|
||||
x:DataType="vm:SendOrderViewModel">
|
||||
|
||||
<DockPanel Margin="24">
|
||||
<Grid DockPanel.Dock="Top" Margin="0,0,0,14" ColumnDefinitions="*,Auto">
|
||||
<StackPanel Grid.Column="0" Spacing="2" VerticalAlignment="Center">
|
||||
<TextBlock Text="Отправить" Classes="h1"/>
|
||||
<TextBlock Text="Отправка неотправленных накладных из раздела «Заказы»" Classes="muted"/>
|
||||
<Grid DockPanel.Dock="Top" Margin="0,0,0,14">
|
||||
<StackPanel Spacing="2" VerticalAlignment="Center">
|
||||
<TextBlock Text="Отправить заказ" Classes="h1"/>
|
||||
<TextBlock Text="Найдите позиции в прайсе, соберите корзину и отправьте поставщику" Classes="muted"/>
|
||||
</StackPanel>
|
||||
<Button Grid.Column="1" Content="Обновить" VerticalAlignment="Center"
|
||||
Command="{Binding RefreshCommand}"/>
|
||||
</Grid>
|
||||
|
||||
<Border DockPanel.Dock="Bottom" Margin="0,10,0,0" Padding="10,7" CornerRadius="6"
|
||||
@ -22,68 +19,86 @@
|
||||
<TextBlock Text="{Binding Status}" Foreground="{DynamicResource AccentPressed}" TextWrapping="Wrap" FontSize="12.5"/>
|
||||
</Border>
|
||||
|
||||
<!-- Панель отправки: главное действие — отправить все -->
|
||||
<Border DockPanel.Dock="Top" Classes="card" Padding="16" Margin="0,0,0,12">
|
||||
<Grid ColumnDefinitions="*,Auto" VerticalAlignment="Center">
|
||||
<StackPanel Grid.Column="0" Spacing="2" VerticalAlignment="Center">
|
||||
<TextBlock Foreground="{DynamicResource Text}" FontWeight="SemiBold" FontSize="15"
|
||||
Text="{Binding Count, StringFormat='Неотправленных накладных: {0}'}"/>
|
||||
<TextBlock Classes="muted" FontSize="12.5"
|
||||
Text="{Binding TotalSum, StringFormat='на сумму {0:N2}'}"/>
|
||||
</StackPanel>
|
||||
<Button Grid.Column="1" Classes="primary" Padding="26,12" FontSize="15"
|
||||
Content="Отправить все ▸" Command="{Binding SendAllCommand}"
|
||||
IsEnabled="{Binding !Busy}" IsVisible="{Binding !IsEmpty}"/>
|
||||
</Grid>
|
||||
</Border>
|
||||
<Grid ColumnDefinitions="*,16,440">
|
||||
|
||||
<!-- Список накладных -->
|
||||
<Border Classes="card" Padding="0" ClipToBounds="True">
|
||||
<Grid>
|
||||
<!-- Пусто -->
|
||||
<StackPanel IsVisible="{Binding IsEmpty}" VerticalAlignment="Center" HorizontalAlignment="Center"
|
||||
Spacing="8" Margin="24">
|
||||
<TextBlock Text="📭" FontSize="34" HorizontalAlignment="Center"/>
|
||||
<TextBlock Text="Нет неотправленных накладных" Classes="h2" HorizontalAlignment="Center"/>
|
||||
<TextBlock Classes="muted" HorizontalAlignment="Center" TextAlignment="Center" MaxWidth="360" TextWrapping="Wrap"
|
||||
Text="Соберите заказ в разделе «Прайс-лист» и нажмите «Сохранить заказ» — он появится здесь для отправки."/>
|
||||
</StackPanel>
|
||||
<!-- Прайс: поиск + результаты -->
|
||||
<Border Grid.Column="0" Classes="card" Padding="0" ClipToBounds="True">
|
||||
<DockPanel>
|
||||
<StackPanel DockPanel.Dock="Top" Orientation="Horizontal" Spacing="10" Margin="14">
|
||||
<TextBox Text="{Binding Search}" Width="330" Watermark="поиск по наименованию…"/>
|
||||
<Button Classes="primary" Content="Найти" Command="{Binding SearchCommand}" IsEnabled="{Binding !Busy}"/>
|
||||
</StackPanel>
|
||||
<Button DockPanel.Dock="Bottom" Classes="primary" Content="Добавить в заказ ▸"
|
||||
Margin="14" HorizontalAlignment="Left"
|
||||
Command="{Binding AddToCartCommand}"/>
|
||||
<DataGrid ItemsSource="{Binding Results}" SelectedItem="{Binding SelectedResult}"
|
||||
x:CompileBindings="False" AutoGenerateColumns="False" IsReadOnly="True"
|
||||
HeadersVisibility="Column" CanUserResizeColumns="True" CanUserSortColumns="False">
|
||||
<DataGrid.Columns>
|
||||
<DataGridTextColumn Header="Наименование" Width="*" Binding="{Binding DrugName}"/>
|
||||
<DataGridTextColumn Header="Поставщик" Width="120" Binding="{Binding SupplierName}"/>
|
||||
<DataGridTextColumn Header="Цена" Width="100" Binding="{Binding Price, StringFormat='{}{0:N2}'}" CellStyleClasses="num"/>
|
||||
</DataGrid.Columns>
|
||||
</DataGrid>
|
||||
</DockPanel>
|
||||
</Border>
|
||||
|
||||
<!-- Есть накладные -->
|
||||
<ScrollViewer IsVisible="{Binding !IsEmpty}">
|
||||
<ItemsControl ItemsSource="{Binding Drafts}" x:CompileBindings="False" Margin="0,0,0,14">
|
||||
<ItemsControl.ItemTemplate>
|
||||
<DataTemplate x:DataType="core:DraftOrder">
|
||||
<Border Padding="14" Margin="14,14,14,0" CornerRadius="10"
|
||||
Background="{DynamicResource RowAlt}" BorderThickness="1" BorderBrush="{DynamicResource Border}">
|
||||
<Grid ColumnDefinitions="*,Auto" VerticalAlignment="Center">
|
||||
<StackPanel Grid.Column="0" Spacing="3">
|
||||
<TextBlock Text="{Binding Number}" FontWeight="Bold" FontSize="15" Foreground="{DynamicResource Text}"/>
|
||||
<TextBlock Classes="muted" FontSize="12.5"
|
||||
Text="{Binding CreatedAt, StringFormat='dd.MM.yyyy HH:mm'}"/>
|
||||
<TextBlock Classes="muted" FontSize="12.5" TextTrimming="CharacterEllipsis"
|
||||
Text="{Binding LocationAddress}"/>
|
||||
<TextBlock Classes="muted" FontSize="12.5"
|
||||
Text="{Binding ItemsCount, StringFormat='Позиций: {0}'}"/>
|
||||
</StackPanel>
|
||||
<StackPanel Grid.Column="1" Orientation="Horizontal" Spacing="12" VerticalAlignment="Center">
|
||||
<TextBlock Text="{Binding Total, StringFormat='{}{0:N2}'}" FontWeight="Bold"
|
||||
FontSize="16" Foreground="{DynamicResource Accent}" VerticalAlignment="Center"/>
|
||||
<Button Classes="primary" Content="Отправить"
|
||||
Command="{Binding $parent[ItemsControl].DataContext.SendCommand}"
|
||||
<!-- Корзина -->
|
||||
<Border Grid.Column="2" Classes="card" Padding="16">
|
||||
<DockPanel>
|
||||
<TextBlock DockPanel.Dock="Top" Text="Корзина" Classes="h2" Margin="0,0,0,10"/>
|
||||
|
||||
<StackPanel DockPanel.Dock="Bottom" Spacing="10" Margin="0,12,0,0">
|
||||
<StackPanel Spacing="5">
|
||||
<TextBlock Text="Грузополучатель" Classes="muted"/>
|
||||
<ComboBox ItemsSource="{Binding Locations}" SelectedItem="{Binding SelectedLocation}"
|
||||
HorizontalAlignment="Stretch"/>
|
||||
</StackPanel>
|
||||
<TextBox Text="{Binding Comment}" Watermark="комментарий к заказу (необязательно)"/>
|
||||
<Grid ColumnDefinitions="*,Auto">
|
||||
<TextBlock Grid.Column="0" Text="Итого" FontWeight="SemiBold" VerticalAlignment="Center"
|
||||
Foreground="{DynamicResource Text}"/>
|
||||
<TextBlock Grid.Column="1" Text="{Binding CartTotal, StringFormat='{}{0:N2}'}"
|
||||
FontWeight="Bold" FontSize="16" Foreground="{DynamicResource Accent}"/>
|
||||
</Grid>
|
||||
<Button Classes="primary" Content="Отправить заказ" HorizontalAlignment="Stretch"
|
||||
Command="{Binding SendCommand}" IsEnabled="{Binding !Busy}"/>
|
||||
</StackPanel>
|
||||
|
||||
<ScrollViewer>
|
||||
<ItemsControl ItemsSource="{Binding Cart}" x:CompileBindings="False">
|
||||
<ItemsControl.ItemTemplate>
|
||||
<DataTemplate x:DataType="vm:CartItem">
|
||||
<Border Padding="10" Margin="0,0,0,8" CornerRadius="9"
|
||||
Background="{DynamicResource RowAlt}" BorderThickness="1" BorderBrush="{DynamicResource Border}">
|
||||
<DockPanel>
|
||||
<Button DockPanel.Dock="Right" Content="✕" Width="30" Height="30" Padding="0"
|
||||
VerticalAlignment="Top" Background="Transparent"
|
||||
Foreground="{DynamicResource Muted}" Cursor="Hand"
|
||||
Command="{Binding $parent[ItemsControl].DataContext.RemoveFromCartCommand}"
|
||||
CommandParameter="{Binding}"/>
|
||||
<Button Content="✕" Width="32" Height="32" Padding="0" VerticalAlignment="Center"
|
||||
Background="Transparent" Foreground="{DynamicResource Muted}" Cursor="Hand"
|
||||
Command="{Binding $parent[ItemsControl].DataContext.DeleteCommand}"
|
||||
CommandParameter="{Binding}"/>
|
||||
</StackPanel>
|
||||
</Grid>
|
||||
</Border>
|
||||
</DataTemplate>
|
||||
</ItemsControl.ItemTemplate>
|
||||
</ItemsControl>
|
||||
</ScrollViewer>
|
||||
</Grid>
|
||||
</Border>
|
||||
<StackPanel Spacing="6">
|
||||
<TextBlock Text="{Binding Name}" Foreground="{DynamicResource Text}"
|
||||
TextWrapping="Wrap" MaxLines="2" TextTrimming="CharacterEllipsis"/>
|
||||
<TextBlock Text="{Binding Supplier}" Classes="muted"/>
|
||||
<Grid ColumnDefinitions="Auto,*,Auto" VerticalAlignment="Center">
|
||||
<NumericUpDown Grid.Column="0" Value="{Binding Qty}" Minimum="1" Maximum="100000"
|
||||
Increment="1" FormatString="0" Width="120"/>
|
||||
<TextBlock Grid.Column="1" Text="{Binding Price, StringFormat='× {0:N2}'}"
|
||||
Classes="muted" VerticalAlignment="Center" Margin="10,0"/>
|
||||
<TextBlock Grid.Column="2" Text="{Binding LineSum, StringFormat='{}{0:N2}'}"
|
||||
FontWeight="SemiBold" Foreground="{DynamicResource Text}"
|
||||
VerticalAlignment="Center"/>
|
||||
</Grid>
|
||||
</StackPanel>
|
||||
</DockPanel>
|
||||
</Border>
|
||||
</DataTemplate>
|
||||
</ItemsControl.ItemTemplate>
|
||||
</ItemsControl>
|
||||
</ScrollViewer>
|
||||
</DockPanel>
|
||||
</Border>
|
||||
</Grid>
|
||||
</DockPanel>
|
||||
</UserControl>
|
||||
|
||||
@ -37,14 +37,12 @@ public sealed class ApiClient
|
||||
public async Task<LoginResponse> LoginAsync(string username, string password)
|
||||
{
|
||||
var body = JsonSerializer.Serialize(new LoginRequest { Username = username, Password = password }, Json);
|
||||
HttpResponseMessage resp;
|
||||
try
|
||||
using var req = new HttpRequestMessage(HttpMethod.Post, Url("/auth/login"))
|
||||
{
|
||||
resp = await SendWithRetryAsync(() => new HttpRequestMessage(HttpMethod.Post, Url("/auth/login"))
|
||||
{
|
||||
Content = new StringContent(body, Encoding.UTF8, "application/json")
|
||||
}).ConfigureAwait(false);
|
||||
}
|
||||
Content = new StringContent(body, Encoding.UTF8, "application/json")
|
||||
};
|
||||
HttpResponseMessage resp;
|
||||
try { resp = await _http.SendAsync(req).ConfigureAwait(false); }
|
||||
catch (Exception ex) { throw new ApiException($"Не удалось связаться с сервером ({_baseUrl}): {ex.Message}", ex); }
|
||||
|
||||
var text = await resp.Content.ReadAsStringAsync().ConfigureAwait(false);
|
||||
@ -166,16 +164,10 @@ public sealed class ApiClient
|
||||
|
||||
private async Task<string> AuthorizedGetAsync(string path, string op)
|
||||
{
|
||||
using var req = new HttpRequestMessage(HttpMethod.Get, Url(path));
|
||||
req.Headers.Authorization = new AuthenticationHeaderValue("Bearer", _token);
|
||||
HttpResponseMessage resp;
|
||||
try
|
||||
{
|
||||
resp = await SendWithRetryAsync(() =>
|
||||
{
|
||||
var r = new HttpRequestMessage(HttpMethod.Get, Url(path));
|
||||
r.Headers.Authorization = new AuthenticationHeaderValue("Bearer", _token);
|
||||
return r;
|
||||
}).ConfigureAwait(false);
|
||||
}
|
||||
try { resp = await _http.SendAsync(req).ConfigureAwait(false); }
|
||||
catch (Exception ex) { throw new ApiException($"Не удалось выполнить {op}: {ex.Message}", ex); }
|
||||
|
||||
var text = await resp.Content.ReadAsStringAsync().ConfigureAwait(false);
|
||||
@ -184,21 +176,6 @@ public sealed class ApiClient
|
||||
return text;
|
||||
}
|
||||
|
||||
/// <summary>Отправляет запрос с повтором на транзиентных сбоях (холодный TLS,
|
||||
/// сброс соединения, hairpin-NAT) — чтобы поиск/загрузка не падали с первого раза.</summary>
|
||||
private async Task<HttpResponseMessage> SendWithRetryAsync(Func<HttpRequestMessage> make, int retries = 2)
|
||||
{
|
||||
Exception? last = null;
|
||||
for (int attempt = 0; attempt <= retries; attempt++)
|
||||
{
|
||||
using var req = make();
|
||||
try { return await _http.SendAsync(req).ConfigureAwait(false); }
|
||||
catch (HttpRequestException ex) { last = ex; } // сеть/TLS — повторяем
|
||||
if (attempt < retries) await Task.Delay(250 * (attempt + 1)).ConfigureAwait(false);
|
||||
}
|
||||
throw last!;
|
||||
}
|
||||
|
||||
private void EnsureAuth()
|
||||
{
|
||||
if (!IsAuthenticated) throw new ApiException("Требуется авторизация.");
|
||||
|
||||
@ -1,9 +1,6 @@
|
||||
using System;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace Elfisa.Core;
|
||||
|
||||
/// <summary>Текущая сессия пользователя (токен, роль, логин). Сохраняется между запусками.</summary>
|
||||
/// <summary>Текущая сессия пользователя (токен, роль, логин).</summary>
|
||||
public sealed class Session
|
||||
{
|
||||
public static Session Current { get; } = new();
|
||||
@ -14,57 +11,10 @@ public sealed class Session
|
||||
|
||||
public bool IsAuthenticated => !string.IsNullOrWhiteSpace(Token);
|
||||
|
||||
/// <summary>Сохранить сессию в настройки (чтобы не логиниться каждый раз).</summary>
|
||||
public void Save()
|
||||
{
|
||||
SettingsStore.Current.Token = Token;
|
||||
SettingsStore.Current.Role = Role;
|
||||
SettingsStore.Current.Username = Username;
|
||||
SettingsStore.Save();
|
||||
}
|
||||
|
||||
/// <summary>Восстановить сессию из настроек, если токен есть и не истёк.</summary>
|
||||
public static bool TryRestore()
|
||||
{
|
||||
var s = SettingsStore.Current;
|
||||
if (string.IsNullOrWhiteSpace(s.Token) || IsJwtExpired(s.Token))
|
||||
return false;
|
||||
Current.Token = s.Token;
|
||||
Current.Role = s.Role;
|
||||
Current.Username = s.Username;
|
||||
return true;
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
Token = null;
|
||||
Role = null;
|
||||
Username = null;
|
||||
SettingsStore.Current.Token = null;
|
||||
SettingsStore.Current.Role = null;
|
||||
SettingsStore.Current.Username = null;
|
||||
SettingsStore.Save();
|
||||
}
|
||||
|
||||
/// <summary>Проверяет, истёк ли JWT (по claim exp), с запасом в 1 минуту.</summary>
|
||||
private static bool IsJwtExpired(string token)
|
||||
{
|
||||
try
|
||||
{
|
||||
var parts = token.Split('.');
|
||||
if (parts.Length != 3) return true;
|
||||
var payload = parts[1].Replace('-', '+').Replace('_', '/');
|
||||
switch (payload.Length % 4)
|
||||
{
|
||||
case 2: payload += "=="; break;
|
||||
case 3: payload += "="; break;
|
||||
}
|
||||
var json = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(payload));
|
||||
using var doc = JsonDocument.Parse(json);
|
||||
if (doc.RootElement.TryGetProperty("exp", out var exp) && exp.TryGetInt64(out var expUnix))
|
||||
return DateTimeOffset.UtcNow.ToUnixTimeSeconds() >= expUnix - 60;
|
||||
return false; // нет exp — считаем валидным
|
||||
}
|
||||
catch { return true; }
|
||||
}
|
||||
}
|
||||
|
||||
@ -116,8 +116,6 @@
|
||||
background:var(--brand-soft); color:var(--brand-ink); border:1px solid color-mix(in srgb,var(--brand) 26%, transparent)}
|
||||
.dl:hover{background:var(--brand); color:#fff}
|
||||
.dl svg{width:17px; height:17px; fill:currentColor}
|
||||
.dl.soon{background:var(--panel-2); color:var(--muted); border-color:var(--line); cursor:default}
|
||||
.dl.soon:hover{background:var(--panel-2); color:var(--muted)}
|
||||
|
||||
/* ---- features ---- */
|
||||
.feats{padding:30px 0 8px; display:grid; grid-template-columns:repeat(3,1fr); gap:14px}
|
||||
@ -175,7 +173,7 @@
|
||||
<div class="spec"><span>Формат</span><b>.exe установщик</b></div>
|
||||
<div class="spec"><span>Система</span><b>Windows 10 / 11</b></div>
|
||||
<div class="spec"><span>Разрядность</span><b>64-бит</b></div>
|
||||
<div class="spec"><span>Размер</span><b>~48 МБ</b></div>
|
||||
<div class="spec"><span>Размер</span><b>~90 МБ</b></div>
|
||||
</div>
|
||||
<a class="dl" href="ElfisaPharmacy-Setup.exe" download>
|
||||
<svg viewBox="0 0 24 24"><path d="M12 3v10m0 0 4-4m-4 4-4-4M4 17v2a1 1 0 0 0 1 1h14a1 1 0 0 0 1-1v-2" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>
|
||||
@ -187,14 +185,17 @@
|
||||
<span class="youbadge">Ваша система</span>
|
||||
<div class="os-ico"><svg viewBox="0 0 24 24"><path d="M16.4 12.7c0-2.3 1.9-3.4 2-3.5-1.1-1.6-2.8-1.8-3.4-1.8-1.4-.1-2.8.9-3.5.9-.7 0-1.8-.8-3-.8-1.5 0-3 .9-3.8 2.3-1.6 2.8-.4 7 1.2 9.3.8 1.1 1.7 2.4 2.9 2.3 1.2 0 1.6-.7 3-.7s1.8.7 3 .7c1.2 0 2-1.1 2.8-2.2.5-.8.9-1.6 1.2-2.5-.1 0-2.1-.9-2.1-3.2zM14.2 5.9c.6-.8 1-1.8.9-2.9-.9 0-2 .6-2.6 1.4-.6.7-1.1 1.7-.9 2.7 1 .1 2-.5 2.6-1.2z"/></svg></div>
|
||||
<h3>macOS</h3>
|
||||
<p class="os-sub">Apple Silicon и Intel · готовим сборку</p>
|
||||
<p class="os-sub">Образ диска · Apple Silicon и Intel</p>
|
||||
<div class="specs">
|
||||
<div class="spec"><span>Формат</span><b>.dmg образ</b></div>
|
||||
<div class="spec"><span>Система</span><b>macOS 12+</b></div>
|
||||
<div class="spec"><span>Процессор</span><b>Apple / Intel</b></div>
|
||||
<div class="spec"><span>Статус</span><b>скоро</b></div>
|
||||
<div class="spec"><span>Размер</span><b>~95 МБ</b></div>
|
||||
</div>
|
||||
<span class="dl soon">Скоро</span>
|
||||
<a class="dl" href="Elfisa-macOS.dmg" download>
|
||||
<svg viewBox="0 0 24 24"><path d="M12 3v10m0 0 4-4m-4 4-4-4M4 17v2a1 1 0 0 0 1 1h14a1 1 0 0 0 1-1v-2" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>
|
||||
Скачать образ .dmg
|
||||
</a>
|
||||
</div>
|
||||
|
||||
<div class="card" data-os="linux">
|
||||
@ -206,7 +207,7 @@
|
||||
<div class="spec"><span>Формат</span><b>.AppImage</b></div>
|
||||
<div class="spec"><span>Система</span><b>glibc 2.31+</b></div>
|
||||
<div class="spec"><span>Разрядность</span><b>x86-64</b></div>
|
||||
<div class="spec"><span>Размер</span><b>~38 МБ</b></div>
|
||||
<div class="spec"><span>Размер</span><b>~95 МБ</b></div>
|
||||
</div>
|
||||
<a class="dl" href="Elfisa-Linux-x86_64.AppImage" download>
|
||||
<svg viewBox="0 0 24 24"><path d="M12 3v10m0 0 4-4m-4 4-4-4M4 17v2a1 1 0 0 0 1 1h14a1 1 0 0 0 1-1v-2" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>
|
||||
@ -233,9 +234,9 @@
|
||||
|
||||
<script>
|
||||
(function(){
|
||||
var files = { windows:"ElfisaPharmacy-Setup.exe", mac:null, linux:"Elfisa-Linux-x86_64.AppImage" };
|
||||
var labels = { windows:"Скачать для Windows", mac:"Версия для macOS — скоро", linux:"Скачать для Linux" };
|
||||
var metas = { windows:"Windows 10/11 · 64-бит · ~48 МБ", mac:"сборка готовится · выберите систему ниже", linux:"x86-64 · glibc 2.31+ · ~38 МБ" };
|
||||
var files = { windows:"ElfisaPharmacy-Setup.exe", mac:"Elfisa-macOS.dmg", linux:"Elfisa-Linux-x86_64.AppImage" };
|
||||
var labels = { windows:"Скачать для Windows", mac:"Скачать для macOS", linux:"Скачать для Linux" };
|
||||
var metas = { windows:"Windows 10/11 · 64-бит · ~90 МБ", mac:"macOS 12+ · Apple/Intel · ~95 МБ", linux:"x86-64 · glibc 2.31+ · ~95 МБ" };
|
||||
|
||||
function detect(){
|
||||
var ua = navigator.userAgent || "";
|
||||
@ -248,12 +249,9 @@
|
||||
|
||||
var os = detect();
|
||||
var hero = document.getElementById("heroBtn");
|
||||
if (hero){ hero.setAttribute("href", files[os]); }
|
||||
var lbl = document.getElementById("heroLbl"); if(lbl) lbl.textContent = labels[os];
|
||||
var meta = document.getElementById("heroMeta"); if(meta) meta.textContent = metas[os];
|
||||
if (hero){
|
||||
if (files[os]){ hero.setAttribute("href", files[os]); hero.setAttribute("download",""); }
|
||||
else { hero.setAttribute("href", "#os"); hero.removeAttribute("download"); hero.classList.add("soon-hero"); }
|
||||
}
|
||||
|
||||
var card = document.querySelector('.card[data-os="'+os+'"]');
|
||||
if (card){ card.classList.add("you"); }
|
||||
|
||||
Loading…
Reference in New Issue
Block a user