|
|
|
@ -25,10 +25,10 @@ namespace Shentun.Peis |
|
|
|
.Build(); |
|
|
|
_appBaseAddress = AppConfig.GetSection("App") |
|
|
|
.GetSection("SelfUrl").Value; |
|
|
|
_appBaseAddress = AppConfig.GetSection("App") |
|
|
|
.GetSection("SelfUrl").Value; |
|
|
|
_appBaseAddress = AppConfig.GetSection("App") |
|
|
|
.GetSection("SelfUrl").Value; |
|
|
|
_selfUser = AppConfig.GetSection("App") |
|
|
|
.GetSection("SelfUser").Value; |
|
|
|
_selfPassword = AppConfig.GetSection("App") |
|
|
|
.GetSection("SelfPassword").Value; |
|
|
|
} |
|
|
|
|
|
|
|
public async Task<TOut> CallAppServiceAsync<TInput, TOut>(string url, TInput data) |
|
|
|
@ -48,7 +48,7 @@ namespace Shentun.Peis |
|
|
|
{ |
|
|
|
sendData = JsonConvert.SerializeObject(data); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
using (HttpContent httpContent = new StringContent(sendData)) |
|
|
|
{ |
|
|
|
httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); |
|
|
|
@ -60,7 +60,62 @@ namespace Shentun.Peis |
|
|
|
throw new Exception("http通信错误:" + response.StatusCode + ",结果:" + result); |
|
|
|
} |
|
|
|
result = await response.Content.ReadAsStringAsync(); |
|
|
|
|
|
|
|
var resultDto = JsonConvert.DeserializeObject<TOut>(result); |
|
|
|
if (resultDto is WebApiOutDtoBase) |
|
|
|
{ |
|
|
|
var webApiOutDtoBase = resultDto as WebApiOutDtoBase; |
|
|
|
if (webApiOutDtoBase.Code == "-1") |
|
|
|
{ |
|
|
|
throw new Exception($"调用服务失败{webApiOutDtoBase.Message}"); |
|
|
|
} |
|
|
|
} |
|
|
|
return resultDto; |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public TOut CallAppService<TInput, TOut>(string url, TInput data) |
|
|
|
{ |
|
|
|
string baseAddress = _appBaseAddress; |
|
|
|
using (var httpClientHandler = new HttpClientHandler()) |
|
|
|
{ |
|
|
|
using (var httpClient = new HttpClient(httpClientHandler)) |
|
|
|
{ |
|
|
|
httpClient.BaseAddress = new Uri(baseAddress); |
|
|
|
|
|
|
|
httpClient.DefaultRequestHeaders.Accept.Add( |
|
|
|
new MediaTypeWithQualityHeaderValue("application/json"));//设置accept标头,告诉JSON是可接受的响应类型
|
|
|
|
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _accesToken); |
|
|
|
string sendData = ""; |
|
|
|
if (data != null) |
|
|
|
{ |
|
|
|
sendData = JsonConvert.SerializeObject(data); |
|
|
|
} |
|
|
|
|
|
|
|
using (HttpContent httpContent = new StringContent(sendData)) |
|
|
|
{ |
|
|
|
httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); |
|
|
|
HttpResponseMessage response = httpClient.PostAsync(url, httpContent).Result; |
|
|
|
string result; |
|
|
|
if (!response.IsSuccessStatusCode) |
|
|
|
{ |
|
|
|
result = response.Content.ReadAsStringAsync().Result; |
|
|
|
throw new Exception("http通信错误:" + response.StatusCode + ",结果:" + result); |
|
|
|
} |
|
|
|
result = response.Content.ReadAsStringAsync().Result; |
|
|
|
|
|
|
|
var resultDto = JsonConvert.DeserializeObject<TOut>(result); |
|
|
|
if (resultDto is WebApiOutDtoBase) |
|
|
|
{ |
|
|
|
var webApiOutDtoBase = resultDto as WebApiOutDtoBase; |
|
|
|
if (webApiOutDtoBase.Code == "-1") |
|
|
|
{ |
|
|
|
throw new Exception($"调用服务失败{webApiOutDtoBase.Message}"); |
|
|
|
} |
|
|
|
} |
|
|
|
return resultDto; |
|
|
|
} |
|
|
|
|
|
|
|
@ -68,10 +123,15 @@ namespace Shentun.Peis |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public async Task LoginAsync() |
|
|
|
public async Task LoginAsync() |
|
|
|
{ |
|
|
|
await LoginAsync(_selfUser, _selfPassword); |
|
|
|
} |
|
|
|
public void Login() |
|
|
|
{ |
|
|
|
Login(_selfUser, _selfPassword); |
|
|
|
return; |
|
|
|
} |
|
|
|
public async Task LoginAsync(string userId, string password) |
|
|
|
{ |
|
|
|
|
|
|
|
@ -111,7 +171,54 @@ namespace Shentun.Peis |
|
|
|
throw new Exception($"登录失败{restultDto.Message}"); |
|
|
|
} |
|
|
|
_accesToken = restultDto.Data.access_token; |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public void Login(string userId, string password) |
|
|
|
{ |
|
|
|
|
|
|
|
using (var httpClientHandler = new HttpClientHandler()) |
|
|
|
{ |
|
|
|
using (var httpClient = new HttpClient(httpClientHandler)) |
|
|
|
{ |
|
|
|
httpClient.BaseAddress = new Uri(_appBaseAddress); |
|
|
|
|
|
|
|
httpClient.DefaultRequestHeaders.Accept.Add( |
|
|
|
new MediaTypeWithQualityHeaderValue("application/json"));//设置accept标头,告诉JSON是可接受的响应类型
|
|
|
|
var url = "api/identity/users/login"; |
|
|
|
var loginUser = new LoginInputDto() |
|
|
|
{ |
|
|
|
UserName = userId, |
|
|
|
Password = password |
|
|
|
}; |
|
|
|
var sendData = JsonConvert.SerializeObject(loginUser); |
|
|
|
using (HttpContent httpContent = new StringContent(sendData)) |
|
|
|
{ |
|
|
|
httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); |
|
|
|
HttpResponseMessage response = httpClient.PostAsync(url, httpContent).Result; |
|
|
|
string result; |
|
|
|
if (!response.IsSuccessStatusCode) |
|
|
|
{ |
|
|
|
result = response.Content.ReadAsStringAsync().Result; |
|
|
|
throw new Exception("http通信错误:" + response.StatusCode + ",结果:" + result); |
|
|
|
} |
|
|
|
result = response.Content.ReadAsStringAsync().Result; |
|
|
|
var restultDto = JsonConvert.DeserializeObject<LoginOutDto>(result); |
|
|
|
if (restultDto == null) |
|
|
|
{ |
|
|
|
throw new Exception("返回结果是空"); |
|
|
|
} |
|
|
|
if (restultDto.Code != "1") |
|
|
|
{ |
|
|
|
throw new Exception($"登录失败{restultDto.Message}"); |
|
|
|
} |
|
|
|
_accesToken = restultDto.Data.access_token; |
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|