@@ -224,14 +224,8 @@ protected function echoJson(int $code, string $enmsg, ?string $cnmsg, $data): vo
224224 'cnmsg ' => &$ cnmsg ,
225225 'data ' => &$ data
226226 ]));
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- }
231227 if (isset ($ this ->encrypt_key )) {
232228 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 ' );
235229 $ this ->swoole_http_response ->header ('Mango-Response-Crypt ' , 'On ' );
236230 $ echo = base64_encode (openssl_encrypt ($ echo , 'aes-128-cbc ' , $ this ->encrypt_key , OPENSSL_RAW_DATA ,
237231 '1234567890123456 ' ));
@@ -246,7 +240,6 @@ protected function echoJson(int $code, string $enmsg, ?string $cnmsg, $data): vo
246240 }
247241 }
248242 } else {
249- $ this ->swoole_http_response ->header ('Access-Control-Allow-Headers ' , 'Content-Type, Mango-Request-Rand ' );
250243 $ this ->swoole_http_response ->header ('Content-Type ' , 'application/json ' );
251244 $ this ->endRequest ($ echo );
252245 }
@@ -265,7 +258,8 @@ protected function echoJson(int $code, string $enmsg, ?string $cnmsg, $data): vo
265258 }
266259 }
267260 }
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 {
269263 if ($ this ->response_finished ) {
270264 return ;
271265 }
@@ -313,7 +307,42 @@ public function deleteCookie(string $key, string $path = '/'): void {
313307 protected function getAction (string $ classname ): string {
314308 return substr ($ classname , strpos ($ classname , "\\" ) + 1 );
315309 }
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 {
317346 if ($ this ->response_finished ) {
318347 return false ;
319348 }
@@ -336,7 +365,7 @@ protected function E_404(): void {
336365 }
337366 protected function E_500 (): void {
338367 $ 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> ' );
340369 $ this ->json_response_code = 500 ;
341370 $ this ->response_finished = true ;
342371 }
0 commit comments