Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

:art 增加easywechat 自动获取token #3

Merged
merged 4 commits into from
Jul 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class WechatNotification extends Notification
- 直接传递 accesstoken 值到 `WechatMessage::create($accesstoken)` 方法中,如上例所示;
- 传递一个 `Yansongda\LaravelNotificatinoWechat\Contracts\AccessTokenInterface` 类到 `WechatMessage::create($CredentialClass)` 方法;
- 直接在 config 文件夹中的 services.php 中添加 `'wechat' => ['appid' => 'xxx', 'appsecret' => 'xxx']`,系统将自动获取 accesstoken 并缓存,缓存的 key 为 `wechatAccessToken您的APPID` 您可以直接通过 laravel 的 Cache Facade 获取缓存的 accesstoken,当然,最保险的方案是通过 `(new Yansongda\LaravelNotificationWechat\Credential($appid, $appsecret))->getAccessToken()` 去获取 accesstoken。
- 若使用EasyWechat(laravel包)且正常通信后,`WechatMessage::create()` 参数留空即可

具体可查看源码。

Expand Down
56 changes: 56 additions & 0 deletions src/EasyWechatCredential.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Yansongda\LaravelNotificationWechat;

use Yansongda\LaravelNotificationWechat\Contracts\AccessTokenInterface;

class EasyWechatCredential implements AccessTokenInterface
{
/**
* Wechat access token.
*
* @var string
*/
public $accessToken;

/**
* Bootstap.
*
* @author osi <yaoiluo@gmail.cn>
*/
public function __construct()
{
}

/**
* Set wechat accesstoken.
*
* @author yansongda <me@yansongda.cn>
*
* @param string $token
*/
public function setAccessToken($token)
{
$this->accessToken = $token;

return $this;
}

/**
* Get wechat accesstoken.
*
* @author osi <yaoiluo@gmail.cn>
*
* @return string
*/
public function getAccessToken()
{
if (!isset($this->accessToken)) {
$app = app('wechat.official_account');

return $app->access_token->getToken()['access_token'];
}

return $this->accessToken;
}
}
23 changes: 16 additions & 7 deletions src/WechatServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,22 @@ class WechatServiceProvider extends ServiceProvider
*/
public function boot()
{
$this->app->when(WechatChannel::class)
->needs(Wechat::class)
->give(function () {
return new Wechat(
new Credential(config('services.wechat.appid'), config('services.wechat.appsecret'))
);
});
if (class_exists('EasyWeChat\Factory')) {
// 如果用了easywechat
$this->app->when(WechatChannel::class)
->needs(Wechat::class)
->give(function () {
return new Wechat(new EasyWechatCredential());
});
} else {
$this->app->when(WechatChannel::class)
->needs(Wechat::class)
->give(function () {
return new Wechat(
new Credential(config('services.wechat.appid'), config('services.wechat.appsecret'))
);
});
}
}

/**
Expand Down