Config.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. 文件名:Config.cs
  17. 文件功能描述:全局设置
  18. 创建标识:Senparc - 20150211
  19. 修改标识:Senparc - 20150303
  20. 修改描述:整理接口
  21. 修改标识:Senparc - 20160813
  22. 修改描述:v4.7.7 添加DefaultCacheNamespace
  23. 修改标识:Senparc - 20171127
  24. 修改描述:v4.18.5 添加Config.ApiMpHost属性,可以设置API域名
  25. 修改标识:Senparc - 20180622
  26. 修改描述:v5.0.2.1 修复 IsDebug 逻辑判断错误
  27. 修改标识:Senparc - 20180717
  28. 修改描述:v5.1.2 Config.SenparcWeixinSetting 提供默认实例
  29. ----------------------------------------------------------------*/
  30. using System;
  31. using Senparc.Weixin.Entities;
  32. using System.Collections.Generic;
  33. using System.Diagnostics;
  34. using System.IO;
  35. using System.Threading;
  36. namespace Senparc.Weixin
  37. {
  38. /// <summary>
  39. /// Senparc.Weixin 全局设置
  40. /// </summary>
  41. public static class Config
  42. {
  43. //private static bool _isDebug = false;
  44. /// <summary>
  45. /// <para>指定是否是Debug状态,如果是,系统会自动输出日志。</para>
  46. /// <para>如果 CO2NET.Config.IsDebug 为 true,则此参数也会为 true,否则以此参数为准。</para>
  47. /// </summary>
  48. public static bool IsDebug
  49. {
  50. get { return CO2NET.Config.IsDebug || SenparcWeixinSetting.IsDebug; }
  51. set { SenparcWeixinSetting.IsDebug = value; }
  52. }
  53. /// <summary>
  54. /// 默认微信配置
  55. /// </summary>
  56. [Obsolete("请使用 SenparcWeixinSetting")]
  57. public static SenparcWeixinSetting DefaultSenparcWeixinSetting { get { return SenparcWeixinSetting; } set { SenparcWeixinSetting = value; } }
  58. /// <summary>
  59. /// <para>微信全局配置</para>
  60. /// <para>注意:在程序运行过程中修改 SenparcWeixinSetting.Items 中的微信配置值,并不能修改 Container 中的对应信息(如AppSecret),</para>
  61. /// <para>如果需要修改微信信息(如AppSecret)应该使用 xxContainer.Register() 修改,这里的值也会随之更新。</para>
  62. /// </summary>
  63. public static SenparcWeixinSetting SenparcWeixinSetting { get; set; }
  64. /// <summary>
  65. /// 微信支付使用沙箱模式(默认为false)
  66. /// </summary>
  67. public static bool UseSandBoxPay { get; set; }
  68. /// <summary>
  69. /// 请求超时设置(以毫秒为单位),默认为10秒。
  70. /// 说明:此处常量专为提供给方法的参数的默认值,不是方法内所有请求的默认超时时间。
  71. /// </summary>
  72. public const int TIME_OUT = CO2NET.Config.TIME_OUT;
  73. /// <summary>
  74. /// 网站根目录绝对路径
  75. /// </summary>
  76. public static string RootDictionaryPath
  77. {
  78. get { return CO2NET.Config.RootDictionaryPath; }
  79. set { CO2NET.Config.RootDictionaryPath = value; }
  80. }
  81. /// <summary>
  82. /// 默认缓存键的第一级命名空间,默认值:DefaultCache
  83. /// </summary>
  84. public static string DefaultCacheNamespace
  85. {
  86. get { return CO2NET.Config.DefaultCacheNamespace; }
  87. set { CO2NET.Config.DefaultCacheNamespace = value; }
  88. }
  89. #region API地址(前缀)设置
  90. #region 公众号(小程序)、开放平台 API 的服务器地址(默认为:https://api.weixin.qq.com)
  91. /// <summary>
  92. /// 公众号(小程序)、开放平台 API 的服务器地址(默认为:https://api.weixin.qq.com)
  93. /// </summary>
  94. private static string _apiMpHost = "https://api.weixin.qq.com";
  95. /// <summary>
  96. /// 公众号(小程序)、开放平台 API 的服务器地址(默认为:https://api.weixin.qq.com)
  97. /// </summary>
  98. public static string ApiMpHost
  99. {
  100. get { return _apiMpHost; }
  101. set { _apiMpHost = value; }
  102. }
  103. /// <summary>
  104. /// 公众号(小程序)、开放平台【文件下载】 API 的服务器地址(默认为:https://api.weixin.qq.com)
  105. /// </summary>
  106. private static string _apiMpFileHost = "http://api.weixin.qq.com";
  107. /// <summary>
  108. /// 公众号(小程序)、开放平台【文件下载】 API 的服务器地址(默认为:http://file.api.weixin.qq.com)
  109. /// </summary>
  110. public static string ApiMpFileHost
  111. {
  112. get { return _apiMpFileHost; }
  113. set { _apiMpFileHost = value; }
  114. }
  115. #endregion
  116. #region 企业微信API的服务器地址(默认为:https://qyapi.weixin.qq.com)
  117. /// <summary>
  118. /// 企业微信API的服务器地址(默认为:https://qyapi.weixin.qq.com)
  119. /// </summary>
  120. private static string _apiWorkHost = "https://qyapi.weixin.qq.com";
  121. /// <summary>
  122. /// 企业微信API的服务器地址(默认为:https://qyapi.weixin.qq.com)
  123. /// </summary>
  124. public static string ApiWorkHost
  125. {
  126. get { return _apiWorkHost; }
  127. set { _apiWorkHost = value; }
  128. }
  129. #endregion
  130. #endregion
  131. /// <summary>
  132. /// 默认的AppId检查规则
  133. /// </summary>
  134. public static Func<string, PlatformType, bool> DefaultAppIdCheckFunc = (accessTokenOrAppId, platFormType) =>
  135. {
  136. if (platFormType == PlatformType.QY || platFormType == PlatformType.Work)
  137. {
  138. /*
  139. * 企业号(企业微信)AppKey(Length=84):wx7618c00000000222@044ZI5s6-ACxpAuOcm4md410pZ460pQUmxO9hIoMd09kRaJ1iSqhPfmg3-aBFF7q
  140. * 企业号(企业微信)AccessToken(length=300):MGelzm_P0N-41qH3PwHsNxp70rdVuB0SMEN7dE4E8eKpb0OpNQSp8jPUfgwIL_P9jcz-qGIOLbLEy3d8XQEJFfZtOLgTJqyg0rJbj6WyQJxdRVjbLnHr0-pg7oN9dD1NFI7-T7GLuJER3Pun-5cSiSmZgAegTDhXKZC8XfgjQAPPYLjZl7StBnO7dVcZStdyivZ92zq4PrDdNif9fa2p9lPSLqkur2PpDB9P7MsR8PDJWsKghEcmjB41OXohHGnqPWd5lUZaV1Y8p35BVz6PqjF-90UgAjI9IohVKVRClks
  141. */
  142. //return accessTokenOrAppId != null && accessTokenOrAppId.Length < 256;
  143. /*
  144. * 2017年9月26日开始,AccessToken长度有变化(长度有300、215、191等)
  145. * AccessToken(Length=215):_0evr6HbAnWCUfn1tRpbVY2uV63fDOfT-fUnpQcq6egl8bYFp3Xq45ebImXn5Aj1_nz_mFCUz9sDnoEkfy-jyXqJEc4Hty0BAo2VQTB8ogx7qkL2w1p0H2E1fKWwJrQ1285V0XhEQ0pcHMLwy9RbHuD4sHdAJ5ZkXGchNQ1eHsmseoBxucKvyAnEq9psJVLMjkU4G3ZRa0NoTBSy0g6ujg
  146. */
  147. return accessTokenOrAppId != null && accessTokenOrAppId.Contains("@");
  148. }
  149. else
  150. {
  151. /*
  152. * 公众号AppId:wxe273c3a02e09ff8c
  153. * 公众号AccessToken:ga0wJ5ZmdB1Ef1gMMxmps6Uz1a9TXoutQtRqgYTbIqHfTm4Ssfoj0DjMLp1_KkG7FkaqS7m7f9rrYbqBQMBizRBQjHFG5ZIov8Wb0FBnHDq5fGpCu0S2H2j2aM8c6KDqGGEiAIAJJH
  154. */
  155. return accessTokenOrAppId != null && accessTokenOrAppId.Length <= 32 /*wxc3c90837b0e76080*/
  156. ;
  157. }
  158. };
  159. static Config()
  160. {
  161. SenparcWeixinSetting = new SenparcWeixinSetting();//提供默认实例
  162. }
  163. }
  164. }