145 lines
5.2 KiB
PHP
145 lines
5.2 KiB
PHP
<?php
|
||
|
||
namespace app\models\log;
|
||
|
||
use Yii;
|
||
use app\models\Model;
|
||
use app\components\SiteHelper;
|
||
use app\components\IPUtils;
|
||
|
||
/**
|
||
* This is the model class for table "{{%api_log}}".
|
||
*
|
||
* @property int $id ID
|
||
* @property int $cx_mch_id 平台商户ID
|
||
* @property int $user_id 用户ID
|
||
* @property string|null $route 路由
|
||
* @property string|null $module_id 模块ID
|
||
* @property string|null $controller_id 控制器ID
|
||
* @property string|null $action_id 动作ID
|
||
* @property string|null $url URL
|
||
* @property string|null $method 请求方法
|
||
* @property string|null $method_param 方法数据
|
||
* @property string|null $ipv4 IPV4
|
||
* @property string|null $ipv6 IPV6
|
||
* @property int $is_pass 是否拦截,0=否,1=是
|
||
* @property int $request_time 请求时间,毫秒
|
||
* @property int $response_time 响应时间,毫秒
|
||
* @property string $request_date 请求日期
|
||
* @property int $process_consume_time 请求处理耗时,毫秒
|
||
* @property int $is_delete 是否删除,0=否,1=是
|
||
* @property int $created_at 添加时间
|
||
*/
|
||
class ApiLog extends \yii\db\ActiveRecord
|
||
{
|
||
/**
|
||
* {@inheritdoc}
|
||
*/
|
||
public static function tableName()
|
||
{
|
||
return '{{%api_log}}';
|
||
}
|
||
|
||
/**
|
||
* {@inheritdoc}
|
||
*/
|
||
public function rules()
|
||
{
|
||
return [
|
||
[['cx_mch_id', 'user_id', 'is_pass', 'request_time', 'response_time', 'process_consume_time', 'is_delete', 'created_at'], 'integer'],
|
||
[['url', 'method_param'], 'string'],
|
||
[['request_date'], 'required'],
|
||
[['request_date'], 'safe'],
|
||
[['route'], 'string', 'max' => 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;
|
||
}
|
||
}
|