@@ -224,14 +224,8 @@ protected function echoJson(int $code, string $enmsg, ?string $cnmsg, $data): vo
224
224
'cnmsg ' => &$ cnmsg ,
225
225
'data ' => &$ data
226
226
]));
227
- if (IS_DEV ) {
228
- $ this ->swoole_http_response ->header ('Access-Control-Expose-Headers ' , 'Mango-Response-Crypt ' );
229
- $ this ->swoole_http_response ->header ('Access-Control-Allow-Origin ' , '* ' );
230
- }
231
227
if (isset ($ this ->encrypt_key )) {
232
228
if (strlen ($ echo ) < 256 ) {
233
- $ this ->swoole_http_response ->header ('Access-Control-Allow-Headers ' ,
234
- 'Rsa-Certificate-Id, Mango-Rsa-Cert, Mango-Request-Rand, Content-Type ' );
235
229
$ this ->swoole_http_response ->header ('Mango-Response-Crypt ' , 'On ' );
236
230
$ echo = base64_encode (openssl_encrypt ($ echo , 'aes-128-cbc ' , $ this ->encrypt_key , OPENSSL_RAW_DATA ,
237
231
'1234567890123456 ' ));
@@ -246,7 +240,6 @@ protected function echoJson(int $code, string $enmsg, ?string $cnmsg, $data): vo
246
240
}
247
241
}
248
242
} else {
249
- $ this ->swoole_http_response ->header ('Access-Control-Allow-Headers ' , 'Content-Type, Mango-Request-Rand ' );
250
243
$ this ->swoole_http_response ->header ('Content-Type ' , 'application/json ' );
251
244
$ this ->endRequest ($ echo );
252
245
}
@@ -265,7 +258,8 @@ protected function echoJson(int $code, string $enmsg, ?string $cnmsg, $data): vo
265
258
}
266
259
}
267
260
}
268
- public function jsonResponse (?array $ data = null , ?string $ enmsg = 'ok ' , ?string $ cnmsg = '成功 ' , int $ code = 200 ): void {
261
+ public function jsonResponse (?array $ data = null , ?string $ enmsg = 'ok ' , ?string $ cnmsg = '成功 ' ,
262
+ int $ code = 200 ): void {
269
263
if ($ this ->response_finished ) {
270
264
return ;
271
265
}
@@ -313,7 +307,42 @@ public function deleteCookie(string $key, string $path = '/'): void {
313
307
protected function getAction (string $ classname ): string {
314
308
return substr ($ classname , strpos ($ classname , "\\" ) + 1 );
315
309
}
316
- protected function redirect (string $ path , bool $ visible = false ) {
310
+ protected function renderTemplate (string $ template , array $ variable = [],
311
+ ?string $ content_type = 'text/html; charset=UTF-8 ' ,
312
+ ?string $ cache_control = 'no-store ' ): void {
313
+ if ($ this ->response_finished ) {
314
+ return ;
315
+ }
316
+ if (! file_exists ($ template )) {
317
+ $ this ->E_404 ();
318
+ return ;
319
+ }
320
+ foreach ($ variable as $ k => &$ v )
321
+ $ {$ k } = $ v ;
322
+ unset($ v );
323
+ ob_start ();
324
+ try {
325
+ include $ template ;
326
+ $ content = ob_get_clean ();
327
+ if (false === $ content ) {
328
+ throw new \Exception ('Output buffering not active ' );
329
+ }
330
+ if (isset ($ content_type )) {
331
+ $ this ->swoole_http_response ->header ('Content-Type ' , $ content_type );
332
+ }
333
+ if (isset ($ cache_control )) {
334
+ $ this ->swoole_http_response ->header ('Cache-Control ' , $ cache_control );
335
+ }
336
+ $ this ->swoole_http_response ->end ($ content );
337
+ $ this ->response_finished = true ;
338
+ $ this ->json_response_code ??= 200 ;
339
+ } catch (\Throwable $ e ) {
340
+ ob_end_clean ();
341
+ $ this ->E_500 ();
342
+ throw $ e ;
343
+ }
344
+ }
345
+ protected function redirect (string $ path , bool $ visible = false ): bool {
317
346
if ($ this ->response_finished ) {
318
347
return false ;
319
348
}
@@ -336,7 +365,7 @@ protected function E_404(): void {
336
365
}
337
366
protected function E_500 (): void {
338
367
$ this ->swoole_http_response ->status (500 );
339
- $ this ->swoole_http_response ->end ('<html><head><title>500 Internal Server</title></head><body bgcolor="white"><center><h1>500 Internal Server</h1></center><hr><center>nginx</center></body></html> ' );
368
+ $ this ->swoole_http_response ->end ('<html><head><title>500 Internal Server Error </title></head><body bgcolor="white"><center><h1>500 Internal Server Error </h1></center><hr><center>nginx</center></body></html> ' );
340
369
$ this ->json_response_code = 500 ;
341
370
$ this ->response_finished = true ;
342
371
}
0 commit comments