You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

230 lines
9.7 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. using Microsoft.Data.SqlClient;
  2. using Microsoft.Extensions.Configuration;
  3. using Newtonsoft.Json;
  4. using Newtonsoft.Json.Converters;
  5. using Npgsql;
  6. using Oracle.ManagedDataAccess.Client;
  7. using Shentun.Utilities;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Data.Common;
  11. using System.Linq;
  12. using System.Net.Http.Headers;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. namespace Shentun.Peis.PlugIns
  16. {
  17. public class PlugInsBase
  18. {
  19. protected IConfiguration AppConfig;
  20. protected IConfiguration InterfaceConfig;
  21. protected string? AppConnctionStr;
  22. private string _appBaseAddress;
  23. private string _accesToken;
  24. protected string? AppUser;
  25. protected string? AppPassword;
  26. protected string InterfaceSql;
  27. protected string InterfaceSqlKeyColumn;
  28. protected string InterfaceDbType;
  29. protected string? InterfaceConnctionStr;
  30. protected string? InterfaceWebApiAddress;
  31. protected Guid ItemColumnReferenceId;
  32. protected Guid AsbitemColumnReferenceId;
  33. protected Guid DepartmentColumnReferenceId;
  34. protected Guid UserColumnReferenceId;
  35. static PlugInsBase()
  36. {
  37. Dapper.DefaultTypeMap.MatchNamesWithUnderscores = true;
  38. }
  39. public PlugInsBase(string parmValue)
  40. {
  41. AppConfig = new ConfigurationBuilder()
  42. .SetBasePath(DirectoryHelper.GetAppDirectory()) // 设置基础路径为当前目录
  43. .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
  44. .Build();
  45. AppConnctionStr = AppConfig.GetSection("ConnectionStrings")
  46. .GetSection("Default").Value;
  47. _appBaseAddress = AppConfig.GetSection("App")
  48. .GetSection("SelfUrl").Value;
  49. AppUser = AppConfig.GetSection("App")
  50. .GetSection("AppUser").Value;
  51. AppPassword = AppConfig.GetSection("App")
  52. .GetSection("AppPassword").Value;
  53. var configurationBuilder = new ConfigurationBuilder()
  54. .AddJsonStream(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(parmValue)));
  55. InterfaceConfig = configurationBuilder.Build();
  56. InterfaceSql = InterfaceConfig.GetSection("Interface").GetSection("Sql").Value;
  57. InterfaceSqlKeyColumn = InterfaceConfig.GetSection("Interface").GetSection("SqlKeyColumn").Value;
  58. InterfaceDbType = InterfaceConfig.GetSection("Interface").GetSection("DbType").Value;
  59. InterfaceWebApiAddress = InterfaceConfig.GetSection("Interface").GetSection("WebApiAddress").Value;
  60. InterfaceConnctionStr = InterfaceConfig.GetSection("Interface").GetSection("ConnectionStrings").Value;
  61. var itemColumnReferenceIdStr = InterfaceConfig.GetSection("Interface")
  62. .GetSection("ItemColumnReferenceId").Value;
  63. if (!string.IsNullOrWhiteSpace(itemColumnReferenceIdStr))
  64. {
  65. ItemColumnReferenceId = new Guid(itemColumnReferenceIdStr);
  66. }
  67. var asbItemColumnReferenceIdStr = InterfaceConfig.GetSection("Interface")
  68. .GetSection("AsbitemColumnReferenceId").Value;
  69. if (!string.IsNullOrWhiteSpace(asbItemColumnReferenceIdStr))
  70. {
  71. AsbitemColumnReferenceId = new Guid(asbItemColumnReferenceIdStr);
  72. }
  73. var departmentColumnReferenceIdStr = InterfaceConfig.GetSection("Interface")
  74. .GetSection("DepartmentColumnReferenceId").Value;
  75. if (!string.IsNullOrWhiteSpace(departmentColumnReferenceIdStr))
  76. {
  77. DepartmentColumnReferenceId = new Guid(departmentColumnReferenceIdStr);
  78. }
  79. var userColumnReferenceIdStr = InterfaceConfig.GetSection("Interface")
  80. .GetSection("UserColumnReferenceId").Value;
  81. if (!string.IsNullOrWhiteSpace(userColumnReferenceIdStr))
  82. {
  83. UserColumnReferenceId = new Guid(userColumnReferenceIdStr);
  84. }
  85. }
  86. protected DbConnection CreateInterfaceDbConnect()
  87. {
  88. DbConnection conn;
  89. if (string.IsNullOrWhiteSpace(InterfaceConnctionStr))
  90. {
  91. throw new ArgumentException("数据连接设置中的DbType不能为空");
  92. }
  93. InterfaceDbType = InterfaceDbType.ToLower();
  94. if (InterfaceDbType == "sqlserver")
  95. {
  96. conn = new SqlConnection(InterfaceConnctionStr);
  97. }
  98. else if (InterfaceDbType == "postgres")
  99. {
  100. conn = new NpgsqlConnection(InterfaceConnctionStr);
  101. }
  102. else if (InterfaceDbType == "oracle")
  103. {
  104. conn = new OracleConnection(InterfaceConnctionStr);
  105. }
  106. else
  107. {
  108. throw new ArgumentException("数据连接设置中的DbType不支持");
  109. }
  110. return conn;
  111. }
  112. public async Task<TOut> CallAppServiceAsync<TInput,TOut>(string url, TInput data)
  113. {
  114. string baseAddress = _appBaseAddress;
  115. using (var httpClientHandler = new HttpClientHandler())
  116. {
  117. using (var httpClient = new HttpClient(httpClientHandler))
  118. {
  119. httpClient.BaseAddress = new Uri(baseAddress);
  120. httpClient.DefaultRequestHeaders.Accept.Add(
  121. new MediaTypeWithQualityHeaderValue("application/json"));//设置accept标头,告诉JSON是可接受的响应类型
  122. //httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _accesToken);
  123. IsoDateTimeConverter timeFormat = new IsoDateTimeConverter();
  124. timeFormat.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
  125. var sendData = JsonConvert.SerializeObject(data,Newtonsoft.Json.Formatting.Indented, timeFormat);
  126. using (HttpContent httpContent = new StringContent(sendData))
  127. {
  128. httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
  129. HttpResponseMessage response = await httpClient.PostAsync(url, httpContent);
  130. string result;
  131. if (!response.IsSuccessStatusCode)
  132. {
  133. result = response.Content.ReadAsStringAsync().Result;
  134. throw new Exception("http通信错误:" + response.StatusCode + ",结果:" + result);
  135. }
  136. result = await response.Content.ReadAsStringAsync();
  137. dynamic resultDto = JsonConvert.DeserializeObject<TOut>(result);
  138. if(resultDto != null)
  139. {
  140. if(resultDto.code == "-1")
  141. {
  142. throw new Exception($"调用WebApi失败,返回-1,消息:"+resultDto.message);
  143. }
  144. }
  145. return resultDto;
  146. }
  147. }
  148. }
  149. }
  150. public virtual async Task DoWorkAsync()
  151. {
  152. }
  153. public virtual Task DoWork()
  154. {
  155. return Task.CompletedTask;
  156. }
  157. protected async virtual Task<LoginOutDto> LoginAsync()
  158. {
  159. var relult = await LoginAsync(AppUser, AppPassword);
  160. return relult;
  161. }
  162. public async Task<LoginOutDto> LoginAsync(string userId,string password)
  163. {
  164. using (var httpClientHandler = new HttpClientHandler())
  165. {
  166. using (var httpClient = new HttpClient(httpClientHandler))
  167. {
  168. httpClient.BaseAddress = new Uri(_appBaseAddress);
  169. httpClient.DefaultRequestHeaders.Accept.Add(
  170. new MediaTypeWithQualityHeaderValue("application/json"));//设置accept标头,告诉JSON是可接受的响应类型
  171. var url = "api/identity/users/login";
  172. var loginUser = new LoginInputDto()
  173. {
  174. UserName = userId,
  175. Password = password
  176. };
  177. var sendData = JsonConvert.SerializeObject(loginUser);
  178. using (HttpContent httpContent = new StringContent(sendData))
  179. {
  180. httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
  181. HttpResponseMessage response = await httpClient.PostAsync(url, httpContent);
  182. string result;
  183. if (!response.IsSuccessStatusCode)
  184. {
  185. result = response.Content.ReadAsStringAsync().Result;
  186. throw new Exception("http通信错误:" + response.StatusCode + ",结果:" + result);
  187. }
  188. result = await response.Content.ReadAsStringAsync();
  189. var restultDto = JsonConvert.DeserializeObject<LoginOutDto>(result);
  190. if (restultDto == null)
  191. {
  192. throw new Exception("返回结果是空");
  193. }
  194. if (restultDto.Code != "1")
  195. {
  196. throw new Exception($"登录失败{restultDto.Message}");
  197. }
  198. _accesToken = restultDto.Data.access_token;
  199. return restultDto;
  200. }
  201. }
  202. }
  203. }
  204. }
  205. }