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.

257 lines
7.1 KiB

2 years ago
  1. using EasyBL.WebApi.Message;
  2. using Newtonsoft.Json;
  3. using Newtonsoft.Json.Linq;
  4. using System;
  5. using System.Collections.Generic;
  6. namespace EasyBL
  7. {
  8. public class ServiceBase : MessageBase
  9. {
  10. public const string SERVICE = "service";
  11. protected Dictionary<string, object> _dicInitData;
  12. private static string _sRootPath = null;
  13. public static string RootPath { get { return _sRootPath; } }
  14. public static void SetRootPath(string i_sRootPath)
  15. {
  16. _sRootPath = i_sRootPath;
  17. }
  18. #region 1-1-1
  19. /// <summary>
  20. /// 1-1-1 JS路徑
  21. /// </summary>
  22. protected virtual string JSPath
  23. {
  24. get { return "NOT SET"; }
  25. }
  26. /// <summary>
  27. /// 1-1-1 HTML路徑
  28. /// </summary>
  29. protected virtual string HTMLPath
  30. {
  31. get { return "NOT SET"; }
  32. }
  33. /// <summary>
  34. /// 1-1-1 JS Version
  35. /// </summary>
  36. protected string JSVersion
  37. {
  38. get { return _getVersion(JSPath, "g_JSRevisionVersioin"); }
  39. }
  40. /// <summary>
  41. /// 1-1-1 JS Version
  42. /// </summary>
  43. protected string HTMLVersion
  44. {
  45. get { return _getVersion(HTMLPath, "g_HTMLRevisionVersioin"); }
  46. }
  47. /// <summary>
  48. /// 取得Service版本號
  49. /// </summary>
  50. protected string ServiceVersion
  51. {
  52. get { return ParseServiceVersion(); }
  53. }
  54. private static string _getVersion(string i_sPath, string i_sPattern)
  55. {
  56. var sRes = "-1";
  57. try
  58. {
  59. do
  60. {
  61. if (i_sPath == "NOT SET")
  62. {
  63. break;
  64. }
  65. var sPath = System.IO.Path.Combine(RootPath, i_sPath);
  66. if (!System.IO.File.Exists(sPath))
  67. {
  68. break;
  69. }
  70. var sContent = System.IO.File.ReadAllText(sPath);
  71. var nIdx = sContent.LastIndexOf(i_sPattern, StringComparison.Ordinal);
  72. if (nIdx == -1)
  73. {
  74. break;
  75. }
  76. var nVersionStart = sContent.IndexOf('"', nIdx);
  77. if (nVersionStart == -1)
  78. {
  79. break;
  80. }
  81. nVersionStart++;
  82. var nVersionEnd = sContent.IndexOf('"', nVersionStart);
  83. if (nVersionEnd == -1)
  84. {
  85. break;
  86. }
  87. sRes = sContent.Substring(nVersionStart, nVersionEnd - nVersionStart);
  88. }
  89. while (false);
  90. }
  91. catch (Exception ex)
  92. {
  93. sRes = Util.GetLastExceptionMsg(ex);
  94. }
  95. return sRes;
  96. }
  97. /// <summary>
  98. /// 取得Service版本號
  99. /// </summary>
  100. /// <returns>版號。 -1表示沒有取得版本號</returns>
  101. protected string ParseServiceVersion()
  102. {
  103. var sRes = "-1";
  104. var pi = this.GetType().GetProperty("ServiceRevisionVersion");
  105. if (pi != null)
  106. {
  107. var oValue = pi.GetValue(this, null);
  108. if (oValue != null)
  109. {
  110. sRes = oValue.ToString();
  111. }
  112. }
  113. return sRes;
  114. }
  115. public Dictionary<string, string> VersionInfo
  116. {
  117. get
  118. {
  119. return new Dictionary<string, string>() {
  120. { "html", HTMLVersion },
  121. { "js", JSVersion },
  122. { "service", ServiceVersion },
  123. { "htmlpath", HTMLPath },
  124. { "jspath", JSPath },
  125. { "servicename", this.GetType().Name }
  126. };
  127. }
  128. }
  129. #endregion 1-1-1
  130. public ResponseMessage Entry(RequestMessage i_crm)
  131. {
  132. string sMsg = null;
  133. ResponseMessage rm = null;
  134. try
  135. {
  136. do
  137. {
  138. // Find Native Method
  139. var sFunctionName = i_crm.TYPE;
  140. if (null == sFunctionName)
  141. {
  142. //sMsg = string.Format("NO METHOD - {0}", sFunctionName);
  143. sMsg = BaseExceptionWord.ex000001; //NO METHOD
  144. break;
  145. }
  146. var mi = this.GetType().GetMethod(sFunctionName);
  147. if (null == mi)
  148. {
  149. //sMsg = string.Format("NO MATCH METHOD - {0}", sFunctionName);
  150. sMsg = BaseExceptionWord.ex000002; //NO MATCH METHOD
  151. break;
  152. }
  153. object[] ao = { i_crm };
  154. rm = (ResponseMessage)mi.Invoke(this, ao);
  155. }
  156. while (false);
  157. }
  158. catch (Exception ex)
  159. {
  160. sMsg = Util.GetLastExceptionMsg(ex);
  161. }
  162. if (null != sMsg)
  163. {
  164. //crm = new ErrorResponseMessage(sMsg, i_crm);
  165. }
  166. return rm;
  167. }
  168. public static string MakeDebugFullDump(string i_sMsg, RequestMessage i_crm)
  169. {
  170. var sRes = Environment.NewLine;
  171. sRes += "**** Start Dump **************************" + Environment.NewLine;
  172. sRes += $"Custom Message:{i_sMsg}" + Environment.NewLine;
  173. sRes += $"Parameter Json:{JsonConvert.SerializeObject(i_crm)}" + Environment.NewLine;
  174. sRes += "Call Stack Dump:" + Environment.NewLine;
  175. sRes += Environment.StackTrace + Environment.NewLine;
  176. sRes += "*****End Dump *************************" + Environment.NewLine;
  177. return sRes;
  178. }
  179. public static string JProperty2Dic(JObject i_jpData, ref Dictionary<string, object> io_dicData)
  180. {
  181. string sRes = null;
  182. var dicRes = new Dictionary<string, object>();
  183. try
  184. {
  185. foreach (JProperty property in i_jpData.Properties())
  186. {
  187. var jv = property.Value;
  188. dicRes.Add(property.Name, jv.Value<string>());
  189. }
  190. }
  191. catch (Exception ex)
  192. {
  193. sRes = Util.GetLastExceptionMsg(ex);
  194. }
  195. io_dicData = dicRes;
  196. return sRes;
  197. }
  198. #region JsonToString
  199. /// <summary>
  200. /// 轉換json字串
  201. /// </summary>
  202. /// <param name="o">要轉化的物件</param>
  203. public static string JsonToString(object o)
  204. {
  205. if (o != null)
  206. {
  207. return JsonConvert.SerializeObject(o, Formatting.Indented);//序列化,將物件轉化為字符串
  208. }
  209. return "";
  210. }
  211. #endregion JsonToString
  212. }
  213. }