-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
437 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
<?php | ||
|
||
namespace app\controller; | ||
|
||
use think\Controller; | ||
|
||
class Wechat extends Controller | ||
{ | ||
|
||
public function index() | ||
{ | ||
// 先初始化微信 | ||
$app = app('wechat.official_account'); | ||
$app->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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,118 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
return [ | ||
|
||
/* | ||
* 默认配置,将会合并到各模块中 | ||
*/ | ||
'default' => [ | ||
/* | ||
* 指定 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 | ||
// ], | ||
// ], | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
<?php | ||
declare (strict_types=1); | ||
|
||
namespace larva\wechat; | ||
|
||
use Psr\SimpleCache\CacheInterface; | ||
use think\Cache; | ||
|
||
class CacheBridge implements CacheInterface | ||
{ | ||
/** | ||
* @var Cache | ||
*/ | ||
protected Cache $cache; | ||
|
||
/** | ||
* CacheBridge constructor. | ||
* @param Cache $cache | ||
*/ | ||
public function __construct(Cache $cache) | ||
{ | ||
$this->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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
declare (strict_types=1); | ||
namespace larva\wechat; | ||
|
||
use think\Facade; | ||
|
||
class Wechat extends Facade | ||
{ | ||
/** | ||
* 默认为 Server. | ||
* | ||
* @return string | ||
*/ | ||
public static function getFacadeAccessor(): string | ||
{ | ||
return 'wechat.official_account'; | ||
} | ||
|
||
/** | ||
* @param mixed $name | ||
* @return \EasyWeChat\OfficialAccount\Application | ||
*/ | ||
public static function officialAccount($name = '') | ||
{ | ||
return $name ? app('wechat.official_account.' . $name) : app('wechat.official_account'); | ||
} | ||
|
||
/** | ||
* @param mixed $name | ||
* @return \EasyWeChat\Work\Application | ||
*/ | ||
public static function work($name = '') | ||
{ | ||
return $name ? app('wechat.work.' . $name) : app('wechat.work'); | ||
} | ||
|
||
/** | ||
* @param mixed $name | ||
* @param mixed $config | ||
* @return \EasyWeChat\Payment\Application | ||
*/ | ||
public static function payment($name = '') | ||
{ | ||
return $name ? app('wechat.payment.' . $name) : app('wechat.payment'); | ||
} | ||
|
||
/** | ||
* @param mixed $name | ||
* @return \EasyWeChat\MiniProgram\Application | ||
*/ | ||
public static function miniProgram($name = '') | ||
{ | ||
return $name ? app('wechat.mini_program.' . $name) : app('wechat.mini_program'); | ||
} | ||
|
||
/** | ||
* @param mixed $name | ||
* @return \EasyWeChat\OpenPlatform\Application | ||
*/ | ||
public static function openPlatform($name = '') | ||
{ | ||
return $name ? app('wechat.open_platform.' . $name) : app('wechat.open_platform'); | ||
} | ||
|
||
/** | ||
* @param mixed $name | ||
* @return \EasyWeChat\OpenWork\Application | ||
*/ | ||
public static function openWork($name = '') | ||
{ | ||
return $name ? app('wechat.open_work.' . $name) : app('wechat.open_work'); | ||
} | ||
|
||
/** | ||
* @param mixed $name | ||
* @return \EasyWeChat\MicroMerchant\Application | ||
*/ | ||
public static function microMerchant($name = '') | ||
{ | ||
return $name ? app('wechat.micro_merchant.' . $name) : app('wechat.micro_merchant'); | ||
} | ||
} |
Oops, something went wrong.