AppStoreManager.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #region Apache License Version 2.0
  2. /*----------------------------------------------------------------
  3. Copyright 2019 Jeffrey Su & Suzhou Senparc Network Technology Co.,Ltd.
  4. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
  5. except in compliance with the License. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software distributed under the
  8. License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  9. either express or implied. See the License for the specific language governing permissions
  10. and limitations under the License.
  11. Detail: https://github.com/JeffreySu/WeiXinMPSDK/blob/master/license.md
  12. ----------------------------------------------------------------*/
  13. #endregion Apache License Version 2.0
  14. /*----------------------------------------------------------------
  15. Copyright (C) 2019 Senparc
  16. 文件名:AppStoreManager.cs
  17. 文件功能描述:AppStoreOAuth
  18. 创建标识:Senparc - 20150319
  19. ----------------------------------------------------------------*/
  20. using System.Collections.Generic;
  21. using Senparc.Weixin.Exceptions;
  22. using Senparc.Weixin.HttpUtility;
  23. using Senparc.Weixin.MP.AppStore.Api;
  24. namespace Senparc.Weixin.MP.AppStore
  25. {
  26. public class AppStoreManager
  27. {
  28. public const string DEFAULT_URL = "http://api.weiweihi.com:8080";//默认Api Url地址
  29. //private static string _appKey;
  30. //private static string _secret;
  31. private static PassportCollection _passportCollection;
  32. public static PassportCollection PassportCollection
  33. {
  34. get
  35. {
  36. if (_passportCollection == null)
  37. {
  38. //LoadPassport();
  39. _passportCollection = new PassportCollection();
  40. }
  41. return _passportCollection;
  42. }
  43. set { _passportCollection = value; }
  44. }
  45. public static PassportBag GetPassportBag(string appKey)
  46. {
  47. if (PassportCollection.ContainsKey(appKey))
  48. {
  49. return PassportCollection[appKey];
  50. }
  51. return null;
  52. }
  53. public const string BasicApiPath = "/App/Api/";
  54. /// <summary>
  55. /// 注册P2P应用基本信息(可以选择不立即载入Passport以节省系统启动时间)
  56. /// </summary>
  57. /// <param name="appKey">P2P后台申请到微信应用后的AppKey</param>
  58. /// <param name="secret">AppKey对应的Secret</param>
  59. /// <param name="url">API地址,建议使用默认值</param>
  60. /// <param name="getPassportImmediately">是否马上获取Passport,默认为False</param>
  61. private static void Register(string appKey, string secret, string url = DEFAULT_URL, bool getPassportImmediately = false)
  62. {
  63. //if (PassportCollection.BasicUrl != url)
  64. //{
  65. ////不使用默认地址
  66. PassportCollection.BasicUrl = url + BasicApiPath;
  67. //}
  68. PassportCollection[appKey] = new PassportBag(appKey, secret, url + BasicApiPath);
  69. if (getPassportImmediately)
  70. {
  71. ApplyPassport(appKey, secret, url);
  72. }
  73. }
  74. /// <summary>
  75. /// 申请新的通行证。
  76. /// 每次调用完毕前将有1秒左右的Thread.Sleep时间
  77. /// </summary>
  78. public static void ApplyPassport(string appKey, string appSecret, string url = DEFAULT_URL)
  79. {
  80. if (!PassportCollection.ContainsKey(appKey))
  81. {
  82. //如果不存在,则自动注册(注册之后PassportCollection[appKey]一定有存在值)
  83. Register(appKey, appSecret, url, true);
  84. }
  85. var passportBag = PassportCollection[appKey];
  86. var getPassportUrl = PassportCollection.BasicUrl + "GetPassport";
  87. var formData = new Dictionary<string, string>();
  88. formData["appKey"] = passportBag.AppKey;
  89. formData["secret"] = passportBag.AppSecret;
  90. var result = CO2NET.HttpUtility.Post.PostGetJson<PassportResult>(getPassportUrl, formData: formData);
  91. if (result.Result != AppResultKind.成功)
  92. {
  93. throw new WeixinException("获取Passort失败!错误信息:" + result.Result, null);
  94. }
  95. passportBag.Passport = result.Data;
  96. passportBag.Passport.ApiUrl = PassportCollection.BasicUrl;
  97. }
  98. /// <summary>
  99. /// 清除当前的通行证
  100. /// </summary>
  101. public static void RemovePassport(string appKey)
  102. {
  103. try
  104. {
  105. PassportCollection.Remove(appKey);
  106. }
  107. catch
  108. {
  109. }
  110. }
  111. /// <summary>
  112. /// 获取appKey对应的接口集合。
  113. /// 调用此方法请确认此appKey已经成功使用SdkManager.Register(appKey, appSecret, appUrl)方法注册过。
  114. /// </summary>
  115. /// <param name="appKey"></param>
  116. /// <returns></returns>
  117. public static ApiContainer GetApiContainer(string appKey, string appSecret, string url = DEFAULT_URL)
  118. {
  119. return new ApiContainer(appKey, appSecret, url);
  120. }
  121. }
  122. }