|
|
<?php
|
|
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
|
|
use App\Models\AuthNode;
|
|
|
use App\Models\OperateLog;
|
|
|
use App\Models\Permission;
|
|
|
use Illuminate\Http\Request;
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
|
|
class MenuController extends CommonController
|
|
|
{
|
|
|
/**
|
|
|
* @OA\Get(
|
|
|
* path="/api/admin/menu",
|
|
|
* tags={"后台管理"},
|
|
|
* summary="获取菜单",
|
|
|
* description="",
|
|
|
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
|
|
|
* @OA\Response(
|
|
|
* response="200",
|
|
|
* description="获取菜单"
|
|
|
* )
|
|
|
* )
|
|
|
*/
|
|
|
public function index()
|
|
|
{
|
|
|
$data = (new Permission())->ofGuard('admin')->orderBy("sortnumber")->get()->toArray();
|
|
|
foreach ($data as &$item) {
|
|
|
$item['auth_node_tags_array'] = AuthNode::whereIn('tag', $item['auth_node_tags_array'])->get()->toArray();
|
|
|
}
|
|
|
$data = array2tree($data);
|
|
|
return $this->success($data);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @OA\Get(
|
|
|
* path="/api/admin/menu/show",
|
|
|
* tags={"后台管理"},
|
|
|
* summary="获取菜单详情",
|
|
|
* description="",
|
|
|
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
|
|
|
* @OA\Response(
|
|
|
* response="200",
|
|
|
* description="获取菜单"
|
|
|
* )
|
|
|
* )
|
|
|
*/
|
|
|
public function show()
|
|
|
{
|
|
|
$data = (new Permission())->ofGuard('admin')->find(request()->id);
|
|
|
|
|
|
return $this->success($data);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @OA\Post(
|
|
|
* path="/api/admin/menu/save",
|
|
|
* tags={"后台管理"},
|
|
|
* summary="保存菜单",
|
|
|
* description="",
|
|
|
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
|
|
|
* @OA\Parameter(name="id", in="query", @OA\Schema(type="string"), description="菜单ID,为空表示新增,不为空表示更新"),
|
|
|
* @OA\Parameter(name="name", in="query", @OA\Schema(type="string"), required=true, description="名称"),
|
|
|
* @OA\Parameter(name="pid", in="query", @OA\Schema(type="string"), description="父id,默认为0"),
|
|
|
* @OA\Parameter(name="url", in="query", @OA\Schema(type="string"), description="页面路由"),
|
|
|
* @OA\Parameter(name="ptah", in="query", @OA\Schema(type="string"), description="路由"),
|
|
|
* @OA\Parameter(name="api_prefix", in="query", @OA\Schema(type="string"), description="API前缀(基于前后端约定同一个页面的API前缀相同)"),
|
|
|
* @OA\Parameter(name="icon", in="query", @OA\Schema(type="string"), description="图标样式"),
|
|
|
* @OA\Parameter(name="visible", in="query", @OA\Schema(type="string"), description="是否可见,默认为1"),
|
|
|
* @OA\Parameter(name="auth_node_tags", in="query", @OA\Schema(type="string"), description="绑定的权限节点tag,多个标示英文逗号分隔"),
|
|
|
* @OA\Parameter(name="sortnumber", in="query", @OA\Schema(type="integer"), description="同级排序,默认为0"),
|
|
|
* @OA\Response(
|
|
|
* response="200",
|
|
|
* description="保存菜单"
|
|
|
* )
|
|
|
* )
|
|
|
*/
|
|
|
public function save()
|
|
|
{
|
|
|
if (request()->id) {
|
|
|
return $this->update();
|
|
|
} else {
|
|
|
return $this->store();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public function store()
|
|
|
{
|
|
|
DB::beginTransaction();
|
|
|
try {
|
|
|
$data = (new Permission())->filterRequestColumns(request(), ["id"]);
|
|
|
$data["guard_name"] = $this->guardName;
|
|
|
$model = Permission::create($data);
|
|
|
DB::commit();
|
|
|
// 加日志
|
|
|
OperateLog::addLogs($this->getUser(), "新增导航栏[$model->name]成功");
|
|
|
return $this->success($model);
|
|
|
} catch (\Exception $exception) {
|
|
|
DB::rollBack();
|
|
|
// 加日志
|
|
|
OperateLog::addLogs($this->getUser(), "新增导航栏失败", $exception->getMessage());
|
|
|
return $this->fail([$exception->getCode(), $exception->getMessage()]);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public function update()
|
|
|
{
|
|
|
DB::beginTransaction();
|
|
|
try {
|
|
|
$model = Permission::find(request()->id);
|
|
|
$data = $model->filterRequestColumns(request(), ["id"]);
|
|
|
$model->update($data);
|
|
|
DB::commit();
|
|
|
// 加日志
|
|
|
OperateLog::addLogs($this->getUser(), "更新导航栏[$model->name]成功");
|
|
|
return $this->success($model);
|
|
|
} catch (\Exception $exception) {
|
|
|
DB::rollBack();
|
|
|
// 加日志
|
|
|
OperateLog::addLogs($this->getUser(), "新增导航栏失败", $exception->getMessage());
|
|
|
return $this->fail([$exception->getCode(), $exception->getMessage()]);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @OA\Post(
|
|
|
* path="/api/admin/menu/delete",
|
|
|
* tags={"后台管理"},
|
|
|
* summary="删除菜单",
|
|
|
* description="",
|
|
|
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
|
|
|
* @OA\Parameter(name="id", in="query", @OA\Schema(type="integer"), description="ID"),
|
|
|
* @OA\Response(
|
|
|
* response="200",
|
|
|
* description="删除菜单"
|
|
|
* )
|
|
|
* )
|
|
|
*/
|
|
|
public function delete(Request $request)
|
|
|
{
|
|
|
$permission = Permission::find($request->id);
|
|
|
try {
|
|
|
$permission->delete();
|
|
|
OperateLog::addLogs($this->getUser(), "删除导航栏[$permission->name]成功");
|
|
|
return $this->success("删除成功");
|
|
|
} catch (\Exception $exception) {
|
|
|
OperateLog::addLogs($this->getUser(), "删除导航栏[$permission->name]失败", $exception->getMessage());
|
|
|
return $this->fail([$exception->getCode(), $exception->getMessage()]);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
}
|