diff --git a/Attributes/ApiBody.php b/Attributes/ApiBody.php index 229348e..d3395ef 100644 --- a/Attributes/ApiBody.php +++ b/Attributes/ApiBody.php @@ -21,7 +21,6 @@ class ApiBody extends Params * @param string $describe 参数描述 * @param bool $required 是否必填 * @param float $version 参数版本, 版本废弃用负数表示,例 2.0 版本废弃了 id参数:-2.0 - * @param array|ApiBody[] $children 子参数 */ public function __construct( public string|array $name, @@ -29,8 +28,7 @@ class ApiBody extends Params public string $type = 'Integer', public string $describe = '', public bool $required = true, - public float $version = 0, - public array $children = [] + public float $version = 0 ) { if (is_array($name)) { diff --git a/Controller/DemoController.php b/Controller/DemoController.php index 9d86aac..ebf7ae6 100644 --- a/Controller/DemoController.php +++ b/Controller/DemoController.php @@ -57,16 +57,14 @@ class DemoController #[Api] #[ApiBody('id', 'Integer', 'ID', true)] #[ApiBody('array', 'Array', '数组', true)] - #[ApiBody('arrayObject', 'Array', '包含对象的数组', true, children: [ - new ApiBody('field1', 'String', '字段一'), - new ApiBody('field2', 'String', '字段二'), - new ApiBody('field3', 'String', '字段三'), - ])] - #[ApiBody('Object', 'Object', '对象', true, children: [ - new ApiBody('field1', 'String', '字段一'), - new ApiBody('field2', 'String', '字段二'), - new ApiBody('field3', 'String', '字段三'), - ])] + #[ApiBody('arrayObject', 'Array', '包含对象的数组')] + #[ApiBody('arrayObject.field1', 'String', '字段一')] + #[ApiBody('arrayObject.field2', 'String', '字段二')] + #[ApiBody('arrayObject.field3', 'String', '字段三')] + #[ApiBody('Object', 'Object', '对象')] + #[ApiBody('Object.field1', 'String', '字段一')] + #[ApiBody('Object.field2', 'String', '字段二')] + #[ApiBody('Object.field3', 'String', '字段三')] #[ApiBody('file', 'File', '文件上传')] #[ApiReturn('id', 'Integer', 'ID')] #[ApiReturn('title', 'String', '标题')] diff --git a/Service/ApiDocService.php b/Service/ApiDocService.php index af20d78..c6ecbe4 100644 --- a/Service/ApiDocService.php +++ b/Service/ApiDocService.php @@ -96,8 +96,8 @@ class ApiDocService private function apiResolve(\ReflectionClass $reflectionClass): array { $authenticateType = $this->authenticateType($reflectionClass); - $groupTitle = $this->apiTitleResolve($reflectionClass->getDocComment()); - $baseUri = $reflectionClass->getAttributes(Controller::class)[0]->newInstance()->prefix; + $groupTitle = $this->apiTitleResolve($reflectionClass->getDocComment()); + $baseUri = $reflectionClass->getAttributes(Controller::class)[0]->newInstance()->prefix; $apis = []; foreach ($reflectionClass->getMethods() as $reflectionMethod) { @@ -197,10 +197,53 @@ class ApiDocService $api['version'][] = $attribute->version; } } + $api['responseParams'] = $this->childrenParamHandle($api['responseParams']); + if ($api['method'] === 'POST') { + $api['requestParams'] = $this->childrenParamHandle($api['requestParams']); + } return $api; } + /** + * @param array $initialData + * + * @return array + */ + private function childrenParamHandle(array $initialData): array + { + $newData = []; + foreach ($initialData as $item) { + $item['children'] = []; + $names = explode('.', $item['name']); + $current = &$newData; + + foreach (array_slice($names, 0, -1) as $attr) { + $current = &$current[$attr]['children']; + } + + $item['name'] = end($names); + $current[$item['name']] = $item; + } + + return $this->childrenData(array_values($newData)); + } + + /** + * @param array $data + * + * @return array + */ + private function childrenData(array $data): array + { + return array_map(function ($value){ + if ($value['children']){ + $value['children'] = array_values($this->childrenData($value['children'])); + } + return $value; + }, $data); + } + /** * @param \ReflectionMethod $method * diff --git a/View/Document/view.blade.php b/View/Document/view.blade.php index 0fad9dc..4ac9840 100644 --- a/View/Document/view.blade.php +++ b/View/Document/view.blade.php @@ -93,8 +93,10 @@ - 文档说明 + 文档说明 我的标记 + 检索: + 版本检索: @@ -221,31 +223,35 @@ - - - 已做标记,可认为是收藏,接口详情点击可切换标记状态 - 未做标记 - 必须使用Token验证 - 使用Token数据, 不验证 - 无需token - 发送模拟请求 - - - code: 状态码 - data: 数据 - msg: 响应提示语 - ts: 当前时间戳(秒) - - code 值说明: - 200:成功 - 202:失败 - 1001:刷新token,此时响应数据会带上新的token - 1002:token无效,应要求用户重新登录 - - + + + 已做标记,可认为是收藏,接口详情点击可切换标记状态 + 未做标记 + 必须使用Token验证 + 使用Token数据, 不验证 + 无需token + 发送模拟请求 + + + code: 状态码 + data: 数据 + msg: 响应提示语 + ts: 当前时间戳(秒) + + code 值说明: + 200:成功 + 202:失败 + 1001:刷新token,此时响应数据会带上新的token + 1002:token无效,应要求用户重新登录 + + + 认证方式采用JWT方式, 传输方式使用Header参数: Authenticate + 例:Authenticate: Bearer G0peyJhbGciF7hOiJyaXB.G0peyJhbGciF7hOiJyaXB.G0peyJhbGciF7hOiJyaXB + +