MiniProgramConfig.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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\DefaultConfig;
  14. /**
  15. * 小程序配置
  16. * Class MiniProgramConfig
  17. * @package crmeb\services\wechat\config
  18. */
  19. class MiniProgramConfig implements ConfigHandlerInterface
  20. {
  21. /**
  22. * APPid
  23. * @var string
  24. */
  25. protected $appId;
  26. /**
  27. * APPsecret
  28. * @var string
  29. */
  30. protected $secret;
  31. /**
  32. * Token
  33. * @var string
  34. */
  35. protected $token;
  36. /**
  37. * EncodingAESKey
  38. * @var string
  39. */
  40. protected $aesKey;
  41. /**
  42. * @var string
  43. */
  44. protected $responseType = 'array';
  45. /**
  46. * 日志记录
  47. * @var LogCommonConfig
  48. */
  49. protected $logConfig;
  50. /**
  51. * http配置
  52. * @var HttpCommonConfig
  53. */
  54. protected $httpConfig;
  55. /**
  56. * 是否初始化过
  57. * @var bool
  58. */
  59. protected $init = false;
  60. /**
  61. * MiniProgramConfig constructor.
  62. * @param LogCommonConfig $config
  63. * @param HttpCommonConfig $commonConfig
  64. */
  65. public function __construct(LogCommonConfig $config, HttpCommonConfig $commonConfig)
  66. {
  67. $this->logConfig = $config;
  68. $this->httpConfig = $commonConfig;
  69. }
  70. /**
  71. * 初始化
  72. */
  73. protected function init()
  74. {
  75. if ($this->init) {
  76. return;
  77. }
  78. $this->init = true;
  79. $this->appId = $this->appId ?: $this->httpConfig->getConfig(DefaultConfig::MINI_APPID, '');
  80. $this->secret = $this->secret ?: $this->httpConfig->getConfig('mini.secret', '');
  81. $this->token = $this->token ?: $this->httpConfig->getConfig('mini.token', '');
  82. $this->aesKey = $this->aesKey ?: $this->httpConfig->getConfig('mini.key', '');
  83. }
  84. /**
  85. * 获取配置
  86. * @param string $key
  87. * @param null $default
  88. * @return mixed
  89. */
  90. public function getConfig(string $key, $default = null)
  91. {
  92. return $this->httpConfig->getConfig($key, $default);
  93. }
  94. /**
  95. * 设置
  96. * @param string $key
  97. * @param $value
  98. * @return $this|mixed
  99. */
  100. public function set(string $key, $value)
  101. {
  102. $this->{$key} = $value;
  103. return $this;
  104. }
  105. /**
  106. * @param string|null $key
  107. * @return array|mixed
  108. */
  109. public function get(string $key = null)
  110. {
  111. $this->init();
  112. if ('log' === $key) {
  113. return $this->logConfig->all();
  114. }
  115. if ('http' === $key) {
  116. return $this->httpConfig->all();
  117. }
  118. return $this->{$key};
  119. }
  120. /**
  121. * 全部
  122. * @return array
  123. */
  124. public function all(): array
  125. {
  126. $this->init();
  127. return [
  128. 'app_id' => $this->appId,
  129. 'secret' => $this->secret,
  130. 'token' => $this->token,
  131. 'aes_key' => $this->aesKey,
  132. 'response_type' => $this->responseType,
  133. 'log' => $this->logConfig->all(),
  134. 'http' => $this->httpConfig->all()
  135. ];
  136. }
  137. }