This commit is contained in:
JaguarJack
2020-09-07 08:29:12 +08:00
parent a6cebc3750
commit d533aeaa9e
6 changed files with 109 additions and 161 deletions

View File

@@ -95,96 +95,6 @@ class Config extends CatchModel
}
return true;
// 子配置
if ($data['pid'] ?? false) {
$config = \json_decode($data['config'], true);
$pid = $data['pid'];
unset($data['pid']);
/**[
'key' => [
'k' => 'v'
],
'k' => 'v'
]*/
/**
foreach ($config as $key => $value) {
if (empty($value)) {
continue;
}
// 如果二级配置存在
$secondLevel = $this->isExistConfig($key, $pid);
if ($secondLevel) {
// value 是字符串
if (!is_array($value)) {
if ($value != $secondLevel->value) {
$secondLevel->value = $value;
$secondLevel->save();
}
} else {
// 数组
$thirdLevel = [];
$this->subConfig($secondLevel->id, ['id', 'key', 'value'])
->each(function ($item, $key) use (&$thirdLevel){
$thirdLevel[$item['key']] = $item;
});
if (!empty($value)) {
$new = [];
foreach ($value as $k => $v) {
if (isset($thirdLevel[$k])) {
if ($v != $thirdLevel[$k]->value) {
$thirdLevel[$k]->value = $v;
$thirdLevel[$k]->save();
}
} else {
$new[] = [
'pid' => $secondLevel->id,
'key' => $k,
'value' => $v,
];
}
}
if (!empty($new)) {
parent::insertAllBy($new);
}
}
}
} else {
if (!is_array($value)) {
parent::createBy([
'pid' => $pid,
'key' => $key,
'value' => $value,
]);
} else {
$id = parent::createBy([
'pid' => $pid,
'key' => $key,
]);
if (!empty($value)) {
$newConfig = [];
foreach ($value as $k => $v) {
$newConfig[] = [
'key' => $k,
'value' => $v,
'pid' => $id,
];
}
parent::insertAllBy($newConfig);
}
}
}
}
*/
return true;
}
return parent::storeBy($data);
}
/**
@@ -227,28 +137,26 @@ class Config extends CatchModel
* 获取配置
*
* @time 2020年04月20日
* @param int $pid
* @param string $component
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @return array|mixed
*/
public function getConfig($pid = 0)
public function getConfig(string $component)
{
$data = [];
$configs = $this->where('pid', $pid)
$configs = $this->where('pid', $this->where('component', $component)->value('id'))
->field('id,`key` as k,value,pid')
->select();
foreach ($configs as $config) {
if ($config->value !== '') {
$data[$config->k] = $config->value;
} else {
$data[$config->k] = $this->getConfig($config->id);
if (strpos($config['k'], '.') !== false) {
list($object, $key) = explode('.', $config['k']);
$data[$object][$key] = $config['value'];
}
}
return empty($data) ? '' : $data;
return $data;
}
}