2048], [['module_id', 'controller_id', 'action_id'], 'string', 'max' => 256], [['method'], 'string', 'max' => 16], [['ipv4'], 'string', 'max' => 32], [['ipv6'], 'string', 'max' => 128], ]; } /** * {@inheritdoc} */ public function attributeLabels() { return [ 'id' => 'ID', 'cx_mch_id' => '平台商户ID', 'user_id' => '用户ID', 'route' => '路由', 'module_id' => '模块ID', 'controller_id' => '控制器ID', 'action_id' => '动作ID', 'url' => 'URL', 'method' => '请求方法', 'method_param' => '方法数据', 'ipv4' => 'IPV4', 'ipv6' => 'IPV6', 'is_pass' => '是否拦截,0=否,1=是', 'request_time' => '请求时间,毫秒', 'response_time' => '响应时间,毫秒', 'request_date' => '请求日期', 'process_consume_time' => '请求处理耗时,毫秒', 'is_delete' => '是否删除,0=否,1=是', 'created_at' => '添加时间', ]; } public function beforeSave($insert) { if(parent::beforeSave($insert)){ if($this->isNewRecord){ $this->created_at = time(); } return true; } else { return false; } } public static function logger($user_id, $request_time, $is_pass, $cx_mch_id = 0, $route = null, $module_id = null, $controller_id = null, $action_id = null, $url = null) { $model = new ApiLog(); $model->cx_mch_id = $cx_mch_id; $model->user_id = $user_id; $model->route = $route != null ? $route : \Yii::$app->requestedRoute; $model->module_id = $module_id != null ? $module_id : \Yii::$app->controller->module->id; $model->controller_id = $controller_id != null ? $controller_id : \Yii::$app->controller->id; $model->action_id = $action_id != null ? $action_id : \Yii::$app->controller->action->id; $model->url = $url != null ? $url : \Yii::$app->request->getUrl(); $model->method = \Yii::$app->request->method; if(\Yii::$app->request->isGet){ $method_param = \Yii::$app->request->get(); } else { $method_param = \Yii::$app->request->getBodyParams(); } $method_param = self::filterMethodParam($method_param); $model->method_param = json_encode($method_param,JSON_UNESCAPED_UNICODE); $model->ipv4 = IPUtils::getIp(); $model->is_pass = $is_pass; $model->request_date = date('Y-m-d', $request_time / 1000); $model->request_time = $request_time; $model->response_time = intval(microtime(true) * 1000); $model->process_consume_time = $model->response_time - $model->request_time; $model->is_delete = 0; if(!$model->save()){ return SiteHelper::getModelError($model); } return [ 'code' => 0, 'msg' => 'ok' ]; } public static function filterMethodParam($params) { //@TODO过滤参数 $black_list = ['X-Csrf', '_csrf', 'access_token', 'refresh_token', 'captcha_code', 'X-App-Platform']; foreach ($black_list as $i => $key){ if(isset($params[$key])) unset($params[$key]); } return $params; } }