95 lines
2.8 KiB
PHP
95 lines
2.8 KiB
PHP
<?php
|
|
|
|
declare (strict_types = 1);
|
|
|
|
namespace app\admin\controller;
|
|
|
|
use app\admin\BaseController;
|
|
use app\admin\validate\MenuCheck;
|
|
use think\exception\ValidateException;
|
|
use think\facade\Db;
|
|
use think\facade\View;
|
|
|
|
class Menu extends BaseController
|
|
{
|
|
public function index()
|
|
{
|
|
|
|
return view();
|
|
}
|
|
|
|
//菜单列表
|
|
|
|
public function get_list()
|
|
{
|
|
$menu = Db::name('AdminMenu')->order('order asc')->select();
|
|
return to_assign(0, '', $menu);
|
|
}
|
|
|
|
//添加菜单页面
|
|
|
|
public function add()
|
|
{
|
|
return view('', ['pid' => get_params('pid')]);
|
|
}
|
|
|
|
//提交添加
|
|
|
|
public function post_submit()
|
|
{
|
|
if ($this->request->isPost()) {
|
|
$param = get_params();
|
|
if ($param['id'] > 0) {
|
|
try {
|
|
validate(MenuCheck::class)->scene('edit')->check($param);
|
|
} catch (ValidateException $e) {
|
|
// 验证失败 输出错误信息
|
|
return to_assign(0, $e->getError());
|
|
}
|
|
$data[$param['field']] = $param['value'];
|
|
$data['id'] = $param['id'];
|
|
Db::name('AdminMenu')->strict(false)->field(true)->update($data);
|
|
add_log('edit', $param['id'], $data);
|
|
} else {
|
|
try {
|
|
validate(MenuCheck::class)->scene('add')->check($param);
|
|
} catch (ValidateException $e) {
|
|
// 验证失败 输出错误信息
|
|
return to_assign(0, $e->getError());
|
|
}
|
|
$mid = Db::name('AdminMenu')->strict(false)->field(true)->insertGetId($param);
|
|
//自动为系统所有者管理组分配新增的菜单
|
|
$group = Db::name('AdminGroup')->find(1);
|
|
if (!empty($group)) {
|
|
$newGroup['id'] = 1;
|
|
$newGroup['menus'] = $group['menus'] . ',' . $mid;
|
|
Db::name('AdminGroup')->strict(false)->field(true)->update($newGroup);
|
|
add_log('add', $mid, $param);
|
|
}
|
|
}
|
|
// 删除后台菜单缓存
|
|
clear_cache('adminMenu');
|
|
return to_assign();
|
|
}
|
|
}
|
|
|
|
//删除
|
|
|
|
public function delete()
|
|
{
|
|
$id = get_params('id');
|
|
$count = Db::name('AdminMenu')->where(['pid' => $id])->count();
|
|
if ($count > 0) {
|
|
return to_assign(0, '该菜单下还有子菜单,无法删除');
|
|
}
|
|
if (Db::name('AdminMenu')->delete($id) !== false) {
|
|
// 删除后台菜单缓存
|
|
clear_cache('adminMenu');
|
|
add_log('delete', $id, []);
|
|
return to_assign(1, '删除菜单成功');
|
|
} else {
|
|
return to_assign(0, '删除失败');
|
|
}
|
|
}
|
|
}
|