Wechat.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. namespace addons\shopro\facade;
  3. use think\Cache;
  4. class Wechat extends Base
  5. {
  6. public static function getFacadeClass()
  7. {
  8. return self::officialAccount(); // 默认是公众号
  9. }
  10. /**
  11. * 公众号
  12. *
  13. * @return \EasyWeChat\OfficialAccount\Application
  14. */
  15. public static function officialAccount()
  16. {
  17. if (isset($GLOBALS['WECHAT']['OFFICIALACCOUNT'])) {
  18. return $GLOBALS['WECHAT']['OFFICIALACCOUNT'];
  19. }
  20. $defaultConfig = self::defaultConfig();
  21. $officialAccount = sheep_config('shop.platform.WechatOfficialAccount', false);
  22. $config = array_merge($defaultConfig, [
  23. 'app_id' => $officialAccount['app_id'],
  24. 'secret' => $officialAccount['secret'],
  25. ]);
  26. $app = new \EasyWeChat\OfficialAccount\Application($config);
  27. $GLOBALS['WECHAT']['OFFICIALACCOUNT'] = $app;
  28. return $GLOBALS['WECHAT']['OFFICIALACCOUNT'];
  29. }
  30. /**
  31. * 公众号管理
  32. *
  33. * @return \EasyWeChat\OfficialAccount\Application
  34. */
  35. public static function officialAccountManage()
  36. {
  37. if (isset($GLOBALS['WECHAT']['OFFICIALACCOUNT_MANAGE'])) {
  38. return $GLOBALS['WECHAT']['OFFICIALACCOUNT_MANAGE'];
  39. }
  40. $defaultConfig = self::defaultConfig();
  41. $officialAccount = sheep_config('wechat.officialAccount', false);
  42. $config = array_merge($defaultConfig, [
  43. 'app_id' => $officialAccount['app_id'],
  44. 'secret' => $officialAccount['secret'],
  45. 'token' => $officialAccount['token'],
  46. 'aes_key' => $officialAccount['aes_key'],
  47. ]);
  48. $app = new \EasyWeChat\OfficialAccount\Application($config);
  49. $GLOBALS['WECHAT']['OFFICIALACCOUNT_MANAGE'] = $app;
  50. return $GLOBALS['WECHAT']['OFFICIALACCOUNT_MANAGE'];
  51. }
  52. /**
  53. * 小程序
  54. *
  55. * @return \EasyWeChat\MiniProgram\Application
  56. */
  57. public static function miniProgram()
  58. {
  59. if (isset($GLOBALS['WECHAT']['MINIPROGRAM'])) {
  60. return $GLOBALS['WECHAT']['MINIPROGRAM'];
  61. }
  62. $defaultConfig = self::defaultConfig();
  63. $miniProgram = sheep_config('shop.platform.WechatMiniProgram', false);
  64. $config = array_merge($defaultConfig, [
  65. 'app_id' => $miniProgram['app_id'],
  66. 'secret' => $miniProgram['secret'],
  67. ]);
  68. $app = new \EasyWeChat\MiniProgram\Application($config);
  69. $GLOBALS['WECHAT']['MINIPROGRAM'] = $app;
  70. return $GLOBALS['WECHAT']['MINIPROGRAM'];
  71. }
  72. /**
  73. * 小程序
  74. *
  75. * @return \EasyWeChat\OpenPlatform\Application
  76. */
  77. public static function openPlatform()
  78. {
  79. if (isset($GLOBALS['WECHAT']['OPENPLATFORM'])) {
  80. return $GLOBALS['WECHAT']['OPENPLATFORM'];
  81. }
  82. $defaultConfig = self::defaultConfig();
  83. $openPlatform = sheep_config('shop.platform.App', false);
  84. $config = array_merge($defaultConfig, [
  85. 'app_id' => $openPlatform['app_id'],
  86. 'secret' => $openPlatform['secret'],
  87. ]);
  88. $app = new \EasyWeChat\OpenPlatform\Application($config);
  89. $GLOBALS['WECHAT']['OPENPLATFORM'] = $app;
  90. return $GLOBALS['WECHAT']['OPENPLATFORM'];
  91. }
  92. protected static function defaultConfig () {
  93. return [
  94. 'response_type' => 'array',
  95. // 日志配置 level: 日志级别, 可选为:debug/info/notice/warning/error/critical/alert/emergency path:日志文件位置(绝对路径!!!),要求可写权限
  96. 'log' => [
  97. 'default' => config('app_debug') ? 'dev' : 'prod', // 默认使用的 channel,生产环境可以改为下面的 prod
  98. 'channels' => [
  99. // 测试环境
  100. 'dev' => [
  101. 'driver' => 'single',
  102. 'path' => RUNTIME_PATH . 'log/wechat/easywechat-dev.log',
  103. 'level' => 'debug',
  104. ],
  105. // 生产环境
  106. 'prod' => [
  107. 'driver' => 'daily',
  108. 'path' => RUNTIME_PATH . 'log/wechat/easywechat-prod.log',
  109. 'level' => 'info',
  110. ]
  111. ]
  112. ],
  113. 'http' => [
  114. 'connect_timeout' => 5,
  115. 'max_retries' => 1,
  116. 'retry_delay' => 500,
  117. 'timeout' => 5,
  118. 'verify' => ROOT_PATH . 'addons/shopro/library/cacert.pem',
  119. // 'base_uri' => 'https://api.weixin.qq.com/', // 如果你在国外想要覆盖默认的 url 的时候才使用,根据不同的模块配置不同的 uri
  120. ],
  121. ];
  122. }
  123. }