esaywechat 源码分析

业务需求:公众号授权给第三方平台。

方案:用esaywechat 快速开发微信开放平台“第三方平台”。

坑1:查看esaywechat “开放平台”文档,文档很简洁,一些需要的api并没有写到(但是人家肯定已经实现了)

例:获取授权方令牌authorizer_access_token,文档并没有写,这时需要自己从源码里面找到。

1. 全局搜索  authorizer_access_token

 

class AccessToken: protected $tokenKey = 'authorizer_access_token'; 
=》
父类 abstract class AccessToken:protected $tokenKey = 'access_token';
public function getToken(bool $refresh = false): array
{
$cacheKey = $this->getCacheKey();
$cache = $this->getCache(); //这里是获取缓存,可以看看他怎么实现的

if (!$refresh && $cache->has($cacheKey)) {
return $cache->get($cacheKey);
}

$token = $this->requestToken($this->getCredentials(), true);

$this->setToken($token[$this->tokenKey], $token['expires_in'] ?? 7200);

return $token;
}

找到父类里有getToken方法,非常鸡冻试试这个

于是,new AccessToken(); 但是子类构造器需要传参
/**
* AuthorizerAccessToken constructor.
*
* @param \Pimple\Container $app
* @param \EasyWeChat\OpenPlatform\Application $component
*/
public function __construct(Container $app, Application $component)
{
parent::__construct($app);

$this->component = $component;
}

。。这是什么参数。。头大。
Container $app 是来自第依赖包类
Application $component 是easywechat 自己的。
继续向下找,这两个类又需要传各种参数。。不能这么玩,改变思路。

2. easywechat 对外暴露的只有Factory类,我们参考文档如何获取access_token$openPlatform对象去获取authorizer_access_token

  $openPlatform = Factory::openPlatform($this->config);

 

思想:获取access_token和获取authorizer_access_token都是实现同一个接口同一个规范,用的时候可以参考之前写过的文档

 
 
 


 

posted @ 2018-06-14 11:17  王玉岩  阅读(1539)  评论(0编辑  收藏  举报