OfficialAccountConfig.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. /**
  14. * 公众号配置
  15. * Class OfficialAccountConfig
  16. * @package crmeb\services\wechat\config
  17. */
  18. class OfficialAccountConfig implements ConfigHandlerInterface
  19. {
  20. /**
  21. * AppID
  22. * @var string
  23. */
  24. protected $appId;
  25. /**
  26. * AppSecret
  27. * @var string
  28. */
  29. protected $secret;
  30. /**
  31. * Token
  32. * @var string
  33. */
  34. protected $token;
  35. /**
  36. * EncodingAESKey
  37. * @var string
  38. */
  39. protected $aesKey;
  40. /**
  41. * 指定 API 调用返回结果的类型
  42. * @var string
  43. */
  44. protected $responseType = 'array';
  45. /**
  46. * @var LogCommonConfig
  47. */
  48. protected $logConfig;
  49. /**
  50. * @var HttpCommonConfig
  51. */
  52. protected $httpConfig;
  53. /**
  54. * @var bool
  55. */
  56. protected $init = false;
  57. /**
  58. * OfficialAccountConfig constructor.
  59. * @param LogCommonConfig $config
  60. * @param HttpCommonConfig $commonConfig
  61. */
  62. public function __construct(LogCommonConfig $config, HttpCommonConfig $commonConfig)
  63. {
  64. $this->logConfig = $config;
  65. $this->httpConfig = $commonConfig;
  66. }
  67. /**
  68. * 初始化
  69. */
  70. protected function init()
  71. {
  72. if ($this->init) {
  73. return;
  74. }
  75. $this->init = true;
  76. $this->appId = $this->appId ?: $this->httpConfig->getConfig('official.appid', '');
  77. $this->secret = $this->secret ?: $this->httpConfig->getConfig('official.secret', '');
  78. $this->token = $this->token ?: $this->httpConfig->getConfig('official.token', '');
  79. $this->aesKey = $this->aesKey ?: ($this->httpConfig->getConfig('official.encode', -1) > 0 ? $this->httpConfig->getConfig('official.key', '') : '');
  80. }
  81. /**
  82. * 设置
  83. * @param string $key
  84. * @param $value
  85. * @return $this|mixed
  86. */
  87. public function set(string $key, $value)
  88. {
  89. $this->{$key} = $value;
  90. return $this;
  91. }
  92. /**
  93. * 获取配置
  94. * @param string|null $key
  95. * @return array|mixed|null
  96. */
  97. public function get(string $key = null)
  98. {
  99. $this->init();
  100. if ($key) {
  101. if (isset($this->{$key})) {
  102. return $this->{$key};
  103. }
  104. if ('log' === $key) {
  105. return $this->logConfig->all();
  106. }
  107. if ('http' === $key) {
  108. return $this->httpConfig->all();
  109. }
  110. }
  111. return null;
  112. }
  113. /**
  114. * 获取所有配置
  115. * @return array
  116. */
  117. public function all(): array
  118. {
  119. $this->init();
  120. return [
  121. 'app_id' => $this->appId,
  122. 'secret' => $this->secret,
  123. 'token' => $this->token,
  124. 'aes_key' => $this->aesKey,
  125. 'response_type' => $this->responseType,
  126. 'log' => $this->logConfig->all(),
  127. 'http' => $this->httpConfig->all()
  128. ];
  129. }
  130. }