Notebook
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

147 lines
4.0 KiB

<?php
namespace Plugins\Notebook\Controller\Admin;
use App\Exception\ApiMessageException;
use App\Controller\AbstractController;
use Plugins\Notebook\Request\NotebookRequest;
use Plugins\Notebook\Service\Admin\NotebookService;
use App\Middleware\Admin\AuthenticateMiddleware;
use App\Middleware\Admin\PermissionsMiddleware;
use App\Util\Response;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\PostMapping;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\Validation\Annotation\Scene;
use Hyperf\Validation\Middleware\ValidationMiddleware;
use Hyperf\View\Render;
use Psr\Http\Message\ResponseInterface;
/**
* 笔记本控制器
*/
#[Controller(prefix: 'admin/plugins/notebook')]
#[Middlewares([AuthenticateMiddleware::class, PermissionsMiddleware::class])]
class NotebookController extends AbstractController
{
/**
* 列表页
*
* @param Render $render
*
* @return ResponseInterface
*/
#[GetMapping(path: 'lists')]
public function lists(Render $render): ResponseInterface
{
return $render->render('plugins.Notebook.Admin.Notebook.lists');
}
/**
* 列表数据
*
* @return ResponseInterface
*/
#[GetMapping(path: 'lists-data')]
public function listsData(): ResponseInterface
{
return Response::json()->success(NotebookService::aop()->lists());
}
/**
* 创建数据
*
* @param NotebookRequest $notebookRequest
*
* @return ResponseInterface
* @throws ApiMessageException
*/
#[Scene('store')]
#[PostMapping(path: 'create')]
#[Middlewares([ValidationMiddleware::class])]
public function store(NotebookRequest $notebookRequest): ResponseInterface
{
$model = NotebookService::aop()->store($notebookRequest->post());
$data = $model->setVisible(['id', "title", "share_key", "content", 'mode', "create_time", 'update_time'])->toArray();
$data['create_name'] = $model->createAdmin->name;
$data['latest_name'] = $model->latestAdmin->name;
$data['user_mode'] = 'super';
$data['user'] = [];
return Response::json()->success($data);
}
/**
* 更新数据
*
* @param NotebookRequest $notebookRequest
*
* @return ResponseInterface
* @throws ApiMessageException
*/
#[Scene('update')]
#[PostMapping(path: 'update')]
#[Middlewares([ValidationMiddleware::class])]
public function update(NotebookRequest $notebookRequest): ResponseInterface
{
$data = NotebookService::aop()->update($notebookRequest->post());
return Response::json()->success([
'update_time' => $data->update_time
]);
}
/**
* 删除数据
*
* @param NotebookRequest $notebookRequest
*
* @return ResponseInterface
* @throws ApiMessageException
*/
#[Scene('destroy')]
#[PostMapping(path: 'destroy')]
#[Middlewares([ValidationMiddleware::class])]
public function destroy(NotebookRequest $notebookRequest): ResponseInterface
{
NotebookService::aop()->destroy($notebookRequest->post('ids'));
return Response::json()->success();
}
/**
* 获取最新内容
*
* @return ResponseInterface
*/
#[GetMapping(path: 'get-latest')]
public function getLatest(): ResponseInterface
{
$data = NotebookService::aop()->getLatest(
(int)($this->request->query('id') ?: 0),
$this->request->query('update_time')
);
return Response::json()->success($data);
}
/**
* 添加协作用户
*
* @return ResponseInterface
*/
#[PostMapping(path: 'add-user')]
public function addUser(): ResponseInterface
{
NotebookService::aop()->addUser(
(int)$this->request->post('notebook_id'),
(array)$this->request->post('user_ids')
);
return Response::json()->success();
}
}