| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- <?php
- namespace common\components;
- use Yii;
- use common\helpers\FileHelper;
- use common\helpers\ArrayHelper;
- /**
- * Class Wechat
- * @package common\components
- */
- class Wechat extends \jianyan\easywechat\Wechat
- {
- public $rebinds = [];
- /**
- * Wechat constructor.
- * @param array $config
- * @throws \yii\base\InvalidConfigException
- */
- public function __construct($config = [])
- {
- parent::__construct($config);
- $this->initParams();
- }
- /**
- * @param $config
- * @throws \yii\base\InvalidConfigException
- */
- protected function initParams()
- {
- //获取配置
- $config = [];
- $callbackUrl = $notifyUrl = '';
- // 微信公众号
- if (!empty(Yii::$app->params['wechatConfig'])) {
- Yii::$app->params['wechatConfig'] = ArrayHelper::merge([
- /**
- * Debug 模式,bool 值:true/false
- *
- * 当值为 false 时,所有的日志都不会记录
- */
- 'debug' => true,
- /**
- * 账号基本信息,请从微信公众平台/开放平台获取
- */
- 'app_id' => $config['wechat_appid'] ?? '',
- 'secret' => $config['wechat_appsecret'] ?? '',
- 'token' => $config['wechat_token'] ?? '',
- 'aes_key' => $config['wechat_encodingaeskey'] ?? '', // 兼容与安全模式下请一定要填写!!!
- /**
- * 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名
- * 使用自定义类名时,构造函数将会接收一个 `EasyWeChat\Kernel\Http\Response` 实例
- */
- 'response_type' => 'array',
- /**
- * 日志配置
- *
- * level: 日志级别, 可选为:
- * debug/info/notice/warning/error/critical/alert/emergency
- * permission:日志文件权限(可选),默认为null(若为null值,monolog会取0644)
- * file:日志文件位置(绝对路径!!!),要求可写权限
- */
- 'log' => [
- 'level' => 'debug',
- 'permission' => 0777,
- 'file' => $this->createLogPath('wechat'),
- ],
- /**
- * 接口请求相关配置,超时时间等,具体可用参数请参考:
- * http://docs.guzzlephp.org/en/stable/request-options.html
- *
- * - retries: 重试次数,默认 1,指定当 http 请求失败时重试的次数。
- * - retry_delay: 重试延迟间隔(单位:ms),默认 500
- * - log_template: 指定 HTTP 日志模板,请参考:https://github.com/guzzle/guzzle/blob/master/src/MessageFormatter.php
- */
- 'http' => [
- 'retries' => 1,
- 'retry_delay' => 500,
- 'timeout' => 5.0,
- // 'base_uri' => 'https://api.weixin.qq.com/',
- ],
- /**
- * OAuth 配置
- *
- * scopes:公众平台(snsapi_userinfo / snsapi_base),开放平台:snsapi_login
- * callback:OAuth授权完成后的回调页地址
- */
- 'oauth' => [
- 'scopes' => ['snsapi_userinfo'],
- 'callback' => $callbackUrl,
- ]
- ], Yii::$app->params['wechatConfig']);
- }
- // 微信支付
- if (!empty(Yii::$app->params['wechatPaymentConfig'])) {
- Yii::$app->params['wechatPaymentConfig'] = ArrayHelper::merge([
- 'app_id' => $config['wechat_appid'] ?? '',
- 'secret' => $config['wechat_appsecret'] ?? '',
- 'mch_id' => $config['wechat_mchid'] ?? '',
- 'key' => $config['wechat_api_key'] ?? '', // API 密钥
- // 如需使用敏感接口(如退款、发送红包等)需要配置 API 证书路径(登录商户平台下载 API 证书)
- 'cert_path' => $config['wechat_cert_path'] ?? '', // XXX: 绝对路径!!!!
- 'key_path' => $config['wechat_key_path'] ?? '', // XXX: 绝对路径!!!!
- // 支付回调地址
- 'notify_url' => $notifyUrl,
- 'sandbox' => false, // 设置为 false 或注释则关闭沙箱模式
- ], Yii::$app->params['wechatPaymentConfig']);
- }
- // 小程序
- if (!empty(Yii::$app->params['wechatMiniProgramConfig'])) {
- Yii::$app->params['wechatMiniProgramConfig'] = ArrayHelper::merge([
- 'app_id' => $config['miniprogram_appid'] ?? '',
- 'secret' => $config['miniprogram_secret'] ?? '',
- // 下面为可选项
- // 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名
- 'response_type' => 'array',
- 'log' => [
- 'level' => 'debug',
- 'file' => $this->createLogPath('miniprogram'),
- ],
- ], Yii::$app->params['wechatMiniProgramConfig']);
- }
- unset($config);
- }
- /**
- * 创建日志文件
- *
- * @param $path
- * @return bool
- */
- private function createLogPath($catalogue)
- {
- $logPathArr = [];
- $logPathArr[] = Yii::getAlias('@runtime');
- $logPathArr[] = 'wechat-' . $catalogue;
- $logPathArr[] = date('Y-m');
- $logPath = implode(DIRECTORY_SEPARATOR, $logPathArr);;
- FileHelper::mkdirs($logPath);
- $logPath .= DIRECTORY_SEPARATOR;
- $logPath .= date('d') . '.log';
- return $logPath;
- }
- }
|