Wechat.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. namespace common\components;
  3. use Yii;
  4. use common\helpers\FileHelper;
  5. use common\helpers\ArrayHelper;
  6. /**
  7. * Class Wechat
  8. * @package common\components
  9. */
  10. class Wechat extends \jianyan\easywechat\Wechat
  11. {
  12. public $rebinds = [];
  13. /**
  14. * Wechat constructor.
  15. * @param array $config
  16. * @throws \yii\base\InvalidConfigException
  17. */
  18. public function __construct($config = [])
  19. {
  20. parent::__construct($config);
  21. $this->initParams();
  22. }
  23. /**
  24. * @param $config
  25. * @throws \yii\base\InvalidConfigException
  26. */
  27. protected function initParams()
  28. {
  29. //获取配置
  30. $config = [];
  31. $callbackUrl = $notifyUrl = '';
  32. // 微信公众号
  33. if (!empty(Yii::$app->params['wechatConfig'])) {
  34. Yii::$app->params['wechatConfig'] = ArrayHelper::merge([
  35. /**
  36. * Debug 模式,bool 值:true/false
  37. *
  38. * 当值为 false 时,所有的日志都不会记录
  39. */
  40. 'debug' => true,
  41. /**
  42. * 账号基本信息,请从微信公众平台/开放平台获取
  43. */
  44. 'app_id' => $config['wechat_appid'] ?? '',
  45. 'secret' => $config['wechat_appsecret'] ?? '',
  46. 'token' => $config['wechat_token'] ?? '',
  47. 'aes_key' => $config['wechat_encodingaeskey'] ?? '', // 兼容与安全模式下请一定要填写!!!
  48. /**
  49. * 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名
  50. * 使用自定义类名时,构造函数将会接收一个 `EasyWeChat\Kernel\Http\Response` 实例
  51. */
  52. 'response_type' => 'array',
  53. /**
  54. * 日志配置
  55. *
  56. * level: 日志级别, 可选为:
  57. * debug/info/notice/warning/error/critical/alert/emergency
  58. * permission:日志文件权限(可选),默认为null(若为null值,monolog会取0644)
  59. * file:日志文件位置(绝对路径!!!),要求可写权限
  60. */
  61. 'log' => [
  62. 'level' => 'debug',
  63. 'permission' => 0777,
  64. 'file' => $this->createLogPath('wechat'),
  65. ],
  66. /**
  67. * 接口请求相关配置,超时时间等,具体可用参数请参考:
  68. * http://docs.guzzlephp.org/en/stable/request-options.html
  69. *
  70. * - retries: 重试次数,默认 1,指定当 http 请求失败时重试的次数。
  71. * - retry_delay: 重试延迟间隔(单位:ms),默认 500
  72. * - log_template: 指定 HTTP 日志模板,请参考:https://github.com/guzzle/guzzle/blob/master/src/MessageFormatter.php
  73. */
  74. 'http' => [
  75. 'retries' => 1,
  76. 'retry_delay' => 500,
  77. 'timeout' => 5.0,
  78. // 'base_uri' => 'https://api.weixin.qq.com/',
  79. ],
  80. /**
  81. * OAuth 配置
  82. *
  83. * scopes:公众平台(snsapi_userinfo / snsapi_base),开放平台:snsapi_login
  84. * callback:OAuth授权完成后的回调页地址
  85. */
  86. 'oauth' => [
  87. 'scopes' => ['snsapi_userinfo'],
  88. 'callback' => $callbackUrl,
  89. ]
  90. ], Yii::$app->params['wechatConfig']);
  91. }
  92. // 微信支付
  93. if (!empty(Yii::$app->params['wechatPaymentConfig'])) {
  94. Yii::$app->params['wechatPaymentConfig'] = ArrayHelper::merge([
  95. 'app_id' => $config['wechat_appid'] ?? '',
  96. 'secret' => $config['wechat_appsecret'] ?? '',
  97. 'mch_id' => $config['wechat_mchid'] ?? '',
  98. 'key' => $config['wechat_api_key'] ?? '', // API 密钥
  99. // 如需使用敏感接口(如退款、发送红包等)需要配置 API 证书路径(登录商户平台下载 API 证书)
  100. 'cert_path' => $config['wechat_cert_path'] ?? '', // XXX: 绝对路径!!!!
  101. 'key_path' => $config['wechat_key_path'] ?? '', // XXX: 绝对路径!!!!
  102. // 支付回调地址
  103. 'notify_url' => $notifyUrl,
  104. 'sandbox' => false, // 设置为 false 或注释则关闭沙箱模式
  105. ], Yii::$app->params['wechatPaymentConfig']);
  106. }
  107. // 小程序
  108. if (!empty(Yii::$app->params['wechatMiniProgramConfig'])) {
  109. Yii::$app->params['wechatMiniProgramConfig'] = ArrayHelper::merge([
  110. 'app_id' => $config['miniprogram_appid'] ?? '',
  111. 'secret' => $config['miniprogram_secret'] ?? '',
  112. // 下面为可选项
  113. // 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名
  114. 'response_type' => 'array',
  115. 'log' => [
  116. 'level' => 'debug',
  117. 'file' => $this->createLogPath('miniprogram'),
  118. ],
  119. ], Yii::$app->params['wechatMiniProgramConfig']);
  120. }
  121. unset($config);
  122. }
  123. /**
  124. * 创建日志文件
  125. *
  126. * @param $path
  127. * @return bool
  128. */
  129. private function createLogPath($catalogue)
  130. {
  131. $logPathArr = [];
  132. $logPathArr[] = Yii::getAlias('@runtime');
  133. $logPathArr[] = 'wechat-' . $catalogue;
  134. $logPathArr[] = date('Y-m');
  135. $logPath = implode(DIRECTORY_SEPARATOR, $logPathArr);;
  136. FileHelper::mkdirs($logPath);
  137. $logPath .= DIRECTORY_SEPARATOR;
  138. $logPath .= date('d') . '.log';
  139. return $logPath;
  140. }
  141. }