Factory.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. using System;
  2. using Alipay.EasySDK.Kernel;
  3. using System.Reflection;
  4. namespace Alipay.EasySDK.Factory
  5. {
  6. /// <summary>
  7. /// 客户端工厂,用于快速配置和访问各种场景下的API Client
  8. ///
  9. /// 注:该Factory获取的Client不可储存重复使用,请每次均通过Factory完成调用
  10. /// </summary>
  11. public static class Factory
  12. {
  13. public const string SDK_VERSION = "alipay-easysdk-net-2.1.0";
  14. /// <summary>
  15. /// 将一些初始化耗时较多的信息缓存在上下文中
  16. /// </summary>
  17. private static Context context;
  18. /// <summary>
  19. /// 设置客户端参数,只需设置一次,即可反复使用各种场景下的API Client
  20. /// </summary>
  21. /// <param name="options">客户端参数对象</param>
  22. public static void SetOptions(Config options)
  23. {
  24. context = new Context(options, SDK_VERSION);
  25. }
  26. /// <summary>
  27. /// 获取调用OpenAPI所需的客户端实例
  28. /// 本方法用于调用SDK扩展包中的API Client下的方法
  29. ///
  30. /// 注:返回的实例不可重复使用,只可用于单次调用
  31. /// </summary>
  32. /// <typeparam name="T">泛型参数</typeparam>
  33. /// <param name="client">API Client的类型对象</param>
  34. /// <returns>client实例,用于发起单次调用</returns>
  35. public static T GetClient<T>()
  36. {
  37. Type type = typeof(T);
  38. ConstructorInfo constructor = type.GetConstructor(new Type[] { typeof(Client) });
  39. context.SdkVersion = GetSdkVersion(type);
  40. return (T)constructor.Invoke(new object[] { new Client(context) });
  41. }
  42. private static string GetSdkVersion(Type client)
  43. {
  44. return context.SdkVersion + "-" + client.FullName
  45. .Replace("EasySDK.", "")
  46. .Replace(".Client", "")
  47. .Replace(".", "-");
  48. }
  49. /// <summary>
  50. /// 基础能力相关
  51. /// </summary>
  52. public static class Base
  53. {
  54. /// <summary>
  55. /// 获取图片相关API Client
  56. /// </summary>
  57. /// <returns>图片相关API Client</returns>
  58. public static EasySDK.Base.Image.Client Image()
  59. {
  60. return new EasySDK.Base.Image.Client(new Client(context));
  61. }
  62. /// <summary>
  63. /// 获取视频相关API Client
  64. /// </summary>
  65. /// <returns>视频相关API Client</returns>
  66. public static EasySDK.Base.Video.Client Video()
  67. {
  68. return new EasySDK.Base.Video.Client(new Client(context));
  69. }
  70. /// <summary>
  71. /// 获取OAuth认证相关API Client
  72. /// </summary>
  73. /// <returns>OAuth认证相关API Client</returns>
  74. public static EasySDK.Base.OAuth.Client OAuth()
  75. {
  76. return new EasySDK.Base.OAuth.Client(new Client(context));
  77. }
  78. /// <summary>
  79. /// 获取小程序二维码相关API Client
  80. /// </summary>
  81. /// <returns>小程序二维码相关API Client</returns>
  82. public static EasySDK.Base.Qrcode.Client Qrcode()
  83. {
  84. return new EasySDK.Base.Qrcode.Client(new Client(context));
  85. }
  86. }
  87. /// <summary>
  88. /// 营销能力相关
  89. /// </summary>
  90. public static class Marketing
  91. {
  92. /// <summary>
  93. /// 获取生活号相关API Client
  94. /// </summary>
  95. /// <returns>生活号相关API Client</returns>
  96. public static EasySDK.Marketing.OpenLife.Client OpenLife()
  97. {
  98. return new EasySDK.Marketing.OpenLife.Client(new Client(context));
  99. }
  100. /// <summary>
  101. /// 获取支付宝卡包相关API Client
  102. /// </summary>
  103. /// <returns>支付宝卡包相关API Client</returns>
  104. public static EasySDK.Marketing.Pass.Client Pass()
  105. {
  106. return new EasySDK.Marketing.Pass.Client(new Client(context));
  107. }
  108. /// <summary>
  109. /// 获取小程序模板消息相关API Client
  110. /// </summary>
  111. /// <returns>小程序模板消息相关API Client</returns>
  112. public static EasySDK.Marketing.TemplateMessage.Client TemplateMessage()
  113. {
  114. return new EasySDK.Marketing.TemplateMessage.Client(new Client(context));
  115. }
  116. }
  117. /// <summary>
  118. /// 会员能力相关
  119. /// </summary>
  120. public static class Member
  121. {
  122. /// <summary>
  123. /// 获取支付宝身份认证相关API Client
  124. /// </summary>
  125. /// <returns>支付宝身份认证相关API Client</returns>
  126. public static EasySDK.Member.Identification.Client Identification()
  127. {
  128. return new EasySDK.Member.Identification.Client(new Client(context));
  129. }
  130. }
  131. /// <summary>
  132. /// 支付能力相关
  133. /// </summary>
  134. public static class Payment
  135. {
  136. /// <summary>
  137. /// 获取支付通用API Client
  138. /// </summary>
  139. /// <returns>支付通用API Client</returns>
  140. public static EasySDK.Payment.Common.Client Common()
  141. {
  142. return new EasySDK.Payment.Common.Client(new Client(context));
  143. }
  144. /// <summary>
  145. /// 获取当面付API Client
  146. /// </summary>
  147. /// <returns>当面付API Client</returns>
  148. public static EasySDK.Payment.FaceToFace.Client FaceToFace()
  149. {
  150. return new EasySDK.Payment.FaceToFace.Client(new Client(context));
  151. }
  152. /// <summary>
  153. /// 获取花呗API Client
  154. /// </summary>
  155. /// <returns>花呗API Client</returns>
  156. public static EasySDK.Payment.Huabei.Client Huabei()
  157. {
  158. return new EasySDK.Payment.Huabei.Client(new Client(context));
  159. }
  160. /// <summary>
  161. /// 获取手机APP支付API Client
  162. /// </summary>
  163. /// <returns>手机APP支付API Client</returns>
  164. public static EasySDK.Payment.App.Client App()
  165. {
  166. return new EasySDK.Payment.App.Client(new Client(context));
  167. }
  168. /// <summary>
  169. /// 获取电脑网站支付API Client
  170. /// </summary>
  171. /// <returns>电脑网站支付API</returns>
  172. public static EasySDK.Payment.Page.Client Page()
  173. {
  174. return new EasySDK.Payment.Page.Client(new Client(context));
  175. }
  176. /// <summary>
  177. /// 获取手机网站支付API Client
  178. /// </summary>
  179. /// <returns>手机网站支付API</returns>
  180. public static EasySDK.Payment.Wap.Client Wap()
  181. {
  182. return new EasySDK.Payment.Wap.Client(new Client(context));
  183. }
  184. }
  185. /// <summary>
  186. /// 安全能力相关
  187. /// </summary>
  188. public static class Security
  189. {
  190. /// <summary>
  191. /// 获取文本风险识别相关API Client
  192. /// </summary>
  193. /// <returns>文本风险识别相关API Client</returns>
  194. public static EasySDK.Security.TextRisk.Client TextRisk()
  195. {
  196. return new EasySDK.Security.TextRisk.Client(new Client(context));
  197. }
  198. }
  199. /// <summary>
  200. /// 辅助工具
  201. /// </summary>
  202. public static class Util
  203. {
  204. /// <summary>
  205. /// 获取OpenAPI通用接口,可通过自行拼装参数,调用几乎所有OpenAPI
  206. /// </summary>
  207. /// <returns>OpenAPI通用接口</returns>
  208. public static EasySDK.Util.Generic.Client Generic()
  209. {
  210. return new EasySDK.Util.Generic.Client(new Client(context));
  211. }
  212. /// <summary>
  213. /// 获取AES128加解密相关API Client,常用于会员手机号的解密
  214. /// </summary>
  215. /// <returns>AES128加解密相关API Client</returns>
  216. public static EasySDK.Util.AES.Client AES()
  217. {
  218. return new EasySDK.Util.AES.Client(new Client(context));
  219. }
  220. }
  221. }
  222. }