DefaultAopClient.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using Aop.Api.Parser;
  5. using Aop.Api.Request;
  6. using Aop.Api.Util;
  7. using System.Text;
  8. using System.Web;
  9. using System.IO;
  10. using System.Net;
  11. using System.Xml;
  12. using Jayrock.Json;
  13. using Jayrock.Json.Conversion;
  14. using System.Threading.Tasks;
  15. namespace Aop.Api
  16. {
  17. /// <summary>
  18. /// AOP客户端。
  19. /// </summary>
  20. public class DefaultAopClient : IAopClient
  21. {
  22. public const string APP_ID = "app_id";
  23. public const string FORMAT = "format";
  24. public const string METHOD = "method";
  25. public const string TIMESTAMP = "timestamp";
  26. public const string VERSION = "version";
  27. public const string SIGN_TYPE = "sign_type";
  28. public const string ACCESS_TOKEN = "auth_token";
  29. public const string SIGN = "sign";
  30. public const string TERMINAL_TYPE = "terminal_type";
  31. public const string TERMINAL_INFO = "terminal_info";
  32. public const string PROD_CODE = "prod_code";
  33. public const string NOTIFY_URL = "notify_url";
  34. public const string CHARSET = "charset";
  35. public const string ENCRYPT_TYPE = "encrypt_type";
  36. public const string BIZ_CONTENT = "biz_content";
  37. public const string APP_AUTH_TOKEN = "app_auth_token";
  38. public const string RETURN_URL = "return_url";
  39. private string version;
  40. private string format;
  41. private string serverUrl;
  42. private string appId;
  43. private string privateKeyPem;
  44. private string signType = "RSA";
  45. private string charset;
  46. private string alipayPublicKey;
  47. private bool keyFromFile = false;
  48. private string httpmethod;
  49. public string return_url;
  50. public string notify_url;
  51. private string encyptKey;
  52. private string encyptType = "AES";
  53. private WebUtils webUtils;
  54. public string Version
  55. {
  56. get { return version != null ? version : "1.0"; }
  57. set { version = value; }
  58. }
  59. public string Format
  60. {
  61. get { return format != null ? format : "json"; }
  62. set { format = value; }
  63. }
  64. public string AppId
  65. {
  66. get { return appId; }
  67. set { appId = value; }
  68. }
  69. #region DefaultAopClient Constructors
  70. public DefaultAopClient(string serverUrl, string appId, string privateKeyPem)
  71. {
  72. this.appId = appId;
  73. this.privateKeyPem = privateKeyPem;
  74. this.serverUrl = serverUrl;
  75. this.webUtils = new WebUtils();
  76. }
  77. public DefaultAopClient(string serverUrl, string appId, string privateKeyPem, bool keyFromFile)
  78. {
  79. this.appId = appId;
  80. this.privateKeyPem = privateKeyPem;
  81. this.serverUrl = serverUrl;
  82. this.keyFromFile = keyFromFile;
  83. this.webUtils = new WebUtils();
  84. }
  85. public DefaultAopClient(string serverUrl, string appId, string privateKeyPem, string format)
  86. {
  87. this.appId = appId;
  88. this.privateKeyPem = privateKeyPem;
  89. this.serverUrl = serverUrl;
  90. this.format = format;
  91. this.webUtils = new WebUtils();
  92. }
  93. public DefaultAopClient(string serverUrl, string appId, string privateKeyPem, string format, string charset)
  94. : this(serverUrl, appId, privateKeyPem, format)
  95. {
  96. this.charset = charset;
  97. }
  98. public DefaultAopClient(string serverUrl, string appId, string privateKeyPem, string format, string version, string signType)
  99. : this(serverUrl, appId, privateKeyPem)
  100. {
  101. this.format = format;
  102. this.version = version;
  103. this.signType = signType;
  104. }
  105. public DefaultAopClient(string serverUrl, string appId, string privateKeyPem, string format, string version, string signType, string alipayPulicKey)
  106. : this(serverUrl, appId, privateKeyPem, format, version, signType)
  107. {
  108. this.alipayPublicKey = alipayPulicKey;
  109. }
  110. public DefaultAopClient(string serverUrl, string appId, string privateKeyPem, string format, string version, string signType, string alipayPulicKey, string charset)
  111. : this(serverUrl, appId, privateKeyPem, format, version, signType, alipayPulicKey)
  112. {
  113. this.charset = charset;
  114. }
  115. //
  116. public DefaultAopClient(string serverUrl, string appId, string privateKeyPem, string format, string version, string signType, string alipayPulicKey, string charset, bool keyFromFile)
  117. : this(serverUrl, appId, privateKeyPem, format, version, signType, alipayPulicKey)
  118. {
  119. this.keyFromFile = keyFromFile;
  120. this.charset = charset;
  121. }
  122. public DefaultAopClient(string serverUrl, string appId, string privateKeyPem, string format, string version, string signType, string alipayPulicKey, string charset, string encyptKey)
  123. : this(serverUrl, appId, privateKeyPem, format, version, signType, alipayPulicKey, charset)
  124. {
  125. this.encyptKey = encyptKey;
  126. this.encyptType = "AES";
  127. }
  128. public void SetTimeout(int timeout)
  129. {
  130. webUtils.Timeout = timeout;
  131. }
  132. #endregion
  133. #region IAopClient Members
  134. public async Task<T> Execute<T>(IAopRequest<T> request) where T : AopResponse
  135. {
  136. return await Execute<T>(request, null);
  137. }
  138. public async Task<T> Execute<T>(IAopRequest<T> request, string accessToken) where T : AopResponse
  139. {
  140. return await Execute<T>(request, accessToken, null);
  141. }
  142. #endregion
  143. #region IAopClient Members
  144. public async Task<T> pageExecute<T>(IAopRequest<T> request) where T : AopResponse
  145. {
  146. return await pageExecute<T>(request, null, "POST");
  147. }
  148. #endregion
  149. #region IAopClient Members
  150. public async Task<T> pageExecute<T>(IAopRequest<T> request, string accessToken, string reqMethod) where T : AopResponse
  151. {
  152. if (string.IsNullOrEmpty(this.charset))
  153. {
  154. this.charset = "utf-8";
  155. }
  156. string apiVersion = null;
  157. if (!string.IsNullOrEmpty(request.GetApiVersion()))
  158. {
  159. apiVersion = request.GetApiVersion();
  160. }
  161. else
  162. {
  163. apiVersion = Version;
  164. }
  165. AopDictionary txtParams = new AopDictionary(request.GetParameters());
  166. // 序列化BizModel
  167. txtParams = SerializeBizModel(txtParams, request);
  168. System.Text.StringBuilder xmlData = new System.Text.StringBuilder();
  169. // 添加协议级请求参数
  170. //AopDictionary txtParams = new AopDictionary(request.GetParameters());
  171. txtParams.Add(METHOD, request.GetApiName());
  172. txtParams.Add(VERSION, apiVersion);
  173. txtParams.Add(APP_ID, appId);
  174. txtParams.Add(FORMAT, format);
  175. txtParams.Add(TIMESTAMP, DateTime.Now);
  176. txtParams.Add(ACCESS_TOKEN, accessToken);
  177. txtParams.Add(SIGN_TYPE, signType);
  178. txtParams.Add(TERMINAL_TYPE, request.GetTerminalType());
  179. txtParams.Add(TERMINAL_INFO, request.GetTerminalInfo());
  180. txtParams.Add(PROD_CODE, request.GetProdCode());
  181. txtParams.Add(NOTIFY_URL, request.GetNotifyUrl());
  182. txtParams.Add(CHARSET, this.charset);
  183. txtParams.Add(RETURN_URL, this.return_url);
  184. // txtParams.Add("return_url", request.GetReturnUrl() );
  185. //字典排序
  186. IDictionary<string, string> sortedTxtParams = new SortedDictionary<string, string>(txtParams);
  187. txtParams = new AopDictionary(sortedTxtParams);
  188. // 排序返回字典类型添加签名参数
  189. txtParams.Add(SIGN, AopUtils.SignAopRequest(sortedTxtParams, privateKeyPem, this.charset, this.keyFromFile, this.signType));
  190. // 是否需要上传文件
  191. string body;
  192. if (request is IAopUploadRequest<T>)
  193. {
  194. IAopUploadRequest<T> uRequest = (IAopUploadRequest<T>)request;
  195. IDictionary<string, FileItem> fileParams = AopUtils.CleanupDictionary(uRequest.GetFileParameters());
  196. body = await webUtils.DoPostAsync(this.serverUrl + "?" + CHARSET + "=" + this.charset, txtParams, fileParams, this.charset);
  197. }
  198. else
  199. {
  200. if (reqMethod.Equals("GET"))
  201. {
  202. //直接调用DoGet方法请求
  203. //body=webUtils .DoGet (this.serverUrl ,txtParams ,this.charset);
  204. //拼接get请求的url
  205. string tmpUrl = serverUrl;
  206. if (txtParams != null && txtParams.Count > 0)
  207. {
  208. if (tmpUrl.Contains("?"))
  209. {
  210. tmpUrl = tmpUrl + "&" + Aop.Api.Util.WebUtils.BuildQuery(txtParams, charset);
  211. }
  212. else
  213. {
  214. tmpUrl = tmpUrl + "?" + Aop.Api.Util.WebUtils.BuildQuery(txtParams, charset);
  215. }
  216. }
  217. body = tmpUrl;
  218. }
  219. else
  220. {
  221. //直接调用DoPost方法请求
  222. // body = webUtils.DoPost(this.serverUrl, txtParams, this.charset);
  223. //输出post表单
  224. body = BuildHtmlRequest(txtParams, reqMethod, reqMethod);
  225. }
  226. }
  227. T rsp = null;
  228. IAopParser<T> parser = null;
  229. if ("xml".Equals(format))
  230. {
  231. parser = new AopXmlParser<T>();
  232. rsp = parser.Parse(body, charset);
  233. }
  234. else
  235. {
  236. parser = new AopJsonParser<T>();
  237. rsp = parser.Parse(body, charset);
  238. }
  239. //验签
  240. // CheckResponseSign(request, rsp, parser, this.alipayPublicKey, this.charset);
  241. return rsp;
  242. }
  243. #endregion
  244. #region IAopClient Members
  245. public async Task<T> Execute<T>(IAopRequest<T> request, string accessToken, string appAuthToken) where T : AopResponse
  246. {
  247. if (string.IsNullOrEmpty(this.charset))
  248. {
  249. this.charset = "utf-8";
  250. }
  251. string apiVersion = null;
  252. if (!string.IsNullOrEmpty(request.GetApiVersion()))
  253. {
  254. apiVersion = request.GetApiVersion();
  255. }
  256. else
  257. {
  258. apiVersion = Version;
  259. }
  260. // 添加协议级请求参数
  261. AopDictionary txtParams = new AopDictionary(request.GetParameters());
  262. // 序列化BizModel
  263. txtParams = SerializeBizModel(txtParams, request);
  264. txtParams.Add(METHOD, request.GetApiName());
  265. txtParams.Add(VERSION, apiVersion);
  266. txtParams.Add(APP_ID, appId);
  267. txtParams.Add(FORMAT, format);
  268. txtParams.Add(TIMESTAMP, DateTime.Now);
  269. txtParams.Add(ACCESS_TOKEN, accessToken);
  270. txtParams.Add(SIGN_TYPE, signType);
  271. txtParams.Add(TERMINAL_TYPE, request.GetTerminalType());
  272. txtParams.Add(TERMINAL_INFO, request.GetTerminalInfo());
  273. txtParams.Add(PROD_CODE, request.GetProdCode());
  274. txtParams.Add(CHARSET, charset);
  275. if (!string.IsNullOrEmpty(request.GetNotifyUrl()))
  276. {
  277. txtParams.Add(NOTIFY_URL, request.GetNotifyUrl());
  278. }
  279. if (!string.IsNullOrEmpty(appAuthToken))
  280. {
  281. txtParams.Add(APP_AUTH_TOKEN, appAuthToken);
  282. }
  283. if (request.GetNeedEncrypt())
  284. {
  285. if (string.IsNullOrEmpty(txtParams[BIZ_CONTENT]))
  286. {
  287. throw new AopException("api request Fail ! The reason: encrypt request is not supported!");
  288. }
  289. if (string.IsNullOrEmpty(this.encyptKey) || string.IsNullOrEmpty(this.encyptType))
  290. {
  291. throw new AopException("encryptType or encryptKey must not null!");
  292. }
  293. if (!"AES".Equals(this.encyptType))
  294. {
  295. throw new AopException("api only support Aes!");
  296. }
  297. string encryptContent = AopUtils.AesEncrypt(this.encyptKey, txtParams[BIZ_CONTENT], this.charset);
  298. txtParams.Remove(BIZ_CONTENT);
  299. txtParams.Add(BIZ_CONTENT, encryptContent);
  300. txtParams.Add(ENCRYPT_TYPE, this.encyptType);
  301. }
  302. // 添加签名参数
  303. txtParams.Add(SIGN, AopUtils.SignAopRequest(txtParams, privateKeyPem, charset, this.keyFromFile, signType));
  304. // 是否需要上传文件
  305. string body;
  306. if (request is IAopUploadRequest<T>)
  307. {
  308. IAopUploadRequest<T> uRequest = (IAopUploadRequest<T>)request;
  309. IDictionary<string, FileItem> fileParams = AopUtils.CleanupDictionary(uRequest.GetFileParameters());
  310. body = await webUtils.DoPostAsync(this.serverUrl + "?" + CHARSET + "=" + this.charset, txtParams, fileParams, this.charset);
  311. }
  312. else
  313. {
  314. body = await webUtils.DoPostAsync(this.serverUrl + "?" + CHARSET + "=" + this.charset, txtParams, this.charset);
  315. }
  316. T rsp = null;
  317. IAopParser<T> parser = null;
  318. if ("xml".Equals(format))
  319. {
  320. parser = new AopXmlParser<T>();
  321. rsp = parser.Parse(body, charset);
  322. }
  323. else
  324. {
  325. parser = new AopJsonParser<T>();
  326. rsp = parser.Parse(body, charset);
  327. }
  328. ResponseParseItem item = parseRespItem(request, body, parser, this.encyptKey, this.encyptType, charset);
  329. rsp = parser.Parse(item.realContent, charset);
  330. CheckResponseSign(request, item.respContent, rsp.IsError, parser, this.alipayPublicKey, this.charset, signType, this.keyFromFile);
  331. return rsp;
  332. }
  333. private static ResponseParseItem parseRespItem<T>(IAopRequest<T> request, string respBody, IAopParser<T> parser, string encryptKey, string encryptType, string charset) where T : AopResponse
  334. {
  335. string realContent = null;
  336. if (request.GetNeedEncrypt())
  337. {
  338. realContent = parser.EncryptSourceData(request, respBody, encryptType, encryptKey, charset);
  339. }
  340. else
  341. {
  342. realContent = respBody;
  343. }
  344. ResponseParseItem item = new ResponseParseItem();
  345. item.realContent = realContent;
  346. item.respContent = respBody;
  347. return item;
  348. }
  349. public static void CheckResponseSign<T>(IAopRequest<T> request, string responseBody, bool isError, IAopParser<T> parser, string alipayPublicKey, string charset, string signType) where T : AopResponse
  350. {
  351. if (string.IsNullOrEmpty(alipayPublicKey) || string.IsNullOrEmpty(charset))
  352. {
  353. return;
  354. }
  355. SignItem signItem = parser.GetSignItem(request, responseBody);
  356. if (signItem == null)
  357. {
  358. throw new AopException("sign check fail: Body is Empty!");
  359. }
  360. if (!isError ||
  361. (isError && !string.IsNullOrEmpty(signItem.Sign)))
  362. {
  363. bool rsaCheckContent = AlipaySignature.RSACheckContent(signItem.SignSourceDate, signItem.Sign, alipayPublicKey, charset, signType);
  364. if (!rsaCheckContent)
  365. {
  366. if (!string.IsNullOrEmpty(signItem.SignSourceDate) && signItem.SignSourceDate.Contains("\\/"))
  367. {
  368. string srouceData = signItem.SignSourceDate.Replace("\\/", "/");
  369. bool jsonCheck = AlipaySignature.RSACheckContent(srouceData, signItem.Sign, alipayPublicKey, charset, signType);
  370. if (!jsonCheck)
  371. {
  372. throw new AopException(
  373. "sign check fail: check Sign and Data Fail JSON also");
  374. }
  375. }
  376. else
  377. {
  378. throw new AopException(
  379. "sign check fail: check Sign and Data Fail!");
  380. }
  381. }
  382. }
  383. }
  384. public static void CheckResponseSign<T>(IAopRequest<T> request, string responseBody, bool isError, IAopParser<T> parser, string alipayPublicKey, string charset, string signType, bool keyFromFile) where T : AopResponse
  385. {
  386. if (string.IsNullOrEmpty(alipayPublicKey) || string.IsNullOrEmpty(charset))
  387. {
  388. return;
  389. }
  390. SignItem signItem = parser.GetSignItem(request, responseBody);
  391. if (signItem == null)
  392. {
  393. throw new AopException("sign check fail: Body is Empty!");
  394. }
  395. if (!isError ||
  396. (isError && !string.IsNullOrEmpty(signItem.Sign)))
  397. {
  398. bool rsaCheckContent = AlipaySignature.RSACheckContent(signItem.SignSourceDate, signItem.Sign, alipayPublicKey, charset, signType, keyFromFile);
  399. if (!rsaCheckContent)
  400. {
  401. if (!string.IsNullOrEmpty(signItem.SignSourceDate) && signItem.SignSourceDate.Contains("\\/"))
  402. {
  403. string srouceData = signItem.SignSourceDate.Replace("\\/", "/");
  404. bool jsonCheck = AlipaySignature.RSACheckContent(srouceData, signItem.Sign, alipayPublicKey, charset, signType, keyFromFile);
  405. if (!jsonCheck)
  406. {
  407. throw new AopException(
  408. "sign check fail: check Sign and Data Fail JSON also");
  409. }
  410. }
  411. else
  412. {
  413. throw new AopException(
  414. "sign check fail: check Sign and Data Fail!");
  415. }
  416. }
  417. }
  418. }
  419. #endregion
  420. #region IAopClient Members
  421. public string BuildHtmlRequest(IDictionary<string, string> sParaTemp, string strMethod, string strButtonValue)
  422. {
  423. //待请求参数数组
  424. IDictionary<string, string> dicPara = new Dictionary<string, string>();
  425. dicPara = sParaTemp;
  426. StringBuilder sbHtml = new StringBuilder();
  427. //sbHtml.Append("<head><meta http-equiv=\"Content-Type\" content=\"text/html\" charset= \"" + charset + "\" /></head>");
  428. sbHtml.Append("<form id='alipaysubmit' name='alipaysubmit' action='" + this.serverUrl + "?charset=" + this.charset +
  429. "' method='" + strMethod + "'>");
  430. foreach (KeyValuePair<string, string> temp in dicPara)
  431. {
  432. sbHtml.Append("<input name='" + temp.Key + "' value='" + temp.Value + "'/>");
  433. }
  434. //submit按钮控件请不要含有name属性
  435. sbHtml.Append("<input type='submit' value='" + strButtonValue + "' style='display:none;'></form>");
  436. // sbHtml.Append("<input type='submit' value='" + strButtonValue + "'></form></div>");
  437. //表单实现自动提交
  438. sbHtml.Append("<script>document.forms['alipaysubmit'].submit();</script>");
  439. return sbHtml.ToString();
  440. }
  441. #endregion
  442. #region IAopClient Members
  443. public Dictionary<string, string> FilterPara(SortedDictionary<string, string> dicArrayPre)
  444. {
  445. Dictionary<string, string> dicArray = new Dictionary<string, string>();
  446. foreach (KeyValuePair<string, string> temp in dicArrayPre)
  447. {
  448. if (temp.Key.ToLower() != "sign" && temp.Key.ToLower() != "sign_type" && temp.Value != "" && temp.Value != null)
  449. {
  450. dicArray.Add(temp.Key, temp.Value);
  451. }
  452. }
  453. return dicArray;
  454. }
  455. public static string CreateLinkStringUrlencode(Dictionary<string, string> dicArray, Encoding code)
  456. {
  457. StringBuilder prestr = new StringBuilder();
  458. foreach (KeyValuePair<string, string> temp in dicArray)
  459. {
  460. prestr.Append(temp.Key + "=" + HttpUtility.UrlEncode(temp.Value, code) + "&");
  461. }
  462. //去掉最後一個&字符
  463. int nLen = prestr.Length;
  464. prestr.Remove(nLen - 1, 1);
  465. return prestr.ToString();
  466. }
  467. #endregion
  468. #region SDK Execute
  469. public async Task<T> SdkExecute<T>(IAopRequest<T> request) where T : AopResponse
  470. {
  471. // 构造请求参数
  472. AopDictionary requestParams = buildRequestParams(request, null, null);
  473. // 字典排序
  474. IDictionary<string, string> sortedParams = new SortedDictionary<String, String>(requestParams);
  475. AopDictionary sortedAopDic = new AopDictionary(sortedParams);
  476. // 参数签名
  477. String charset = String.IsNullOrEmpty(this.charset) ? "utf-8" : this.charset;
  478. String signResult = AopUtils.SignAopRequest(sortedAopDic, privateKeyPem, charset, this.keyFromFile, this.signType);
  479. // 添加签名结果参数
  480. sortedAopDic.Add(SIGN, signResult);
  481. // 参数拼接
  482. String signedResult = WebUtils.BuildQuery(sortedAopDic, charset);
  483. // 构造结果
  484. T rsp = (T) Activator.CreateInstance(typeof(T));
  485. rsp.Body = signedResult;
  486. return rsp;
  487. }
  488. #endregion
  489. #region Common Method
  490. private AopDictionary buildRequestParams<T>(IAopRequest<T> request, String accessToken, String appAuthToken) where T : AopResponse
  491. {
  492. // 默认参数
  493. AopDictionary oriParams = new AopDictionary(request.GetParameters());
  494. // 序列化BizModel
  495. AopDictionary result = SerializeBizModel(oriParams, request);
  496. // 获取参数
  497. String charset = String.IsNullOrEmpty(this.charset) ? "utf-8" : this.charset;
  498. String apiVersion = String.IsNullOrEmpty(request.GetApiVersion()) ? this.Version : request.GetApiVersion();
  499. // 添加协议级请求参数,为空的参数后面会自动过滤,这里不做处理。
  500. result.Add(METHOD, request.GetApiName());
  501. result.Add(VERSION, apiVersion);
  502. result.Add(APP_ID, appId);
  503. result.Add(FORMAT, format);
  504. result.Add(TIMESTAMP, DateTime.Now);
  505. result.Add(ACCESS_TOKEN, accessToken);
  506. result.Add(SIGN_TYPE, signType);
  507. result.Add(TERMINAL_TYPE, request.GetTerminalType());
  508. result.Add(TERMINAL_INFO, request.GetTerminalInfo());
  509. result.Add(PROD_CODE, request.GetProdCode());
  510. result.Add(NOTIFY_URL, request.GetNotifyUrl());
  511. result.Add(CHARSET, charset);
  512. result.Add(RETURN_URL, this.return_url);
  513. result.Add(APP_AUTH_TOKEN, appAuthToken);
  514. if (request.GetNeedEncrypt())
  515. {
  516. if (String.IsNullOrEmpty(result[BIZ_CONTENT]))
  517. {
  518. throw new AopException("api request Fail ! The reason: encrypt request is not supported!");
  519. }
  520. if (String.IsNullOrEmpty(this.encyptKey) || String.IsNullOrEmpty(this.encyptType))
  521. {
  522. throw new AopException("encryptType or encryptKey must not null!");
  523. }
  524. if (!"AES".Equals(this.encyptType))
  525. {
  526. throw new AopException("api only support Aes!");
  527. }
  528. String encryptContent = AopUtils.AesEncrypt(this.encyptKey, result[BIZ_CONTENT], this.charset);
  529. result.Remove(BIZ_CONTENT);
  530. result.Add(BIZ_CONTENT, encryptContent);
  531. result.Add(ENCRYPT_TYPE, this.encyptType);
  532. }
  533. return result;
  534. }
  535. #endregion
  536. #region Model Serialize
  537. /// <summary>
  538. ///
  539. /// </summary>
  540. /// <typeparam name="T"></typeparam>
  541. /// <param name="requestParams"></param>
  542. /// <param name="request"></param>
  543. /// <returns></returns>
  544. private AopDictionary SerializeBizModel<T>(AopDictionary requestParams, IAopRequest<T> request) where T : AopResponse
  545. {
  546. AopDictionary result = requestParams;
  547. Boolean isBizContentEmpty = !requestParams.ContainsKey(BIZ_CONTENT) || String.IsNullOrEmpty(requestParams[BIZ_CONTENT]);
  548. if (isBizContentEmpty && request.GetBizModel() != null)
  549. {
  550. AopObject bizModel = request.GetBizModel();
  551. String content = Serialize(bizModel);
  552. result.Add(BIZ_CONTENT, content);
  553. }
  554. return result;
  555. }
  556. /// <summary>
  557. /// AopObject序列化
  558. /// </summary>
  559. /// <param name="obj"></param>
  560. /// <returns></returns>
  561. private String Serialize(AopObject obj)
  562. {
  563. // 使用AopModelParser序列化对象
  564. AopModelParser parser = new AopModelParser();
  565. JsonObject jo = parser.serializeAopObject(obj);
  566. // 根据JsonObject导出String格式的Json
  567. String result = JsonConvert.ExportToString(jo);
  568. return result;
  569. }
  570. #endregion
  571. }
  572. }