2021-07-26 17:41:59 +08:00

190 lines
5.9 KiB
PHP

<?php
/**
* @copyright Copyright (c) 2021 勾股工作室
* @license https://opensource.org/licenses/GPL-2.0
* @link https://www.gougucms.com
*/
declare (strict_types = 1);
namespace app\admin\controller;
use app\admin\BaseController;
use app\admin\model\Nav as NavList;
use app\admin\model\NavInfo;
use app\admin\validate\NavCheck;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\View;
class Nav extends BaseController
{
public function index()
{
if (request()->isAjax()) {
$param = get_params();
$where = array();
if (!empty($param['keywords'])) {
$where[] = ['id|name|title|desc', 'like', '%' . $param['keywords'] . '%'];
}
$rows = empty($param['limit']) ? get_config(app . page_size) : $param['limit'];
$nav = NavList::where($where)
->order('create_time asc')
->paginate($rows, false, ['query' => $param]);
return table_assign(1, '', $nav);
} else {
return view();
}
}
//添加
public function add()
{
$id = empty(get_params('id')) ? 0 : get_params('id');
if ($id > 0) {
$nav = Db::name('Nav')->where(['id' => $id])->find();
View::assign('nav', $nav);
}
View::assign('id', $id);
return view();
}
//提交添加
public function post_submit()
{
if (request()->isAjax()) {
$param = get_params();
if (!empty($param['id']) && $param['id'] > 0) {
try {
validate(NavCheck::class)->scene('edit')->check($param);
} catch (ValidateException $e) {
// 验证失败 输出错误信息
return to_assign(0, $e->getError());
}
NavList::where(['id' => $param['id']])
->strict(false)
->field(true)
->update($param);
// 删除导航缓存
clear_cache('homeNav');
add_log('edit', $param['id'], $param);
return to_assign();
} else {
try {
validate(NavCheck::class)->scene('add')->check($param);
} catch (ValidateException $e) {
// 验证失败 输出错误信息
return to_assign(0, $e->getError());
}
$nid = NavList::strict(false)->field(true)->insertGetId($param);
// 删除导航缓存
clear_cache('homeNav');
add_log('add', $nid, $param);
return to_assign();
}
}
}
//删除
public function delete()
{
$id = get_params('id');
$count = Db::name('NavInfo')->where([
'nav_id' => $id,
])->count();
if ($count > 0) {
return to_assign(0, '该组下还有导航,无法删除');
}
if (Db::name('Nav')->delete($id) !== false) {
return to_assign(1, '删除成功');
// 删除导航缓存
clear_cache('homeNav');
add_log('delete', $id, []);
} else {
return to_assign(0, '删除失败');
}
}
//管理导航
public function nav_info()
{
$param = get_params();
if (request()->isAjax()) {
$id = $param['id'];
$navInfoList = Db::name('NavInfo')
->where(['nav_id' => $id])
->order('sort asc')
->select();
return to_assign(0, '', $navInfoList);
} else {
return view('', [
'nav_id' => $param['id'],
]);
}
}
//添加导航
public function nav_info_add()
{
$id = empty(get_params('id')) ? 0 : get_params('id');
View::assign('id', $id);
View::assign('nav_id', get_params('nid'));
View::assign('pid', get_params('pid'));
if ($id > 0) {
$nav = Db::name('NavInfo')->where(['id' => $id])->find();
View::assign('nav', $nav);
View::assign('nav_id', $nav['nav_id']);
View::assign('pid', $nav['pid']);
}
return view();
}
//保存添加
public function nav_info_submit()
{
if (request()->isAjax()) {
$param = get_params();
if (!empty($param['id']) && $param['id'] > 0) {
try {
validate(NavCheck::class)->scene('editInfo')->check($param);
} catch (ValidateException $e) {
// 验证失败 输出错误信息
return to_assign(0, $e->getError());
}
NavInfo::strict(false)->field(true)->update($param);
// 删除导航缓存
clear_cache('homeNav');
add_log('edit', $param['id'], $param);
return to_assign();
} else {
try {
validate(NavCheck::class)->scene('addInfo')->check($param);
} catch (ValidateException $e) {
// 验证失败 输出错误信息
return to_assign(0, $e->getError());
}
$nid = NavInfo::strict(false)->field(true)->insertGetId($param);
// 删除导航缓存
clear_cache('homeNav');
add_log('add', $nid, $param);
return to_assign();
}
}
}
//删除
public function nav_info_delete()
{
$id = get_params('id');
if (Db::name('NavInfo')->delete($id) !== false) {
//清除导航缓存
clear_cache('homeNav');
add_log('delete', $id, []);
return to_assign(1, '删除成功');
} else {
return to_assign(0, '删除失败');
}
}
}