From c98997cd587545056b8d9773ec6998dd47e7dfb0 Mon Sep 17 00:00:00 2001 From: chenlong Date: Thu, 21 Sep 2023 18:42:49 +0800 Subject: [PATCH] asd --- Attributes/ApiBody.php | 4 +- Controller/DemoController.php | 18 ++++---- Service/ApiDocService.php | 47 ++++++++++++++++++- View/Document/view.blade.php | 85 ++++++++++++++++++++++------------- 4 files changed, 108 insertions(+), 46 deletions(-) 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
+
+
@@ -253,7 +259,7 @@ var APP = new Vue({ el:'#app', data: { - editApiDoc:false, + docDesVisible:true, paramInfo: 'request', paramShowType: 'form', requestUrl: 'https://vuejs.org/guide/introduction.html', @@ -271,15 +277,27 @@ myMark:[], showMark:false, searchText:"", - apiActive:-1 + apiActive:-1, + version:'' }, created(){ if (localStorage.getItem("ScBefore")){ this.scriptBeforeRequest = localStorage.getItem("ScBefore"); } + if (!/requestData\.headers/.test(this.scriptBeforeRequest)) { + this.scriptBeforeRequest += `\nif(ApiInfo.auth){\n`; + this.scriptBeforeRequest += ` requestData.headers = { Authenticate: 'Bearer ' + localStorage.getItem("token")};\n`; + this.scriptBeforeRequest += `}`; + } if (localStorage.getItem("ScAfter")){ this.scriptAfterRequest = localStorage.getItem("ScAfter"); } + if (!/response\.data\.token/.test(this.scriptAfterRequest)) { + this.scriptAfterRequest += `\nif(response.data.token){\n` + + ` localStorage.setItem("token", response.data.token);\n` + + `}`; + } + this.getApiInfo(); this.myMark = localStorage.getItem("myMark").split(','); }, @@ -303,7 +321,13 @@ } if(this.searchText){ let length = item.children.filter(a => { - return a.url.indexOf(this.searchText) < 0 || a.name.indexOf(this.searchText) < 0 + return a.url.indexOf(this.searchText) < 0 && a.name.indexOf(this.searchText) < 0 + }); + if (length > 0) show = false; + } + if (this.version) { + let length = item.children.filter(a => { + return !a.version.includes(this.version * 1) }); if (length > 0) show = false; } @@ -314,7 +338,10 @@ if(this.showMark && !this.myMark.includes(a.url)){ show = false; } - if(this.searchText && (a.url.indexOf(this.searchText) < 0 || a.name.indexOf(this.searchText) < 0)){ + if(this.searchText && (a.url.indexOf(this.searchText) < 0 && a.name.indexOf(this.searchText) < 0)){ + show = false; + } + if(this.version && !a.version.includes(this.version * 1)) { show = false; } return show; @@ -326,10 +353,6 @@ this.form = this.requestParamsResolve(this.currentApiInfo.requestParams); this.paramShowJsonEl && this.paramShowJsonEl.set(this.form); }, - clearCurrentApi(){ - this.currentApiInfo = {}; - this.form = {}; - }, getPath(){ try{ return new URL(this.currentApiInfo.url).pathname