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.

166 lines
6.7 KiB

2 years ago
  1. using EasyBL.WebApi.Message;
  2. using Newtonsoft.Json;
  3. using System;
  4. using System.Data;
  5. using System.Linq;
  6. using System.Net.Http;
  7. using System.Reflection;
  8. using System.Text;
  9. using System.Web;
  10. using System.Web.Http;
  11. namespace EasyBL.WebApi.Models
  12. {
  13. public partial class CmdService : ApiServiceBase
  14. {
  15. static public string DecodeParm(string i_sEncodedData)
  16. {
  17. i_sEncodedData = i_sEncodedData.Replace(" ", "+");
  18. var encodedDataAsBytes = Convert.FromBase64String(i_sEncodedData);
  19. var returnValue = ASCIIEncoding.ASCII.GetString(encodedDataAsBytes);
  20. return HttpUtility.UrlDecode(returnValue);
  21. }
  22. public HttpResponseMessage GetData([FromBody]dynamic i_value, bool i_bDecode, HttpRequestMessage i_rRequest)
  23. {
  24. string sRes = null;
  25. try
  26. {
  27. string value = (i_bDecode) ? DecodeParm(i_value) : null;
  28. sRes = ProgessJson(value, i_rRequest);
  29. }
  30. catch (Exception ex)
  31. {
  32. var exCur = ex;
  33. while (null != exCur.InnerException)
  34. {
  35. exCur = exCur.InnerException;
  36. }
  37. sRes = JsonConvert.SerializeObject(new ErrorResponseMessage(exCur.Message));
  38. }
  39. return new HttpResponseMessage
  40. {
  41. Content = new StringContent(sRes, Encoding.UTF8, "application/json")
  42. };
  43. }
  44. protected override string HandleRequest(RequestMessage i_joRequest, HttpRequestMessage i_rRequest)
  45. {
  46. var sRes = "";
  47. try
  48. {
  49. do
  50. {
  51. if (i_joRequest == null || string.IsNullOrEmpty(i_joRequest.TYPE) || string.IsNullOrEmpty(i_joRequest.MODULE))
  52. {
  53. sRes = MakeErrorReturn(i_joRequest, string.Format(BLWording.REQUEST_IS_NULL));
  54. break;
  55. }
  56. var sModuleType = i_joRequest.CUSTOMDATA.ContainsKey("module_id") ? i_joRequest.CUSTOMDATA["module_id"] : "";
  57. var sModuleName = i_joRequest.MODULE + "Service";
  58. var sCreateError = GetInstByClassName(sModuleName, sModuleType, out object oModule);
  59. if (sCreateError != null || oModule == null)
  60. {
  61. sRes = MakeErrorReturn(i_joRequest, sCreateError);
  62. break;
  63. }
  64. if (!(oModule is ServiceBase bls))
  65. {
  66. sRes = MakeErrorReturn(i_joRequest, BLWording.COVERT_FAIL);
  67. break;
  68. }
  69. sRes = MakeMessage(bls.Entry(i_joRequest));
  70. }
  71. while (false);
  72. }
  73. catch (Exception e)
  74. {
  75. sRes = MakeErrorReturn(i_joRequest, e.Message);
  76. }
  77. if (string.IsNullOrWhiteSpace(sRes))
  78. {
  79. sRes = MakeErrorReturn(i_joRequest, "Unknow Error");
  80. }
  81. return sRes;
  82. }
  83. /// <summary>
  84. /// </summary>
  85. /// <param name="i_sTypeName"></param>
  86. /// <param name="i_sModuleId"></param>
  87. /// <param name="o_oRes"></param>
  88. /// <returns></returns>
  89. public static string GetInstByClassName(string i_sTypeName, string i_sModuleId, out object o_oRes)
  90. {
  91. object obj2 = null;
  92. string str = null;
  93. var typeByTypeName = GetTypeByTypeName(i_sTypeName, i_sModuleId);
  94. if (typeByTypeName == null)
  95. {
  96. str = "NO THIS ENTITY";
  97. }
  98. else
  99. {
  100. obj2 = Activator.CreateInstance(typeByTypeName);
  101. if (obj2 == null)
  102. {
  103. str = "ENTITY CREATE FAIL";
  104. }
  105. }
  106. o_oRes = obj2;
  107. return str;
  108. }
  109. /// <summary>
  110. /// </summary>
  111. /// <param name="i_sTypeName"></param>
  112. /// <param name="i_sModuleId"></param>
  113. /// <returns></returns>
  114. public static Type GetTypeByTypeName(string i_sTypeName, string i_sModuleId)//
  115. {
  116. Type type = null;
  117. var codeBase = Assembly.GetExecutingAssembly().GetName().CodeBase;
  118. codeBase = codeBase.Substring(0, codeBase.LastIndexOf("/"));
  119. var assemblyArray = (from f in AppDomain.CurrentDomain.GetAssemblies()
  120. where !f.IsDynamic
  121. && f.CodeBase != null
  122. && f.CodeBase.StartsWith(codeBase, StringComparison.Ordinal)
  123. && f.IsDefined(typeof(AssemblyCompanyAttribute), false)
  124. && f.IsDefined(typeof(AssemblyDescriptionAttribute), false)
  125. && f.IsDefined(typeof(AssemblyProductAttribute), false)
  126. && f.GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false).OfType<AssemblyDescriptionAttribute>().FirstOrDefault().Description == "Service"
  127. && ((f.GetCustomAttributes(typeof(AssemblyProductAttribute), false).OfType<AssemblyProductAttribute>().FirstOrDefault().Product.Equals(nameof(EasyBL)))
  128. || (f.GetCustomAttributes(typeof(AssemblyProductAttribute), false).OfType<AssemblyProductAttribute>().FirstOrDefault().Product.Contains("EasyBL." + i_sModuleId.ToUpper())))
  129. orderby f.GetCustomAttributes(typeof(AssemblyProductAttribute), false).OfType<AssemblyProductAttribute>().FirstOrDefault().Product descending
  130. select f).ToArray<Assembly>();
  131. // && f.GetCustomAttributes(typeof(AssemblyCompanyAttribute),
  132. // false).OfType<AssemblyCompanyAttribute>().FirstOrDefault() != null &&
  133. // f.GetCustomAttributes(typeof(AssemblyDescriptionAttribute),
  134. // false).OfType<AssemblyDescriptionAttribute>().FirstOrDefault() != null &&
  135. // f.GetCustomAttributes(typeof(AssemblyProductAttribute),
  136. // false).OfType<AssemblyProductAttribute>().FirstOrDefault() != null
  137. foreach (Assembly assembly2 in assemblyArray)
  138. {
  139. var typeArray = assembly2.GetTypes().Where(x => x.IsSubclassOf(typeof(ServiceBase))).ToArray<Type>();
  140. foreach (Type classType in typeArray)
  141. {
  142. if (classType.IsClass && (classType.Name == i_sTypeName))
  143. {
  144. type = classType;
  145. return type;
  146. }
  147. }
  148. }
  149. return type;
  150. }
  151. }
  152. }