diff --git a/README.md b/README.md new file mode 100644 index 0000000..906a6e4 --- /dev/null +++ b/README.md @@ -0,0 +1,75 @@ +# think-wechat + +微信 SDK For ThinkPHP 6.0 基于[overtrue/wechat](https://github.com/overtrue/wechat) + +## 框架要求 + +ThinkPHP6.0(中间件要求支持 ThinkPH6.0+) + +## 安装 + +``` +composer require larva/think-wechat -vv +``` + +## 配置 + +1. 修改配置文件 + 修改项目根目录下 config/wechat.php 中对应的参数 + +2. 每个模块基本都支持多账号,默认为 default。 + +## 使用 + +### 接受普通消息 + +新建一个 Controller,我这边用的是 Wechat + +```php +server->push(function($message){ + return 'hello,world'; + }); + $app->server->serve()->send(); + } +} +``` + +#### 获得 SDK 实例 使用 Facade + +```php +use larva\wechat\Wechat; + +$officialAccount = Wechat::officialAccount(); // 公众号 +$work = Wechat::work(); // 企业微信 +$payment = Wechat::payment(); // 微信支付 +$openPlatform = Wechat::openPlatform(); // 开放平台 +$miniProgram = Wechat::miniProgram(); // 小程序 +$openWork = Wechat::openWork(); // 企业微信第三方服务商 +$microMerchant = Wechat::microMerchant(); // 小微商户 +``` + +以上均支持传入自定义账号:例如 + +```php +$officialAccount = Wechat::officialAccount('test'); // 公众号 +``` + +更多 SDK 的具体使用请参考:https://easywechat.com + +## 参考项目 + +- [overtrue/laravel-wechat](https://github.com/overtrue/laravel-wechat) +- [naixiaoxin/think-wechat](https://github.com/naixiaoxin/think-wechat) diff --git a/composer.json b/composer.json index 76e266a..9b1e507 100644 --- a/composer.json +++ b/composer.json @@ -15,6 +15,7 @@ ], "require": { "php": "^7.4|^8.0", + "ext-json": "*", "topthink/framework": "^6.0.0", "overtrue/wechat": "^5.5" }, @@ -32,7 +33,7 @@ "larva\\wechat\\WechatService" ], "config":{ - "sms": "config/wechat.php" + "wechat": "config/wechat.php" } } }, diff --git a/config/wechat.php b/config/wechat.php index 28a3b2c..0aabc7b 100644 --- a/config/wechat.php +++ b/config/wechat.php @@ -1,5 +1,118 @@ [ + /* + * 指定 API 调用返回结果的类型:array(default)/object/raw/自定义类名 + */ + 'response_type' => 'array', + + /* + * 使用 ThinkPHP 的缓存系统 + */ + 'use_tp_cache' => true, + + /* + * 日志配置 + * + * level: 日志级别,可选为: + * debug/info/notice/warning/error/critical/alert/emergency + * file:日志文件位置(绝对路径!!!),要求可写权限 + */ + 'log' => [ + 'level' => env('WECHAT_LOG_LEVEL', 'debug'), + 'file' => env('WECHAT_LOG_FILE', runtime_path() . 'log/wechat.log'), + ], + ], + + //公众号 + 'official_account' => [ + 'default' => [ + // AppID + 'app_id' => env('WECHAT_OFFICIAL_ACCOUNT_APPID', 'your-app-id'), + // AppSecret + 'secret' => env('WECHAT_OFFICIAL_ACCOUNT_SECRET', 'your-app-secret'), + // Token + 'token' => env('WECHAT_OFFICIAL_ACCOUNT_TOKEN', 'your-token'), + // EncodingAESKey + 'aes_key' => env('WECHAT_OFFICIAL_ACCOUNT_AES_KEY', ''), + + /* + * OAuth 配置 + * + * scopes:公众平台(snsapi_userinfo / snsapi_base),开放平台:snsapi_login + * callback:OAuth授权完成后的回调页地址(如果使用中间件,则随便填写。。。) + */ + 'oauth' => [ + 'scopes' => array_map( + 'trim', + explode(',', env('WECHAT_OFFICIAL_ACCOUNT_OAUTH_SCOPES', 'snsapi_userinfo')) + ), + 'callback' => env('WECHAT_OFFICIAL_ACCOUNT_OAUTH_CALLBACK', '/examples/oauth_callback.php'), + ], + ], + ], + +// //第三方开发平台 +// 'open_platform' => [ +// 'default' => [ +// 'app_id' => env('WECHAT_OPEN_PLATFORM_APPID', ''), +// 'secret' => env('WECHAT_OPEN_PLATFORM_SECRET', ''), +// 'token' => env('WECHAT_OPEN_PLATFORM_TOKEN', ''), +// 'aes_key' => env('WECHAT_OPEN_PLATFORM_AES_KEY', ''), +// ], +// ], + + //小程序 + 'mini_program' => [ + 'default' => [ + 'app_id' => env('WECHAT_MINI_PROGRAM_APPID', ''), + 'secret' => env('WECHAT_MINI_PROGRAM_SECRET', ''), + 'token' => env('WECHAT_MINI_PROGRAM_TOKEN', ''), + 'aes_key' => env('WECHAT_MINI_PROGRAM_AES_KEY', ''), + ], + ], + + //支付 + 'payment' => [ + 'default' => [ + 'sandbox' => env('WECHAT_PAYMENT_SANDBOX', false), + 'app_id' => env('WECHAT_PAYMENT_APPID', ''), + 'mch_id' => env('WECHAT_PAYMENT_MCH_ID', 'your-mch-id'), + 'key' => env('WECHAT_PAYMENT_KEY', 'key-for-signature'), + 'cert_path' => env('WECHAT_PAYMENT_CERT_PATH', 'path/to/cert/apiclient_cert.pem'), // XXX: 绝对路径!!!! + 'key_path' => env('WECHAT_PAYMENT_KEY_PATH', 'path/to/cert/apiclient_key.pem'), // XXX: 绝对路径!!!! + 'notify_url' => 'http://example.com/payments/wechat-notify', // 默认支付结果通知地址 + ], + ], + +// //企业微信 +// 'work' => [ +// 'default' => [ +// 'corp_id' => 'xxxxxxxxxxxxxxxxx', +// 'agent_id' => 100000, +// 'secret' => env('WECHAT_WORK_AGENT_CONTACTS_SECRET', ''), +// ], +// ], + +// //企业开放平台 +// 'open_work' => [ +// 'default' => [ +// //参考EasyWechat官方文档 +// //https://www.easywechat.com/docs/4.1/open-work/index +// ], +// ], + +// //小微商户 +// 'micro_merchant' => [ +// 'default' => [ +// //参考EasyWechat官方文档 +// //https://www.easywechat.com/docs/4.1/micro-merchant/index +// ], +// ], ]; diff --git a/src/CacheBridge.php b/src/CacheBridge.php new file mode 100644 index 0000000..066b828 --- /dev/null +++ b/src/CacheBridge.php @@ -0,0 +1,100 @@ +cache = $cache; + } + + /** + * @param string $key + * @param null $default + * @return mixed + */ + public function get($key, $default = null) + { + return $this->cache->get($key, $default); + } + + /** + * @param string $key + * @param mixed $value + * @param null $ttl + * @return bool + */ + public function set($key, $value, $ttl = null): bool + { + return $this->cache->set($key, $value, $ttl); + } + + /** + * @param string $key + * @return bool + */ + public function delete($key): bool + { + return $this->cache->rm($key); + } + + /** + * @return bool + */ + public function clear(): bool + { + return $this->cache->clear(); + } + + /** + * @param iterable $keys + * @param null $default + * @return iterable|void|null + */ + public function getMultiple($keys, $default = null) + { + return null; + } + + /** + * @param iterable $values + * @param null $ttl + * @return bool + */ + public function setMultiple($values, $ttl = null): bool + { + return false; + } + + /** + * @param iterable $keys + * @return false + */ + public function deleteMultiple($keys): bool + { + return false; + } + + /** + * @param string $key + * @return bool + */ + public function has($key): bool + { + return $this->cache->has($key); + } +} \ No newline at end of file diff --git a/src/Wechat.php b/src/Wechat.php new file mode 100644 index 0000000..01870c0 --- /dev/null +++ b/src/Wechat.php @@ -0,0 +1,82 @@ + \EasyWeChat\OfficialAccount\Application::class, + 'work' => \EasyWeChat\Work\Application::class, + 'mini_program' => \EasyWeChat\MiniProgram\Application::class, + 'payment' => \EasyWeChat\Payment\Application::class, + 'open_platform' => \EasyWeChat\OpenPlatform\Application::class, + 'open_work' => \EasyWeChat\OpenWork\Application::class, + 'micro_merchant' => \EasyWeChat\MicroMerchant\Application::class, + ]; + + /** + * 注册服务 + * + * @return void + */ + public function register() + { + $default = config('wechat.default') ? config('wechat.default') : []; + foreach ($this->apps as $name => $app) { + if (! config('wechat.' . $name)) { + continue; + } + $configs = config('wechat.' . $name); + foreach ($configs as $config_name => $module_default) { + $this->app->bind('wechat.' . $name . '.' . $config_name, function ($config = []) use ($app, $module_default, $default) { + //合并配置文件 + $account_config = array_merge($module_default, $default, $config); + $account_app = app($app, ['config' => $account_config]); + if (config('wechat.default.use_tp_cache')) { + $account_app['cache'] = app(CacheBridge::class); + } + return $account_app; + }); + } + if (isset($configs['default'])) { + $this->app->bind('wechat.' . $name, 'wechat.' . $name . '.default'); + } + } + + $this->app->bind('wechat.facade', Wechat::class); + } + + /** + * 执行服务 + * + * @return void + */ + public function boot() + { + // + } +} \ No newline at end of file