// +---------------------------------------------------------------------- namespace app\controller\admin; use app\controller\Admin; use app\model\Config as ConfigModel; use think\facade\Cache; class Config extends Admin { /** * @title 系统首页 */ public function index(ConfigModel $config) { if($this->request->isAjax()){ $param = $this->request->param(); $group = input('group', 0, 'trim'); $name = input('name', '', 'trim'); $system_config = Cache::get('system_config'); /* 查询条件初始化 */ $map = array('status' => 1); if ($group) { $map['group'] = $group; } if ($name) { $map['name'] = array('like', '%' . $name . '%'); } $res = $config->where($map)->order('id desc')->paginate($this->request->pageConfig); $data = $res->append(['type_text','group_text'])->toArray(); $this->data['data'] = $data; return $this->data; }else{ $data = array( 'group_id' => $this->request->get('group', 0), ); $this->data['data'] = $data; return $this->data; } } /** * @title 系统首页 */ public function group(ConfigModel $config) { if ($this->request->isAjax()) { $data = $this->request->param(); $result = $config->updateConfig($data); if (false !== $result) { $this->data['code'] = 0; $this->data['msg'] = "更新成功!"; }else{ $this->data['code'] = 1; $this->data['msg'] = $config->error; } return $this->data; } else { $id = $this->request->param('id', 1); $res = $config->where(array('status' => 1, 'group' => $id))->field('id,name,title,extra,value,remark,type')->order('sort')->select(); $list = $res->toArray(); $this->data['data'] = array('list' => $list, 'id' => $id); return $this->data; } } /** * @title 新增配置 * @author 麦当苗儿 */ public function add(ConfigModel $config) { if ($this->request->isPost()) { $config = model('Config'); $data = $this->request->post(); if ($data) { $id = $config->validate(true)->save($data); if ($id) { cache('db_config_data', null); //记录行为 action_log('update_config', 'config', $id, session('user_auth.uid')); return $this->success('新增成功', url('index')); } else { return $this->error('新增失败'); } } else { return $this->error($config->getError()); } } else { $this->data['data'] = array('info' => array()); $this->data['template'] = "edit"; return $this->data; } } /** * @title 编辑配置 * @author 麦当苗儿 */ public function edit(ConfigModel $config) { if ($this->request->isPost()) { $config = model('Config'); $data = $this->request->post(); if ($data) { $result = $config->validate('Config.edit')->save($data, array('id' => $data['id'])); if (false !== $result) { cache('db_config_data', null); //记录行为 action_log('update_config', 'config', $data['id'], session('user_auth.uid')); return $this->success('更新成功', Cookie('__forward__')); } else { return $this->error($config->getError(), ''); } } else { return $this->error($config->getError()); } } else { $id = $this->request->param('id', 0); if (!$id) { $this->data['msg'] = "非法操作!"; return $this->data; } $info = array(); /* 获取数据 */ $info = $config->field(true)->find($id); if (false === $info) { return $this->error('获取配置信息错误'); } $this->data['data'] = array( 'info' => $info, ); return $this->data; } } /** * @title 批量保存配置 * @author 麦当苗儿 */ public function save($config) { if ($config && is_array($config)) { $Config = db('Config'); foreach ($config as $name => $value) { $map = array('name' => $name); $Config->where($map)->setField('value', $value); } } cache('db_config_data', null); return $this->success('保存成功!'); } /** * @title 删除配置 * @author 麦当苗儿 */ public function del() { $id = array_unique((array) input('id', 0)); if (empty($id)) { return $this->error('请选择要操作的数据!'); } $map = array('id' => array('in', $id)); if (db('Config')->where($map)->delete()) { cache('DB_CONFIG_DATA', null); //记录行为 action_log('update_config', 'config', $id, session('user_auth.uid')); return $this->success('删除成功'); } else { return $this->error('删除失败!'); } } /** * @title 配置排序 * @author huajie */ public function sort() { if ($this->request->isGet()) { $ids = input('ids'); //获取排序的数据 $map = array('status' => array('gt', -1)); if (!empty($ids)) { $map['id'] = array('in', $ids); } elseif (input('group')) { $map['group'] = input('group'); } $list = db('Config')->where($map)->field('id,title')->order('sort asc,id asc')->select(); $this->assign('list', $list); $this->setMeta('配置排序'); return $this->fetch(); } elseif ($this->request->isPost()) { $ids = input('post.ids'); $ids = explode(',', $ids); foreach ($ids as $key => $value) { $res = db('Config')->where(array('id' => $value))->setField('sort', $key + 1); } if ($res !== false) { return $this->success('排序成功!', Cookie('__forward__')); } else { return $this->error('排序失败!'); } } else { return $this->error('非法请求!'); } } /** * @title 主题选择 */ public function themes(ConfigModel $config) { if ($this->request->isPost()) { $result = $config->where('name', $name . '_themes')->setField('value', $id); if (false !== $result) { \think\Cache::clear(); return $this->success('设置成功!'); }else{ return $this->error('设置失败!'); } }else{ $list = $config->getThemesList(); $this->data['data'] = array( 'pc' => $this->data['config']['pc_themes'], 'mobile' => $this->data['config']['mobile_themes'], 'list' => $list, ); return $this->data; } } }