SettingService.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. namespace services\common;
  3. use common\components\Service;
  4. use common\models\common\AddonsSetting;
  5. use common\models\common\MallSetting;
  6. use common\models\common\PlatformSetting;
  7. use common\traits\ErrorTrait;
  8. use Exception;
  9. /**
  10. *
  11. * Class SettingService
  12. * @package services\common
  13. * @property \common\models\common\MallSetting $mall
  14. * @property \common\models\common\AddonsSetting $addons
  15. * @property \common\models\common\PlatformSetting $platform
  16. * @author lun
  17. */
  18. class SettingService extends Service
  19. {
  20. use ErrorTrait;
  21. public $modelClass = null;
  22. public $scene = 'platform';
  23. public function init()
  24. {
  25. $this->modelClass = PlatformSetting::class;
  26. }
  27. public function __get($name)
  28. {
  29. switch($name){
  30. case 'mall':
  31. $this->modelClass = MallSetting::class;
  32. break;
  33. case 'addons':
  34. $this->modelClass = AddonsSetting::class;
  35. break;
  36. case 'platform':
  37. $this->modelClass = PlatformSetting::class;
  38. }
  39. $this->scene = $name;
  40. return $this;
  41. }
  42. /**
  43. * 获取配置
  44. *
  45. * [平台配置] Yii::$app->services->setting->platform->get($key,$no_cache);
  46. * [商城配置] Yii::$app->services->setting->mall->get($mall_id,$key,$no_cache);
  47. * [插件配置] Yii::$app->services->setting->addons->get($mall_id,$addons_name,$key,$no_cache);
  48. * @param $mall_id [integer] 商城id
  49. * @param $addons_name [string] 插件name
  50. * @param $key [string] 配置项 参参数格式:goup组名或group组名.配置项name,如system,获取整个系统配置,system.copyright,获取系统配置项下的版权信息
  51. * @param $no_cache [boolean] 是否不走缓存
  52. * @param $getOne [boolean] 获取单个键值对,或者多个值
  53. * @Author bing
  54. * @DateTime 2021-08-20
  55. * @copyright Copyright (c) 2020 广东七件事集团
  56. * [mixed] $args
  57. * @return array|null
  58. */
  59. public function get(... $args)
  60. {
  61. if(!is_array($args) || empty($args)) return null;
  62. $conf = null;
  63. $no_cache = false;
  64. $model = $this->modelClass;
  65. switch($this->scene){
  66. case 'platform':
  67. $key = $args[0];
  68. $no_cache = $args[1] ?? false;
  69. $conf = $model::conf($key, [],(bool) $no_cache);
  70. break;
  71. case 'mall':
  72. if(count($args) >= 2){
  73. $mall_id = $args[0];
  74. if(!is_numeric($mall_id)) return null;
  75. $key = $args[1];
  76. $no_cache = $args[2] ?? false;
  77. $conf = $model::conf($mall_id,$key, [],(bool) $no_cache);
  78. }
  79. break;
  80. case 'addons':
  81. if(count($args) >= 3){
  82. $mall_id = $args[0];
  83. if(!is_numeric($mall_id)) return null;
  84. $addons_name = $args[1];
  85. $key = $args[2];
  86. $no_cache = $args[3] ?? false;
  87. $conf = $model::conf($mall_id,$key,$addons_name, [],(bool) $no_cache);
  88. }
  89. break;
  90. }
  91. return $conf;
  92. }
  93. /**
  94. * 设置配置
  95. * [平台设置] Yii::$app->services->setting->platform->set($conf);
  96. * [商城设置] Yii::$app->services->setting->mall->set($mall_id,$conf);
  97. * [插件设置] Yii::$app->services->setting->addons->set($mall_id,$addons_name,$conf);
  98. * @param $mall_id [integer] 商城id
  99. * @param $addons_name [string] 插件name
  100. * @param $group 配置组名
  101. * @Author bing
  102. * @DateTime 2021-08-20
  103. * @copyright Copyright (c) 2020 广东七件事集团
  104. * @param [type] $args
  105. * @return boolean
  106. */
  107. public function set(... $args){
  108. try{
  109. if(!is_array($args) || empty($args)) throw new Exception('配置参数不能为空');
  110. $conf = [];
  111. switch($this->scene){
  112. case 'platform':
  113. $conf = $args[0];
  114. if(empty($conf)) return false;
  115. $res = $this->modelClass::setData($conf);
  116. break;
  117. case 'mall':
  118. if(count($args) >= 2){
  119. $mall_id = $args[0];
  120. if(!is_numeric($mall_id)) throw new Exception('mall_id必须为数字');
  121. $group = $args[1];
  122. $res = $this->modelClass::setData($mall_id,$group);
  123. }
  124. break;
  125. case 'addons':
  126. if(count($args) >= 3){
  127. $mall_id = $args[0];
  128. if(!is_numeric($mall_id)) throw new Exception('mall_id必须为数字');
  129. $addons_name = $args[1];
  130. $group = $args[2];
  131. $res = $this->modelClass::setData($mall_id,$group,$addons_name);
  132. }
  133. break;
  134. }
  135. if($res === false) throw new Exception($this->modelClass::getStaticError());
  136. return true;
  137. }catch(Exception $e){
  138. $this->setError($e->getMessage());
  139. return false;
  140. }
  141. }
  142. }