| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- <?php
- namespace services\common;
- use common\components\Service;
- use common\models\common\AddonsSetting;
- use common\models\common\MallSetting;
- use common\models\common\PlatformSetting;
- use common\traits\ErrorTrait;
- use Exception;
- /**
- *
- * Class SettingService
- * @package services\common
- * @property \common\models\common\MallSetting $mall
- * @property \common\models\common\AddonsSetting $addons
- * @property \common\models\common\PlatformSetting $platform
- * @author lun
- */
- class SettingService extends Service
- {
- use ErrorTrait;
- public $modelClass = null;
- public $scene = 'platform';
- public function init()
- {
- $this->modelClass = PlatformSetting::class;
- }
- public function __get($name)
- {
- switch($name){
- case 'mall':
- $this->modelClass = MallSetting::class;
- break;
- case 'addons':
- $this->modelClass = AddonsSetting::class;
- break;
- case 'platform':
- $this->modelClass = PlatformSetting::class;
- }
- $this->scene = $name;
- return $this;
- }
- /**
- * 获取配置
- *
- * [平台配置] Yii::$app->services->setting->platform->get($key,$no_cache);
- * [商城配置] Yii::$app->services->setting->mall->get($mall_id,$key,$no_cache);
- * [插件配置] Yii::$app->services->setting->addons->get($mall_id,$addons_name,$key,$no_cache);
- * @param $mall_id [integer] 商城id
- * @param $addons_name [string] 插件name
- * @param $key [string] 配置项 参参数格式:goup组名或group组名.配置项name,如system,获取整个系统配置,system.copyright,获取系统配置项下的版权信息
- * @param $no_cache [boolean] 是否不走缓存
- * @param $getOne [boolean] 获取单个键值对,或者多个值
- * @Author bing
- * @DateTime 2021-08-20
- * @copyright Copyright (c) 2020 广东七件事集团
- * [mixed] $args
- * @return array|null
- */
- public function get(... $args)
- {
- if(!is_array($args) || empty($args)) return null;
- $conf = null;
- $no_cache = false;
- $model = $this->modelClass;
- switch($this->scene){
- case 'platform':
- $key = $args[0];
- $no_cache = $args[1] ?? false;
- $conf = $model::conf($key, [],(bool) $no_cache);
- break;
- case 'mall':
- if(count($args) >= 2){
- $mall_id = $args[0];
- if(!is_numeric($mall_id)) return null;
- $key = $args[1];
- $no_cache = $args[2] ?? false;
- $conf = $model::conf($mall_id,$key, [],(bool) $no_cache);
- }
- break;
- case 'addons':
- if(count($args) >= 3){
- $mall_id = $args[0];
- if(!is_numeric($mall_id)) return null;
- $addons_name = $args[1];
- $key = $args[2];
- $no_cache = $args[3] ?? false;
- $conf = $model::conf($mall_id,$key,$addons_name, [],(bool) $no_cache);
- }
- break;
- }
- return $conf;
- }
- /**
- * 设置配置
- * [平台设置] Yii::$app->services->setting->platform->set($conf);
- * [商城设置] Yii::$app->services->setting->mall->set($mall_id,$conf);
- * [插件设置] Yii::$app->services->setting->addons->set($mall_id,$addons_name,$conf);
- * @param $mall_id [integer] 商城id
- * @param $addons_name [string] 插件name
- * @param $group 配置组名
- * @Author bing
- * @DateTime 2021-08-20
- * @copyright Copyright (c) 2020 广东七件事集团
- * @param [type] $args
- * @return boolean
- */
- public function set(... $args){
- try{
- if(!is_array($args) || empty($args)) throw new Exception('配置参数不能为空');
- $conf = [];
- switch($this->scene){
- case 'platform':
- $conf = $args[0];
- if(empty($conf)) return false;
- $res = $this->modelClass::setData($conf);
- break;
- case 'mall':
- if(count($args) >= 2){
- $mall_id = $args[0];
- if(!is_numeric($mall_id)) throw new Exception('mall_id必须为数字');
- $group = $args[1];
- $res = $this->modelClass::setData($mall_id,$group);
- }
- break;
- case 'addons':
- if(count($args) >= 3){
- $mall_id = $args[0];
- if(!is_numeric($mall_id)) throw new Exception('mall_id必须为数字');
- $addons_name = $args[1];
- $group = $args[2];
- $res = $this->modelClass::setData($mall_id,$group,$addons_name);
- }
- break;
- }
- if($res === false) throw new Exception($this->modelClass::getStaticError());
- return true;
- }catch(Exception $e){
- $this->setError($e->getMessage());
- return false;
- }
- }
- }
|