Bläddra i källkod

api接口日志

lizhen_gitee 3 år sedan
förälder
incheckning
d2d761c82e

+ 2 - 2
application/admin/model/User.php

@@ -45,7 +45,7 @@ class User extends Model
         });
 
 
-        self::beforeUpdate(function ($row) {
+        /*self::beforeUpdate(function ($row) {
             $changedata = $row->getChangedData();
             $origin = $row->getOriginData();
             if (isset($changedata['money']) && (function_exists('bccomp') ? bccomp($changedata['money'], $origin['money'], 2) !== 0 : (double) $changedata['money'] !== (double) $origin['money'])) {
@@ -54,7 +54,7 @@ class User extends Model
             if (isset($changedata['score']) && (int) $changedata['score'] !== (int) $origin['score']) {
                 ScoreLog::create(['user_id' => $row['id'], 'score' => $changedata['score'] - $origin['score'], 'before' => $origin['score'], 'after' => $changedata['score'], 'memo' => '管理员变更积分']);
             }
-        });
+        });*/
     }
 
     public function getGenderList()

+ 21 - 17
application/api/library/ExceptionHandle.php

@@ -13,25 +13,29 @@ class ExceptionHandle extends Handle
 
     public function render(Exception $e)
     {
-        // 在生产环境下返回code信息
-        if (!\think\Config::get('app_debug')) {
-            $statuscode = $code = 500;
-            $msg = 'An error occurred';
-            // 验证异常
-            if ($e instanceof \think\exception\ValidateException) {
-                $code = 0;
-                $statuscode = 200;
-                $msg = $e->getError();
-            }
-            // Http异常
-            if ($e instanceof \think\exception\HttpException) {
-                $statuscode = $code = $e->getStatusCode();
-            }
-            return json(['code' => $code, 'msg' => $msg, 'time' => time(), 'data' => null], $statuscode);
+        $statuscode = $code = 500;
+        $msg = $e->getMessage();
+        // 验证异常
+        if ($e instanceof \think\exception\ValidateException) {
+            $code = 0;
+            $statuscode = 200;
+            $msg = $e->getError();
+
+        }
+        // Http异常
+        if ($e instanceof \think\exception\HttpException) {
+            $statuscode = $code = $e->getStatusCode();
+            $msg = $e->getMessage();
+        }
+
+        $result = ['code' => $code, 'msg' => $msg, 'time' => time(), 'data' => null];
+
+        //记录app异常返回结果
+        if(defined('API_REQUEST_ID')) { //记录app正常返回结果
+            db('api_request_log')->where('id',API_REQUEST_ID)->update(['result'=>json_encode($result)]);
         }
 
-        //其它此交由系统处理
-        return parent::render($e);
+        return json($result, $statuscode);
     }
 
 }

+ 39 - 1
application/common/controller/Api.php

@@ -64,6 +64,8 @@ class Api
      */
     protected $responseType = 'json';
 
+    public $page = 1;
+    public $limit = 10;
     /**
      * 构造方法
      * @access public
@@ -72,9 +74,11 @@ class Api
     public function __construct(Request $request = null)
     {
         $this->request = is_null($request) ? Request::instance() : $request;
-
+        $this->page = input('page',1);
         // 控制器初始化
         $this->_initialize();
+        //日志
+        $this->request_log();
 
         // 前置操作方法
         if ($this->beforeActionList) {
@@ -201,6 +205,10 @@ class Api
             'time' => Request::instance()->server('REQUEST_TIME'),
             'data' => $data,
         ];
+
+        //日志
+        $this->request_log_update($result);
+
         // 如果未设置类型则自动判断
         $type = $type ? $type : ($this->request->param(config('var_jsonp_handler')) ? 'jsonp' : $this->responseType);
 
@@ -324,4 +332,34 @@ class Api
         //刷新Token
         $this->request->token();
     }
+
+    /*
+     * api 请求日志
+     * */
+    protected function request_log(){
+        //api_request_log
+        $modulename     = $this->request->module();
+        $controllername = $this->request->controller();
+        $actionname     = $this->request->action();
+
+        $data = [
+            'uid'   => $this->auth->id,
+            'api'   => $modulename.'/'.$controllername.'/'.$actionname,
+            'params' => json_encode($this->request->request()),
+            'addtime'  => time(),
+            'adddatetime'  => date('Y-m-d H:i:s'),
+            'ip'   => request()->ip(),
+        ];
+        $request_id = db('api_request_log')->insertGetId($data);
+        defined('API_REQUEST_ID') or define('API_REQUEST_ID', $request_id);
+    }
+
+    protected function request_log_update($log_result){
+        if(defined('API_REQUEST_ID')) { //记录app正常返回结果
+            if(strlen(json_encode($log_result['data'])) > 10000) {
+                $log_result['data'] = '数据太多,不记录';
+            }
+            db('api_request_log')->where('id',API_REQUEST_ID)->update(['result'=>json_encode($log_result)]);
+        }
+    }
 }