mirror of
https://github.com/crmeb/CRMEB.git
synced 2025-12-11 01:52:49 +00:00
改进swagger路由加载
This commit is contained in:
parent
d97438f4a7
commit
4949a52ed4
@ -13,4 +13,23 @@ class Index
|
||||
{
|
||||
return view();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取应用下的jsonApi
|
||||
* @return false|string
|
||||
*/
|
||||
public function getApiContent(string $api_name = 'api')
|
||||
{
|
||||
if (!in_array($api_name, ['api', 'kefuapi', 'adminapi'])) {
|
||||
return [];
|
||||
}
|
||||
$swaggerJsonPath = app()->getAppPath() . 'view/json/' . $api_name . '_swagger.json';
|
||||
if (file_exists($swaggerJsonPath)) {
|
||||
return json_decode(file_get_contents($swaggerJsonPath), true);
|
||||
} else {
|
||||
$openapi = \OpenApi\scan(root_path('app/' . $api_name))->toJson();
|
||||
@file_put_contents($swaggerJsonPath, $openapi);
|
||||
return json_decode($openapi, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,34 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace app\swagger\route;
|
||||
|
||||
use think\facade\Route;
|
||||
|
||||
Route::group(function () {
|
||||
Route::get('create_api', function() { //生成api文件
|
||||
$openapi = \OpenApi\scan('../app/api')->toJson();
|
||||
$swagger_json_path = '../app/swagger/view/json/api_swagger.json';
|
||||
@file_put_contents($swagger_json_path, $openapi);
|
||||
});
|
||||
Route::get('api', function() { //获取api json数据
|
||||
header('Content-Type: application/json');
|
||||
$swagger_json_path = '../app/swagger/view/json/api_swagger.json';
|
||||
if(file_exists($swagger_json_path)){
|
||||
$jsonStr = file_get_contents($swagger_json_path);
|
||||
echo $jsonStr;
|
||||
}
|
||||
});
|
||||
Route::get('create_adminapi', function() { //生成adminapi文件
|
||||
$openapi = \OpenApi\scan('../app/adminapi')->toJson();
|
||||
$swagger_json_path = '../app/swagger/view/json/adminapi_swagger.json';
|
||||
@file_put_contents($swagger_json_path, $openapi);
|
||||
});
|
||||
|
||||
Route::get('adminapi', function() { //获取adminapi json 数据
|
||||
header('Content-Type: application/json');
|
||||
$swagger_json_path = '../app/swagger/view/json/adminapi_swagger.json';
|
||||
if(file_exists($swagger_json_path)){
|
||||
$jsonStr = file_get_contents($swagger_json_path);
|
||||
echo $jsonStr;
|
||||
}
|
||||
});
|
||||
Route::get('index', 'Index/index');
|
||||
});
|
||||
|
||||
Route::get('api', 'Index/getApiContent')->append(['api_name' => 'api']);
|
||||
Route::get('adminapi', 'Index/getApiContent')->append(['api_name' => 'adminapi']);
|
||||
Route::get('index', 'Index/index');
|
||||
Route::get('/', 'Index/index');
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user