UserService.cs 1.1 KB
Newer Older
麦壳饼's avatar
麦壳饼 已提交
1 2 3
using System.Net.Http;
using System.Net.Http.Json;
using System.Threading.Tasks;
麦壳饼's avatar
麦壳饼 已提交
4
using IoTSharp.ClientApp.Models;
5
using IoTSharp.Sdk.Http;
麦壳饼's avatar
麦壳饼 已提交
6

麦壳饼's avatar
麦壳饼 已提交
7
namespace IoTSharp.ClientApp.Services
麦壳饼's avatar
麦壳饼 已提交
8 9 10 11 12 13 14 15 16
{
    public interface IUserService
    {
        Task<CurrentUser> GetCurrentUserAsync();
    }

    public class UserService : IUserService
    {
        private readonly HttpClient _httpClient;
17
        private readonly IoTSharpClient _client;
麦壳饼's avatar
麦壳饼 已提交
18

19
        public UserService(HttpClient httpClient, IoTSharpClient client)
麦壳饼's avatar
麦壳饼 已提交
20 21
        {
            _httpClient = httpClient;
22
            _client = client;
麦壳饼's avatar
麦壳饼 已提交
23 24 25 26
        }

        public async Task<CurrentUser> GetCurrentUserAsync()
        {
27 28 29 30 31 32 33 34 35
            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 ;
麦壳饼's avatar
麦壳饼 已提交
36 37 38
        }
    }
}