Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
hector committed Nov 6, 2019
0 parents commit d29c937
Show file tree
Hide file tree
Showing 10 changed files with 605 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.idea
.vscode
/vendor/
composer.lock
22 changes: 22 additions & 0 deletions LICENSE
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.
85 changes: 85 additions & 0 deletions README.md
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)
35 changes: 35 additions & 0 deletions composer.json
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"
}
}
}
}
62 changes: 62 additions & 0 deletions src/Behavior/AppInit.php
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');
}
}

}
}
59 changes: 59 additions & 0 deletions src/CacheBridge.php
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));
}

}
99 changes: 99 additions & 0 deletions src/Facade.php
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;
}

}
Loading

0 comments on commit d29c937

Please sign in to comment.