提交代码
This commit is contained in:
4
vendor/overtrue/laravel-wechat/.gitignore
vendored
Normal file
4
vendor/overtrue/laravel-wechat/.gitignore
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
/vendor/
|
||||
composer.lock
|
||||
.php_cs.cache
|
||||
.idea
|
||||
28
vendor/overtrue/laravel-wechat/.php_cs
vendored
Normal file
28
vendor/overtrue/laravel-wechat/.php_cs
vendored
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
$header = <<<EOF
|
||||
This file is part of the overtrue/laravel-wechat.
|
||||
|
||||
(c) overtrue <i@overtrue.me>
|
||||
|
||||
This source file is subject to the MIT license that is bundled
|
||||
with this source code in the file LICENSE.
|
||||
EOF;
|
||||
|
||||
return PhpCsFixer\Config::create()
|
||||
->setRiskyAllowed(true)
|
||||
->setRules(array(
|
||||
'@Symfony' => true,
|
||||
'header_comment' => array('header' => $header),
|
||||
'array_syntax' => array('syntax' => 'short'),
|
||||
'ordered_imports' => true,
|
||||
'no_useless_else' => true,
|
||||
'no_useless_return' => true,
|
||||
'php_unit_construct' => true,
|
||||
'php_unit_strict' => true,
|
||||
))
|
||||
->setFinder(
|
||||
PhpCsFixer\Finder::create()
|
||||
->exclude('vendor')
|
||||
->in(__DIR__)
|
||||
)
|
||||
;
|
||||
22
vendor/overtrue/laravel-wechat/LICENSE
vendored
Normal file
22
vendor/overtrue/laravel-wechat/LICENSE
vendored
Normal file
@@ -0,0 +1,22 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2015 安正超
|
||||
|
||||
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.
|
||||
|
||||
260
vendor/overtrue/laravel-wechat/README.md
vendored
Normal file
260
vendor/overtrue/laravel-wechat/README.md
vendored
Normal file
@@ -0,0 +1,260 @@
|
||||
# laravel-wechat
|
||||
|
||||
微信 SDK for Laravel 5 / Lumen, 基于 [overtrue/wechat](https://github.com/overtrue/wechat)
|
||||
|
||||
> 交流QQ群:319502940
|
||||
|
||||
|
||||
## 框架要求
|
||||
|
||||
Laravel/Lumen >= 5.1
|
||||
|
||||
## 安装
|
||||
|
||||
```shell
|
||||
# Laravel < 5.8
|
||||
composer require "overtrue/laravel-wechat:~4.0"
|
||||
|
||||
# Laravel >= 5.8
|
||||
composer require "overtrue/laravel-wechat:~5.0"
|
||||
```
|
||||
|
||||
## 配置
|
||||
|
||||
### Laravel 应用
|
||||
|
||||
1. 在 `config/app.php` 注册 ServiceProvider 和 Facade (Laravel 5.5 + 无需手动注册)
|
||||
|
||||
```php
|
||||
'providers' => [
|
||||
// ...
|
||||
Overtrue\LaravelWeChat\ServiceProvider::class,
|
||||
],
|
||||
'aliases' => [
|
||||
// ...
|
||||
'EasyWeChat' => Overtrue\LaravelWeChat\Facade::class,
|
||||
],
|
||||
```
|
||||
|
||||
2. 创建配置文件:
|
||||
|
||||
```shell
|
||||
php artisan vendor:publish --provider="Overtrue\LaravelWeChat\ServiceProvider"
|
||||
```
|
||||
|
||||
3. 修改应用根目录下的 `config/wechat.php` 中对应的参数即可。
|
||||
|
||||
4. 每个模块基本都支持多账号,默认为 `default`。
|
||||
|
||||
### Lumen 应用
|
||||
|
||||
1. 在 `bootstrap/app.php` 中 82 行左右:
|
||||
|
||||
```php
|
||||
$app->register(Overtrue\LaravelWeChat\ServiceProvider::class);
|
||||
```
|
||||
|
||||
2. 如果你习惯使用 `config/wechat.php` 来配置的话,将 `vendor/overtrue/laravel-wechat/src/config.php` 拷贝到`项目根目录/config`目录下,并将文件名改成`wechat.php`。
|
||||
|
||||
## 使用
|
||||
|
||||
:rotating_light: 在中间件 `App\Http\Middleware\VerifyCsrfToken` 排除微信相关的路由,如:
|
||||
|
||||
```php
|
||||
protected $except = [
|
||||
// ...
|
||||
'wechat',
|
||||
];
|
||||
```
|
||||
|
||||
下面以接收普通消息为例写一个例子:
|
||||
|
||||
> 假设您的域名为 `overtrue.me` 那么请登录微信公众平台 “开发者中心” 修改 “URL(服务器配置)” 为: `http://overtrue.me/wechat`。
|
||||
|
||||
路由:
|
||||
|
||||
```php
|
||||
Route::any('/wechat', 'WeChatController@serve');
|
||||
```
|
||||
|
||||
> 注意:一定是 `Route::any`, 因为微信服务端认证的时候是 `GET`, 接收用户消息时是 `POST` !
|
||||
|
||||
然后创建控制器 `WeChatController`:
|
||||
|
||||
```php
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Log;
|
||||
|
||||
class WeChatController extends Controller
|
||||
{
|
||||
|
||||
/**
|
||||
* 处理微信的请求消息
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function serve()
|
||||
{
|
||||
Log::info('request arrived.'); # 注意:Log 为 Laravel 组件,所以它记的日志去 Laravel 日志看,而不是 EasyWeChat 日志
|
||||
|
||||
$app = app('wechat.official_account');
|
||||
$app->server->push(function($message){
|
||||
return "欢迎关注 overtrue!";
|
||||
});
|
||||
|
||||
return $app->server->serve();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
> 上面例子里的 Log 是 Laravel 组件,所以它的日志不会写到 EasyWeChat 里的,建议把 wechat 的日志配置到 Laravel 同一个日志文件,便于调试。
|
||||
|
||||
### 我们有以下方式获取 SDK 的服务实例
|
||||
|
||||
##### 使用外观
|
||||
|
||||
```php
|
||||
$officialAccount = \EasyWeChat::officialAccount(); // 公众号
|
||||
$work = \EasyWeChat::work(); // 企业微信
|
||||
$payment = \EasyWeChat::payment(); // 微信支付
|
||||
$openPlatform = \EasyWeChat::openPlatform(); // 开放平台
|
||||
$miniProgram = \EasyWeChat::miniProgram(); // 小程序
|
||||
|
||||
// 均支持传入配置账号名称
|
||||
\EasyWeChat::officialAccount('foo'); // `foo` 为配置文件中的名称,默认为 `default`
|
||||
//...
|
||||
```
|
||||
|
||||
|
||||
## OAuth 中间件
|
||||
|
||||
使用中间件的情况下 `app/config/wechat.php` 中的 `oauth.callback` 就随便填写吧(因为用不着了 :smile:)。
|
||||
|
||||
1. 在 `app/Http/Kernel.php` 中添加路由中间件:
|
||||
|
||||
```php
|
||||
protected $routeMiddleware = [
|
||||
// ...
|
||||
'wechat.oauth' => \Overtrue\LaravelWeChat\Middleware\OAuthAuthenticate::class,
|
||||
];
|
||||
```
|
||||
|
||||
2. 在路由中添加中间件:
|
||||
|
||||
```php
|
||||
//...
|
||||
Route::group(['middleware' => ['web', 'wechat.oauth']], function () {
|
||||
Route::get('/user', function () {
|
||||
$user = session('wechat.oauth_user.default'); // 拿到授权用户资料
|
||||
|
||||
dd($user);
|
||||
});
|
||||
});
|
||||
```
|
||||
|
||||
中间件支持指定配置名称:`'wechat.oauth:default'`,当然,你也可以在中间件参数指定当前的 `scopes`:
|
||||
|
||||
```php
|
||||
Route::group(['middleware' => ['wechat.oauth:snsapi_userinfo']], function () {
|
||||
// ...
|
||||
});
|
||||
|
||||
// 或者指定账户的同时指定 scopes:
|
||||
Route::group(['middleware' => ['wechat.oauth:default,snsapi_userinfo']], function () {
|
||||
// ...
|
||||
});
|
||||
```
|
||||
|
||||
上面的路由定义了 `/user` 是需要微信授权的,那么在这条路由的**回调 或 控制器对应的方法里**, 你就可以从 `session('wechat.oauth_user.default')` 拿到已经授权的用户信息了。
|
||||
|
||||
## 模拟授权
|
||||
|
||||
有时候我们希望在本地开发完成后线上才真实的走微信授权流程,这将减少我们的开发成本,那么你需要做以下两步:
|
||||
|
||||
1. 准备假资料:
|
||||
|
||||
> 以下字段在 scope 为 `snsapi_userinfo` 时尽可能配置齐全哦,当然,如果你的模式只是 `snsapi_base` 的话只需要 `openid` 就好了。
|
||||
|
||||
```php
|
||||
use Illuminate\Support\Arr;
|
||||
use Overtrue\Socialite\User as SocialiteUser;
|
||||
|
||||
$user = new SocialiteUser([
|
||||
'id' => Arr::get($user, 'openid'),
|
||||
'name' => Arr::get($user, 'nickname'),
|
||||
'nickname' => Arr::get($user, 'nickname'),
|
||||
'avatar' => Arr::get($user, 'headimgurl'),
|
||||
'email' => null,
|
||||
'original' => [],
|
||||
'provider' => 'WeChat',
|
||||
]);
|
||||
|
||||
```
|
||||
|
||||
2. 将资料写入 session:
|
||||
|
||||
> 注意:一定要在 OAuth 中间件之前写入,比如你可以创建一个全局中间件来完成这件事儿,当然了,只在开发环境启用即可。
|
||||
|
||||
```php
|
||||
session(['wechat.oauth_user.default' => $user]); // 同理,`default` 可以更换为您对应的其它配置名
|
||||
```
|
||||
|
||||
## 事件
|
||||
|
||||
> 你可以监听相应的事件,并对事件发生后执行相应的操作。
|
||||
|
||||
- OAuth 网页授权:`Overtrue\LaravelWeChat\Events\WeChatUserAuthorized`
|
||||
|
||||
```php
|
||||
// 该事件有以下属性
|
||||
$event->user; // 同 session('wechat.oauth_user.default') 一样
|
||||
$event->isNewSession; // 是不是新的会话(第一次创建 session 时为 true)
|
||||
$event->account; // 当前中间件所使用的账号,对应在配置文件中的配置项名称
|
||||
```
|
||||
|
||||
|
||||
## 开放平台路由支持
|
||||
|
||||
在配置文件 `route` 处取消注释即可启用。
|
||||
|
||||
```php
|
||||
'open_platform' => [
|
||||
'uri' => 'serve',
|
||||
'action' => Overtrue\LaravelWeChat\Controllers\OpenPlatformController::class,
|
||||
'attributes' => [
|
||||
'prefix' => 'open-platform',
|
||||
'middleware' => null,
|
||||
],
|
||||
],
|
||||
```
|
||||
|
||||
Tips: 默认的控制器会根据微信开放平台的推送内容触发如下事件,你可以监听相应的事件并进行处理:
|
||||
|
||||
- 授权方成功授权:`Overtrue\LaravelWeChat\Events\OpenPlatform\Authorized`
|
||||
- 授权方更新授权:`Overtrue\LaravelWeChat\Events\OpenPlatform\UpdateAuthorized`
|
||||
- 授权方取消授权:`Overtrue\LaravelWeChat\Events\OpenPlatform\Unauthorized`
|
||||
- 开放平台推送 VerifyTicket:`Overtrue\LaravelWeChat\Events\OpenPlatform\VerifyTicketRefreshed`
|
||||
|
||||
```php
|
||||
// 事件有如下属性
|
||||
$message = $event->payload; // 开放平台事件通知内容
|
||||
```
|
||||
|
||||
配置后 `http://example.com/open-platform/serve` 则为开放平台第三方应用设置的授权事件接收 URL。
|
||||
|
||||
|
||||
|
||||
更多 SDK 的具体使用请参考:https://easywechat.com
|
||||
|
||||
## PHP 扩展包开发
|
||||
|
||||
> 想知道如何从零开始构建 PHP 扩展包?
|
||||
>
|
||||
> 请关注我的实战课程,我会在此课程中分享一些扩展开发经验 —— [《PHP 扩展包实战教程 - 从入门到发布》](https://learnku.com/courses/creating-package)
|
||||
|
||||
## License
|
||||
|
||||
MIT
|
||||
31
vendor/overtrue/laravel-wechat/composer.json
vendored
Normal file
31
vendor/overtrue/laravel-wechat/composer.json
vendored
Normal file
@@ -0,0 +1,31 @@
|
||||
{
|
||||
"name": "overtrue/laravel-wechat",
|
||||
"description": "微信 SDK for Laravel",
|
||||
"keywords": ["wechat", "weixin","laravel", "sdk"],
|
||||
"license": "MIT",
|
||||
"authors": [
|
||||
{
|
||||
"name": "overtrue",
|
||||
"email": "anzhengchao@gmail.com"
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"illuminate/container": "^5.1||~6.0",
|
||||
"overtrue/wechat": "^4.0"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Overtrue\\LaravelWeChat\\": "src/"
|
||||
}
|
||||
},
|
||||
"extra": {
|
||||
"laravel": {
|
||||
"providers": [
|
||||
"Overtrue\\LaravelWeChat\\ServiceProvider"
|
||||
],
|
||||
"aliases": {
|
||||
"EasyWeChat": "Overtrue\\LaravelWeChat\\Facade"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
24
vendor/overtrue/laravel-wechat/src/Controllers/Controller.php
vendored
Normal file
24
vendor/overtrue/laravel-wechat/src/Controllers/Controller.php
vendored
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the overtrue/laravel-wechat.
|
||||
*
|
||||
* (c) overtrue <i@overtrue.me>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace Overtrue\LaravelWeChat\Controllers;
|
||||
|
||||
use Barryvdh\Debugbar\LaravelDebugbar;
|
||||
|
||||
class Controller
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
if (class_exists(LaravelDebugbar::class)) {
|
||||
resolve(LaravelDebugbar::class)->disable();
|
||||
}
|
||||
}
|
||||
}
|
||||
46
vendor/overtrue/laravel-wechat/src/Controllers/OpenPlatformController.php
vendored
Normal file
46
vendor/overtrue/laravel-wechat/src/Controllers/OpenPlatformController.php
vendored
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the overtrue/laravel-wechat.
|
||||
*
|
||||
* (c) overtrue <i@overtrue.me>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace Overtrue\LaravelWeChat\Controllers;
|
||||
|
||||
use EasyWeChat\OpenPlatform\Application;
|
||||
use EasyWeChat\OpenPlatform\Server\Guard;
|
||||
use Overtrue\LaravelWeChat\Events\OpenPlatform as Events;
|
||||
|
||||
class OpenPlatformController extends Controller
|
||||
{
|
||||
/**
|
||||
* Register for open platform.
|
||||
*
|
||||
* @param \EasyWeChat\OpenPlatform\Application $application
|
||||
*
|
||||
* @return \Symfony\Component\HttpFoundation\Response
|
||||
*/
|
||||
public function __invoke(Application $application)
|
||||
{
|
||||
$server = $application->server;
|
||||
|
||||
$server->on(Guard::EVENT_AUTHORIZED, function ($payload) {
|
||||
event(new Events\Authorized($payload));
|
||||
});
|
||||
$server->on(Guard::EVENT_UNAUTHORIZED, function ($payload) {
|
||||
event(new Events\Unauthorized($payload));
|
||||
});
|
||||
$server->on(Guard::EVENT_UPDATE_AUTHORIZED, function ($payload) {
|
||||
event(new Events\UpdateAuthorized($payload));
|
||||
});
|
||||
$server->on(Guard::EVENT_COMPONENT_VERIFY_TICKET, function ($payload) {
|
||||
event(new Events\VerifyTicketRefreshed($payload));
|
||||
});
|
||||
|
||||
return $server->serve();
|
||||
}
|
||||
}
|
||||
25
vendor/overtrue/laravel-wechat/src/Events/OpenPlatform/Authorized.php
vendored
Normal file
25
vendor/overtrue/laravel-wechat/src/Events/OpenPlatform/Authorized.php
vendored
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the overtrue/laravel-wechat.
|
||||
*
|
||||
* (c) overtrue <i@overtrue.me>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace Overtrue\LaravelWeChat\Events\OpenPlatform;
|
||||
|
||||
/**
|
||||
* @method string getAppId()
|
||||
* @method string getCreateTime()
|
||||
* @method string getInfoType()
|
||||
* @method string getAuthorizerAppid()
|
||||
* @method string getAuthorizationCode()
|
||||
* @method string getAuthorizationCodeExpiredTime()
|
||||
* @method string getPreAuthCode()
|
||||
*/
|
||||
class Authorized extends OpenPlatformEvent
|
||||
{
|
||||
}
|
||||
35
vendor/overtrue/laravel-wechat/src/Events/OpenPlatform/OpenPlatformEvent.php
vendored
Normal file
35
vendor/overtrue/laravel-wechat/src/Events/OpenPlatform/OpenPlatformEvent.php
vendored
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the overtrue/laravel-wechat.
|
||||
*
|
||||
* (c) overtrue <i@overtrue.me>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace Overtrue\LaravelWeChat\Events\OpenPlatform;
|
||||
|
||||
abstract class OpenPlatformEvent
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
public $payload;
|
||||
|
||||
/**
|
||||
* Create a new event instance.
|
||||
*
|
||||
* @param mixed $payload
|
||||
*/
|
||||
public function __construct($payload)
|
||||
{
|
||||
$this->payload = $payload;
|
||||
}
|
||||
|
||||
public function __call($name, $args)
|
||||
{
|
||||
return $this->payload[substr($name, 3)] ?? null;
|
||||
}
|
||||
}
|
||||
22
vendor/overtrue/laravel-wechat/src/Events/OpenPlatform/Unauthorized.php
vendored
Normal file
22
vendor/overtrue/laravel-wechat/src/Events/OpenPlatform/Unauthorized.php
vendored
Normal file
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the overtrue/laravel-wechat.
|
||||
*
|
||||
* (c) overtrue <i@overtrue.me>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace Overtrue\LaravelWeChat\Events\OpenPlatform;
|
||||
|
||||
/**
|
||||
* @method string getAppId()
|
||||
* @method string getCreateTime()
|
||||
* @method string getInfoType()
|
||||
* @method string getAuthorizerAppid()
|
||||
*/
|
||||
class Unauthorized extends OpenPlatformEvent
|
||||
{
|
||||
}
|
||||
25
vendor/overtrue/laravel-wechat/src/Events/OpenPlatform/UpdateAuthorized.php
vendored
Normal file
25
vendor/overtrue/laravel-wechat/src/Events/OpenPlatform/UpdateAuthorized.php
vendored
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the overtrue/laravel-wechat.
|
||||
*
|
||||
* (c) overtrue <i@overtrue.me>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace Overtrue\LaravelWeChat\Events\OpenPlatform;
|
||||
|
||||
/**
|
||||
* @method string getAppId()
|
||||
* @method string getCreateTime()
|
||||
* @method string getInfoType()
|
||||
* @method string getAuthorizerAppid()
|
||||
* @method string getAuthorizationCode()
|
||||
* @method string getAuthorizationCodeExpiredTime()
|
||||
* @method string getPreAuthCode()
|
||||
*/
|
||||
class UpdateAuthorized extends OpenPlatformEvent
|
||||
{
|
||||
}
|
||||
22
vendor/overtrue/laravel-wechat/src/Events/OpenPlatform/VerifyTicketRefreshed.php
vendored
Normal file
22
vendor/overtrue/laravel-wechat/src/Events/OpenPlatform/VerifyTicketRefreshed.php
vendored
Normal file
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the overtrue/laravel-wechat.
|
||||
*
|
||||
* (c) overtrue <i@overtrue.me>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace Overtrue\LaravelWeChat\Events\OpenPlatform;
|
||||
|
||||
/**
|
||||
* @method string getAppId()
|
||||
* @method string getCreateTime()
|
||||
* @method string getInfoType()
|
||||
* @method string getComponentVerifyTicket()
|
||||
*/
|
||||
class VerifyTicketRefreshed extends OpenPlatformEvent
|
||||
{
|
||||
}
|
||||
79
vendor/overtrue/laravel-wechat/src/Events/WeChatUserAuthorized.php
vendored
Normal file
79
vendor/overtrue/laravel-wechat/src/Events/WeChatUserAuthorized.php
vendored
Normal file
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the overtrue/laravel-wechat.
|
||||
*
|
||||
* (c) overtrue <i@overtrue.me>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace Overtrue\LaravelWeChat\Events;
|
||||
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Overtrue\Socialite\User;
|
||||
|
||||
class WeChatUserAuthorized
|
||||
{
|
||||
use SerializesModels;
|
||||
|
||||
public $user;
|
||||
|
||||
public $isNewSession;
|
||||
|
||||
public $account;
|
||||
|
||||
/**
|
||||
* Create a new event instance.
|
||||
*
|
||||
* @param \Overtrue\Socialite\User $user
|
||||
* @param bool $isNewSession
|
||||
*/
|
||||
public function __construct(User $user, $isNewSession = false, string $account)
|
||||
{
|
||||
$this->user = $user;
|
||||
$this->isNewSession = $isNewSession;
|
||||
$this->account = $account;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the authorized user.
|
||||
*
|
||||
* @return \Overtrue\Socialite\User
|
||||
*/
|
||||
public function getUser()
|
||||
{
|
||||
return $this->user;
|
||||
}
|
||||
|
||||
/**
|
||||
* The name of official account.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getAccount()
|
||||
{
|
||||
return $this->account;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check the user session is first created.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isNewSession()
|
||||
{
|
||||
return $this->isNewSession;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the channels the event should be broadcast on.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function broadcastOn()
|
||||
{
|
||||
return [];
|
||||
}
|
||||
}
|
||||
72
vendor/overtrue/laravel-wechat/src/Facade.php
vendored
Normal file
72
vendor/overtrue/laravel-wechat/src/Facade.php
vendored
Normal file
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the overtrue/laravel-wechat.
|
||||
*
|
||||
* (c) overtrue <i@overtrue.me>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace Overtrue\LaravelWeChat;
|
||||
|
||||
use Illuminate\Support\Facades\Facade as LaravelFacade;
|
||||
|
||||
/**
|
||||
* Class Facade.
|
||||
*
|
||||
* @author overtrue <i@overtrue.me>
|
||||
*/
|
||||
class Facade extends LaravelFacade
|
||||
{
|
||||
/**
|
||||
* 默认为 Server.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function getFacadeAccessor()
|
||||
{
|
||||
return 'wechat.official_account';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \EasyWeChat\OfficialAccount\Application
|
||||
*/
|
||||
public static function officialAccount($name = '')
|
||||
{
|
||||
return $name ? app('wechat.official_account.'.$name) : app('wechat.official_account');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \EasyWeChat\Work\Application
|
||||
*/
|
||||
public static function work($name = '')
|
||||
{
|
||||
return $name ? app('wechat.work.'.$name) : app('wechat.work');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \EasyWeChat\Payment\Application
|
||||
*/
|
||||
public static function payment($name = '')
|
||||
{
|
||||
return $name ? app('wechat.payment.'.$name) : app('wechat.payment');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \EasyWeChat\MiniProgram\Application
|
||||
*/
|
||||
public static function miniProgram($name = '')
|
||||
{
|
||||
return $name ? app('wechat.mini_program.'.$name) : app('wechat.mini_program');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \EasyWeChat\OpenPlatform\Application
|
||||
*/
|
||||
public static function openPlatform($name = '')
|
||||
{
|
||||
return $name ? app('wechat.open_platform.'.$name) : app('wechat.open_platform');
|
||||
}
|
||||
}
|
||||
84
vendor/overtrue/laravel-wechat/src/Middleware/OAuthAuthenticate.php
vendored
Normal file
84
vendor/overtrue/laravel-wechat/src/Middleware/OAuthAuthenticate.php
vendored
Normal file
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the overtrue/laravel-wechat.
|
||||
*
|
||||
* (c) overtrue <i@overtrue.me>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace Overtrue\LaravelWeChat\Middleware;
|
||||
|
||||
use Closure;
|
||||
use http\Env\Request;
|
||||
use Illuminate\Support\Arr;
|
||||
use Overtrue\LaravelWeChat\Events\WeChatUserAuthorized;
|
||||
|
||||
/**
|
||||
* Class OAuthAuthenticate: 微信公众号, 企业微信的网页应用。
|
||||
*/
|
||||
class OAuthAuthenticate
|
||||
{
|
||||
/**
|
||||
* Handle an incoming request.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param \Closure $next
|
||||
* @param string|null $scope
|
||||
* @param string|null $type : service(服务号), subscription(订阅号), work(企业微信)
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function handle($request, Closure $next, $account = 'default', $scope = null, $type = 'service')
|
||||
{
|
||||
$isNewSession = false;
|
||||
//保证兼容性
|
||||
$class = ('work' !== $type) ? 'wechat' : 'work';
|
||||
$prefix = ('work' !== $type) ? 'official_account' : 'work';
|
||||
$sessionKey = \sprintf($class.'.oauth_user.%s', $account);
|
||||
$config = config(\sprintf('wechat.'.$prefix.'.%s', $account), []);
|
||||
$officialAccount = app(\sprintf('wechat.'.$prefix.'.%s', $account));
|
||||
$scope = $scope ?: Arr::get($config, 'oauth.scopes', ['snsapi_base']);
|
||||
|
||||
if (is_string($scope)) {
|
||||
$scope = array_map('trim', explode(',', $scope));
|
||||
}
|
||||
|
||||
$session = session($sessionKey, []);
|
||||
|
||||
if (!$session) {
|
||||
if ($request->has('code')) {
|
||||
session([$sessionKey => $officialAccount->oauth->user() ?? []]);
|
||||
$isNewSession = true;
|
||||
|
||||
event(new WeChatUserAuthorized(session($sessionKey), $isNewSession, $account));
|
||||
|
||||
return redirect()->to($this->getTargetUrl($request));
|
||||
}
|
||||
|
||||
session()->forget($sessionKey);
|
||||
|
||||
return $officialAccount->oauth->scopes($scope)->redirect($request->fullUrl());
|
||||
}
|
||||
|
||||
event(new WeChatUserAuthorized(session($sessionKey), $isNewSession, $account));
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the target business url.
|
||||
*
|
||||
* @param Request $request
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function getTargetUrl($request)
|
||||
{
|
||||
$queries = Arr::except($request->query(), ['code', 'state']);
|
||||
|
||||
return $request->url().(empty($queries) ? '' : '?'.http_build_query($queries));
|
||||
}
|
||||
}
|
||||
117
vendor/overtrue/laravel-wechat/src/ServiceProvider.php
vendored
Normal file
117
vendor/overtrue/laravel-wechat/src/ServiceProvider.php
vendored
Normal file
@@ -0,0 +1,117 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the overtrue/laravel-wechat.
|
||||
*
|
||||
* (c) overtrue <i@overtrue.me>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace Overtrue\LaravelWeChat;
|
||||
|
||||
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 Illuminate\Foundation\Application as LaravelApplication;
|
||||
use Illuminate\Support\ServiceProvider as LaravelServiceProvider;
|
||||
use Laravel\Lumen\Application as LumenApplication;
|
||||
|
||||
/**
|
||||
* Class ServiceProvider.
|
||||
*
|
||||
* @author overtrue <i@overtrue.me>
|
||||
*/
|
||||
class ServiceProvider extends LaravelServiceProvider
|
||||
{
|
||||
/**
|
||||
* Boot the provider.
|
||||
*/
|
||||
public function boot()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Setup the config.
|
||||
*/
|
||||
protected function setupConfig()
|
||||
{
|
||||
$source = realpath(__DIR__.'/config.php');
|
||||
|
||||
if ($this->app instanceof LaravelApplication && $this->app->runningInConsole()) {
|
||||
$this->publishes([$source => config_path('wechat.php')], 'laravel-wechat');
|
||||
} elseif ($this->app instanceof LumenApplication) {
|
||||
$this->app->configure('wechat');
|
||||
}
|
||||
|
||||
$this->mergeConfigFrom($source, 'wechat');
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the provider.
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
$this->setupConfig();
|
||||
|
||||
$apps = [
|
||||
'official_account' => OfficialAccount::class,
|
||||
'work' => Work::class,
|
||||
'mini_program' => MiniProgram::class,
|
||||
'payment' => Payment::class,
|
||||
'open_platform' => OpenPlatform::class,
|
||||
'open_work' => OpenWork::class,
|
||||
];
|
||||
|
||||
foreach ($apps as $name => $class) {
|
||||
if (empty(config('wechat.'.$name))) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($config = config('wechat.route.'.$name)) {
|
||||
$this->getRouter()->group($config['attributes'], function ($router) use ($config) {
|
||||
$router->post($config['uri'], $config['action']);
|
||||
});
|
||||
}
|
||||
|
||||
if (!empty(config('wechat.'.$name.'.app_id')) || !empty(config('wechat.'.$name.'.corp_id'))) {
|
||||
$accounts = [
|
||||
'default' => config('wechat.'.$name),
|
||||
];
|
||||
config(['wechat.'.$name.'.default' => $accounts['default']]);
|
||||
} else {
|
||||
$accounts = config('wechat.'.$name);
|
||||
}
|
||||
|
||||
foreach ($accounts as $account => $config) {
|
||||
$this->app->singleton("wechat.{$name}.{$account}", function ($laravelApp) use ($name, $account, $config, $class) {
|
||||
$app = new $class(array_merge(config('wechat.defaults', []), $config));
|
||||
if (config('wechat.defaults.use_laravel_cache')) {
|
||||
$app['cache'] = $laravelApp['cache.store'];
|
||||
}
|
||||
$app['request'] = $laravelApp['request'];
|
||||
|
||||
return $app;
|
||||
});
|
||||
}
|
||||
$this->app->alias("wechat.{$name}.default", 'wechat.'.$name);
|
||||
$this->app->alias("wechat.{$name}.default", 'easywechat.'.$name);
|
||||
|
||||
$this->app->alias('wechat.'.$name, $class);
|
||||
$this->app->alias('easywechat.'.$name, $class);
|
||||
}
|
||||
}
|
||||
|
||||
protected function getRouter()
|
||||
{
|
||||
if ($this->app instanceof LumenApplication && !class_exists('Laravel\Lumen\Routing\Router')) {
|
||||
return $this->app;
|
||||
}
|
||||
|
||||
return $this->app->router;
|
||||
}
|
||||
}
|
||||
131
vendor/overtrue/laravel-wechat/src/config.php
vendored
Normal file
131
vendor/overtrue/laravel-wechat/src/config.php
vendored
Normal file
@@ -0,0 +1,131 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the overtrue/laravel-wechat.
|
||||
*
|
||||
* (c) overtrue <i@overtrue.me>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
return [
|
||||
/*
|
||||
* 默认配置,将会合并到各模块中
|
||||
*/
|
||||
'defaults' => [
|
||||
/*
|
||||
* 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名
|
||||
*/
|
||||
'response_type' => 'array',
|
||||
|
||||
/*
|
||||
* 使用 Laravel 的缓存系统
|
||||
*/
|
||||
'use_laravel_cache' => true,
|
||||
|
||||
/*
|
||||
* 日志配置
|
||||
*
|
||||
* level: 日志级别,可选为:
|
||||
* debug/info/notice/warning/error/critical/alert/emergency
|
||||
* file:日志文件位置(绝对路径!!!),要求可写权限
|
||||
*/
|
||||
'log' => [
|
||||
'level' => env('WECHAT_LOG_LEVEL', 'debug'),
|
||||
'file' => env('WECHAT_LOG_FILE', storage_path('logs/wechat.log')),
|
||||
],
|
||||
],
|
||||
|
||||
/*
|
||||
* 路由配置
|
||||
*/
|
||||
'route' => [
|
||||
/*
|
||||
* 开放平台第三方平台路由配置
|
||||
*/
|
||||
// 'open_platform' => [
|
||||
// 'uri' => 'serve',
|
||||
// 'action' => Overtrue\LaravelWeChat\Controllers\OpenPlatformController::class,
|
||||
// 'attributes' => [
|
||||
// 'prefix' => 'open-platform',
|
||||
// 'middleware' => null,
|
||||
// ],
|
||||
// ],
|
||||
],
|
||||
|
||||
/*
|
||||
* 公众号
|
||||
*/
|
||||
'official_account' => [
|
||||
'default' => [
|
||||
'app_id' => env('WECHAT_OFFICIAL_ACCOUNT_APPID', 'your-app-id'), // AppID
|
||||
'secret' => env('WECHAT_OFFICIAL_ACCOUNT_SECRET', 'your-app-secret'), // AppSecret
|
||||
'token' => env('WECHAT_OFFICIAL_ACCOUNT_TOKEN', 'your-token'), // Token
|
||||
'aes_key' => env('WECHAT_OFFICIAL_ACCOUNT_AES_KEY', ''), // EncodingAESKey
|
||||
|
||||
/*
|
||||
* 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' => 100020,
|
||||
// 'secret' => env('WECHAT_WORK_AGENT_CONTACTS_SECRET', ''),
|
||||
// //...
|
||||
// ],
|
||||
// ],
|
||||
];
|
||||
Reference in New Issue
Block a user