-
Notifications
You must be signed in to change notification settings - Fork 1
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
hector
committed
Nov 6, 2019
0 parents
commit d29c937
Showing
10 changed files
with
605 additions
and
0 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,4 @@ | ||
.idea | ||
.vscode | ||
/vendor/ | ||
composer.lock |
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,22 @@ | ||
|
||
The MIT License (MIT) | ||
|
||
Copyright (c) hectorqin <[email protected]> | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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,85 @@ | ||
# think-wechat | ||
|
||
[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fhectorqin%2Fthink-wechat.svg?type=shield)](https://app.fossa.io/projects/git%2Bgithub.com%2Fhectorqin%2Fthink-wechat?ref=badge_shield) | ||
|
||
微信SDK For ThinkPHP 5.1, ThinkPHP 6.0 基于[overtrue/wechat](https://github.com/overtrue/wechat) | ||
|
||
## 框架要求 | ||
|
||
ThinkPHP >= 5.1 | ||
|
||
## 安装 | ||
|
||
```bash | ||
composer require hectorqin/think-wechat | ||
``` | ||
|
||
## 配置 | ||
|
||
1. 修改配置文件 | ||
修改项目根目录下config/wechat.php中对应的参数 | ||
|
||
2. 每个模块基本都支持多账号,默认为 default。 | ||
|
||
## 使用 | ||
|
||
### 接受普通消息 | ||
|
||
```php | ||
<?php | ||
|
||
namespace app\index\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 Hectorqin\ThinkWechat\Facade; | ||
|
||
$officialAccount = Facade::officialAccount(); // 公众号 | ||
$work = Facade::work(); // 企业微信 | ||
$payment = Facade::payment(); // 微信支付 | ||
$openPlatform = Facade::openPlatform(); // 开放平台 | ||
$miniProgram = Facade::miniProgram(); // 小程序 | ||
$openWork = Facade::openWork(); // 企业微信第三方服务商 | ||
$microMerchant = Facade::microMerchant(); // 小微商户 | ||
``` | ||
|
||
以上均支持传入自定义账号:例如 | ||
|
||
```php | ||
$officialAccount = Facade::officialAccount('test'); // 公众号 | ||
``` | ||
|
||
以上均支持传入自定义账号+配置(注:这里的config和配置文件中账号的格式相同):例如 | ||
|
||
```php | ||
$officialAccount = Facade::officialAccount('',$config); // 公众号 | ||
``` | ||
|
||
更多 SDK 的具体使用请参考:[https://easywechat.com](https://easywechat.com) | ||
|
||
## License | ||
|
||
MIT | ||
|
||
[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fhectorqin%2Fthink-wechat.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2Fhectorqin%2Fthink-wechat?ref=badge_large) |
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,35 @@ | ||
{ | ||
"name": "hectorqin/think-wechat", | ||
"description": "EasyWechat For Thnkphp6.0", | ||
"type": "think-extend", | ||
"keywords": ["wechat", "weixin","easywechat","thinkphp","think", "sdk"], | ||
"require": { | ||
"php": ">=7.1.0", | ||
"overtrue/wechat": "~4.0", | ||
"topthink/framework": ">=5.1", | ||
"psr/simple-cache": "^1.0" | ||
}, | ||
"license": "MIT", | ||
"authors": [{ | ||
"name": "hectorqin", | ||
"email": "[email protected]" | ||
}], | ||
"autoload": { | ||
"psr-4": { | ||
"Hectorqin\\ThinkWechat\\": "src/" | ||
}, | ||
"files": [ | ||
"src/helper.php" | ||
] | ||
}, | ||
"extra": { | ||
"think": { | ||
"services": [ | ||
"Hectorqin\\ThinkWechat\\ThinkWechatService" | ||
], | ||
"config":{ | ||
"wechat": "src/config.php" | ||
} | ||
} | ||
} | ||
} |
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,62 @@ | ||
<?php | ||
/** | ||
* 在AppInit去做一些操作 | ||
* @author hectorqin<[email protected]> | ||
* @copyright hectorqin | ||
*/ | ||
|
||
namespace Hectorqin\ThinkWechat\Behavior; | ||
|
||
use EasyWeChat\MicroMerchant\Application as MicroMerchant; | ||
use EasyWeChat\MiniProgram\Application as MiniProgram; | ||
use EasyWeChat\OfficialAccount\Application as OfficialAccount; | ||
use EasyWeChat\OpenPlatform\Application as OpenPlatform; | ||
use EasyWeChat\OpenWork\Application as OpenWork; | ||
use EasyWeChat\Payment\Application as Payment; | ||
use EasyWeChat\Work\Application as Work; | ||
use Hectorqin\ThinkWechat\CacheBridge; | ||
|
||
class AppInit | ||
{ | ||
protected $apps = [ | ||
'official_account' => OfficialAccount::class, | ||
'work' => Work::class, | ||
'mini_program' => MiniProgram::class, | ||
'payment' => Payment::class, | ||
'open_platform' => OpenPlatform::class, | ||
'open_work' => OpenWork::class, | ||
'micro_merchant' => MicroMerchant::class, | ||
]; | ||
|
||
/* | ||
* usage: | ||
* app( $module_name) | ||
* or | ||
* app( $module_name, [ app_id => 'test' ]) | ||
*/ | ||
public function run() | ||
{ | ||
$wechat_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) { | ||
bind('wechat.' . $name . '.' . $config_name, function ($config = []) use ($app, $module_default, $wechat_default) { | ||
//合并配置文件 | ||
$account_config = array_merge($module_default, $wechat_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'])) { | ||
bind('wechat.' . $name, 'wechat.' . $name . '.default'); | ||
} | ||
} | ||
|
||
} | ||
} |
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,59 @@ | ||
<?php | ||
/** | ||
* ThinkPHP微信缓存类 | ||
* | ||
* @author hectorqin<[email protected]> | ||
* @copyright hectorqin | ||
*/ | ||
|
||
namespace Hectorqin\ThinkWechat; | ||
|
||
use Psr\SimpleCache\CacheInterface; | ||
|
||
class CacheBridge implements CacheInterface | ||
{ | ||
protected $cache = null; | ||
|
||
public function __construct() | ||
{ | ||
$this->cache = app('cache'); | ||
} | ||
|
||
public function get($key, $default = null) | ||
{ | ||
return $this->cache->get($key, $default); | ||
} | ||
|
||
public function set($key, $value, $ttl = null) | ||
{ | ||
return $this->cache->set($key, $value, $ttl); | ||
} | ||
|
||
public function delete($key) | ||
{ | ||
return $this->cache->rm($key); | ||
} | ||
|
||
public function clear() | ||
{ | ||
return $this->cache->clear(); | ||
} | ||
|
||
public function getMultiple($keys, $default = null) | ||
{ | ||
} | ||
|
||
public function setMultiple($values, $ttl = null) | ||
{ | ||
} | ||
|
||
public function deleteMultiple($keys) | ||
{ | ||
} | ||
|
||
public function has($key) | ||
{ | ||
return !is_null($this->cache->get($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,99 @@ | ||
<?php | ||
/** | ||
* @author hectorqin<[email protected]> | ||
* @copyright hectorqin | ||
*/ | ||
|
||
namespace Hectorqin\ThinkWechat; | ||
|
||
use think\Facade as ThinkFacade; | ||
|
||
/** | ||
* Class Facade. | ||
* | ||
* @author overtrue <[email protected]> | ||
*/ | ||
class Facade extends ThinkFacade | ||
{ | ||
/** | ||
* 默认为 Server. | ||
* | ||
* @return string | ||
*/ | ||
public static function getFacadeAccessor() | ||
{ | ||
return 'wechat.official_account'; | ||
} | ||
|
||
/** | ||
* @return \EasyWeChat\OfficialAccount\Application | ||
*/ | ||
public static function officialAccount($name = '',$config = []) | ||
{ | ||
$app = $name ? app('wechat.official_account.' . $name, ["config"=>$config]) : app('wechat.official_account', ["config"=>$config]); | ||
injectThinkLoggerToWechatApp($app); | ||
return $app; | ||
} | ||
|
||
/** | ||
* @return \EasyWeChat\Work\Application | ||
*/ | ||
public static function work($name = '',$config = []) | ||
{ | ||
$app = $name ? app('wechat.work.' . $name, ["config"=>$config]) : app('wechat.work', ["config"=>$config]); | ||
injectThinkLoggerToWechatApp($app); | ||
return $app; | ||
} | ||
|
||
/** | ||
* @return \EasyWeChat\Payment\Application | ||
*/ | ||
public static function payment($name = '',$config = []) | ||
{ | ||
$app = $name ? app('wechat.payment.' . $name, ["config"=>$config]) : app('wechat.payment', ["config"=>$config]); | ||
injectThinkLoggerToWechatApp($app); | ||
return $app; | ||
} | ||
|
||
/** | ||
* @return \EasyWeChat\MiniProgram\Application | ||
*/ | ||
public static function miniProgram($name = '',$config = []) | ||
{ | ||
$app = $name ? app('wechat.mini_program.' . $name, ["config"=>$config]) : app('wechat.mini_program', ["config"=>$config]); | ||
injectThinkLoggerToWechatApp($app); | ||
return $app; | ||
} | ||
|
||
/** | ||
* @return \EasyWeChat\OpenPlatform\Application | ||
*/ | ||
public static function openPlatform($name = '',$config = []) | ||
{ | ||
$app = $name ? app('wechat.open_platform.' . $name, ["config"=>$config]) : app('wechat.open_platform', ["config"=>$config]); | ||
injectThinkLoggerToWechatApp($app); | ||
return $app; | ||
} | ||
|
||
|
||
/** | ||
* @return \EasyWeChat\OpenWork\Application | ||
*/ | ||
public static function openWork($name = '',$config = []) | ||
{ | ||
$app = $name ? app('wechat.open_work.' . $name, ["config"=>$config]) : app('wechat.open_work', ["config"=>$config]); | ||
injectThinkLoggerToWechatApp($app); | ||
return $app; | ||
} | ||
|
||
/** | ||
* @return \EasyWeChat\MicroMerchant\Application | ||
*/ | ||
public static function microMerchant($name = '',$config = []) | ||
{ | ||
$app = $name ? app('wechat.micro_merchant.' . $name, ["config"=>$config]) : app('wechat.micro_merchant', ["config"=>$config]); | ||
injectThinkLoggerToWechatApp($app); | ||
return $app; | ||
} | ||
|
||
} |
Oops, something went wrong.