146 lines
4.8 KiB
PHP
146 lines
4.8 KiB
PHP
<?php
|
|
// +----------------------------------------------------------------------
|
|
// | YwxApp [ WE CAN DO IT JUST THINK ]
|
|
// +----------------------------------------------------------------------
|
|
// | Copyright (c) 2026-2036 http://ywxapp.cn All rights reserved.
|
|
// +----------------------------------------------------------------------
|
|
// | Author: ywxapp<admin@ywxapp.cn>
|
|
// +----------------------------------------------------------------------
|
|
declare (strict_types = 1);
|
|
|
|
namespace app\backend\controller;
|
|
|
|
use think\facade\Request;
|
|
use think\facade\View;
|
|
use app\backend\model\NavMenu;
|
|
use ywxapp\controller\BackendBase;
|
|
|
|
/**
|
|
* 前台主导航菜单管理(支持两级下拉,可后台配置).
|
|
*/
|
|
class Navbar extends BackendBase
|
|
{
|
|
protected function initialize()
|
|
{
|
|
}
|
|
|
|
/**
|
|
* 列表页.
|
|
*/
|
|
public function index()
|
|
{
|
|
if (Request::isAjax()) {
|
|
$list = NavMenu::getAdminTree();
|
|
return json(['code' => 0, 'msg' => 'ok', 'data' => $list, 'count' => count($list)]);
|
|
}
|
|
return View::fetch();
|
|
}
|
|
|
|
/**
|
|
* 添加/编辑页(页面式,复用 edit.html).
|
|
*/
|
|
public function edit($id = 0)
|
|
{
|
|
$row = $id ? NavMenu::find($id) : null;
|
|
if (Request::isPost()) {
|
|
$data = [
|
|
'parent_id' => (int) Request::post('parent_id', 0),
|
|
'title' => trim((string) Request::post('title', '')),
|
|
'url' => trim((string) Request::post('url', '')),
|
|
'icon' => trim((string) Request::post('icon', '')),
|
|
'sort' => (int) Request::post('sort', 0),
|
|
'status' => (int) Request::post('status', 1),
|
|
];
|
|
if ($data['title'] === '') {
|
|
return json(['code' => 1, 'msg' => '请输入菜单名称']);
|
|
}
|
|
// 不能把自己设为自己的父级
|
|
if ($id && $data['parent_id'] === (int) $id) {
|
|
return json(['code' => 1, 'msg' => '父级不能选择自己']);
|
|
}
|
|
if ($id) {
|
|
$row = NavMenu::find($id);
|
|
$row->save($data);
|
|
return json(['code' => 0, 'msg' => '已保存']);
|
|
}
|
|
NavMenu::create($data);
|
|
return json(['code' => 0, 'msg' => '已添加']);
|
|
}
|
|
// 父级下拉选项(仅一级)
|
|
$parents = NavMenu::where('parent_id', 0)
|
|
->where('delete_at', 0)
|
|
->order('sort', 'asc')
|
|
->field('id,title')
|
|
->select();
|
|
View::assign('parents', $parents);
|
|
View::assign('row', $row);
|
|
return View::fetch();
|
|
}
|
|
|
|
/**
|
|
* 保存(弹窗式 AJAX 提交,与前端 nav.js 对应).
|
|
*/
|
|
public function save()
|
|
{
|
|
$data = [
|
|
'parent_id' => (int) Request::post('parent_id', 0),
|
|
'title' => trim((string) Request::post('title', '')),
|
|
'url' => trim((string) Request::post('url', '')),
|
|
'icon' => trim((string) Request::post('icon', '')),
|
|
'sort' => (int) Request::post('sort', 0),
|
|
'status' => (int) Request::post('status', 1),
|
|
];
|
|
if ($data['title'] === '') {
|
|
return json(['code' => 1, 'msg' => '请输入菜单名称']);
|
|
}
|
|
$id = (int) Request::post('id', 0);
|
|
if ($id) {
|
|
if ($data['parent_id'] === $id) {
|
|
return json(['code' => 1, 'msg' => '父级不能选择自己']);
|
|
}
|
|
NavMenu::find($id)->save($data);
|
|
return json(['code' => 0, 'msg' => '已保存']);
|
|
}
|
|
NavMenu::create($data);
|
|
return json(['code' => 0, 'msg' => '已添加']);
|
|
}
|
|
|
|
/**
|
|
* 更新(与 save 同逻辑,兼容 PUT).
|
|
*/
|
|
public function update()
|
|
{
|
|
return $this->save();
|
|
}
|
|
|
|
/**
|
|
* 删除(支持单 id;有子项时拒绝,避免孤儿).
|
|
*/
|
|
public function delete($id = 0)
|
|
{
|
|
$id = $id ?: (int) Request::post('id', 0);
|
|
if (! $id) {
|
|
return json(['code' => 1, 'msg' => '请选择要删除的项']);
|
|
}
|
|
$hasChild = NavMenu::where('parent_id', $id)->where('delete_at', 0)->count();
|
|
if ($hasChild) {
|
|
return json(['code' => 1, 'msg' => '请先删除该菜单下的子项']);
|
|
}
|
|
NavMenu::destroy($id);
|
|
return json(['code' => 0, 'msg' => '已删除']);
|
|
}
|
|
|
|
/**
|
|
* 切换显示/隐藏.
|
|
*/
|
|
public function status($id = 0, $status = 1)
|
|
{
|
|
$id = $id ?: (int) Request::post('id', 0);
|
|
if (! $id) {
|
|
return json(['code' => 1, 'msg' => '参数错误']);
|
|
}
|
|
NavMenu::find($id)->save(['status' => (int) $status]);
|
|
return json(['code' => 0, 'msg' => '已更新']);
|
|
}
|
|
}
|