|
@@ -254,7 +254,7 @@ class Apic
|
|
|
{
|
|
|
$result = [
|
|
|
'code' => $code,
|
|
|
- 'msg' => $msg,
|
|
|
+ 'msg' => __($msg),
|
|
|
'time' => Request::instance()->server('REQUEST_TIME'),
|
|
|
'data' => $data,
|
|
|
];
|
|
@@ -417,5 +417,45 @@ class Apic
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 接口请求限制
|
|
|
+ * @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']);
|
|
|
+ }
|
|
|
+ $check = $redis->exists($key);
|
|
|
+ if ($check) {
|
|
|
+ $redis->incr($key);
|
|
|
+ $count = $redis->get($key);
|
|
|
+ if ($count > $apiLimit) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ $redis->incr($key);
|
|
|
+ $redis->pExpire($key, $apiLimitTime);
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
}
|