Skip to content

Commit 97fe9d9

Browse files
committed
Update OpenPlatform.
1 parent 7c03bb5 commit 97fe9d9

File tree

7 files changed

+72
-197
lines changed

7 files changed

+72
-197
lines changed

src/Controllers/OpenPlatformController.php

Lines changed: 17 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,51 +9,36 @@
99
* with this source code in the file LICENSE.
1010
*/
1111

12-
namespace Overtrue\LaravelWechat\Controllers;
12+
namespace Overtrue\LaravelWeChat\Controllers;
1313

14-
use EasyWeChat\Foundation\Application;
14+
use EasyWeChat\OpenPlatform\Application;
15+
use EasyWeChat\OpenPlatform\Server\Guard;
1516
use Event;
16-
use Illuminate\Routing\Controller;
17-
use Overtrue\LaravelWechat\Events\OpenPlatform as Events;
17+
use Overtrue\LaravelWeChat\Events\OpenPlatform as Events;
1818

19-
class OpenPlatformController extends Controller
19+
class OpenPlatformController
2020
{
21-
/**
22-
* Events.
23-
*
24-
* @var array
25-
*/
26-
protected $events = [
27-
'authorized' => Events\Authorized::class,
28-
'unauthorized' => Events\Unauthorized::class,
29-
'updateauthorized' => Events\UpdateAuthorized::class,
30-
];
31-
3221
/**
3322
* Register for open platform.
3423
*
35-
* @param \EasyWeChat\Foundation\Application $application
24+
* @param \EasyWeChat\OpenPlatform\Application $application
3625
*
3726
* @return \Symfony\Component\HttpFoundation\Response
3827
*/
3928
public function index(Application $application)
4029
{
41-
$server = $application->open_platform->server;
42-
43-
$server->setMessageHandler([$this, 'handle']);
30+
$server = $application->server;
31+
32+
$server->on(Guard::EVENT_AUTHORIZED, function ($payload) {
33+
Event::fire(new Events\Authorized($payload));
34+
});
35+
$server->on(Guard::EVENT_UNAUTHORIZED, function ($payload) {
36+
Event::fire(new Events\Unauthorized($payload));
37+
});
38+
$server->on(Guard::EVENT_UPDATE_AUTHORIZED, function ($payload) {
39+
Event::fire(new Events\UpdateAuthorized($payload));
40+
});
4441

4542
return $server->serve();
4643
}
47-
48-
/**
49-
* Handle event message and fire event.
50-
*
51-
* @param \EasyWeChat\Support\Collection $message
52-
*/
53-
public function handle($message)
54-
{
55-
if ($event = array_get($this->events, $message->InfoType)) {
56-
Event::fire(new $event($message));
57-
}
58-
}
5944
}

src/Events/OpenPlatform/Authorized.php

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,19 @@
99
* with this source code in the file LICENSE.
1010
*/
1111

12-
namespace Overtrue\LaravelWechat\Events\OpenPlatform;
13-
14-
use EasyWeChat\Support\Collection;
15-
use Illuminate\Queue\SerializesModels;
12+
namespace Overtrue\LaravelWeChat\Events\OpenPlatform;
1613

1714
class Authorized
1815
{
19-
use SerializesModels;
20-
21-
public $message;
16+
public $payload;
2217

2318
/**
2419
* Create a new event instance.
2520
*
26-
* @param \EasyWeChat\Support\Collection $message
27-
*/
28-
public function __construct(Collection $message)
29-
{
30-
$this->message = $message;
31-
}
32-
33-
/**
34-
* Get the channels the event should be broadcast on.
35-
*
36-
* @return array
21+
* @param array $payload
3722
*/
38-
public function broadcastOn()
23+
public function __construct(array $payload)
3924
{
40-
return [];
25+
$this->payload = $payload;
4126
}
4227
}

src/Events/OpenPlatform/Unauthorized.php

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,19 @@
99
* with this source code in the file LICENSE.
1010
*/
1111

12-
namespace Overtrue\LaravelWechat\Events\OpenPlatform;
13-
14-
use EasyWeChat\Support\Collection;
15-
use Illuminate\Queue\SerializesModels;
12+
namespace Overtrue\LaravelWeChat\Events\OpenPlatform;
1613

1714
class Unauthorized
1815
{
19-
use SerializesModels;
20-
21-
public $message;
16+
public $payload;
2217

2318
/**
2419
* Create a new event instance.
2520
*
26-
* @param \EasyWeChat\Support\Collection $message
27-
*/
28-
public function __construct(Collection $message)
29-
{
30-
$this->message = $message;
31-
}
32-
33-
/**
34-
* Get the channels the event should be broadcast on.
35-
*
36-
* @return array
21+
* @param array $payload
3722
*/
38-
public function broadcastOn()
23+
public function __construct(array $payload)
3924
{
40-
return [];
25+
$this->payload = $payload;
4126
}
4227
}

src/Events/OpenPlatform/UpdateAuthorized.php

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,19 @@
99
* with this source code in the file LICENSE.
1010
*/
1111

12-
namespace Overtrue\LaravelWechat\Events\OpenPlatform;
13-
14-
use EasyWeChat\Support\Collection;
15-
use Illuminate\Queue\SerializesModels;
12+
namespace Overtrue\LaravelWeChat\Events\OpenPlatform;
1613

1714
class UpdateAuthorized
1815
{
19-
use SerializesModels;
20-
21-
public $message;
16+
public $payload;
2217

2318
/**
2419
* Create a new event instance.
2520
*
26-
* @param \EasyWeChat\Support\Collection $message
27-
*/
28-
public function __construct(Collection $message)
29-
{
30-
$this->message = $message;
31-
}
32-
33-
/**
34-
* Get the channels the event should be broadcast on.
35-
*
36-
* @return array
21+
* @param array $payload
3722
*/
38-
public function broadcastOn()
23+
public function __construct(array $payload)
3924
{
40-
return [];
25+
$this->payload = $payload;
4126
}
4227
}

src/Providers/RouteServiceProvider.php

Lines changed: 0 additions & 91 deletions
This file was deleted.

src/ServiceProvider.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* with this source code in the file LICENSE.
1010
*/
1111

12-
namespace Overtrue\LaravelWechat;
12+
namespace Overtrue\LaravelWeChat;
1313

1414
use EasyWeChat\MiniProgram\Application as MiniProgram;
1515
use EasyWeChat\OfficialAccount\Application as OfficialAccount;
@@ -19,7 +19,7 @@
1919
use Illuminate\Foundation\Application as LaravelApplication;
2020
use Illuminate\Support\ServiceProvider as LaravelServiceProvider;
2121
use Laravel\Lumen\Application as LumenApplication;
22-
use Overtrue\LaravelWechat\Providers\RouteServiceProvider;
22+
use Overtrue\LaravelWeChat\Controllers\OpenPlatformController;
2323
use Overtrue\Socialite\User as SocialiteUser;
2424

2525
/**
@@ -36,7 +36,9 @@ public function boot()
3636
{
3737
$this->setupConfig();
3838

39-
$this->app->register(RouteServiceProvider::class);
39+
if (config('wechat.route.enabled')) {
40+
$this->registerRoutes();
41+
}
4042
}
4143

4244
/**
@@ -94,6 +96,19 @@ public function register()
9496
}
9597
}
9698

99+
/**
100+
* Register routes.
101+
*/
102+
protected function registerRoutes()
103+
{
104+
$router = $this->app instanceof LaravelApplication ? $this->app['router'] : $this->app;
105+
106+
// Register open-platform routes...
107+
$router->group(config('wechat.route.open_platform.attributes', []), function ($router) {
108+
$router->post(config('wechat.route.open_platform.uri'), OpenPlatformController::class.'@index');
109+
});
110+
}
111+
97112
/**
98113
* 创建模拟登录.
99114
*/

0 commit comments

Comments
 (0)