Wechat.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. namespace app\common\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. $defaultConfig = self::defaultConfig();
  18. $officialAccount = shop_config('shop.platform.wechat_official_account', false);
  19. $config = array_merge($defaultConfig, [
  20. 'app_id' => $officialAccount['app_id'],
  21. 'secret' => $officialAccount['secret'],
  22. ]);
  23. $app = new \EasyWeChat\OfficialAccount\Application($config);
  24. $GLOBALS['WECHAT']['OFFICIALACCOUNT'] = $app;
  25. return $GLOBALS['WECHAT']['OFFICIALACCOUNT'];
  26. }
  27. /**
  28. * 公众号管理
  29. *
  30. * @return \EasyWeChat\OfficialAccount\Application
  31. */
  32. public static function officialAccountManage()
  33. {
  34. $defaultConfig = self::defaultConfig();
  35. $officialAccount = shop_config('wechat.wechat_official_account', false);
  36. $config = array_merge($defaultConfig, [
  37. 'app_id' => $officialAccount['app_id'],
  38. 'secret' => $officialAccount['secret'],
  39. 'token' => $officialAccount['token'],
  40. 'aes_key' => $officialAccount['aes_key'],
  41. ]);
  42. $app = new \EasyWeChat\OfficialAccount\Application($config);
  43. $GLOBALS['WECHAT']['OFFICIALACCOUNT_MANAGE'] = $app;
  44. return $GLOBALS['WECHAT']['OFFICIALACCOUNT_MANAGE'];
  45. }
  46. /**
  47. * 小程序
  48. *
  49. * @return \EasyWeChat\MiniProgram\Application
  50. */
  51. public static function miniProgram()
  52. {
  53. if (isset($GLOBALS['WECHAT']['MINIPROGRAM'])) {
  54. return $GLOBALS['WECHAT']['MINIPROGRAM'];
  55. }
  56. $defaultConfig = self::defaultConfig();
  57. $miniProgram = shop_config('shop.platform.wechat_mini_program', false);
  58. $config = array_merge($defaultConfig, [
  59. 'app_id' => $miniProgram['app_id'],
  60. 'secret' => $miniProgram['secret'],
  61. ]);
  62. $app = new \EasyWeChat\MiniProgram\Application($config);
  63. $GLOBALS['WECHAT']['MINIPROGRAM'] = $app;
  64. return $GLOBALS['WECHAT']['MINIPROGRAM'];
  65. }
  66. /**
  67. * 开放给小程序的公众号
  68. *
  69. * @return \EasyWeChat\OpenPlatform\Application
  70. */
  71. public static function openAndroidAppPlatform()
  72. {
  73. if (isset($GLOBALS['WECHAT']['OPENPLATFORM'])) {
  74. return $GLOBALS['WECHAT']['OPENPLATFORM'];
  75. }
  76. $defaultConfig = self::defaultConfig();
  77. $openPlatform = shop_config('shop.platform.android_app', false);
  78. $config = array_merge($defaultConfig, [
  79. 'app_id' => $openPlatform['app_id'],
  80. 'secret' => $openPlatform['secret'],
  81. ]);
  82. $app = new \EasyWeChat\OpenPlatform\Application($config);
  83. $GLOBALS['WECHAT']['OPENPLATFORM'] = $app;
  84. return $GLOBALS['WECHAT']['OPENPLATFORM'];
  85. }
  86. /**
  87. * 开放给小程序的公众号
  88. *
  89. * @return \EasyWeChat\OpenPlatform\Application
  90. */
  91. public static function openIosAppPlatform()
  92. {
  93. if (isset($GLOBALS['WECHAT']['OPENPLATFORM'])) {
  94. return $GLOBALS['WECHAT']['OPENPLATFORM'];
  95. }
  96. $defaultConfig = self::defaultConfig();
  97. $openPlatform = shop_config('shop.platform.ios_app', false);
  98. $config = array_merge($defaultConfig, [
  99. 'app_id' => $openPlatform['app_id'],
  100. 'secret' => $openPlatform['secret'],
  101. ]);
  102. $app = new \EasyWeChat\OpenPlatform\Application($config);
  103. $GLOBALS['WECHAT']['OPENPLATFORM'] = $app;
  104. return $GLOBALS['WECHAT']['OPENPLATFORM'];
  105. }
  106. /**
  107. * 开放给小程序的公众号
  108. *
  109. * @return \EasyWeChat\OpenPlatform\Application
  110. */
  111. public static function openPlatform()
  112. {
  113. if (isset($GLOBALS['WECHAT']['OPENPLATFORM'])) {
  114. return $GLOBALS['WECHAT']['OPENPLATFORM'];
  115. }
  116. $defaultConfig = self::defaultConfig();
  117. $openPlatform = shop_config('shop.platform.android_app', false);
  118. $config = array_merge($defaultConfig, [
  119. 'app_id' => $openPlatform['app_id'],
  120. 'secret' => $openPlatform['secret'],
  121. ]);
  122. $app = new \EasyWeChat\OpenPlatform\Application($config);
  123. $GLOBALS['WECHAT']['OPENPLATFORM'] = $app;
  124. return $GLOBALS['WECHAT']['OPENPLATFORM'];
  125. }
  126. protected static function defaultConfig () {
  127. return [
  128. 'response_type' => 'array',
  129. // 日志配置 level: 日志级别, 可选为:debug/info/notice/warning/error/critical/alert/emergency path:日志文件位置(绝对路径!!!),要求可写权限
  130. 'log' => [
  131. 'default' => config('app_debug') ? 'dev' : 'prod', // 默认使用的 channel,生产环境可以改为下面的 prod
  132. 'channels' => [
  133. // 测试环境
  134. 'dev' => [
  135. 'driver' => 'single',
  136. 'path' => RUNTIME_PATH . 'log/wechat/easywechat-dev.log',
  137. 'level' => 'debug',
  138. ],
  139. // 生产环境
  140. 'prod' => [
  141. 'driver' => 'daily',
  142. 'path' => RUNTIME_PATH . 'log/wechat/easywechat-prod.log',
  143. 'level' => 'info',
  144. ]
  145. ]
  146. ],
  147. 'http' => [
  148. 'connect_timeout' => 5,
  149. 'max_retries' => 1,
  150. 'retry_delay' => 500,
  151. 'timeout' => 5,
  152. // 'verify' => ROOT_PATH . 'addons/shopro/library/cacert.pem',
  153. // 'base_uri' => 'https://api.weixin.qq.com/', // 如果你在国外想要覆盖默认的 url 的时候才使用,根据不同的模块配置不同的 uri
  154. ],
  155. ];
  156. }
  157. }