From e9c894073010b4910fe019cefd4a05ab28d7bd25 Mon Sep 17 00:00:00 2001 From: JaguarJack Date: Sat, 6 Jun 2020 22:37:36 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=BE=AE=E4=BF=A1=E5=8F=91?= =?UTF-8?q?=E5=B8=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/wechat.php | 100 -------------- extend/catcher/CatchAdminService.php | 2 + .../catcher/command/publish/WechatCommand.php | 130 ++++++++++++++++++ 3 files changed, 132 insertions(+), 100 deletions(-) delete mode 100644 config/wechat.php create mode 100644 extend/catcher/command/publish/WechatCommand.php diff --git a/config/wechat.php b/config/wechat.php deleted file mode 100644 index 5cddea2..0000000 --- a/config/wechat.php +++ /dev/null @@ -1,100 +0,0 @@ - - * @copyright By CatchAdmin - * @license https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt - */ -return [ - /** - * 公众号配置 - * - */ - 'official_account' => [ - /** - * 账号基本信息,请从微信公众平台/开放平台获取 - */ - 'app_id' => env('wechat.official_app_id'), // AppID - 'secret' => env('wechat.official_secret'), // AppSecret - 'token' => env('wechat.official_token'), // Token - 'aes_key' => env('wechat.official_aes_key'), // EncodingAESKey,兼容与安全模式下请一定要填写!!! - - /** - * 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名 - * 使用自定义类名时,构造函数将会接收一个 `EasyWeChat\Kernel\Http\Response` 实例 - */ - 'response_type' => 'array', - - /** - * 日志配置 - * - * level: 日志级别, 可选为: - * debug/info/notice/warning/error/critical/alert/emergency - * path:日志文件位置(绝对路径!!!),要求可写权限 - */ - 'log' => [ - 'default' => 'dev', // 默认使用的 channel,生产环境可以改为下面的 prod - 'channels' => [ - // 测试环境 - 'dev' => [ - 'driver' => 'single', - 'path' => '/tmp/easywechat.log', - 'level' => 'debug', - ], - // 生产环境 - 'prod' => [ - 'driver' => 'daily', - 'path' => '/tmp/easywechat.log', - 'level' => 'info', - ], - ], - ], - - /** - * 接口请求相关配置,超时时间等,具体可用参数请参考: - * http://docs.guzzlephp.org/en/stable/request-config.html - * - * - retries: 重试次数,默认 1,指定当 http 请求失败时重试的次数。 - * - retry_delay: 重试延迟间隔(单位:ms),默认 500 - * - log_template: 指定 HTTP 日志模板,请参考:https://github.com/guzzle/guzzle/blob/master/src/MessageFormatter.php - */ - 'http' => [ - 'max_retries' => 1, - 'retry_delay' => 500, - 'timeout' => 5.0, - // 'base_uri' => 'https://api.weixin.qq.com/', // 如果你在国外想要覆盖默认的 url 的时候才使用,根据不同的模块配置不同的 uri - ], - - /** - * OAuth 配置 - * - * scopes:公众平台(snsapi_userinfo / snsapi_base),开放平台:snsapi_login - * callback:OAuth授权完成后的回调页地址 - */ - 'oauth' => [ - 'scopes' => ['snsapi_userinfo'], - 'callback' => '/examples/oauth_callback.php', - ], - ], - - /** - * wechat pay - * - */ - 'payment' => [ - 'app_id' => 'xxxx', - 'mch_id' => 'your-mch-id', - 'key' => 'key-for-signature', // API 密钥 - - // 如需使用敏感接口(如退款、发送红包等)需要配置 API 证书路径(登录商户平台下载 API 证书) - 'cert_path' => 'path/to/your/cert.pem', // XXX: 绝对路径!!!! - 'key_path' => 'path/to/your/key', // XXX: 绝对路径!!!! - - 'notify_url' => '默认的订单回调地址', // 你也可以在下单时单独设置来想覆盖它 - ], - - // 更多配置请查看 https://www.easywechat.com/docs -]; diff --git a/extend/catcher/CatchAdminService.php b/extend/catcher/CatchAdminService.php index 41af69b..43e34e0 100644 --- a/extend/catcher/CatchAdminService.php +++ b/extend/catcher/CatchAdminService.php @@ -1,6 +1,7 @@ + * @copyright By CatchAdmin + * @license https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt + */ +namespace catcher\command\publish; + +use think\console\Command; +use think\console\Input; +use think\console\input\Argument; +use think\console\input\Option; +use think\console\Output; +use catcher\CatchAdmin; + +class WechatCommand extends Command +{ + protected function configure() + { + $this->setName('publish:wechat') + ->setDescription('publish wechat config'); + } + + protected function execute(Input $input, Output $output) + { + file_put_contents(config_path() . 'wechat.php', $this->config()); + + file_put_contents(root_path(). '.env', $this->env(), FILE_APPEND); + + $output->warning('wechat publish successfully'); + } + + protected function config() + { + return << [ + /** + * 账号基本信息,请从微信公众平台/开放平台获取 + */ + 'app_id' => env('wechat.official_app_id'), // AppID + 'secret' => env('wechat.official_secret'), // AppSecret + 'token' => env('wechat.official_token'), // Token + 'aes_key' => env('wechat.official_aes_key'), // EncodingAESKey,兼容与安全模式下请一定要填写!!! + + 'response_type' => 'array', + + /** + * OAuth 配置 + * + * scopes:公众平台(snsapi_userinfo / snsapi_base),开放平台:snsapi_login + * callback:OAuth授权完成后的回调页地址 + */ + 'oauth' => [ + 'scopes' => ['snsapi_userinfo'], + 'callback' => '/examples/oauth_callback.php', + ], + ], + + /** + * wechat pay + * + */ + 'payment' => [ + 'app_id' => 'xxxx', + 'mch_id' => 'your-mch-id', + 'key' => 'key-for-signature', // API 密钥 + + // 如需使用敏感接口(如退款、发送红包等)需要配置 API 证书路径(登录商户平台下载 API 证书) + 'cert_path' => 'path/to/your/cert.pem', // XXX: 绝对路径!!!! + 'key_path' => 'path/to/your/key', // XXX: 绝对路径!!!! + + 'notify_url' => '默认的订单回调地址', // 你也可以在下单时单独设置来想覆盖它 + ], + + // 更多配置请查看 https://www.easywechat.com/docs +]; +CONFIG; + } + + protected function env() + { + $filename = file_exists(root_path() . '.env') ? '.env' : '.example.env'; + + + $env = \parse_ini_file(root_path() . $filename, true); + + $env['WECHAT'] = $this->envConfig(); + + $dotEnv = ''; + + foreach ($env as $key => $e) { + if (is_string($e)) { + $dotEnv .= sprintf('%s = %s', $key, $e === '1' ? 'true' : ($e === '' ? 'false' : $e)) . PHP_EOL; + $dotEnv .= PHP_EOL; + } else { + $dotEnv .= sprintf('[%s]', $key) . PHP_EOL; + foreach ($e as $k => $v) { + $dotEnv .= sprintf('%s = %s', $k, $v === '1' ? 'true' : ($v === '' ? 'false' : $v)) . PHP_EOL; + } + + $dotEnv .= PHP_EOL; + } + } + + file_put_contents(root_path() . '.env', $dotEnv); + + } + + + protected function envConfig() + { + return [ + "official_app_id" => null, + "official_secret" => null, + "official_token" => null, + "official_aes_key" => null, + ]; + } +} \ No newline at end of file