提交 17a5f485 编写于 作者: 麦壳饼's avatar 麦壳饼

实现基本的登录界面和右上角的个人信息显示

上级 e8382c81
@namespace IoTSharp.ClientApp
@namespace IoTSharp.ClientApp
@inherits LayoutComponentBase
<AntDesign.Pro.Layout.BasicLayout
......@@ -12,15 +13,21 @@
</ChildContent>
</AntDesign.Pro.Layout.BasicLayout>
<SettingDrawer />
@using IoTSharp.Sdk.Http;
@inject HttpClient HttpClient
@inject IoTSharpClient client
@inject NavigationManager NavigationManager
@code
{
private MenuDataItem[] MenuData { get; set; } = {};
protected override async Task OnInitializedAsync()
{
await base.OnInitializedAsync();
MenuData = await HttpClient.GetFromJsonAsync<MenuDataItem[]>("data/menu.json");
await base.OnInitializedAsync();
MenuData = await HttpClient.GetFromJsonAsync<MenuDataItem[]>("data/menu.json");
if (!client.IsLogin)
{
NavigationManager.NavigateTo("/user/login");
}
}
}
\ No newline at end of file
......@@ -4,7 +4,7 @@
<div class="main__b__0">
<div class="login">
<Form Model="@_model" OnFinish="HandleSubmit">
<Form Model="@_model" OnFinish="HandleSubmitAsync">
<Tabs ActiveKey="@context.LoginType">
<TabPane Key="1">
<Tab>账户密码登录</Tab>
......
......@@ -16,15 +16,17 @@ namespace IoTSharp.ClientApp.Pages.User
[Inject] public MessageService Message { get; set; }
public void HandleSubmit()
public async Task HandleSubmitAsync()
{
if (_model.UserName == "admin" && _model.Password == "ant.design")
var ok= await AccountService.LoginAsync(_model);
if (ok)
{
NavigationManager.NavigateTo("/");
return;
}
if (_model.UserName == "user" && _model.Password == "ant.design") NavigationManager.NavigateTo("/");
else
{
await Message.Error($"登录失败");
}
}
public async Task GetCaptcha()
......
......@@ -3,6 +3,7 @@ using System.Net.Http;
using System.Threading.Tasks;
using AntDesign.Pro.Layout;
using IoTSharp.ClientApp.Services;
using IoTSharp.Sdk.Http;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using Microsoft.Extensions.DependencyInjection;
......@@ -16,15 +17,21 @@ namespace IoTSharp.ClientApp
builder.RootComponents.Add<App>("app");
builder.Services.AddScoped(
sp => new HttpClient {BaseAddress = new Uri(builder.HostEnvironment.BaseAddress)});
sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
builder.Services.AddScoped(fc =>
{
return new IoTSharpClient(new Uri(builder.Configuration["BaseURL"]));
});
builder.Services.AddAntDesign();
builder.Services.Configure<ProSettings>(builder.Configuration.GetSection("ProSettings"));
builder.Services.AddScoped<IChartService, ChartService>();
builder.Services.AddScoped<IProjectService, ProjectService>();
builder.Services.AddScoped<IUserService, UserService>();
builder.Services.AddScoped<IAccountService, AccountService>();
builder.Services.AddScoped<IProfileService, ProfileService>();
await builder.Build().RunAsync();
}
}
......
using System;
using System.Threading.Tasks;
using IoTSharp.ClientApp.Models;
using IoTSharp.Sdk.Http;
namespace IoTSharp.ClientApp.Services
{
public interface IAccountService
{
Task LoginAsync(LoginParamsType model);
Task<bool> LoginAsync(LoginParamsType model);
Task<string> GetCaptchaAsync(string modile);
}
public class AccountService : IAccountService
{
private readonly Random _random = new Random();
private readonly IoTSharpClient _client;
public Task LoginAsync(LoginParamsType model)
public AccountService ( IoTSharpClient client)
{
// todo: login logic
return Task.CompletedTask;
_client = client;
}
public async Task<bool> LoginAsync(LoginParamsType model)
{
return await _client.LoginAsync(model.UserName, model.Password);
}
public Task<string> GetCaptchaAsync(string modile)
......
......@@ -2,6 +2,7 @@
using System.Net.Http.Json;
using System.Threading.Tasks;
using IoTSharp.ClientApp.Models;
using IoTSharp.Sdk.Http;
namespace IoTSharp.ClientApp.Services
{
......@@ -13,15 +14,25 @@ namespace IoTSharp.ClientApp.Services
public class UserService : IUserService
{
private readonly HttpClient _httpClient;
private readonly IoTSharpClient _client;
public UserService(HttpClient httpClient)
public UserService(HttpClient httpClient, IoTSharpClient client)
{
_httpClient = httpClient;
_client = client;
}
public async Task<CurrentUser> GetCurrentUserAsync()
{
return await _httpClient.GetFromJsonAsync<CurrentUser>("data/current_user.json");
var cu = await _httpClient.GetFromJsonAsync<CurrentUser>("data/current_user.json");
var my = _client.MyInfo;
cu.Name = my.Name;
cu.Email = my.Email;
cu.Avatar = my.Avatar;
cu.Title= my.Introduction;
cu.Group = $"{my.Tenant?.Name}-{my.Customer?.Name}";
cu.Userid = _client.MyInfo.Name;
return cu ;
}
}
}
\ No newline at end of file
{
"BaseURL": "http://10.165.83.194:2927/",
"ProSettings": {
"NavTheme": "dark",
"Layout": "side",
......
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net452;netstandard2.0;netstandard2.1;net5.0</TargetFrameworks>
<TargetFrameworks>net452;net5.0</TargetFrameworks>
<RootNamespace>IoTSharp.Sdk.Http</RootNamespace>
<Product>IoTSharp</Product>
<Company>IoTSharp</Company>
......@@ -25,13 +25,10 @@
<Reference Include="System.Net.Http" />
<Reference Include="System.ComponentModel.DataAnnotations" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<ItemGroup Condition="'$(TargetFramework)' == 'net5.0'">
<PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />
<PackageReference Include="System.Net.Http" Version="4.3.4" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.1'">
<PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />
</ItemGroup>
<ItemGroup>
<None Include="..\docs\200x200.png">
<Pack>True</Pack>
......
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
namespace IoTSharp.Sdk.Http
{
public class IoTSharpClient
{
public IoTSharpClient()
{
}
public IoTSharpClient(Uri uri)
{
BaseURL = uri.ToString();
}
public bool Anonymous => (_result?.Roles?.Contains(nameof(UserRole.Anonymous))).GetValueOrDefault();
public bool CustomerAdmin => (_result?.Roles?.Contains(nameof(UserRole.CustomerAdmin))).GetValueOrDefault();
public bool NormalUser => (_result?.Roles?.Contains(nameof(UserRole.NormalUser))).GetValueOrDefault();
public bool SystemAdmin => (_result?.Roles?.Contains(nameof(UserRole.SystemAdmin))).GetValueOrDefault();
public bool TenantAdmin => (_result?.Roles?.Contains(nameof(UserRole.TenantAdmin))).GetValueOrDefault();
public TokenEntity Token => _result.Token;
public bool IsLogin => (_result?.Succeeded).GetValueOrDefault();
public bool CanLogout => IsLogin;
public bool CanLogin => !IsLogin;
public UserInfoDto MyInfo { get; set; }
public string BaseURL { get; set; } = "http://localhost:51498";
public HttpClient HttpClient { get; set; } = new HttpClient();
public T Create<T>() where T : class
{
T t = Activator.CreateInstance(typeof(T), HttpClient) as T;
typeof(T).GetProperty("BaseUrl").SetValue(t, BaseURL);
return t;
}
AccountClient _act_client;
private LoginResult _result;
public async Task<bool> LoginAsync(string username, string password)
{
try
{
_act_client = new AccountClient(HttpClient);
_result = await _act_client.LoginAsync(new LoginDto() { UserName = username, Password = password });
HttpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {Token.Access_token}");
ApiResultOfUserInfoDto userInfoDto = await _act_client.MyInfoAsync();
MyInfo = userInfoDto.Data;
}
catch (Exception ex)
{
throw new Exception("Login error ", ex);
}
return (bool)(_result?.SignIn.Succeeded);
}
public async void LogoutAsync()
{
await _act_client.LogoutAsync();
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册