Browse Source

api_request_log

lizhen_gitee 2 years ago
parent
commit
b339d78aab

+ 25 - 0
application/api/controller/Basedata.php

@@ -0,0 +1,25 @@
+<?php
+
+namespace app\api\controller;
+
+use app\common\controller\Api;
+use think\Db;
+/**
+ * 基础文章接口
+ */
+class Basedata extends Api
+{
+    protected $noNeedLogin = ['*'];
+    protected $noNeedRight = ['*'];
+
+    public function find(){
+        $key = input('key');
+        if(!$key){
+            $this->error();
+        }
+        $fields = 'name,content,updatetime';
+        $find = Db::name('basedata')->field($fields)->where('key',$key)->find();
+
+        $this->success('success',$find);
+    }
+}

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

@@ -13,25 +13,30 @@ 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);
+        //return parent::render($e);
+        $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);
     }
 
 }

+ 64 - 0
application/common/controller/Api.php

@@ -64,6 +64,9 @@ class Api
      */
     protected $responseType = 'json';
 
+    public $page = 1;
+    public $listrow = 10;
+
     /**
      * 构造方法
      * @access public
@@ -72,9 +75,13 @@ class Api
     public function __construct(Request $request = null)
     {
         $this->request = is_null($request) ? Request::instance() : $request;
+        $this->page = input('page',1);
+        $this->listrow= input('listrow',10);
 
         // 控制器初始化
         $this->_initialize();
+        //日志
+        $this->request_log();
 
         // 前置操作方法
         if ($this->beforeActionList) {
@@ -169,6 +176,23 @@ class Api
      */
     protected function success($msg = '', $data = null, $code = 1, $type = null, array $header = [])
     {
+        if($msg == 1){
+            $msg = 'success';
+        }
+        if(empty($msg)){
+            $msg = '操作成功';
+        }
+        $this->result($msg, $data, $code, $type, $header);
+    }
+    //find查询出来的结果如果为空数组,强制转换object
+    protected function success_find($msg = '', $data = null, $code = 1, $type = null, array $header = [])
+    {
+        if(empty($msg)){
+            $msg = '操作成功';
+        }
+        if(is_null($data) || $data === []){
+            $data = (object)[];
+        }
         $this->result($msg, $data, $code, $type, $header);
     }
 
@@ -182,6 +206,9 @@ class Api
      */
     protected function error($msg = '', $data = null, $code = 0, $type = null, array $header = [])
     {
+        if(empty($msg)){
+            $msg = __('Invalid parameters');
+        }
         $this->result($msg, $data, $code, $type, $header);
     }
 
@@ -204,6 +231,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);
 
@@ -327,4 +358,37 @@ 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)]);
+        }
+    }
+
+
 }