123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- <?php
- namespace crmeb\services\wechat\config;
- use crmeb\services\wechat\contract\ConfigHandlerInterface;
- use crmeb\services\wechat\contract\WorkAppConfigHandlerInterface;
- use crmeb\services\wechat\DefaultConfig;
- class WorkConfig implements ConfigHandlerInterface
- {
-
- const TYPE_APP = 'app';
-
- const TYPE_USER = 'user';
-
- const TYPE_ADDRESS = 'address';
-
- const TYPE_KEFU = 'kefu';
-
- const TYPE_APPROVE = 'approve';
-
- const TYPE_MEETING = 'meeting';
-
- const TYPE_USER_APP = 'build';
-
- protected $corpId;
-
- protected $token;
-
- protected $aesKey;
-
- protected $responseType = 'array';
-
- protected $logConfig;
-
- protected $httpConfig;
-
- protected $init = false;
-
- protected $handler;
-
- protected $appConfig;
-
- public function __construct(LogCommonConfig $config, HttpCommonConfig $commonConfig)
- {
- $this->logConfig = $config;
- $this->httpConfig = $commonConfig;
- }
- protected function init()
- {
- if ($this->init) {
- return;
- }
- $this->init = true;
- $this->corpId = $this->corpId ?: $this->httpConfig->getConfig(DefaultConfig::WORK_CORP_ID, '');
- $this->token = $this->token ?: $this->httpConfig->getConfig('work.token', '');
- $this->aesKey = $this->aesKey ?: $this->httpConfig->getConfig('work.key', '');
- }
-
- public function set(string $key, $value)
- {
- $this->{$key} = $value;
- return $this;
- }
-
- public function get(string $key = null)
- {
- $this->init();
- if ($key) {
- if (isset($this->{$key})) {
- return $this->{$key};
- }
- if ('log' === $key) {
- return $this->logConfig->all();
- }
- if ('http' === $key) {
- return $this->httpConfig->all();
- }
- }
- return null;
- }
-
- public function all(): array
- {
- $this->init();
- return [
- 'corp_id' => $this->corpId,
- 'token' => $this->token,
- 'aes_key' => $this->aesKey,
- 'response_type' => $this->responseType,
- 'log' => $this->logConfig->all(),
- 'http' => $this->httpConfig->all()
- ];
- }
-
- public function getAppConfig(string $type): array
- {
- if (!isset($this->appConfig[$type])) {
-
- $make = app()->make($this->handler);
- if (!$this->corpId) {
- $this->init();
- }
- $this->appConfig[$type] = $make->getAppConfig($this->corpId, $type);
- }
- return $this->appConfig[$type];
- }
-
- public function setHandler(string $handler)
- {
- $this->handler = $handler;
- return $this;
- }
- }
|