|
@@ -14,6 +14,7 @@ use think\Response;
|
|
use think\Route;
|
|
use think\Route;
|
|
use think\Validate;
|
|
use think\Validate;
|
|
use Redis;
|
|
use Redis;
|
|
|
|
+use app\utils\LogUtil;
|
|
|
|
|
|
/**
|
|
/**
|
|
* API控制器基类
|
|
* API控制器基类
|
|
@@ -89,12 +90,13 @@ class Apiw
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
// 控制器初始化
|
|
// 控制器初始化
|
|
$this->_initialize();
|
|
$this->_initialize();
|
|
//日志
|
|
//日志
|
|
$this->request_log();
|
|
$this->request_log();
|
|
|
|
|
|
|
|
+
|
|
|
|
+
|
|
// 前置操作方法
|
|
// 前置操作方法
|
|
if ($this->beforeActionList) {
|
|
if ($this->beforeActionList) {
|
|
foreach ($this->beforeActionList as $method => $options) {
|
|
foreach ($this->beforeActionList as $method => $options) {
|
|
@@ -182,7 +184,6 @@ class Apiw
|
|
Lang::load(APP_PATH . $this->request->module() . '/lang/' . $lang . '/' . str_replace('.', '/', $name) . '.php');
|
|
Lang::load(APP_PATH . $this->request->module() . '/lang/' . $lang . '/' . str_replace('.', '/', $name) . '.php');
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
* 操作成功返回的数据
|
|
* 操作成功返回的数据
|
|
* @param string $msg 提示信息
|
|
* @param string $msg 提示信息
|
|
@@ -233,7 +234,7 @@ class Apiw
|
|
{
|
|
{
|
|
$result = [
|
|
$result = [
|
|
'code' => $code,
|
|
'code' => $code,
|
|
- 'msg' => __($msg),
|
|
|
|
|
|
+ 'msg' => $msg,
|
|
'time' => Request::instance()->server('REQUEST_TIME'),
|
|
'time' => Request::instance()->server('REQUEST_TIME'),
|
|
'data' => $data,
|
|
'data' => $data,
|
|
];
|
|
];
|
|
@@ -365,6 +366,48 @@ class Apiw
|
|
$this->request->token();
|
|
$this->request->token();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 接口请求限制
|
|
|
|
+ * @param int $apiLimit
|
|
|
|
+ * @param int $apiLimitTime
|
|
|
|
+ * @param string $key
|
|
|
|
+ * @return bool | true:通过 false:拒绝
|
|
|
|
+ */
|
|
|
|
+ public function apiLimit($apiLimit = 1, $apiLimitTime = 1000, $key = '')
|
|
|
|
+ {
|
|
|
|
+ $userId = $this->auth->id;
|
|
|
|
+ $controller = request()->controller();
|
|
|
|
+ $action = request()->action();
|
|
|
|
+
|
|
|
|
+ if (!$key) {
|
|
|
|
+ $key = strtolower($controller) . '_' . strtolower($action) . '_' . $userId;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ $redis = new Redis();
|
|
|
|
+ $redisconfig = config("redis");
|
|
|
|
+ $redis->connect($redisconfig["host"], $redisconfig["port"]);
|
|
|
|
+ if ($redisconfig['redis_pwd']) {
|
|
|
|
+ $redis->auth($redisconfig['redis_pwd']);
|
|
|
|
+ }
|
|
|
|
+ if($redisconfig['redis_selectdb'] > 0){
|
|
|
|
+ $redis->select($redisconfig['redis_selectdb']);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //
|
|
|
|
+ //指定键值新增+1 并获取
|
|
|
|
+ $count = $redis->incr($key);
|
|
|
|
+ if ($count > $apiLimit) {
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //设置过期时间
|
|
|
|
+ if ($count == 1) {
|
|
|
|
+ $redis->pExpire($key, $apiLimitTime);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+
|
|
/*
|
|
/*
|
|
* api 请求日志
|
|
* api 请求日志
|
|
* */
|
|
* */
|
|
@@ -406,67 +449,21 @@ class Apiw
|
|
}
|
|
}
|
|
|
|
|
|
protected function request_log_update($log_result){
|
|
protected function request_log_update($log_result){
|
|
- $actionname = $this->request->action();
|
|
|
|
- if(strtolower($actionname) == 'givegifttoyou'){
|
|
|
|
- //return true;
|
|
|
|
- }
|
|
|
|
|
|
|
|
if ($this->logType === 1){
|
|
if ($this->logType === 1){
|
|
if (strlen(json_encode($log_result['data'])) > 1000) {
|
|
if (strlen(json_encode($log_result['data'])) > 1000) {
|
|
- //$log_result['data'] = '数据太多,不记录';
|
|
|
|
|
|
+ $log_result['data'] = '数据太多,不记录';
|
|
}
|
|
}
|
|
LogUtil::info('result', 'Api-Middleware-Log', 'request_log', $log_result);
|
|
LogUtil::info('result', 'Api-Middleware-Log', 'request_log', $log_result);
|
|
}else{
|
|
}else{
|
|
if(defined('API_REQUEST_ID')) { //记录app正常返回结果
|
|
if(defined('API_REQUEST_ID')) { //记录app正常返回结果
|
|
if(strlen(json_encode($log_result['data'])) > 1000) {
|
|
if(strlen(json_encode($log_result['data'])) > 1000) {
|
|
- //$log_result['data'] = '数据太多,不记录';
|
|
|
|
|
|
+ $log_result['data'] = '数据太多,不记录';
|
|
}
|
|
}
|
|
db('api_request_log')->where('id',API_REQUEST_ID)->update(['result'=>json_encode($log_result)]);
|
|
db('api_request_log')->where('id',API_REQUEST_ID)->update(['result'=>json_encode($log_result)]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- /**
|
|
|
|
- * 接口请求限制
|
|
|
|
- * @param int $apiLimit
|
|
|
|
- * @param int $apiLimitTime
|
|
|
|
- * @param string $key
|
|
|
|
- * @return bool | true:通过 false:拒绝
|
|
|
|
- */
|
|
|
|
- public function apiLimit($apiLimit = 1, $apiLimitTime = 1000, $key = '')
|
|
|
|
- {
|
|
|
|
- $userId = $this->auth->id;
|
|
|
|
- $controller = request()->controller();
|
|
|
|
- $action = request()->action();
|
|
|
|
-
|
|
|
|
- if (!$key) {
|
|
|
|
- $key = strtolower($controller) . '_' . strtolower($action) . '_' . $userId;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- $redis = new Redis();
|
|
|
|
- $redisconfig = config("redis");
|
|
|
|
- $redis->connect($redisconfig["host"], $redisconfig["port"]);
|
|
|
|
- if ($redisconfig['redis_pwd']) {
|
|
|
|
- $redis->auth($redisconfig['redis_pwd']);
|
|
|
|
- }
|
|
|
|
- if($redisconfig['redis_selectdb'] > 0){
|
|
|
|
- $redis->select($redisconfig['redis_selectdb']);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- //
|
|
|
|
- //指定键值新增+1 并获取
|
|
|
|
- $count = $redis->incr($key);
|
|
|
|
- if ($count > $apiLimit) {
|
|
|
|
- return false;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- //设置过期时间
|
|
|
|
- if ($count == 1) {
|
|
|
|
- $redis->pExpire($key, $apiLimitTime);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- return true;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
|
|
|
|
}
|
|
}
|