Wechat.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. namespace common\components;
  3. use Yii;
  4. use common\helpers\FileHelper;
  5. use common\enums\AppEnum;
  6. use common\helpers\ArrayHelper;
  7. /**
  8. * Class Wechat
  9. * @package common\components
  10. * @author jianyan74 <751393839@qq.com>
  11. */
  12. class Wechat extends \jianyan\easywechat\Wechat
  13. {
  14. /**
  15. * Wechat constructor.
  16. * @param array $config
  17. * @throws \yii\base\InvalidConfigException
  18. */
  19. public function __construct($config = [])
  20. {
  21. parent::__construct($config);
  22. $this->initParams();
  23. }
  24. /**
  25. * @param $config
  26. * @throws \yii\base\InvalidConfigException
  27. */
  28. protected function initParams()
  29. {
  30. $config = Yii::$app->services->config->configAll();
  31. $callbackUrl = $notifyUrl = '';
  32. if (!empty(Yii::$app->id) && Yii::$app->id != AppEnum::CONSOLE) {
  33. $callbackUrl = Yii::$app->request->hostInfo . Yii::$app->request->getUrl();
  34. $notifyUrl = Yii::$app->request->hostInfo . Yii::$app->urlManager->createUrl(['notify/index']);
  35. }
  36. // 微信公众号
  37. Yii::$app->params['wechatConfig'] = ArrayHelper::merge([
  38. /**
  39. * 账号基本信息,请从微信公众平台/开放平台获取
  40. */
  41. 'app_id' => $config['wechat_mp_app_id'] ?? '',
  42. 'secret' => $config['wechat_mp_appsecret'] ?? '',
  43. 'token' => $config['wechat_mp_token'] ?? '',
  44. 'aes_key' => $config['wechat_mp_encodingaeskey'] ?? '', // 兼容与安全模式下请一定要填写!!!
  45. /**
  46. * 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名
  47. * 使用自定义类名时,构造函数将会接收一个 `EasyWeChat\Kernel\Http\Response` 实例
  48. */
  49. 'response_type' => 'array',
  50. /**
  51. * 日志配置
  52. *
  53. * level: 日志级别, 可选为:
  54. * debug/info/notice/warning/error/critical/alert/emergency
  55. * permission:日志文件权限(可选),默认为null(若为null值,monolog会取0644)
  56. * file:日志文件位置(绝对路径!!!),要求可写权限
  57. */
  58. 'log' => [
  59. 'default' => YII_DEBUG ? 'dev' : 'prod', // 默认使用的 channel,生产环境可以改为下面的 prod
  60. 'channels' => [
  61. // 测试环境
  62. 'dev' => [
  63. 'driver' => 'single',
  64. 'path' => $this->createLogPath('dev'),
  65. 'level' => 'debug',
  66. ],
  67. // 生产环境
  68. 'prod' => [
  69. 'driver' => 'daily',
  70. 'path' => $this->createLogPath('prod'),
  71. 'level' => 'info',
  72. ],
  73. ],
  74. ],
  75. /**
  76. * 接口请求相关配置,超时时间等,具体可用参数请参考:
  77. * http://docs.guzzlephp.org/en/stable/request-config.html
  78. *
  79. * - retries: 重试次数,默认 1,指定当 http 请求失败时重试的次数。
  80. * - retry_delay: 重试延迟间隔(单位:ms),默认 500
  81. * - log_template: 指定 HTTP 日志模板,请参考:https://github.com/guzzle/guzzle/blob/master/src/MessageFormatter.php
  82. */
  83. 'http' => [
  84. 'max_retries' => 1,
  85. 'retry_delay' => 500,
  86. 'timeout' => 5.0,
  87. // 'base_uri' => 'https://api.weixin.qq.com/', // 如果你在国外想要覆盖默认的 url 的时候才使用,根据不同的模块配置不同的 uri
  88. ],
  89. /**
  90. * OAuth 配置
  91. *
  92. * scopes:公众平台(snsapi_userinfo / snsapi_base),开放平台:snsapi_login
  93. * callback:OAuth授权完成后的回调页地址
  94. */
  95. 'oauth' => [
  96. 'scopes' => ['snsapi_userinfo'],
  97. 'callback' => $callbackUrl,
  98. ]
  99. ], Yii::$app->params['wechatConfig']);
  100. // 微信支付
  101. Yii::$app->params['wechatPaymentConfig'] = ArrayHelper::merge([
  102. 'app_id' => $config['wechat_mp_app_id'] ?? '',
  103. 'secret' => $config['wechat_mp_appsecret'] ?? '',
  104. 'mch_id' => $config['wechat_mch_id'] ?? '',
  105. 'key' => $config['wechat_mch_secret_key'] ?? '', // API 密钥
  106. // 如需使用敏感接口(如退款、发送红包等)需要配置 API 证书路径(登录商户平台下载 API 证书)
  107. 'cert_path' => $config['wechat_mch_public_cert_path'] ?? '', // XXX: 绝对路径!!!!
  108. 'key_path' => $config['wechat_mch_secret_cert'] ?? '', // XXX: 绝对路径!!!!
  109. // 支付回调地址
  110. 'notify_url' => $notifyUrl,
  111. 'sandbox' => false, // 设置为 false 或注释则关闭沙箱模式
  112. 'log' => [
  113. 'default' => YII_DEBUG ? 'dev' : 'prod', // 默认使用的 channel,生产环境可以改为下面的 prod
  114. 'channels' => [
  115. // 测试环境
  116. 'dev' => [
  117. 'driver' => 'single',
  118. 'path' => $this->createLogPath('payment-dev'),
  119. 'level' => 'debug',
  120. ],
  121. // 生产环境
  122. 'prod' => [
  123. 'driver' => 'daily',
  124. 'path' => $this->createLogPath('payment-prod'),
  125. 'level' => 'info',
  126. ],
  127. ],
  128. ],
  129. ], Yii::$app->params['wechatPaymentConfig']);
  130. // 小程序
  131. Yii::$app->params['wechatMiniProgramConfig'] = ArrayHelper::merge([
  132. 'app_id' => $config['wechat_mini_app_id'] ?? '',
  133. 'secret' => $config['wechat_mini_secret'] ?? '',
  134. // 下面为可选项
  135. // 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名
  136. 'response_type' => 'array',
  137. 'log' => [
  138. 'default' => YII_DEBUG ? 'dev' : 'prod', // 默认使用的 channel,生产环境可以改为下面的 prod
  139. 'channels' => [
  140. // 测试环境
  141. 'dev' => [
  142. 'driver' => 'single',
  143. 'path' => $this->createLogPath('miniProgram-dev'),
  144. 'level' => 'debug',
  145. ],
  146. // 生产环境
  147. 'prod' => [
  148. 'driver' => 'daily',
  149. 'path' => $this->createLogPath('miniProgram-prod'),
  150. 'level' => 'info',
  151. ],
  152. ],
  153. ],
  154. ], Yii::$app->params['wechatMiniProgramConfig']);
  155. unset($config);
  156. }
  157. /**
  158. * 创建日志文件
  159. *
  160. * @param $path
  161. * @return bool
  162. */
  163. private function createLogPath($catalogue)
  164. {
  165. $logPathArr = [];
  166. $logPathArr[] = Yii::getAlias('@runtime');
  167. $logPathArr[] = 'wechat-' . $catalogue;
  168. $logPathArr[] = date('Y-m');
  169. $logPath = implode(DIRECTORY_SEPARATOR, $logPathArr);;
  170. FileHelper::mkdirs($logPath);
  171. $logPath .= DIRECTORY_SEPARATOR;
  172. $logPath .= date('d') . '.log';
  173. return $logPath;
  174. }
  175. }
粤ICP备19079148号