128 lines
4.0 KiB
PHP
128 lines
4.0 KiB
PHP
<?php
|
|
/**
|
|
* Created by PhpStorm.
|
|
* User: chatfeed
|
|
* Date: 2022/5/18
|
|
* Time: 3:19 PM
|
|
*/
|
|
|
|
namespace admin\controllers\system;
|
|
|
|
use admin\components\BaseAdminController;
|
|
use admin\components\UI\Elements\Button;
|
|
use admin\components\UI\Form;
|
|
use admin\components\UI\Layout;
|
|
use admin\components\UI\Node;
|
|
use admin\components\UI\Tabs;
|
|
use admin\components\UI\Widget;
|
|
use admin\models\Config;
|
|
use common\enums\StatusEnum;
|
|
use common\helpers\ArrayHelper;
|
|
|
|
class IndexController extends BaseAdminController
|
|
{
|
|
public function actionIndex2()
|
|
{
|
|
$configs = Config::find()->where(['status'=>StatusEnum::ACTIVE])->orderBy('sort ASC')->asArray()->all();
|
|
$configGroups = [];
|
|
foreach ($configs as $config){
|
|
$configGroups[$config['group']][] = $config;
|
|
}
|
|
foreach (\Yii::$app->params['config_group'] as $idx=>$name){
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
public function actionIndex(){
|
|
$layout = new Layout("基础配置");
|
|
$tabs = new Tabs();
|
|
//return ['node'=>$layout->render(),'setupScript'=>"\n return {}"];
|
|
//$data = [];
|
|
$layout->addChild(Widget::alert("非专业人士或不清楚选项请勿随意修改,否则可能会导致系统崩溃",'安全提示',function ($alert){
|
|
return $alert->type('warning');
|
|
}));
|
|
$configs = Config::find()->where(['status'=>StatusEnum::ACTIVE])->orderBy('sort ASC')->asArray()->all();
|
|
$configGroups = [];
|
|
foreach ($configs as $config){
|
|
$configGroups[$config['group']][] = $config;
|
|
}
|
|
foreach (\Yii::$app->params['config_group'] as $idx=>$name){
|
|
$configGroup = isset($configGroups[$idx])?$configGroups[$idx]:[];
|
|
if(!empty($configGroup)){
|
|
$data = ArrayHelper::map($configGroup, 'name', 'value');
|
|
$form = new Form($data);
|
|
$form->action(\Yii::$app->urlManager->createUrl('system/index/save'));
|
|
foreach ($configGroup as $config){
|
|
$this->buildItem($form,$config);
|
|
}
|
|
$tabs->addTab($name,$idx,'',$form->renderFormWithoutNode());
|
|
}
|
|
}
|
|
$layout->addChild($tabs->render());
|
|
return $layout->render();
|
|
}
|
|
|
|
public function actionSave(){
|
|
/* 表单验证 */
|
|
$data =$this->post();
|
|
|
|
//var_dump($data);exit;
|
|
/* 更改配置值 */
|
|
$error = [];
|
|
foreach ($data as $name => $value) {
|
|
$model = Config::findOne(['name' => $name]);
|
|
$model->value = (string)$value;
|
|
$model->update_time = time();
|
|
if (!$model->save()) {
|
|
$error[$model->name] = $model->getFirstErrors();
|
|
}
|
|
}
|
|
if (empty($error)) {
|
|
return [];
|
|
} else {
|
|
return ['error'=>$error];
|
|
}
|
|
}
|
|
private function buildItem(Form $form,$config){
|
|
//params config_type
|
|
/**
|
|
* 'config_type' => [
|
|
0 => '数字',
|
|
1 => '字符',
|
|
2 => '文本',
|
|
3 => '数组',
|
|
4 => '枚举',
|
|
5 => '富文本'
|
|
],
|
|
*/
|
|
switch ($config['type']){
|
|
case 0:
|
|
case 1:
|
|
case 2:
|
|
$form->text($config['title'], $config['name'])->help($config['remark']);
|
|
break;
|
|
case 3:
|
|
$form->radio($config['title'],$config['name'],Config::parse(3,$config['extra']))->help($config['remark']);
|
|
break;
|
|
case 4:
|
|
$form->textarea($config['title'],$config['name'])->help($config['remark']);
|
|
break;
|
|
case 5:
|
|
$form->editor($config['title'],$config['name']);
|
|
//@todo
|
|
break;
|
|
}
|
|
}
|
|
private function _parseExtra($str){
|
|
$explode = explode("\n", trim($str));
|
|
$ret =[];
|
|
foreach ($explode as $itemStr){
|
|
list($name,$val) = explode(":",$itemStr);
|
|
$ret[$val] = $name;
|
|
}
|
|
return $ret;
|
|
}
|
|
}
|