easy-swoole / wechat

Wechat based on easyswoole.
82 stars 28 forks source link

我们自定义了日志和缓存驱动,但是在开放平台的 Application 下的没有 rebind #161

Closed browningweb closed 2 years ago

browningweb commented 2 years ago

例如可以在 officialAccount 方法里加上,以便自定义日志和缓存驱动: foreach ([ServiceProviders::Cache, ServiceProviders::Logger] as $reuse) { if (isset($this[$reuse])) { $officialAccount[$reuse] = $this[$reuse]; } }

browningweb commented 2 years ago

目前发现导致的结果是 getToken 方法获取缓存的两个地方不对 https://github.com/easy-swoole/wechat/blob/054113ba462f2cba457ec4dab55f454364213121/src/Kernel/AccessToken.php#L40

当使用开放平台替公众号发送模板消息时,第一个 getCache() 获取的是没有自定义的缓存,但是 refresh 后面的是自定义缓存。

XueSiLf commented 2 years ago

好的,感谢反馈。

XueSiLf commented 2 years ago

自定义日志和缓存驱动,组件默认已经是加好了的。会自动进行bind。

配置方式如下:

$config = [
    // 开放平台第三方平台 APPID
    'appId' => 'wxefe41fdeexxxxxx',

    // 开放平台第三方平台 Token
    'token' => 'dczmnau31ea9nzcnxxxxxxxxx',

    // 开放平台第三方平台 AES Key
    'aesKey' => 'easyswoole',

    // 开放平台第三方平台 Secret
    'appSecret' => 'your-AppSecret',

    // 自定义配置日志目录
    'log' => ['tempDir' => '/Tmp1']
    // 自定义配置文件缓存目录
    'cache' => ['tempDir' => '/cacheTmp'] 
];

获取缓存是默认采用自动刷新机制,先从缓存里获取,获取到了则直接返回。获取不到时才自动刷新token,然后存储到文件缓存之中。方便下次获取。

XueSiLf commented 2 years ago

已经看过了这里没啥问题,方便复现的话联系下我QQ 1592328848

browningweb commented 2 years ago

不好意思,可能我没说明白,这里说的是重新绑定了 缓存驱动 或 日志驱动,不是缓存存放目录或日志存放目录。 比如,在开放平台下,我重新绑定了驱动:

$app = Factory::openPlatform($account_config);
$app->rebind(ServiceProviders::Cache, new CacheBridge());
$app->rebind(ServiceProviders::Logger, new LogBridge());

但是在 OpenPlatform 里面的 officialAccount方法里面并没有给 OfficialAccount 重新 rebind 缓存和日志驱动,导致里面获取缓存时,还是用默认的驱动,从而获取缓存与写入缓存的地方不一致。

XueSiLf commented 2 years ago

你使用方式不对

XueSiLf commented 2 years ago
$app = Factory::openPlatform($account_config);
$officialAccount = $app->officialAccount('xxx', 'xxx');
$officialAccount->rebind(ServiceProviders::Cache, new CacheBridge());
$officialAccount->rebind(ServiceProviders::Logger, new LogBridge());
XueSiLf commented 2 years ago
$app = Factory::openPlatform($account_config);
$officialAccount = $app->officialAccount('xxx', 'xxx');
$officialAccount->rebind(ServiceProviders::Cache, new CacheBridge());
$officialAccount->rebind(ServiceProviders::Logger, new LogBridge());

你应该这样使用

browningweb commented 2 years ago

我想过用这样的方法,不过不如直接在 OpenPlatform 设置优雅,因为我要给 OpenPlatform设置一次,还有给 OfficialAccount 再设置一次。

XueSiLf commented 2 years ago

这个后续加上

browningweb commented 2 years ago

这个也加上吧,免得用的时候给疏漏掉,officialAccountminiProgram 方法里,new 以后加上下面的 rebind

foreach ([ServiceProviders::Cache, ServiceProviders::Logger] as $reuse) {
    if (isset($this[$reuse])) {
        $officialAccount[$reuse] = $this[$reuse];
    }
}
XueSiLf commented 2 years ago

ok 等下加下