168 lines
4.7 KiB
PHP
168 lines
4.7 KiB
PHP
<?php
|
||
|
||
namespace admin\controllers\system;
|
||
|
||
use admin\models\search\ConfigSearch;
|
||
use common\core\BaseAdminController;
|
||
use common\helpers\ArrayHelper;
|
||
use common\models\oldDb\Config;
|
||
use Yii;
|
||
use yii\base\Exception;
|
||
use yii\web\NotFoundHttpException;
|
||
|
||
/**
|
||
* 配置
|
||
*/
|
||
class ConfigController extends BaseAdminController
|
||
{
|
||
|
||
/**
|
||
* 配置项列表
|
||
*/
|
||
public function actionList()
|
||
{
|
||
$searchModel = new ConfigSearch();
|
||
$this->field = $this->_listMap();
|
||
return $searchModel->search(Yii::$app->request->queryParams);
|
||
}
|
||
|
||
/**
|
||
* 保存配置项
|
||
*/
|
||
public function actionSave()
|
||
{
|
||
$id = Yii::$app->request->get('id', 0);
|
||
$model = $this->findModel($id);
|
||
if ($model->load($this->post(),'') && $model->save()) {
|
||
$event = 'add';
|
||
if($id>0){
|
||
$event = 'edit';
|
||
}
|
||
$this->callbackEvent(ArrayHelper::toArray([$model],$this->_listMap()),'id',$event);
|
||
return [];
|
||
}else{
|
||
throw new Exception(current($model->getErrors())[0]);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 配置项详情1
|
||
* @doc-param int id ID
|
||
* @doc-return mixed @Config{name,type-int-配置类型0数字1字符2文本3数组4单选5富文本6多选,title,group,extra,remark,value,sort,status,created_at} 配置信息
|
||
*/
|
||
public function actionInfo()
|
||
{
|
||
$post = Yii::$app->request->post();
|
||
$this->requestValidate($post,[
|
||
['id','required']
|
||
]);
|
||
$config = Config::findOne($post['id']);
|
||
if ($config['extra']) {
|
||
$config['extra'] = Config::parse(3, $config['extra']);
|
||
}
|
||
return $config;
|
||
}
|
||
|
||
/**
|
||
* 删除配置项
|
||
*/
|
||
public function actionDel()
|
||
{
|
||
$id = (int)$this->get('id');
|
||
if($id<1){
|
||
throw new \Exception('删除失败!');
|
||
}
|
||
if(in_array($id,[1])){
|
||
throw new \Exception('禁止删除!');
|
||
}
|
||
$model = $this->findModel($id);
|
||
if ($model->delete()) {
|
||
$this->callbackEvent([$model->toArray()],'id','del');
|
||
return [];
|
||
} else {
|
||
throw new \Exception('删除失败!');
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 配置
|
||
* @doc-desc 20处理成number,1是text,2textarea,3textarea,4select或者radio,5富文本,6多选
|
||
* @doc-return mixed Data{name-string-分组名称,@Groups{Config{name,type-int-配置类型0数字1字符2文本3数组4单选5富文本6多选,title,group,extra,remark,value,sort,status}}} 配置数据
|
||
*/
|
||
public function actionGroup()
|
||
{
|
||
$config = \common\models\oldDb\Config::find()->where([
|
||
'name' => 'GROUP'
|
||
])->one();
|
||
$tab_groups = Config::parse(3, $config['value']);
|
||
|
||
$data = [];
|
||
foreach($tab_groups as $id => $group)
|
||
{
|
||
$groups = Config::find()
|
||
->where(['and', ['group' => $id], ['status' => 1]])
|
||
->orderBy('sort ASC')->asArray()->all();
|
||
foreach ($groups as $key => $value) {
|
||
if ($value['extra']) {
|
||
$groups[$key]['extra'] = Config::parse(3, $value['extra']);
|
||
}
|
||
}
|
||
$data[$id] = [
|
||
'name' => $group,
|
||
'groups' => $groups,
|
||
];
|
||
}
|
||
return $data;
|
||
}
|
||
|
||
/**
|
||
* 配置保存
|
||
* @doc-param array param 提交的修改数据[配置项name=>配置项的值,配置项name=>配置项的值]
|
||
*/
|
||
public function actionGroupSave()
|
||
{
|
||
$data = Yii::$app->request->post('param');
|
||
/* 更改配置值 */
|
||
$isSuccess = true;
|
||
foreach ($data as $name => $value) {
|
||
$model = Config::findOne(['name' => $name]);
|
||
$model->value = $value;
|
||
$model->update_time = time();
|
||
if (!$model->save()) {
|
||
$isSuccess = false;
|
||
continue;
|
||
}
|
||
}
|
||
if ($isSuccess) {
|
||
return [];
|
||
} else {
|
||
throw new Exception('有配置值修改失败');
|
||
}
|
||
}
|
||
|
||
protected function findModel($id)
|
||
{
|
||
if ($id == 0) {
|
||
return new Config();
|
||
}
|
||
if (($model = Config::findOne($id)) !== null) {
|
||
return $model;
|
||
} else {
|
||
throw new NotFoundHttpException('The requested page does not exist.');
|
||
}
|
||
}
|
||
|
||
private function _listMap()
|
||
{
|
||
return [
|
||
Config::class=>[
|
||
'id','title','name',
|
||
'type'=>function($m){
|
||
return Yii::$app->params['config_type'][$m->type];
|
||
},
|
||
'group','sort','status'
|
||
]
|
||
];
|
||
}
|
||
}
|