diff --git a/Controller/DemoController.php b/Controller/DemoController.php
index 8289f0f..9d86aac 100644
--- a/Controller/DemoController.php
+++ b/Controller/DemoController.php
@@ -14,6 +14,7 @@ use Plugins\ApiDoc\Attributes\ApiQuery;
use Plugins\ApiDoc\Attributes\ApiReturn;
use Plugins\ApiDoc\Attributes\ApiVersion;
use Psr\Http\Message\ResponseInterface;
+use Sc\Util\Tool;
/**
* API文档demo
@@ -176,4 +177,51 @@ class DemoController
{
return Response::json()->success();
}
+
+ /**
+ * 登录示例
+ *
+ * @return ResponseInterface
+ */
+ #[Api]
+ #[ApiBody('account', 'String', '账号')]
+ #[ApiBody('password', 'String', '密码')]
+ #[ApiReturn('user', 'Object', '用户信息')]
+ #[ApiReturn('user.name', 'String', '用户名')]
+ #[ApiReturn('user.age', 'Integer', '年龄')]
+ #[ApiReturn('user.avatar', 'String', '头像')]
+ #[ApiReturn('user.sex', 'String', '性别')]
+ #[ApiReturn('token', 'Object', 'Token')]
+ #[ApiReturn('token.token', 'String', 'Token')]
+ #[ApiReturn('token.token_exp', 'Integer', 'Token过期时间')]
+ #[PostMapping(path: 'login')]
+ public function login(): ResponseInterface
+ {
+ return Response::json()->success([
+ 'user' => [
+ 'name' => 'test',
+ 'age' => 18,
+ 'avatar' => 'https://test.test.com',
+ 'sex' => '男',
+ ],
+ 'token' => Tool::jwt()->setData(['user_id' => 1])->getToken()
+ ]);
+ }
+
+ /**
+ * 刷新token数据返回示例,实际程序会自动处理无需手动请求
+ *
+ * @return ResponseInterface
+ */
+ #[Api]
+ #[ApiReturn('token', 'Object', 'Token')]
+ #[ApiReturn('token.token', 'String', 'Token')]
+ #[ApiReturn('token.token_exp', 'Integer', 'Token过期时间')]
+ #[GetMapping(path: 'refresh-token')]
+ public function refreshToken(): ResponseInterface
+ {
+ return Response::json()->body(Tool\JWT::EXPIRE_CODE, "token过期 自动刷新", [
+ 'token' => Tool::jwt()->setData(['user_id' => 1])->getToken()
+ ]);
+ }
}
\ No newline at end of file
diff --git a/View/Document/view.blade.php b/View/Document/view.blade.php
index 59bb0a3..0fad9dc 100644
--- a/View/Document/view.blade.php
+++ b/View/Document/view.blade.php
@@ -71,21 +71,21 @@
-
+
- 文档说明
- 我的标记
+ 文档说明
+ 我的标记
+
@{{currentApiInfo.method}}
@{{ApiInfo.host}}@{{currentApiInfo.url}}
@@ -221,14 +222,30 @@
- @{{ ApiInfo.des }}
+
+ 已做标记,可认为是收藏,接口详情点击可切换标记状态
+ 未做标记
+ 必须使用Token验证
+ 使用Token数据, 不验证
+ 无需token
+ 发送模拟请求
+
+
+ code: 状态码
+ data: 数据
+ msg: 响应提示语
+ ts: 当前时间戳(秒)
+
+ code 值说明:
+ 200:成功
+ 202:失败
+ 1001:刷新token,此时响应数据会带上新的token
+ 1002:token无效,应要求用户重新登录
+
-
- 123123123123
-
@@ -250,7 +267,11 @@
scriptAfterRequest:`// 请求之后调用此段代码\n// ApiInfo 对象包含接口信息\n// response 对象包含响应的数据\n// console.log(ApiInfo);\n// console.log(response);`,
response:{},
currentApiInfo:{},
- ApiInfo:{}
+ ApiInfo:{},
+ myMark:[],
+ showMark:false,
+ searchText:"",
+ apiActive:-1
},
created(){
if (localStorage.getItem("ScBefore")){
@@ -260,13 +281,54 @@
this.scriptAfterRequest = localStorage.getItem("ScAfter");
}
this.getApiInfo();
+ this.myMark = localStorage.getItem("myMark").split(',');
+ },
+ mounted(){
+
},
methods: {
+ mark(url){
+ let index = this.myMark.indexOf(url);
+ if(index >= 0){
+ this.myMark.splice(index, 1);
+ }else{
+ this.myMark.push(url);
+ }
+ localStorage.setItem("myMark", this.myMark);
+ },
+ searchP(item){
+ let show = true;
+ if(this.showMark && item.children.filter(a => this.myMark.includes(a.url)).length === 0){
+ show = false;
+ }
+ if(this.searchText){
+ let length = item.children.filter(a => {
+ return a.url.indexOf(this.searchText) < 0 || a.name.indexOf(this.searchText) < 0
+ });
+ if (length > 0) show = false;
+ }
+ return show;
+ },
+ search(a){
+ let show = true;
+ 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)){
+ show = false;
+ }
+ return show;
+ },
handleSelect(key, keyPath) {
let s = key.split('-')
let currentApiInfo = this.ApiInfo.apiLists[s[0]].children[s[1]];
this.currentApiInfo = JSON.parse(JSON.stringify(currentApiInfo));
this.form = this.requestParamsResolve(this.currentApiInfo.requestParams);
+ this.paramShowJsonEl && this.paramShowJsonEl.set(this.form);
+ },
+ clearCurrentApi(){
+ this.currentApiInfo = {};
+ this.form = {};
},
getPath(){
try{
@@ -306,16 +368,18 @@
},
paramShowTypeToggle(el){
if (el.name === 'json') {
- if (!this.paramShowJsonEl) {
- this.paramShowJsonEl = new JSONEditor(
- document.getElementById("paramShowJson"),
- this.jsonEditorInitOptions('code', (jsonString) => {
- this.form = JSON.parse(jsonString);
- })
- );
- }
+ setTimeout(() => {
+ if (!this.paramShowJsonEl) {
+ this.paramShowJsonEl = new JSONEditor(
+ document.getElementById("paramShowJson"),
+ this.jsonEditorInitOptions('code', (jsonString) => {
+ this.form = JSON.parse(jsonString);
+ })
+ );
+ }
- this.paramShowJsonEl.set(this.form);
+ setTimeout(() => this.paramShowJsonEl.set(this.form), 5);
+ }, 10);
} else if (el.name === 'scriptBefore') {
if (!this.scriptBeforeRequestEl) {
let myTextArea = document.getElementById('requestBefore');
@@ -373,7 +437,8 @@
let ApiInfo = JSON.parse(JSON.stringify(this.currentApiInfo));
if (this.scriptBeforeRequestEl){
(function (){
- eval(APP.scriptBeforeRequestEl.getValue());
+ let script = APP.scriptBeforeRequestEl ? APP.scriptBeforeRequestEl.getValue() : this.scriptBeforeRequest;
+ eval(script);
})();
}
@@ -383,7 +448,8 @@
this.responseJsonHandle();
(function (){
let response = JSON.parse(JSON.stringify(res.data));
- eval(APP.scriptAfterRequestEl.getValue());
+ let script = APP.scriptAfterRequestEl ? APP.scriptAfterRequestEl.getValue() : this.scriptAfterRequest;
+ eval(script);
})();
}).catch((error) => {
this.response = error;
@@ -404,7 +470,8 @@
} else {
this.responseJsonEl = null;
}
- });
+ console.log(this.response);
+ }, 10);
},
jsonEditorInitOptions(defaultMode = 'tree', change){
return {
@@ -433,7 +500,6 @@
url:"/admin/plugins/api-doc/doc-data"
}).then(res => {
this.ApiInfo = res.data.data;
- console.log(111);
})
},
tableVersionClass({row, rowIndex}){