Files
nl-admin-api/app/Service/ApiEndpointService.php

79 lines
2.3 KiB
PHP
Raw Normal View History

2026-08-10 18:32:06 +08:00
<?php
namespace App\Service;
use App\BaseApp\BaseService;
use App\Models\ApiEndpointModel;
/**
* 接口注册表 Service分页查询 + 更新是否记日志等元信息
*/
class ApiEndpointService extends BaseService
{
public function __construct()
{
parent::__construct();
$this->model = ApiEndpointModel::class;
$this->selectField = [
'id', 'url', 'method', 'name', 'description', 'controller',
'is_log', 'status', 'created_at', 'updated_at',
];
$this->queryField = [
'url' => 'like',
'name' => 'like',
'is_log' => '=',
'method' => '=',
'status' => '=',
];
$this->orderBy = [
'name' => 'id',
'sort' => 'desc',
];
}
/**
* 分页列表;支持按 url/name/is_log/method 筛选
*/
public function list(): array
{
return $this->getPageList();
}
/**
* 更新接口元信息:仅允许改 name/description/is_log/status
* 为什么不开放改 url/method路由由代码决定随意改会导致日志匹配错乱
*
* @param int $id 接口 ID
* @param array $params 可更新字段
*/
public function update(int $id, array $params): mixed
{
$info = ApiEndpointModel::where('id', $id)->where('deleted_at', 0)->first();
if (!$info) {
$this->utils->notFound('接口不存在');
}
$data = [];
if (array_key_exists('name', $params)) {
$name = trim((string) $params['name']);
if ($name === '') {
$this->utils->errorThrow('操作名不能为空');
}
$data['name'] = $name;
}
if (array_key_exists('description', $params)) {
$data['description'] = trim((string) $params['description']);
}
if (array_key_exists('is_log', $params)) {
$data['is_log'] = (int) $params['is_log'] ? 1 : 0;
}
if (array_key_exists('status', $params)) {
$data['status'] = (int) $params['status'] ? 1 : 0;
}
if (empty($data)) {
return true;
}
$data['updated_at'] = time();
return ApiEndpointModel::where('id', $id)->where('deleted_at', 0)->update($data);
}
}