|
|
|
@ -10,7 +10,9 @@ using Shentun.Utilities; |
|
|
|
using System; |
|
|
|
using System.Collections.Generic; |
|
|
|
using System.Data.Common; |
|
|
|
using System.IdentityModel.Tokens.Jwt; |
|
|
|
using System.Linq; |
|
|
|
using System.Linq.Dynamic.Core.Tokenizer; |
|
|
|
using System.Net.Http.Headers; |
|
|
|
using System.Text; |
|
|
|
using System.Threading.Tasks; |
|
|
|
@ -89,7 +91,7 @@ namespace Shentun.Peis.PlugIns |
|
|
|
{ |
|
|
|
DepartmentColumnReferenceId = new Guid(departmentColumnReferenceIdStr); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
var userColumnReferenceIdStr = InterfaceConfig.GetSection("Interface") |
|
|
|
.GetSection("UserColumnReferenceId").Value; |
|
|
|
if (!string.IsNullOrWhiteSpace(userColumnReferenceIdStr)) |
|
|
|
@ -126,9 +128,10 @@ namespace Shentun.Peis.PlugIns |
|
|
|
return conn; |
|
|
|
} |
|
|
|
|
|
|
|
public async Task<TOut> CallAppServiceAsync<TInput,TOut>(string url, TInput data,string method = "post") |
|
|
|
public async Task<TOut> CallAppServiceAsync<TInput, TOut>(string url, TInput data, string method = "post") |
|
|
|
{ |
|
|
|
string baseAddress = _appBaseAddress; |
|
|
|
await CheckLoginAsync(); |
|
|
|
using (var httpClientHandler = new HttpClientHandler()) |
|
|
|
{ |
|
|
|
using (var httpClient = new HttpClient(httpClientHandler)) |
|
|
|
@ -140,7 +143,7 @@ namespace Shentun.Peis.PlugIns |
|
|
|
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _accesToken); |
|
|
|
IsoDateTimeConverter timeFormat = new IsoDateTimeConverter(); |
|
|
|
timeFormat.DateTimeFormat = "yyyy-MM-dd HH:mm:ss"; |
|
|
|
var sendData = JsonConvert.SerializeObject(data,Newtonsoft.Json.Formatting.Indented, timeFormat); |
|
|
|
var sendData = JsonConvert.SerializeObject(data, Newtonsoft.Json.Formatting.Indented, timeFormat); |
|
|
|
using (HttpContent httpContent = new StringContent(sendData)) |
|
|
|
{ |
|
|
|
httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); |
|
|
|
@ -153,7 +156,7 @@ namespace Shentun.Peis.PlugIns |
|
|
|
{ |
|
|
|
response = await httpClient.GetAsync(url); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
string result; |
|
|
|
if (!response.IsSuccessStatusCode) |
|
|
|
{ |
|
|
|
@ -161,23 +164,23 @@ namespace Shentun.Peis.PlugIns |
|
|
|
throw new Exception("http通信错误:" + response.StatusCode + ",结果:" + result); |
|
|
|
} |
|
|
|
result = await response.Content.ReadAsStringAsync(); |
|
|
|
|
|
|
|
|
|
|
|
dynamic resultDto = JsonConvert.DeserializeObject(result); |
|
|
|
if(resultDto != null) |
|
|
|
if (resultDto != null) |
|
|
|
{ |
|
|
|
if(resultDto.code == "-1") |
|
|
|
if (resultDto.code == "-1") |
|
|
|
{ |
|
|
|
throw new Exception($"调用WebApi失败,返回-1,消息:"+resultDto.message); |
|
|
|
throw new Exception($"调用WebApi失败,返回-1,消息:" + resultDto.message); |
|
|
|
} |
|
|
|
JObject jsonObject = JObject.Parse(result); |
|
|
|
var returnStr = jsonObject["data"].ToString(); |
|
|
|
if(string.IsNullOrWhiteSpace(returnStr) || returnStr == "null") |
|
|
|
if (string.IsNullOrWhiteSpace(returnStr) || returnStr == "null") |
|
|
|
{ |
|
|
|
return default(TOut); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
var returnValue = JsonConvert.DeserializeObject<TOut>(returnStr); |
|
|
|
|
|
|
|
|
|
|
|
return returnValue; |
|
|
|
} |
|
|
|
return resultDto; |
|
|
|
@ -192,9 +195,9 @@ namespace Shentun.Peis.PlugIns |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
public virtual Task DoWork() |
|
|
|
public virtual Task DoWork() |
|
|
|
{ |
|
|
|
return Task.CompletedTask; |
|
|
|
return Task.CompletedTask; |
|
|
|
} |
|
|
|
|
|
|
|
public async virtual Task<LoginOutDto> LoginAsync() |
|
|
|
@ -202,9 +205,16 @@ namespace Shentun.Peis.PlugIns |
|
|
|
var relult = await LoginAsync(SelfUser, SelfPassword); |
|
|
|
return relult; |
|
|
|
} |
|
|
|
public async Task<LoginOutDto> LoginAsync(string userId,string password) |
|
|
|
public async Task<LoginOutDto> LoginAsync(string userId, string password) |
|
|
|
{ |
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(userId)) |
|
|
|
{ |
|
|
|
throw new Exception("用户ID不能为空"); |
|
|
|
} |
|
|
|
if (string.IsNullOrWhiteSpace(password)) |
|
|
|
{ |
|
|
|
throw new Exception("密码不能为空"); |
|
|
|
} |
|
|
|
using (var httpClientHandler = new HttpClientHandler()) |
|
|
|
{ |
|
|
|
using (var httpClient = new HttpClient(httpClientHandler)) |
|
|
|
@ -231,12 +241,12 @@ namespace Shentun.Peis.PlugIns |
|
|
|
throw new Exception("http通信错误:" + response.StatusCode + ",结果:" + result); |
|
|
|
} |
|
|
|
result = await response.Content.ReadAsStringAsync(); |
|
|
|
var restultDto = JsonConvert.DeserializeObject<LoginOutDto>(result); |
|
|
|
var restultDto = JsonConvert.DeserializeObject<LoginOutDto>(result); |
|
|
|
if (restultDto == null) |
|
|
|
{ |
|
|
|
throw new Exception("返回结果是空"); |
|
|
|
} |
|
|
|
if (restultDto.Code != "1") |
|
|
|
if (restultDto.Code != "1") |
|
|
|
{ |
|
|
|
throw new Exception($"登录失败{restultDto.Message}"); |
|
|
|
} |
|
|
|
@ -247,5 +257,41 @@ namespace Shentun.Peis.PlugIns |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private async Task CheckLoginAsync() |
|
|
|
{ |
|
|
|
if (string.IsNullOrWhiteSpace(_accesToken)) |
|
|
|
{ |
|
|
|
await LoginAsync(); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
var handler = new JwtSecurityTokenHandler(); |
|
|
|
var payload = handler.ReadJwtToken(_accesToken).Payload; |
|
|
|
var claims = payload.Claims; |
|
|
|
var exp = claims.First(claim => claim.Type == "exp").Value; |
|
|
|
if (exp == null) |
|
|
|
{ |
|
|
|
await LoginAsync(); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
if (long.TryParse(exp, out var expires)) |
|
|
|
{ |
|
|
|
var expireTime = DateTimeOffset.FromUnixTimeSeconds(expires).LocalDateTime; |
|
|
|
if (expireTime <= DateTime.Now) |
|
|
|
{ |
|
|
|
await LoginAsync(); |
|
|
|
} |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
await LoginAsync(); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |