如果是多台服务器使用了负载均衡,会因为一些信息保存在了一台服务器而不在另一台服务器而报错,通过使用共用的redis来解决
修改symfony/cache缓存方式为redis
/**
* 修改symfony/cache缓存方式为redis
* @param array $config
* @return \EasyWeChat\OfficialAccount\Application
*/
public static function officialAccount(array $config)
{
$app = Factory::officialAccount($config);
$predis = app('redis')->connection()->client(); // connection($name), $name 默认为 `default`
$cache = new RedisAdapter($predis);
$app->rebind('cache', $cache);
return $app;
}
这里改完之后还是报错:"suite_ticket" does not exist in cache,suite_ticket不在缓存中
查看获取缓存key的地方
protected function getCacheKey(): string
{
return 'easywechat.open_work.suite_ticket.'.$this->app['config']['suite_id'];
}
猜测是因为缓存中有加入前缀,而获取时没有,所以报错,但是很遗憾,改完后还是报错
后面留意到我的代码
是这样的,这就相当于设置了work企业微信的缓存方式而没有设置企业微信开放平台的缓存方式
public function __construct(string $corpid, string $permanentCode, $secret = '', int $operateUserId = 0)
{
$this->corpid = $corpid;
$this->permanentCode = $permanentCode;
$this->operateUserId = $operateUserId;
$this->secret = $secret;
$this->app = $secret ? Factory::work([
'corp_id' => $corpid,
'secret' => $secret,
]) : Factory::openWork(config('wechat.open_work.default'))->work($corpid, $permanentCode);
// 修改缓存方式为redis
$predis = app('redis')->connection('default')->client();
$cache = new RedisAdapter($predis);
$this->app->rebind('cache', $cache);
}
后面改成了这样,在openWork之后设置缓存方式,就可以了...
public function __construct(string $corpid, string $permanentCode, $secret = '', int $operateUserId = 0)
{
$this->corpid = $corpid;
$this->permanentCode = $permanentCode;
$this->operateUserId = $operateUserId;
$this->secret = $secret;
$this->app = $secret ? Factory::work([
'corp_id' => $corpid,
'secret' => $secret,
]) : Factory::openWork(config('wechat.open_work.default'));
// 修改缓存方式为redis
$predis = app('redis')->connection('default')->client();
$cache = new RedisAdapter($predis);
$this->app->rebind('cache', $cache);
if (!$secret) $this->app = $this->app->work($corpid, $permanentCode);
}