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.

334 lines
11 KiB

2 years ago
  1. using EasyBL.WebApi.Message;
  2. using Newtonsoft.Json;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Net.Http;
  7. using System.Text;
  8. using System.Text.RegularExpressions;
  9. using System.Web;
  10. namespace EasyBL.WebApi.Models
  11. {
  12. public abstract class ApiServiceBase
  13. {
  14. #region Define
  15. private const string _TAG_CALLBACK = "callback";
  16. private class CQS2Json
  17. {
  18. private class CQSJsonNode
  19. {
  20. public object Data { get; set; }
  21. public override string ToString()
  22. {
  23. var sb = new StringBuilder();
  24. if (null == Data)
  25. {
  26. sb.Append("null");
  27. }
  28. else if (Data is string)
  29. {
  30. var sData = Data.ToString();
  31. if (0 < sData.Length && IsNumeric(sData))
  32. {
  33. sb.Append(Data.ToString());
  34. }
  35. else if (0 == sData.Length)
  36. {
  37. sb.Append("null");
  38. }
  39. else
  40. {
  41. sb.Append("\"" + Data + "\"");
  42. }
  43. }
  44. else if (Data is Dictionary<string, CQSJsonNode>)
  45. {
  46. var dic = Data as Dictionary<string, CQSJsonNode>;
  47. sb.Append("{");
  48. var nCount = 0;
  49. foreach (string sKey in dic.Keys)
  50. {
  51. if (0 != nCount)
  52. {
  53. sb.Append(",");
  54. }
  55. sb.Append("\"" + sKey + "\" : ");
  56. sb.Append(dic[sKey].ToString());
  57. nCount = nCount + 1;
  58. }
  59. sb.Append("}");
  60. }
  61. else if (Data is List<CQSJsonNode>)
  62. {
  63. var l = Data as List<CQSJsonNode>;
  64. sb.Append("[");
  65. var nCount = 0;
  66. foreach (CQSJsonNode n in l)
  67. {
  68. if (0 != nCount)
  69. {
  70. sb.Append(",");
  71. }
  72. sb.Append(n.ToString());
  73. nCount = nCount + 1;
  74. }
  75. sb.Append("]");
  76. }
  77. else
  78. {
  79. throw new Exception("No handle");
  80. }
  81. return sb.ToString();
  82. }
  83. public void AddKeyValue(string i_sKey, CQSJsonNode i_Value)
  84. {
  85. if (null == Data)
  86. {
  87. Data = new Dictionary<string, CQSJsonNode>();
  88. }
  89. var dic = Data as Dictionary<string, CQSJsonNode>;
  90. dic.Add(i_sKey, i_Value);
  91. }
  92. public void AppendList(CQSJsonNode i_Value)
  93. {
  94. if (null == Data)
  95. {
  96. Data = new List<CQSJsonNode>();
  97. }
  98. var l = Data as List<CQSJsonNode>;
  99. l.Add(i_Value);
  100. }
  101. public void SetValue(string i_Value)
  102. {
  103. Data = i_Value;
  104. }
  105. public CQSJsonNode GetMapValue(string i_sKey)
  106. {
  107. CQSJsonNode res = null;
  108. if (null == Data)
  109. {
  110. Data = new Dictionary<string, CQSJsonNode>();
  111. }
  112. var dic = Data as Dictionary<string, CQSJsonNode>;
  113. if (!dic.Keys.Contains(i_sKey))
  114. {
  115. res = new CQSJsonNode();
  116. dic.Add(i_sKey, res);
  117. }
  118. else
  119. {
  120. res = dic[i_sKey];
  121. }
  122. return res;
  123. }
  124. public CQSJsonNode GetListItem(int i_n)
  125. {
  126. CQSJsonNode res = null;
  127. if (null == Data)
  128. {
  129. Data = new List<CQSJsonNode>();
  130. }
  131. var l = Data as List<CQSJsonNode>;
  132. if (i_n >= l.Count)
  133. {
  134. res = new CQSJsonNode();
  135. l.Add(res);
  136. }
  137. else
  138. {
  139. res = l[i_n];
  140. }
  141. return res;
  142. }
  143. public CQSJsonNode GetMapItemByKey(string i_sKey)
  144. {
  145. CQSJsonNode nRes = null;
  146. if (Data != null && Data is Dictionary<string, CQSJsonNode>)
  147. {
  148. var dic = Data as Dictionary<string, CQSJsonNode>;
  149. nRes = dic[i_sKey];
  150. }
  151. return nRes;
  152. }
  153. public CQSJsonNode GetListItemByIndex(int i_nIdx)
  154. {
  155. CQSJsonNode nRes = null;
  156. if (Data != null && Data is List<CQSJsonNode>)
  157. {
  158. var l = Data as List<CQSJsonNode>;
  159. nRes = l[i_nIdx];
  160. }
  161. return nRes;
  162. }
  163. }
  164. public static bool IsNumeric(string inputString)
  165. {
  166. return Regex.IsMatch(inputString, "^[0-9]+$");
  167. }
  168. public static string Convert(IEnumerable<KeyValuePair<string, string>> i_iKeyValue)
  169. {
  170. var rootNode = new CQSJsonNode();
  171. CQSJsonNode curNode = null;
  172. var nCount = 0;
  173. foreach (KeyValuePair<string, string> kp in i_iKeyValue)
  174. {
  175. var v = new CQSJsonNode { Data = kp.Value };
  176. var saToken = kp.Key.Split(new char[] { '[', ']', '.' });
  177. curNode = rootNode;
  178. nCount = 0;
  179. foreach (string sToken in saToken)
  180. {
  181. if ("" == sToken)
  182. {
  183. nCount = nCount + 1;
  184. continue;
  185. }
  186. var isList = IsNumeric(sToken);
  187. if ((saToken.Length - 1) == nCount)
  188. {
  189. if (!isList)
  190. {
  191. curNode.AddKeyValue(sToken, v);
  192. }
  193. else
  194. {
  195. curNode.AppendList(v);
  196. }
  197. }
  198. else
  199. {
  200. curNode = !isList ? curNode.GetMapValue(sToken) : curNode.GetListItem(Int32.Parse(sToken));
  201. }
  202. nCount = nCount + 1;
  203. }
  204. }
  205. return rootNode.ToString();
  206. }
  207. }
  208. #endregion Define
  209. #region Abstract decleare
  210. protected abstract string HandleRequest(RequestMessage i_jo, HttpRequestMessage i_rRequest);
  211. #endregion Abstract decleare
  212. #region helper function
  213. protected ApiServiceBase()
  214. {
  215. }
  216. /// <summary>
  217. /// Converter for http query string to JObject
  218. /// </summary>
  219. /// <param name="i_iKeyValue"></param>
  220. /// <returns></returns>
  221. protected static RequestMessage HttpQueryStringToRequest(IEnumerable<KeyValuePair<string, string>> i_iKeyValue)
  222. {
  223. var sJaon = CQS2Json.Convert(i_iKeyValue);
  224. return JsonConvert.DeserializeObject<RequestMessage>(sJaon);
  225. }
  226. protected static string GetHttpRequestValue(IEnumerable<KeyValuePair<string, string>> i_iKeyValue, string i_sTarget)
  227. {
  228. string sRes = null;
  229. foreach (KeyValuePair<string, string> kp in i_iKeyValue)
  230. {
  231. if (i_sTarget == kp.Key)
  232. {
  233. sRes = kp.Value;
  234. break;
  235. }
  236. }
  237. return sRes;
  238. }
  239. /// <summary>
  240. /// Create json message
  241. /// </summary>
  242. /// <param name="i_o"></param>
  243. /// <returns></returns>
  244. protected static string MakeMessage(object i_o)
  245. {
  246. return JsonConvert.SerializeObject(i_o);
  247. }
  248. /// <summary>
  249. /// Create general error
  250. /// </summary>
  251. /// <param name="i_sMsg"></param>
  252. /// <param name="i_OriRequest">todo: describe i_OriRequest parameter on MakeErrorReturn</param>
  253. /// <returns></returns>
  254. protected static string MakeErrorReturn(RequestMessage i_OriRequest, string i_sMsg)
  255. {
  256. return MakeMessage(new ErrorResponseMessage(i_sMsg, i_OriRequest));
  257. }
  258. /// <summary>
  259. /// Create general Invalid
  260. /// </summary>
  261. /// <param name="i_OriRequest">todo: describe i_OriRequest parameter on MakeInvalidReturn</param>
  262. /// <param name="i_sMsgCode">todo: describe i_sMsgCode parameter on MakeInvalidReturn</param>
  263. /// <param name="i_sMsg">todo: describe i_sMsg parameter on MakeInvalidReturn</param>
  264. /// <returns></returns>
  265. protected static string MakeInvalidReturn(RequestMessage i_OriRequest, int i_sMsgCode, string i_sMsg)
  266. {
  267. return MakeMessage(new InvalidResponseMessage(i_OriRequest, i_sMsgCode, i_sMsg));
  268. }
  269. public string ProgessJson(string i_sJson, HttpRequestMessage i_rRequest)
  270. {
  271. RequestMessage crm = null;
  272. crm = null != i_sJson ? JsonConvert.DeserializeObject<RequestMessage>(i_sJson) : new RequestMessage { TYPE = "LITE" };
  273. crm.ClientIP = GetClientIp(i_rRequest);
  274. return HandleRequest(crm, i_rRequest);
  275. }
  276. /// <summary>
  277. /// </summary>
  278. /// <param name="i_rRequest"></param>
  279. /// <returns></returns>
  280. public static string GetClientIp(HttpRequestMessage i_rRequest)
  281. {
  282. if (i_rRequest.Properties.ContainsKey("MS_HttpContext"))
  283. {
  284. return ((HttpContextWrapper)i_rRequest.Properties["MS_HttpContext"]).Request.UserHostAddress;
  285. }
  286. else if (HttpContext.Current != null)
  287. {
  288. return HttpContext.Current.Request.UserHostAddress;
  289. }
  290. else
  291. {
  292. return null;
  293. }
  294. }
  295. #endregion helper function
  296. }
  297. }