Skip to content

Commit fd89034

Browse files
committed
create 3.0
1 parent 31f9b3b commit fd89034

File tree

17 files changed

+931
-883
lines changed

17 files changed

+931
-883
lines changed

.github/workflows/mirror.yml

Lines changed: 0 additions & 17 deletions
This file was deleted.

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
"type": "think-extend",
55
"keywords": [
66
"thinkphp",
7-
"swagger",
87
"apidoc",
98
"api文档",
109
"接口文档",
11-
"自动生成",
10+
"自动生成api",
1211
"注释生成",
13-
"php接口文档"
12+
"php接口文档",
13+
"php api文档"
1414
],
1515
"require": {
1616
"php": ">=7.1.0",

src/Auth.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ public function verifyAuth(string $password, string $appKey)
5252
*/
5353
protected function getTokenCode(string $password): string
5454
{
55-
return md5(md5($password) . strtotime(date('Y-m-d', time())));
55+
// return md5(md5($password) . strtotime(date('Y-m-d', time())));
56+
return md5(md5($password));
5657
}
5758

5859

@@ -63,7 +64,13 @@ protected function getTokenCode(string $password): string
6364
*/
6465
public function createToken(string $password): string
6566
{
66-
return $this->handleToken($this->getTokenCode($password), "CE");
67+
$expire = $this->config['auth']['expire']?$this->config['auth']['expire']:86400;
68+
$data = [
69+
'key'=>$this->getTokenCode($password),
70+
'expire'=>time()+$expire
71+
];
72+
$code = json_encode($data);
73+
return $this->handleToken($code, "CE");
6774
}
6875

6976
/**
@@ -77,9 +84,16 @@ public function checkToken(string $token, string $password): bool
7784
$password = $this->config['auth']['password'];
7885
}
7986
$decode = $this->handleToken($token, "DE");
80-
if ($decode === $this->getTokenCode($password)) {
87+
$deData = json_decode($decode,true);
88+
89+
if (!empty($deData['key']) && $deData['key'] === $this->getTokenCode($password) && !empty($deData['expire']) && $deData['expire']>time()){
8190
return true;
8291
}
92+
93+
94+
// if ($decode === $this->getTokenCode($password)) {
95+
// return true;
96+
// }
8397
return false;
8498
}
8599

@@ -92,7 +106,7 @@ public function checkAuth(string $appKey): bool
92106
$config = $this->config;
93107
$request = Request::instance();
94108

95-
$token = $request->param("apidocToken");
109+
$token = $request->param("token");
96110

97111
if (!empty($appKey)) {
98112
$currentApps = (new Utils())->getCurrentApps($appKey);

src/Controller.php

Lines changed: 76 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -11,65 +11,19 @@
1111
use think\App;
1212
use think\facade\Config;
1313
use think\facade\Request;
14-
use hg\apidoc\crud\CreateCrud;
1514

1615
class Controller
1716
{
1817
protected $app;
1918

2019
protected $config;
2120

22-
protected $defaultConfig=[
23-
'crud'=>[
24-
'model'=>[
25-
'fields_types'=>[
26-
"int",
27-
"tinyint",
28-
"smallint",
29-
"mediumint",
30-
"integer",
31-
"bigint",
32-
"bit",
33-
"real",
34-
"float",
35-
"decimal",
36-
"numeric",
37-
"char",
38-
"varchar",
39-
"date",
40-
"time",
41-
"year",
42-
"timestamp",
43-
"datetime",
44-
"tinyblob",
45-
"blob",
46-
"mediumblob",
47-
"longblob",
48-
"tinytext",
49-
"text",
50-
"mediumtext",
51-
"longtext",
52-
"enum",
53-
"set",
54-
"binary",
55-
"varbinary",
56-
"point",
57-
"linestring",
58-
"polygon",
59-
"geometry",
60-
"multipoint",
61-
"multilinestring",
62-
"multipolygon",
63-
"geometrycollection"
64-
]
65-
]
66-
]
67-
];
6821

6922
public function __construct(App $app)
7023
{
7124
$this->app = $app;
7225
$this->config = Config::get("apidoc")?Config::get("apidoc"):Config::get("apidoc.");
26+
7327
}
7428

7529
/**
@@ -79,45 +33,22 @@ public function __construct(App $app)
7933
public function getConfig(){
8034
$config = $this->config;
8135
if (!empty($config['auth'])){
82-
unset($config['auth']['auth_password']);
8336
unset($config['auth']['password']);
8437
unset($config['auth']['key']);
8538
}
86-
// 处理统一返回信息
87-
if (!empty($config['responses']) && is_string($config['responses'])){
88-
// 兼容原配置
89-
$config['responses'] = [
90-
'jsonStr'=>$config['responses']
91-
];
92-
}else if (!empty($config['responses']) && isset($config['responses']['show_responses']) && !$config['responses']['show_responses'] && !empty($config['responses']['data'])){
93-
// 显示在提示中
94-
$responsesStr = '{'."\r\n";
95-
$responsesMain = "";
96-
foreach ($config['responses']['data'] as $item){
97-
$responsesStr.='"'.$item['name'].'":"'.$item['desc'].'",'."\r\n";
98-
if (!empty($item['main']) && $item['main']==true){
99-
$responsesMain = $item;
100-
}
101-
}
102-
$responsesStr.= '}';
103-
$config['responses']['jsonStr']=$responsesStr;
104-
$config['responses']['main']=$responsesMain;
39+
$request = Request::instance();
40+
$params = $request->param();
41+
42+
if (!empty($params['lang'])){
43+
\think\facade\App::loadLangPack($params['lang']);
10544
}
45+
$config['headers'] = Utils::getArrayLang($config['headers'],"desc");
46+
$config['parameters'] = Utils::getArrayLang($config['parameters'],"desc");
47+
$config['responses'] = Utils::getArrayLang($config['responses'],"desc");
10648

107-
$config['debug']=app()->isDebug();
10849

109-
if (!empty($config['crud'])){
110-
// 无配置可选字段类型,使用默认的
111-
if (!empty($config['crud']['model']) && empty($config['crud']['model']['fields_types'])){
112-
$config['crud']['model']['fields_types'] = $this->defaultConfig['crud']['model']['fields_types'];
113-
}
114-
// 过滤route文件配置
115-
if (!empty($config['crud']['route'])){
116-
unset($config['crud']['route']);
117-
}
118-
}
11950
// 清除apps配置中的password
120-
$config['apps'] = (new Utils())->handleAppsConfig($config['apps']);
51+
$config['apps'] = (new Utils())->handleAppsConfig($config['apps'],true);
12152
return Utils::showJson(0,"",$config);
12253
}
12354

@@ -142,7 +73,10 @@ public function verifyAuth(){
14273
}
14374
try {
14475
$hasAuth = (new Auth())->verifyAuth($password,$appKey);
145-
return Utils::showJson(0,"",$hasAuth);
76+
$res = [
77+
"token"=>$hasAuth
78+
];
79+
return Utils::showJson(0,"",$res);
14680
} catch (AuthException $e) {
14781
return Utils::showJson($e->getCode(),$e->getMessage());
14882
}
@@ -158,67 +92,99 @@ public function getApidoc(){
15892
$config = $this->config;
15993
$request = Request::instance();
16094
$params = $request->param();
161-
(new Auth())->checkAuth($params['appKey']);
95+
$lang = "";
96+
97+
if (!empty($params['lang'])){
98+
$lang = $params['lang'];
99+
\think\facade\App::loadLangPack($params['lang']);
100+
}
101+
102+
if (!empty($params['appKey'])){
103+
// 获取指定应用
104+
$appKey = $params['appKey'];
105+
}else{
106+
// 获取默认控制器
107+
$default_app = $config = Config::get("app.default_app");
108+
$appKey = $default_app;
109+
110+
}
111+
$currentApps = (new Utils())->getCurrentApps($appKey);
112+
$currentApp = $currentApps[count($currentApps) - 1];
113+
114+
(new Auth())->checkAuth($appKey);
162115

163116
$cacheData=null;
164117
if (!empty($config['cache']) && $config['cache']['enable']){
165-
$cacheData = (new CacheApiData())->get($params['appKey'],$params['cacheFileName']);
166-
if ($cacheData && $params['reload']=='false'){
167-
$apiData = $cacheData['data'];
118+
$cacheKey = $appKey."_".$lang;
119+
$cacheData = (new CacheApiData())->get($cacheKey);
120+
if ($cacheData && empty($params['reload'])){
121+
$apiData = $cacheData;
168122
}else{
169123
// 生成数据并缓存
170-
$apiData = (new ParseAnnotation())->renderApiData($params['appKey']);
171-
$cacheData =(new CacheApiData())->set($params['appKey'],$apiData);
124+
$apiData = (new ParseAnnotation())->renderApiData($appKey);
125+
(new CacheApiData())->set($cacheKey,$apiData);
172126
}
173127
}else{
174128
// 生成数据
175-
$apiData = (new ParseAnnotation())->renderApiData($params['appKey']);
129+
$apiData = (new ParseAnnotation())->renderApiData($appKey);
176130
}
177-
$groups=[['title'=>'全部','name'=>0]];
178-
// 获取md
179-
$docs = (new ParseMarkdown())->getDocsMenu();
180-
if (count($docs)>0){
181-
$menu_title = !empty($config['docs']) && !empty($config['docs']['menu_title'])?$config['docs']['menu_title']:'文档';
182-
$groups[]=['title'=>$menu_title,'name'=>'markdown_doc'];
183-
}
184-
if (!empty($config['groups']) && count($config['groups'])>0 && !empty($apiData['groups']) && count($apiData['groups'])>0){
185-
$configGroups=[];
186-
foreach ($config['groups'] as $group) {
187-
if (in_array($group['name'],$apiData['groups'])){
188-
$configGroups[]=$group;
189-
}
190-
}
191-
$groups = array_merge($groups,$configGroups);
131+
132+
// 接口分组
133+
if (!empty($currentApp['groups'])){
134+
$data = (new ParseAnnotation())->mergeApiGroup($apiData['data'],$currentApp['groups']);
135+
}else{
136+
$data = $apiData['data'];
192137
}
138+
$groups=!empty($currentApp['groups'])?$currentApp['groups']:[];
193139
$json=[
140+
'data'=>$data,
141+
'app'=>$currentApp,
194142
'groups'=>$groups,
195-
'list'=>$apiData['data'],
196-
'docs'=>$docs,
197-
'tags'=>$apiData['tags']
143+
'tags'=>$apiData['tags'],
198144
];
199-
if ($cacheData && !empty($cacheData['list'])){
200-
$json['cacheFiles']=$cacheData['list'];
201-
$json['cacheName']=$cacheData['name'];
202-
}
145+
203146
return Utils::showJson(0,"",$json);
204147
}
205148

149+
public function getMdMenus(){
150+
// 获取md
151+
$request = Request::instance();
152+
$params = $request->param();
153+
154+
if (!empty($params['lang'])){
155+
\think\facade\App::loadLangPack($params['lang']);
156+
}
157+
$docs = (new ParseMarkdown())->getDocsMenu();
158+
return Utils::showJson(0,"",$docs);
159+
160+
}
161+
206162
/**
207163
* 获取md文档内容
208164
* @return \think\response\Json
209165
*/
210166
public function getMdDetail(){
211167
$request = Request::instance();
212168
$params = $request->param();
169+
if (!empty($params['lang'])){
170+
\think\facade\App::loadLangPack($params['lang']);
171+
}
213172
try {
214173
if (empty($params['path'])){
215174
throw new ErrorException("mdPath not found");
216175
}
217176
if (empty($params['appKey'])){
218177
throw new ErrorException("appkey not found");
219178
}
179+
$lang="";
180+
if (!empty($params['lang'])){
181+
$lang=$params['lang'];
182+
}
220183
(new Auth())->checkAuth($params['appKey']);
221-
$res = (new ParseMarkdown())->getContent($params['appKey'],$params['path']);
184+
$content = (new ParseMarkdown())->getContent($params['appKey'],$params['path'],$lang);
185+
$res = [
186+
'content'=>$content,
187+
];
222188
return Utils::showJson(0,"",$res);
223189

224190
} catch (ErrorException $e) {
@@ -227,29 +193,6 @@ public function getMdDetail(){
227193
}
228194

229195

230-
/**
231-
* 创建Crud
232-
* @return \think\response\Json
233-
*/
234-
public function createCrud()
235-
{
236-
if (!\think\facade\App::isDebug()) {
237-
throw new ErrorException("no debug",501);
238-
}
239-
$request = Request::instance();
240-
$params = $request->param();
241-
242-
if (!isset($params['appKey'])){
243-
throw new ErrorException("appkey not found");
244-
}
245-
(new Auth())->checkAuth($params['appKey']);
246-
$res = (new CreateCrud())->create($params);
247-
return Utils::showJson(0,"",$res);
248-
249-
250-
251-
}
252-
253196

254197

255198

0 commit comments

Comments
 (0)