|
@@ -2,7 +2,7 @@
|
|
|
|
|
|
namespace app\common\controller;
|
|
namespace app\common\controller;
|
|
|
|
|
|
-use app\common\library\Authcoach as Auth;
|
|
|
|
|
|
+use app\common\library\Authdoctor as Auth;
|
|
use think\Config;
|
|
use think\Config;
|
|
use think\exception\HttpResponseException;
|
|
use think\exception\HttpResponseException;
|
|
use think\exception\ValidateException;
|
|
use think\exception\ValidateException;
|
|
@@ -66,6 +66,11 @@ class Apic
|
|
protected $responseType = 'json';
|
|
protected $responseType = 'json';
|
|
|
|
|
|
/**
|
|
/**
|
|
|
|
+ * @var int 日志类型 1 文件;2sql
|
|
|
|
+ */
|
|
|
|
+ public $logType = 2;
|
|
|
|
+
|
|
|
|
+ /**
|
|
* 构造方法
|
|
* 构造方法
|
|
* @access public
|
|
* @access public
|
|
* @param Request $request Request 对象
|
|
* @param Request $request Request 对象
|
|
@@ -74,11 +79,23 @@ class Apic
|
|
{
|
|
{
|
|
$this->request = is_null($request) ? Request::instance() : $request;
|
|
$this->request = is_null($request) ? Request::instance() : $request;
|
|
|
|
|
|
|
|
+ if(config('site.apisite_switch') == 0){
|
|
|
|
+ $controllername = $this->request->controller();
|
|
|
|
+ $controllername = strtolower($controllername);
|
|
|
|
+
|
|
|
|
+ if(!in_array($controllername,['notify','easemob','payios'])){
|
|
|
|
+ $notice = config('site.apisite_notice') ?: '全站维护中';
|
|
|
|
+ $this->error($notice);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
|
|
// 控制器初始化
|
|
// 控制器初始化
|
|
$this->_initialize();
|
|
$this->_initialize();
|
|
//日志
|
|
//日志
|
|
$this->request_log();
|
|
$this->request_log();
|
|
|
|
+ //用户活跃
|
|
|
|
+// $this->user_active();
|
|
|
|
|
|
// 前置操作方法
|
|
// 前置操作方法
|
|
if ($this->beforeActionList) {
|
|
if ($this->beforeActionList) {
|
|
@@ -136,6 +153,10 @@ class Apic
|
|
// 如果有传递token才验证是否登录状态
|
|
// 如果有传递token才验证是否登录状态
|
|
if ($token) {
|
|
if ($token) {
|
|
$this->auth->init($token);
|
|
$this->auth->init($token);
|
|
|
|
+ //传就必须传对
|
|
|
|
+ if (!$this->auth->isLogin()) {
|
|
|
|
+ $this->error(__('Please login first'), null, 401);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -260,8 +281,8 @@ class Apic
|
|
//日志
|
|
//日志
|
|
$this->request_log_update($result);
|
|
$this->request_log_update($result);
|
|
|
|
|
|
- // 如果未设置类型则自动判断
|
|
|
|
- $type = $type ? $type : ($this->request->param(config('var_jsonp_handler')) ? 'jsonp' : $this->responseType);
|
|
|
|
|
|
+ // 如果未设置类型则使用默认类型判断
|
|
|
|
+ $type = $type ? : $this->responseType;
|
|
|
|
|
|
if (isset($header['statuscode'])) {
|
|
if (isset($header['statuscode'])) {
|
|
$code = $header['statuscode'];
|
|
$code = $header['statuscode'];
|
|
@@ -388,30 +409,60 @@ class Apic
|
|
* api 请求日志
|
|
* api 请求日志
|
|
* */
|
|
* */
|
|
protected function request_log(){
|
|
protected function request_log(){
|
|
|
|
+
|
|
//api_request_log
|
|
//api_request_log
|
|
$modulename = $this->request->module();
|
|
$modulename = $this->request->module();
|
|
$controllername = $this->request->controller();
|
|
$controllername = $this->request->controller();
|
|
$actionname = $this->request->action();
|
|
$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);
|
|
|
|
|
|
+ if(strtolower($actionname) == 'callback'){
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
|
|
|
|
+ defined('API_REQUEST_LOG_TYPE') or define('API_REQUEST_LOG_TYPE', $this->logType);
|
|
|
|
+ $params = $this->request->request();
|
|
|
|
+ if ($this->logType === 1){
|
|
|
|
+ //日志统一写入
|
|
|
|
+ register_shutdown_function([new LogUtil, 'close']);
|
|
|
|
+ LogUtil::getInstance('Api/'); //设置日志存入通道
|
|
|
|
+
|
|
|
|
+ LogUtil::info('uid', 'Api-Middleware-Log', 'request_log', $this->auth->id);
|
|
|
|
+ LogUtil::info('api', 'Api-Middleware-Log', 'request_log', $modulename . '/' . $controllername . '/' . $actionname);
|
|
|
|
+ LogUtil::info('params', 'Api-Middleware-Log', 'request_log', json_encode($params));
|
|
|
|
+ LogUtil::info('ip', 'Api-Middleware-Log', 'request_log', request()->ip());
|
|
|
|
+
|
|
|
|
+ }else{
|
|
|
|
+ $data = [
|
|
|
|
+ 'uid' => $this->auth->id,
|
|
|
|
+ 'api' => $modulename.'/'.$controllername.'/'.$actionname,
|
|
|
|
+ 'params' => json_encode($params),
|
|
|
|
+ '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){
|
|
protected function request_log_update($log_result){
|
|
- if(defined('API_REQUEST_ID')) { //记录app正常返回结果
|
|
|
|
- if(strlen(json_encode($log_result['data'])) > 10000) {
|
|
|
|
|
|
+ $actionname = $this->request->action();
|
|
|
|
+ if(strtolower($actionname) == 'givegifttoyou'){
|
|
|
|
+ //return true;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if ($this->logType === 1){
|
|
|
|
+ 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)]);
|
|
|
|
|
|
+ LogUtil::info('result', 'Api-Middleware-Log', 'request_log', $log_result);
|
|
|
|
+ }else{
|
|
|
|
+ if(defined('API_REQUEST_ID')) { //记录app正常返回结果
|
|
|
|
+ if(strlen(json_encode($log_result['data'])) > 1000) {
|
|
|
|
+ $log_result['data'] = '数据太多,不记录';
|
|
|
|
+ }
|
|
|
|
+ db('api_request_log')->where('id',API_REQUEST_ID)->update(['result'=>json_encode($log_result)]);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -441,6 +492,7 @@ class Apic
|
|
if($redisconfig['redis_selectdb'] > 0){
|
|
if($redisconfig['redis_selectdb'] > 0){
|
|
$redis->select($redisconfig['redis_selectdb']);
|
|
$redis->select($redisconfig['redis_selectdb']);
|
|
}
|
|
}
|
|
|
|
+
|
|
//
|
|
//
|
|
//指定键值新增+1 并获取
|
|
//指定键值新增+1 并获取
|
|
$count = $redis->incr($key);
|
|
$count = $redis->incr($key);
|