OpenWebConfig.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 OpenWebConfig
  16. * @package crmeb\services\wechat\config
  17. */
  18. class OpenWebConfig 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. * @var string
  32. */
  33. protected $token;
  34. /**
  35. * @var string
  36. */
  37. protected $aesKey;
  38. /**
  39. * @var bool
  40. */
  41. protected $init = false;
  42. /**
  43. * @var HttpCommonConfig
  44. */
  45. protected $config;
  46. /**
  47. * OpenWebConfig constructor.
  48. * @param HttpCommonConfig $config
  49. */
  50. public function __construct(HttpCommonConfig $config)
  51. {
  52. $this->config = $config;
  53. }
  54. /**
  55. * OpenWebConfig constructor.
  56. */
  57. public function init()
  58. {
  59. if ($this->init) {
  60. return;
  61. }
  62. $this->init = true;
  63. $this->appId = $this->appId ?: $this->config->getConfig('web.appid', '');
  64. $this->secret = $this->secret ?: $this->config->getConfig('web.secret', '');
  65. $this->token = $this->token ?: $this->config->getConfig('web.token', '');
  66. $this->aesKey = $this->aesKey ?: $this->config->getConfig('web.key', '');
  67. }
  68. /**
  69. * 获取配置
  70. * @param string $key
  71. * @param null $default
  72. * @return mixed
  73. */
  74. public function getConfig(string $key, $default = null)
  75. {
  76. return $this->config->getConfig($key, $default);
  77. }
  78. /**
  79. * @param string $key
  80. * @param $value
  81. * @return $this|mixed
  82. */
  83. public function set(string $key, $value)
  84. {
  85. $this->{$key} = $value;
  86. return $this;
  87. }
  88. /**
  89. * @param string|null $key
  90. * @return mixed
  91. */
  92. public function get(string $key = null)
  93. {
  94. $this->init();
  95. if ('http' === $key) {
  96. return $this->config->all();
  97. }
  98. return $this->{$key};
  99. }
  100. /**
  101. * @return array
  102. */
  103. public function all(): array
  104. {
  105. $this->init();
  106. return [
  107. 'app_id' => $this->appId,
  108. 'secret' => $this->secret,
  109. 'token' => $this->token,
  110. 'aes_key' => $this->aesKey,
  111. 'http' => $this->config->all()
  112. ];
  113. }
  114. }