Skip to content

Commit 77a91f4

Browse files
committed
t push origin masterMerge branch '3.0'
2 parents 40788f6 + d28010f commit 77a91f4

File tree

8 files changed

+189
-118
lines changed

8 files changed

+189
-118
lines changed

.gitignore

100644100755
File mode changed.

LICENSE

100644100755
File mode changed.

README.md

100644100755
Lines changed: 37 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# laravel-wechat
22

3-
> 注意:此版本为 2.x 版本,不兼容 1.x,已经移除外观,与 [overtrue/wechat 2.x](https://github.com/overtrue/wechat) 同步
4-
5-
> 1.x 的配置文件里面的项目为驼峰,2.x 系列已经改为下划线,请参考: [src/config.php](https://github.com/overtrue/laravel-wechat/blob/master/src/config.php)
3+
> 注意:此版本为 3.x 版本,不兼容 2.x 与 1.x,与 [overtrue/wechat 3.x](https://github.com/overtrue/wechat) 同步
64
75
微信 SDK for Laravel 5 / Lumen, 基于 [overtrue/wechat](https://github.com/overtrue/wechat)
86

@@ -15,7 +13,7 @@
1513
1. 安装包文件
1614

1715
```shell
18-
composer require "overtrue/laravel-wechat:2.1.*"
16+
composer require "overtrue/laravel-wechat:~3.0"
1917
```
2018

2119
## 配置
@@ -50,15 +48,29 @@
5048
$app->register(Overtrue\LaravelWechat\ServiceProvider::class);
5149
```
5250

53-
2. ENV 中配置以下选项
51+
2. ENV 中支持以下配置
5452

5553
```php
56-
WECHAT_USE_ALIAS=false
57-
WECHAT_APPID=xxx
58-
WECHAT_SECRET=xxx
59-
WECHAT_TOKEN=xxx
60-
WECHAT_ENCODING_KEY=xxx
54+
WECHAT_APPID
55+
WECHAT_SECRET
56+
WECHAT_TOKEN
57+
WECHAT_AES_KEY
58+
59+
WECHAT_LOG_LEVEL
60+
WECHAT_LOG_FILE
61+
62+
WECHAT_OAUTH_SCOPES
63+
WECHAT_OAUTH_CALLBACK
64+
65+
WECHAT_PAYMENT_MERCHANT_ID
66+
WECHAT_PAYMENT_KEY
67+
WECHAT_PAYMENT_CERT_PATH
68+
WECHAT_PAYMENT_KEY_PATH
69+
WECHAT_PAYMENT_DEVICE_INFO
70+
WECHAT_PAYMENT_SUB_APP_ID
71+
WECHAT_PAYMENT_SUB_MERCHANT_ID
6172
```
73+
6274
3. 如果你习惯使用 `config/wechat.php` 来配置的话,请记得在 `bootstrap/app.php` 中19行以后添加:
6375

6476
```php
@@ -72,26 +84,6 @@ $app->configure('wechat');
7284
> 1. Laravel 5 默认启用了 CSRF 中间件,因为微信的消息是 POST 过来,所以会触发 CSRF 检查导致无法正确响应消息,所以请去除默认的 CSRF 中间件,改成路由中间件。可以参考我的写法:[overtrue gist:Kernel.php](https://gist.github.com/overtrue/ff6cd3a4e869fbaf6c01#file-kernel-php-L31)
7385
> 5.1 里的 CSRF 已经带了可忽略部分url的功能,你可以参考:http://laravel.com/docs/master/routing#csrf-protection
7486
75-
所有的Wechat对象都已经放到了容器中,直接从容器中取就好。
76-
77-
别名对应关系如下:
78-
79-
'wechat.server' => 'Overtrue\Wechat\Server',
80-
'wechat.user' => 'Overtrue\Wechat\User',
81-
'wechat.group' => 'Overtrue\Wechat\Group',
82-
'wechat.auth' => 'Overtrue\Wechat\Auth',
83-
'wechat.menu' => 'Overtrue\Wechat\Menu',
84-
'wechat.menu.item' => 'Overtrue\Wechat\MenuItem',
85-
'wechat.js' => 'Overtrue\Wechat\Js',
86-
'wechat.staff' => 'Overtrue\Wechat\Staff',
87-
'wechat.store' => 'Overtrue\Wechat\Store',
88-
'wechat.card' => 'Overtrue\Wechat\Card',
89-
'wechat.qrcode' => 'Overtrue\Wechat\QRCode',
90-
'wechat.url' => 'Overtrue\Wechat\Url',
91-
'wechat.media' => 'Overtrue\Wechat\Media',
92-
'wechat.image' => 'Overtrue\Wechat\Image',
93-
'wechat.notice' => 'Overtrue\Wechat\Notice',
94-
'wechat.media' => 'Overtrue\Wechat\Media',
9587

9688
下面以接收普通消息为例写一个例子:
9789

@@ -110,21 +102,19 @@ Route::any('/wechat', 'WechatController@serve');
110102
```php
111103
<?php namespace App\Http\Controllers;
112104

113-
use Overtrue\Wechat\Server;
114105
use Log;
115106

116107
class WechatController extends Controller {
117108

118109
/**
119110
* 处理微信的请求消息
120111
*
121-
* @param Overtrue\Wechat\Server $server
122-
*
123112
* @return string
124113
*/
125-
public function serve(Server $server)
114+
public function serve()
126115
{
127-
$server->on('message', function($message){
116+
$wechat = app('wechat');
117+
$wechat->server->setMessageHandler(function($message){
128118
return "欢迎关注 overtrue!";
129119
});
130120

@@ -133,47 +123,42 @@ class WechatController extends Controller {
133123
}
134124
```
135125

136-
> 注意:不要忘记在头部 `use` 哦,或者你就得用 `\Overtrue\Wechat\Server` 全称咯。:smile:
137-
138126
### 我们有三种方式获取 SDK 的服务实例
139127

140128
##### 使用容器的自动注入
141129

142130
```php
143131
<?php namespace App\Http\Controllers;
144132

145-
use Overtrue\Wechat\Auth;
133+
use EasyWeChat\Foundation\Application;
146134

147135
class WechatController extends Controller {
148136

149-
public function demo(Auth $auth)
137+
public function demo(Application $wechat)
150138
{
151-
// $auth 则为容器中 Overtrue\Wechat\Auth 的实例
139+
// $wechat 则为容器中 EasyWeChat\Foundation\Application 的实例
152140
}
153141
}
154142
```
155143

156-
##### 使用别名/类名从容器获取对应实例
144+
##### 使用外观
157145

158-
上面已经列出了所有可用的别名对应关系,你可以使用别名或者类名获取对应的实例
146+
`config/app.php``alias` 部分添加外观别名
159147

160148
```php
161-
$wechatServer = App::make('wechat.server'); // 服务端
162-
$wechatUser = App::make('wechat.user'); // 用户服务
163-
或者:
164-
$wechatUser = App::make('Overtrue\Wechat\User'); // 用户服务
165-
// ... 其它同理
149+
'EasyWeChat' => Overtrue\LaravelWechat\Facade::class,
166150
```
167151

168-
#### 使用外观 `Wechat`
152+
然后就可以在任何地方使用外观方式调用 SDK 对应的服务了:
169153

170154
```php
171-
$wechatServer = Wechat::server();
172-
$wechatUser = Wechat::user();
173-
//... 其它同理
155+
$wechatServer = EasyWeChat::server(); // 服务端
156+
$wechatUser = EasyWeChat::user(); // 用户服务
157+
// ... 其它同理
174158
```
175159

176-
更多使用请参考:https://github.com/overtrue/wechat/wiki/
160+
161+
更多 SDK 的具体使用请参考:https://easywechat.org
177162

178163
## License
179164

composer.json

100644100755
Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"description": "微信 SDK for Laravel",
44
"keywords": ["wechat", "weixin","laravel", "sdk"],
55
"require": {
6-
"overtrue/wechat": "2.1.*"
6+
"overtrue/wechat": "~3.0"
77
},
88
"autoload": {
99
"psr-4": {
@@ -12,17 +12,6 @@
1212
},
1313

1414
"license": "MIT",
15-
16-
"scripts": {
17-
"post-install-cmd": [
18-
"php artisan vendor:publish --provider=Overtrue\\LaravelWechat\\ServiceProvider"
19-
]
20-
},
21-
"extra": {
22-
"branch-alias": {
23-
"dev-master": "2.1-dev"
24-
}
25-
},
2615
"minimum-stability" : "dev",
2716
"authors": [
2817
{

src/CacheBridge.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
namespace Overtrue\LaravelWechat;
4+
5+
use Doctrine\Common\Cache\Cache as CacheInterface;
6+
use Illuminate\Support\Facades\Cache;
7+
8+
/**
9+
* Cache bridge for laravel.
10+
*/
11+
class CacheBridge implements CacheInterface
12+
{
13+
/**
14+
* Fetches an entry from the cache.
15+
*
16+
* @param string $id The id of the cache entry to fetch.
17+
*
18+
* @return mixed The cached data or FALSE, if no cache entry exists for the given id.
19+
*/
20+
public function fetch($id)
21+
{
22+
return Cache::get($id);
23+
}
24+
25+
/**
26+
* Tests if an entry exists in the cache.
27+
*
28+
* @param string $id The cache id of the entry to check for.
29+
*
30+
* @return bool TRUE if a cache entry exists for the given cache id, FALSE otherwise.
31+
*/
32+
public function contains($id)
33+
{
34+
return Cache::has($id);
35+
}
36+
37+
/**
38+
* Puts data into the cache.
39+
*
40+
* If a cache entry with the given id already exists, its data will be replaced.
41+
*
42+
* @param string $id The cache id.
43+
* @param mixed $data The cache entry/data.
44+
* @param int $lifeTime The lifetime in number of seconds for this cache entry.
45+
* If zero (the default), the entry never expires (although it may be deleted from the cache
46+
* to make place for other entries).
47+
*
48+
* @return bool TRUE if the entry was successfully stored in the cache, FALSE otherwise.
49+
*/
50+
public function save($id, $data, $lifeTime = 0)
51+
{
52+
return Cache::put($id, $data, $lifeTime / 60);
53+
}
54+
55+
/**
56+
* Deletes a cache entry.
57+
*
58+
* @param string $id The cache id.
59+
*
60+
* @return bool TRUE if the cache entry was successfully deleted, FALSE otherwise.
61+
* Deleting a non-existing entry is considered successful.
62+
*/
63+
public function delete($id)
64+
{
65+
return Cache::forget($id);
66+
}
67+
68+
/**
69+
* Retrieves cached information from the data store.
70+
*
71+
* @return array|null An associative array with server's statistics if available, NULL otherwise.
72+
*/
73+
public function getStats()
74+
{
75+
return null;
76+
}
77+
}

src/Facade.php

100644100755
Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class Facade extends LaravelFacade
1212
*/
1313
static public function getFacadeAccessor()
1414
{
15-
return "wechat.server";
15+
return "wechat";
1616
}
1717

1818
/**
@@ -25,6 +25,12 @@ static public function getFacadeAccessor()
2525
*/
2626
static public function __callStatic($name, $args)
2727
{
28-
return self::resolveFacadeInstance("wechat.{$name}");
28+
$app = static::getFacadeRoot();
29+
30+
if (method_exists($app, $name)) {
31+
return call_user_func_array([$app, $name], $args);
32+
}
33+
34+
return $app->$name;
2935
}
3036
}

src/ServiceProvider.php

100644100755
Lines changed: 9 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
namespace Overtrue\LaravelWechat;
44

55
use Illuminate\Support\ServiceProvider as LaravelServiceProvider;
6-
use Overtrue\Wechat\Server as WechatServer;
7-
use Overtrue\Wechat\Alias;
6+
use EasyWeChat\Foundation\Application;
87

98
class ServiceProvider extends LaravelServiceProvider
109
{
@@ -15,39 +14,6 @@ class ServiceProvider extends LaravelServiceProvider
1514
*/
1615
protected $defer = true;
1716

18-
/**
19-
* 补充
20-
*
21-
* @var array
22-
*/
23-
protected $providesAppends = ['wechat.server', 'Overtrue\\Wechat\\Server'];
24-
25-
/**
26-
* 服务列表
27-
*
28-
* @var array
29-
*/
30-
protected $services = [
31-
'wechat.user' => 'Overtrue\\Wechat\\User',
32-
'wechat.group' => 'Overtrue\\Wechat\\Group',
33-
'wechat.auth' => 'Overtrue\\Wechat\\Auth',
34-
'wechat.menu' => 'Overtrue\\Wechat\\Menu',
35-
'wechat.menu.item' => 'Overtrue\\Wechat\\MenuItem',
36-
'wechat.js' => 'Overtrue\\Wechat\\Js',
37-
'wechat.staff' => 'Overtrue\\Wechat\\Staff',
38-
'wechat.store' => 'Overtrue\\Wechat\\Store',
39-
'wechat.card' => 'Overtrue\\Wechat\\Card',
40-
'wechat.qrcode' => 'Overtrue\\Wechat\\QRCode',
41-
'wechat.url' => 'Overtrue\\Wechat\\Url',
42-
'wechat.media' => 'Overtrue\\Wechat\\Media',
43-
'wechat.image' => 'Overtrue\\Wechat\\Image',
44-
'wechat.notice' => 'Overtrue\\Wechat\\Notice',
45-
'wechat.color' => 'Overtrue\\Wechat\\Color',
46-
'wechat.semantic' => 'Overtrue\\Wechat\\Semantic',
47-
'wechat.stats' => 'Overtrue\\Wechat\\Stats',
48-
];
49-
50-
5117
/**
5218
* Boot the provider.
5319
*
@@ -73,19 +39,15 @@ public function register()
7339
__DIR__.'/config.php', 'wechat'
7440
);
7541

76-
if (config('wechat.use_alias')) {
77-
Alias::register();
78-
}
42+
$this->app->singleton(['EasyWeChat\\Foundation\\Application' => 'wechat'], function($app){
43+
$app = new Application(config('wechat'));
7944

80-
$this->app->singleton(['Overtrue\\Wechat\\Server' => 'wechat.server'], function($app){
81-
return new WechatServer(config('wechat.app_id'), config('wechat.token'), config('wechat.encoding_key'));
82-
});
45+
if (config('wechat.use_laravel_cache')) {
46+
$app->cache = new CacheBridge();
47+
}
8348

84-
foreach ($this->services as $alias => $service) {
85-
$this->app->singleton([$service => $alias], function($app) use ($service){
86-
return new $service(config('wechat.app_id'), config('wechat.secret'));
87-
});
88-
}
49+
return $app;
50+
});
8951
}
9052

9153
/**
@@ -95,6 +57,6 @@ public function register()
9557
*/
9658
public function provides()
9759
{
98-
return array_merge(array_keys($this->services), array_values($this->services), $this->providesAppends);
60+
return ['wechat', 'EasyWeChat\\Foundation\\Application'];
9961
}
10062
}

0 commit comments

Comments
 (0)