11
11
use think \App ;
12
12
use think \facade \Config ;
13
13
use think \facade \Request ;
14
- use hg \apidoc \crud \CreateCrud ;
15
14
16
15
class Controller
17
16
{
18
17
protected $ app ;
19
18
20
19
protected $ config ;
21
20
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
- ];
68
21
69
22
public function __construct (App $ app )
70
23
{
71
24
$ this ->app = $ app ;
72
25
$ this ->config = Config::get ("apidoc " )?Config::get ("apidoc " ):Config::get ("apidoc. " );
26
+
73
27
}
74
28
75
29
/**
@@ -79,45 +33,22 @@ public function __construct(App $app)
79
33
public function getConfig (){
80
34
$ config = $ this ->config ;
81
35
if (!empty ($ config ['auth ' ])){
82
- unset($ config ['auth ' ]['auth_password ' ]);
83
36
unset($ config ['auth ' ]['password ' ]);
84
37
unset($ config ['auth ' ]['key ' ]);
85
38
}
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 ' ]);
105
44
}
45
+ $ config ['headers ' ] = Utils::getArrayLang ($ config ['headers ' ],"desc " );
46
+ $ config ['parameters ' ] = Utils::getArrayLang ($ config ['parameters ' ],"desc " );
47
+ $ config ['responses ' ] = Utils::getArrayLang ($ config ['responses ' ],"desc " );
106
48
107
- $ config ['debug ' ]=app ()->isDebug ();
108
49
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
- }
119
50
// 清除apps配置中的password
120
- $ config ['apps ' ] = (new Utils ())->handleAppsConfig ($ config ['apps ' ]);
51
+ $ config ['apps ' ] = (new Utils ())->handleAppsConfig ($ config ['apps ' ], true );
121
52
return Utils::showJson (0 ,"" ,$ config );
122
53
}
123
54
@@ -142,7 +73,10 @@ public function verifyAuth(){
142
73
}
143
74
try {
144
75
$ hasAuth = (new Auth ())->verifyAuth ($ password ,$ appKey );
145
- return Utils::showJson (0 ,"" ,$ hasAuth );
76
+ $ res = [
77
+ "token " =>$ hasAuth
78
+ ];
79
+ return Utils::showJson (0 ,"" ,$ res );
146
80
} catch (AuthException $ e ) {
147
81
return Utils::showJson ($ e ->getCode (),$ e ->getMessage ());
148
82
}
@@ -158,67 +92,99 @@ public function getApidoc(){
158
92
$ config = $ this ->config ;
159
93
$ request = Request::instance ();
160
94
$ 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 );
162
115
163
116
$ cacheData =null ;
164
117
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 ;
168
122
}else {
169
123
// 生成数据并缓存
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 );
172
126
}
173
127
}else {
174
128
// 生成数据
175
- $ apiData = (new ParseAnnotation ())->renderApiData ($ params [ ' appKey ' ] );
129
+ $ apiData = (new ParseAnnotation ())->renderApiData ($ appKey );
176
130
}
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 ' ];
192
137
}
138
+ $ groups =!empty ($ currentApp ['groups ' ])?$ currentApp ['groups ' ]:[];
193
139
$ json =[
140
+ 'data ' =>$ data ,
141
+ 'app ' =>$ currentApp ,
194
142
'groups ' =>$ groups ,
195
- 'list ' =>$ apiData ['data ' ],
196
- 'docs ' =>$ docs ,
197
- 'tags ' =>$ apiData ['tags ' ]
143
+ 'tags ' =>$ apiData ['tags ' ],
198
144
];
199
- if ($ cacheData && !empty ($ cacheData ['list ' ])){
200
- $ json ['cacheFiles ' ]=$ cacheData ['list ' ];
201
- $ json ['cacheName ' ]=$ cacheData ['name ' ];
202
- }
145
+
203
146
return Utils::showJson (0 ,"" ,$ json );
204
147
}
205
148
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
+
206
162
/**
207
163
* 获取md文档内容
208
164
* @return \think\response\Json
209
165
*/
210
166
public function getMdDetail (){
211
167
$ request = Request::instance ();
212
168
$ params = $ request ->param ();
169
+ if (!empty ($ params ['lang ' ])){
170
+ \think \facade \App::loadLangPack ($ params ['lang ' ]);
171
+ }
213
172
try {
214
173
if (empty ($ params ['path ' ])){
215
174
throw new ErrorException ("mdPath not found " );
216
175
}
217
176
if (empty ($ params ['appKey ' ])){
218
177
throw new ErrorException ("appkey not found " );
219
178
}
179
+ $ lang ="" ;
180
+ if (!empty ($ params ['lang ' ])){
181
+ $ lang =$ params ['lang ' ];
182
+ }
220
183
(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
+ ];
222
188
return Utils::showJson (0 ,"" ,$ res );
223
189
224
190
} catch (ErrorException $ e ) {
@@ -227,29 +193,6 @@ public function getMdDetail(){
227
193
}
228
194
229
195
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
-
253
196
254
197
255
198
0 commit comments