瀏覽代碼

异常处理

linwu 3 周之前
父節點
當前提交
a04de3a7e0
共有 3 個文件被更改,包括 65 次插入6 次删除
  1. 7 5
      app/ExceptionHandle.php
  2. 1 1
      config/app.php
  3. 57 0
      public/static/tpl/think_exception.tpl

+ 7 - 5
app/ExceptionHandle.php

@@ -1,4 +1,5 @@
 <?php
+
 namespace app;
 
 use think\exception\Handle;
@@ -12,17 +13,18 @@ use think\exception\HttpException;
  */
 class ExceptionHandle extends Handle
 {
-    public function render($request, Throwable $e): Response {
+    public function render($request, Throwable $e): Response
+    {
         // 调试模式
-        if (env('app_debug')){
+        if (env('app_debug')) {
             // Ajax请求返回JSON
             if ($request->isAjax()) {
                 if ($e instanceof HttpResponseException) {
                     return $e->getResponse();
                 } elseif ($e instanceof HttpException) {
-                    return json(['code'=>$e->getStatusCode(),'msg'=>$e->getMessage()],$e->getStatusCode());
+                    return json(['code' => $e->getStatusCode(), 'msg' => $e->getMessage()], $e->getStatusCode());
                 } else {
-                    return json(['code' => 500, 'msg' => $e->getMessage(),'trace'=>$e->getTrace()],500);
+                    return json(['code' => 500, 'msg' => $e->getMessage(), 'trace' => $e->getTrace()], 500);
                 }
             }
 
@@ -30,7 +32,7 @@ class ExceptionHandle extends Handle
             return parent::render($request, $e);
         } else {
             if ($request->isAjax()) {
-                return json(['code'=>500,'msg'=>'服务器异常'],500);
+                return json(['code' => 500, 'msg' => '服务器升级中,请稍后重试~~'], 500);
             } else {
                 return parent::render($request, $e);
             }

+ 1 - 1
config/app.php

@@ -25,7 +25,7 @@ return [
     'deny_app_list'    => [],
 
     // 异常页面的模板文件
-    'exception_tmpl'   => app()->getThinkPath() . 'tpl/think_exception.tpl',
+    'exception_tmpl'   => env('app_debug') ? app()->getThinkPath() . 'tpl/think_exception.tpl' : app()->getRootPath() . '/public/static/tpl/think_exception.tpl',
 
     // 错误显示信息,非调试模式有效
     'error_message'    => '页面错误!请稍后再试~',

+ 57 - 0
public/static/tpl/think_exception.tpl

@@ -0,0 +1,57 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <title>该页面不存在-404.life</title>
+    <style>
+        *{
+            margin: 0;
+            padding: 0;
+            box-sizing: border-box;
+            color: #F1F1F1;
+        }
+
+        body{
+            font-family: Verdana, Geneva, Tahoma, sans-serif;
+            background: rgb(238,174,202);
+            background: radial-gradient(circle, rgba(238,174,202,1) 0%, rgba(148,187,233,1) 100%);
+        }
+        .container{
+            height : 100vh;
+            width: 80vw;
+            margin: 0 auto;
+            display: flex;
+            align-items: center;
+            justify-content: center;
+            flex-direction: column;
+            text-align: center;
+        }
+
+        .container .num {
+            font-size: 8rem;
+            margin-bottom: 40px;
+        }
+        .container .stg {
+            font-size: 3rem;
+            margin-bottom: 40px;
+            animation: .7s ease-in-out show;
+        }
+
+        @keyframes show {
+            0%{
+                opacity : 0;
+            }100%{
+                 opacity: 1;
+             }
+        }
+    </style>
+</head>
+<body>
+<div class="container">
+    <div class="display">
+        <h1 class="num">404</h1>
+        <h1 class="stg">呀!这个页面走丢了</h1>
+    </div>
+</div>
+</body>
+</html>