BaseAdsSettingService.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. namespace common\services\mall;
  3. use Yii;
  4. use common\interfaces\AdsSettingInterface;
  5. use Exception;
  6. /**
  7. * 插件设置Service
  8. * @package common\services
  9. */
  10. abstract class BaseAdsSettingService extends BaseService implements AdsSettingInterface
  11. {
  12. /**
  13. * @var int
  14. */
  15. public $mall_id;
  16. /**
  17. * @var array
  18. */
  19. protected $hiddens = [];
  20. /**
  21. * @var array
  22. */
  23. protected $shows = [];
  24. /**
  25. * @var boolean
  26. */
  27. protected $isSet = false;
  28. /**
  29. * @return void
  30. * @throws Exception
  31. */
  32. private function _before()
  33. {
  34. if (!isset($this->mall_id)) {
  35. throw new Exception('未设置Mall Id');
  36. }
  37. }
  38. /**
  39. * 获取设置前处理
  40. *
  41. * @param array $setting
  42. * @return array
  43. */
  44. public function beforeGetSetting(array $setting): array
  45. {
  46. return $setting;
  47. }
  48. /**
  49. * 存储设置前处理
  50. *
  51. * @param array $setting
  52. * @return array
  53. */
  54. public function beforeSetSetting(array $setting): array
  55. {
  56. return $setting;
  57. }
  58. /**
  59. * 获取设置
  60. *
  61. * @return array
  62. */
  63. public function getSetting(string $name = 'basic')
  64. {
  65. $this->_before();
  66. $this->isSet = false;
  67. $setting = Yii::$app->services->setting->addons->get($this->mall_id, $this->getAddosName(), $name);
  68. if ($this->hiddens) return $this->hiddenArrayKeys($setting);
  69. if ($this->shows) return $this->showArrayKeys($setting);
  70. return $this->getMergeSetting($setting);
  71. }
  72. /**
  73. * 设置配置
  74. *
  75. * @param array $setting
  76. * @param string $name
  77. * @return boolean
  78. */
  79. public function setSetting(array $setting, string $name = 'basic')
  80. {
  81. $this->_before();
  82. $this->isSet = true;
  83. return Yii::$app->services->setting->addons->set(
  84. $this->mall_id,
  85. $this->getAddosName(),
  86. [$name => $this->getMergeSetting($setting)]
  87. );
  88. }
  89. /**
  90. * 合并设置
  91. *
  92. * @param array $setting
  93. * @return array
  94. */
  95. protected function getMergeSetting(array $setting)
  96. {
  97. !$this->isSet && $setting = $this->beforeGetSetting($setting);
  98. $this->isSet && $setting = $this->beforeSetSetting($setting);
  99. return array_merge($this->defaultSetting(), $setting);
  100. }
  101. /**
  102. * 隐藏部分配置文件不返回
  103. *
  104. * @param array $hidden_keys
  105. * @return self
  106. */
  107. public function hiddenSetting(array $hidden_keys)
  108. {
  109. $this->shows = [];
  110. $this->hiddens = array_values($hidden_keys);
  111. return $this;
  112. }
  113. /**
  114. * 只显示部分配置文件
  115. *
  116. * @param array $show_keys
  117. * @return self
  118. */
  119. public function showSetting(array $show_keys)
  120. {
  121. $this->hiddens = [];
  122. $this->shows = array_values($show_keys);
  123. return $this;
  124. }
  125. /**
  126. * @param array $setting
  127. * @return array
  128. */
  129. protected function hiddenArrayKeys(array $setting)
  130. {
  131. return array_diff_key($this->getMergeSetting($setting), array_flip($this->hiddens));
  132. }
  133. /**
  134. * @param array $setting
  135. * @return array
  136. */
  137. protected function showArrayKeys(array $setting)
  138. {
  139. return array_intersect_key($this->getMergeSetting($setting), array_flip($this->shows));
  140. }
  141. }