WorkConfig.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace crmeb\services\wechat\config;
  12. use crmeb\services\wechat\contract\ConfigHandlerInterface;
  13. use crmeb\services\wechat\contract\WorkAppConfigHandlerInterface;
  14. use crmeb\services\wechat\DefaultConfig;
  15. /**
  16. * 企业微信配置
  17. * Class WorkConfig
  18. * @package crmeb\services\wechat\config
  19. */
  20. class WorkConfig implements ConfigHandlerInterface
  21. {
  22. //应用
  23. const TYPE_APP = 'app';
  24. //客户联系
  25. const TYPE_USER = 'user';
  26. //通讯录同步
  27. const TYPE_ADDRESS = 'address';
  28. //客服
  29. const TYPE_KEFU = 'kefu';
  30. //审批
  31. const TYPE_APPROVE = 'approve';
  32. //会议室
  33. const TYPE_MEETING = 'meeting';
  34. //自建应用
  35. const TYPE_USER_APP = 'build';
  36. /**
  37. * @var string
  38. */
  39. protected $corpId;
  40. /**
  41. * @var string
  42. */
  43. protected $token;
  44. /**
  45. * @var string
  46. */
  47. protected $aesKey;
  48. /**
  49. * @var string
  50. */
  51. protected $responseType = 'array';
  52. /**
  53. * @var LogCommonConfig
  54. */
  55. protected $logConfig;
  56. /**
  57. * @var HttpCommonConfig
  58. */
  59. protected $httpConfig;
  60. /**
  61. * @var bool
  62. */
  63. protected $init = false;
  64. /**
  65. * @var WorkAppConfigHandlerInterface
  66. */
  67. protected $handler;
  68. /**
  69. * @var array
  70. */
  71. protected $appConfig;
  72. /**
  73. * WorkConfig constructor.
  74. * @param LogCommonConfig $config
  75. * @param HttpCommonConfig $commonConfig
  76. */
  77. public function __construct(LogCommonConfig $config, HttpCommonConfig $commonConfig)
  78. {
  79. $this->logConfig = $config;
  80. $this->httpConfig = $commonConfig;
  81. }
  82. protected function init()
  83. {
  84. if ($this->init) {
  85. return;
  86. }
  87. $this->init = true;
  88. $this->corpId = $this->corpId ?: $this->httpConfig->getConfig(DefaultConfig::WORK_CORP_ID, '');
  89. $this->token = $this->token ?: $this->httpConfig->getConfig('work.token', '');
  90. $this->aesKey = $this->aesKey ?: $this->httpConfig->getConfig('work.key', '');
  91. }
  92. /**
  93. * 设置
  94. * @param string $key
  95. * @param $value
  96. * @return $this|mixed
  97. */
  98. public function set(string $key, $value)
  99. {
  100. $this->{$key} = $value;
  101. return $this;
  102. }
  103. /**
  104. * 获取配置
  105. * @param string|null $key
  106. * @return array|mixed|null
  107. */
  108. public function get(string $key = null)
  109. {
  110. $this->init();
  111. if ($key) {
  112. if (isset($this->{$key})) {
  113. return $this->{$key};
  114. }
  115. if ('log' === $key) {
  116. return $this->logConfig->all();
  117. }
  118. if ('http' === $key) {
  119. return $this->httpConfig->all();
  120. }
  121. }
  122. return null;
  123. }
  124. /**
  125. * 获取全部值
  126. * @return array
  127. */
  128. public function all(): array
  129. {
  130. $this->init();
  131. return [
  132. 'corp_id' => $this->corpId,
  133. 'token' => $this->token,
  134. 'aes_key' => $this->aesKey,
  135. 'response_type' => $this->responseType,
  136. 'log' => $this->logConfig->all(),
  137. 'http' => $this->httpConfig->all()
  138. ];
  139. }
  140. /**
  141. * 获取应用配置
  142. * @param string $type
  143. * @return array
  144. */
  145. public function getAppConfig(string $type): array
  146. {
  147. if (!isset($this->appConfig[$type])) {
  148. /** @var WorkAppConfigHandlerInterface $make */
  149. $make = app()->make($this->handler);
  150. if (!$this->corpId) {
  151. $this->init();
  152. }
  153. $this->appConfig[$type] = $make->getAppConfig($this->corpId, $type);
  154. }
  155. return $this->appConfig[$type];
  156. }
  157. /**
  158. * 设置
  159. * @param string $handler
  160. * @return $this
  161. */
  162. public function setHandler(string $handler)
  163. {
  164. $this->handler = $handler;
  165. return $this;
  166. }
  167. }