BasicApi.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. using System;
  2. using System.Collections.Generic;
  3. using cuidian.OpenApi.Exceptions;
  4. using cuidian.OpenApi.Http;
  5. using cuidian.OpenApi.Utils;
  6. using Logs;
  7. namespace cuidian.OpenApi.Api
  8. {
  9. /// <summary>
  10. /// 开放平台 API 基类
  11. /// </summary>
  12. public abstract class BasicApi : Api
  13. {
  14. public BasicApi(string resourceId)
  15. : base(resourceId) { }
  16. public BasicApi(string resourceId, string fromAccount, string toAccount)
  17. : base(resourceId, fromAccount, toAccount) { }
  18. public BasicApi(string resourceId, string fromAccount, string toAccount, string appKey)
  19. : base(resourceId, fromAccount, toAccount, appKey) { }
  20. /// <summary>
  21. /// 获取单个资源
  22. /// </summary>
  23. /// <param name="id"></param>
  24. /// <returns></returns>
  25. public BusinessObject Get(string id)
  26. {
  27. try
  28. {
  29. this.Method = "get";
  30. IDictionary<string, string> parameters = new Dictionary<string, string>();
  31. parameters.Add("id", id);
  32. return BusinessObject.Get(this.ResourceId
  33. , new Response(Client.Get(this.Url, this.CombineParameters(this.GetSystemParameters(), parameters))));
  34. }
  35. catch (Exception e)
  36. {
  37. LogHelper.Error(e.Message);
  38. throw new ApiException(ERR_MSG_SDK_RUNTIME_ERROR, e);
  39. }
  40. }
  41. /// <summary>
  42. /// 批量查询
  43. /// </summary>
  44. /// <param name="parameters">查询参数</param>
  45. /// <returns></returns>
  46. public BusinessObject BatchGet(IDictionary<string, string> parameters)
  47. {
  48. try
  49. {
  50. this.Method = "batch_get";
  51. return BusinessObject.BatchGet(this.ResourceId
  52. , new Response(Client.Get(this.Url, this.CombineParameters(this.GetSystemParameters(), parameters))));
  53. }
  54. catch (Exception e)
  55. {
  56. LogHelper.Error(e.Message);
  57. throw new ApiException(ERR_MSG_SDK_RUNTIME_ERROR, e);
  58. }
  59. }
  60. /// <summary>
  61. /// 有上游业务的新增,比如网上报销单生成凭证。
  62. /// </summary>
  63. /// <param name="data">凭证数据</param>
  64. /// <param name="biz_id">上游主键</param>
  65. /// <returns></returns>
  66. public BusinessObject Add(string data, string biz_id)
  67. {
  68. try
  69. {
  70. this.Method = "add";
  71. IDictionary<string, string> parameters = new Dictionary<string, string>();
  72. parameters.Add("biz_id", biz_id);
  73. parameters.Add("sync", "1");
  74. BusinessObject bo = BusinessObject.Add(this.ResourceId, new Response(Client.Post(this.Url, this.CombineParameters(this.GetSystemParameters(), parameters), data)));
  75. if (bo.IsError)
  76. {
  77. return bo;
  78. }
  79. if (bo.Full.ContainsName("url"))
  80. {
  81. int pingAfter = int.Parse(bo.Full.GetValue("ping_after").ToString());
  82. int counter = 0;
  83. String url = bo.Full.GetValue("url").ToString();
  84. bool success = false;
  85. // 同步获取新增结果,重试 MAX_TIMES_TRADEID_RETRY 次
  86. while (counter < MAX_TIMES_TRADEID_RETRY && !success)
  87. {
  88. counter++;
  89. System.Threading.Thread.Sleep(1000 * pingAfter);
  90. bo = BusinessObject.Add(this.ResourceId, new Response(Client.Get(url, null)));
  91. success = !bo.Full.ContainsName("url");
  92. }
  93. }
  94. return bo;
  95. }
  96. catch (Exception e)
  97. {
  98. LogHelper.Error(e.Message);
  99. throw new ApiException(ERR_MSG_SDK_RUNTIME_ERROR, e);
  100. }
  101. }
  102. /// <summary>
  103. /// 无上游关系的新增
  104. /// </summary>
  105. /// <param name="data">凭证数据</param>
  106. /// <returns></returns>
  107. public BusinessObject Add(string data)
  108. {
  109. try
  110. {
  111. BusinessObject trade = new Trade().Get();
  112. string tradeid = trade.Full.GetObject("trade").GetValue("id").ToString();
  113. //string tradeid = new Trade().Get().BodyObject.Get("id");
  114. this.Method = "add";
  115. IDictionary<string, string> parameters = new Dictionary<string, string>();
  116. parameters.Add("tradeid", tradeid);
  117. parameters.Add("sync", "1");
  118. BusinessObject bo = BusinessObject.Add(this.ResourceId, new Response(Client.Post(this.Url, this.CombineParameters(this.GetSystemParameters(), parameters), data)));
  119. if (bo.IsError)
  120. {
  121. return bo;
  122. }
  123. if (bo.Full.ContainsName("url"))
  124. {
  125. int pingAfter = int.Parse(bo.Full.GetValue("ping_after").ToString());
  126. int counter = 0;
  127. String url = bo.Full.GetValue("url").ToString();
  128. bool success = false;
  129. while (counter < MAX_TIMES_TRADEID_RETRY && !success)
  130. {
  131. counter++;
  132. System.Threading.Thread.Sleep(1000 * pingAfter);
  133. bo = BusinessObject.Add(this.ResourceId, new Response(Client.Get(url, null)));
  134. success = !bo.Full.ContainsName("url");
  135. }
  136. }
  137. return bo;
  138. }
  139. catch (Exception e)
  140. {
  141. LogHelper.Error(e.Message);
  142. throw new ApiException(ERR_MSG_SDK_RUNTIME_ERROR, e);
  143. }
  144. }
  145. /// <summary>
  146. /// 审核
  147. /// </summary>
  148. /// <returns></returns>
  149. public BusinessObject Audit(string voucherCode)
  150. {
  151. try
  152. {
  153. UserApi user = new UserApi();
  154. user.Login();
  155. this.Method = "verify";
  156. //string data = String.Format("{\"{1}\":{\"voucher_code\":\"{0}\",\"person_code\":\"demo\"}}", voucherCode, this.ResourceId);
  157. //string data = "{\"saleorder\": {\"voucher_code\": \"0000000001\",\"person_code\": \"demo\"}}";
  158. string data = "{{\"{0}\": {{\"voucher_code\": \"{1}\"}}}}";
  159. data = string.Format(data, this.ResourceId, voucherCode);
  160. BusinessObject trade = new Trade().Get();
  161. string tradeid = trade.Full.GetObject("trade").GetValue("id").ToString();
  162. IDictionary<string, string> parameters = new Dictionary<string, string>();
  163. parameters.Add("tradeid", tradeid);
  164. parameters.Add("sync", "1");
  165. BusinessObject bo = BusinessObject.Audit(this.ResourceId, new Response(Client.Post(this.Url, this.CombineParameters(this.GetSystemParameters(), parameters), data)));
  166. return bo;
  167. }
  168. catch (Exception e)
  169. {
  170. LogHelper.Error(e.Message);
  171. throw new ApiException(ERR_MSG_SDK_RUNTIME_ERROR, e);
  172. }
  173. }
  174. }
  175. }