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.

206 lines
8.3 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. using System.Web.Script.Serialization;
  12. using System.Collections.Generic;
  13. namespace EasyBL.WebApi.Models
  14. {
  15. public partial class CmdService : ApiServiceBase
  16. {
  17. static public string DecodeParm(string i_sEncodedData)
  18. {
  19. i_sEncodedData = i_sEncodedData.Replace(" ", "+");
  20. var encodedDataAsBytes = Convert.FromBase64String(i_sEncodedData);
  21. var returnValue = ASCIIEncoding.ASCII.GetString(encodedDataAsBytes);
  22. return HttpUtility.UrlDecode(returnValue);
  23. }
  24. public HttpResponseMessage GetData([FromBody]dynamic i_value, bool i_bDecode, HttpRequestMessage i_rRequest)
  25. {
  26. string sRes = null;
  27. try
  28. {
  29. string value = (i_bDecode) ? DecodeParm(i_value) : null;
  30. sRes = ProgessJson(value, i_rRequest);
  31. }
  32. catch (Exception ex)
  33. {
  34. var exCur = ex;
  35. while (null != exCur.InnerException)
  36. {
  37. exCur = exCur.InnerException;
  38. }
  39. sRes = JsonConvert.SerializeObject(new ErrorResponseMessage(exCur.Message));
  40. }
  41. return new HttpResponseMessage
  42. {
  43. Content = new StringContent(sRes, Encoding.UTF8, "application/json")
  44. };
  45. }
  46. class ModuleType
  47. {
  48. public string Module { get; set; }
  49. public string Type { get; set; }
  50. public string Demo { get; set; }
  51. }
  52. protected override string HandleRequest(RequestMessage i_joRequest, HttpRequestMessage i_rRequest)
  53. {
  54. var sRes = "";
  55. try
  56. {
  57. do
  58. {
  59. bool blnDemo = false;
  60. string strPage = "/Demo/ModuleType.json";
  61. string strPath = HttpContext.Current.Server.MapPath(strPage);
  62. if (System.IO.File.Exists(strPath))
  63. {
  64. string content = System.IO.File.ReadAllText(strPath);
  65. JavaScriptSerializer js = new JavaScriptSerializer();
  66. List<ModuleType> list = js.Deserialize<List<ModuleType>>(content);
  67. foreach (ModuleType vo in list)
  68. {
  69. if (vo.Demo == "Y" && i_joRequest.TYPE == vo.Type && i_joRequest.MODULE == vo.Module)
  70. {
  71. blnDemo = true;
  72. }
  73. }
  74. strPage = "/Demo/" + i_joRequest.MODULE + "_" + i_joRequest.TYPE + ".json";
  75. strPath = HttpContext.Current.Server.MapPath(strPage);
  76. }
  77. //file exsist
  78. if (blnDemo && System.IO.File.Exists(strPath))
  79. {
  80. sRes = System.IO.File.ReadAllText(strPath);
  81. } else
  82. {
  83. if (i_joRequest == null || string.IsNullOrEmpty(i_joRequest.TYPE) || string.IsNullOrEmpty(i_joRequest.MODULE))
  84. {
  85. sRes = MakeErrorReturn(i_joRequest, string.Format(BLWording.REQUEST_IS_NULL));
  86. break;
  87. }
  88. var sModuleType = i_joRequest.CUSTOMDATA.ContainsKey("module_id") ? i_joRequest.CUSTOMDATA["module_id"] : "";
  89. var sModuleName = i_joRequest.MODULE + "Service";
  90. var sCreateError = GetInstByClassName(sModuleName, sModuleType, out object oModule);
  91. if (sCreateError != null || oModule == null)
  92. {
  93. sRes = MakeErrorReturn(i_joRequest, sCreateError);
  94. break;
  95. }
  96. if (!(oModule is ServiceBase bls))
  97. {
  98. sRes = MakeErrorReturn(i_joRequest, BLWording.COVERT_FAIL);
  99. break;
  100. }
  101. sRes = MakeMessage(bls.Entry(i_joRequest));
  102. }
  103. }
  104. while (false);
  105. }
  106. catch (Exception e)
  107. {
  108. sRes = MakeErrorReturn(i_joRequest, e.Message);
  109. }
  110. if (string.IsNullOrWhiteSpace(sRes))
  111. {
  112. sRes = MakeErrorReturn(i_joRequest, "Unknow Error");
  113. }
  114. return sRes;
  115. }
  116. /// <summary>
  117. /// </summary>
  118. /// <param name="i_sTypeName"></param>
  119. /// <param name="i_sModuleId"></param>
  120. /// <param name="o_oRes"></param>
  121. /// <returns></returns>
  122. public static string GetInstByClassName(string i_sTypeName, string i_sModuleId, out object o_oRes)
  123. {
  124. object obj2 = null;
  125. string str = null;
  126. var typeByTypeName = GetTypeByTypeName(i_sTypeName, i_sModuleId);
  127. if (typeByTypeName == null)
  128. {
  129. str = "NO THIS ENTITY";
  130. }
  131. else
  132. {
  133. obj2 = Activator.CreateInstance(typeByTypeName);
  134. if (obj2 == null)
  135. {
  136. str = "ENTITY CREATE FAIL";
  137. }
  138. }
  139. o_oRes = obj2;
  140. return str;
  141. }
  142. /// <summary>
  143. /// </summary>
  144. /// <param name="i_sTypeName"></param>
  145. /// <param name="i_sModuleId"></param>
  146. /// <returns></returns>
  147. public static Type GetTypeByTypeName(string i_sTypeName, string i_sModuleId)//
  148. {
  149. Type type = null;
  150. var codeBase = Assembly.GetExecutingAssembly().GetName().CodeBase;
  151. codeBase = codeBase.Substring(0, codeBase.LastIndexOf("/"));
  152. var assemblyArray = (from f in AppDomain.CurrentDomain.GetAssemblies()
  153. where !f.IsDynamic
  154. && f.CodeBase != null
  155. && f.CodeBase.StartsWith(codeBase, StringComparison.Ordinal)
  156. && f.IsDefined(typeof(AssemblyCompanyAttribute), false)
  157. && f.IsDefined(typeof(AssemblyDescriptionAttribute), false)
  158. && f.IsDefined(typeof(AssemblyProductAttribute), false)
  159. && f.GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false).OfType<AssemblyDescriptionAttribute>().FirstOrDefault().Description == "Service"
  160. && ((f.GetCustomAttributes(typeof(AssemblyProductAttribute), false).OfType<AssemblyProductAttribute>().FirstOrDefault().Product.Equals(nameof(EasyBL)))
  161. || (f.GetCustomAttributes(typeof(AssemblyProductAttribute), false).OfType<AssemblyProductAttribute>().FirstOrDefault().Product.Contains("EasyBL." + i_sModuleId.ToUpper())))
  162. orderby f.GetCustomAttributes(typeof(AssemblyProductAttribute), false).OfType<AssemblyProductAttribute>().FirstOrDefault().Product descending
  163. select f).ToArray<Assembly>();
  164. // && f.GetCustomAttributes(typeof(AssemblyCompanyAttribute),
  165. // false).OfType<AssemblyCompanyAttribute>().FirstOrDefault() != null &&
  166. // f.GetCustomAttributes(typeof(AssemblyDescriptionAttribute),
  167. // false).OfType<AssemblyDescriptionAttribute>().FirstOrDefault() != null &&
  168. // f.GetCustomAttributes(typeof(AssemblyProductAttribute),
  169. // false).OfType<AssemblyProductAttribute>().FirstOrDefault() != null
  170. foreach (Assembly assembly2 in assemblyArray)
  171. {
  172. var typeArray = assembly2.GetTypes().Where(x => x.IsSubclassOf(typeof(ServiceBase))).ToArray<Type>();
  173. foreach (Type classType in typeArray)
  174. {
  175. if (classType.IsClass && (classType.Name == i_sTypeName))
  176. {
  177. type = classType;
  178. return type;
  179. }
  180. }
  181. }
  182. return type;
  183. }
  184. }
  185. }