|
@@ -64,6 +64,9 @@ class Api
|
|
|
*/
|
|
|
protected $responseType = 'json';
|
|
|
|
|
|
+ public $page = 1;
|
|
|
+ public $listrow = 10;
|
|
|
+
|
|
|
/**
|
|
|
* 构造方法
|
|
|
* @access public
|
|
@@ -72,9 +75,13 @@ class Api
|
|
|
public function __construct(Request $request = null)
|
|
|
{
|
|
|
$this->request = is_null($request) ? Request::instance() : $request;
|
|
|
+ $this->page = input('page',1);
|
|
|
+ $this->listrow= input('listrow',10);
|
|
|
|
|
|
// 控制器初始化
|
|
|
$this->_initialize();
|
|
|
+ //日志
|
|
|
+ $this->request_log();
|
|
|
|
|
|
// 前置操作方法
|
|
|
if ($this->beforeActionList) {
|
|
@@ -169,6 +176,23 @@ class Api
|
|
|
*/
|
|
|
protected function success($msg = '', $data = null, $code = 1, $type = null, array $header = [])
|
|
|
{
|
|
|
+ if($msg == 1){
|
|
|
+ $msg = 'success';
|
|
|
+ }
|
|
|
+ if(empty($msg)){
|
|
|
+ $msg = '操作成功';
|
|
|
+ }
|
|
|
+ $this->result($msg, $data, $code, $type, $header);
|
|
|
+ }
|
|
|
+ //find查询出来的结果如果为空数组,强制转换object
|
|
|
+ protected function success_find($msg = '', $data = null, $code = 1, $type = null, array $header = [])
|
|
|
+ {
|
|
|
+ if(empty($msg)){
|
|
|
+ $msg = '操作成功';
|
|
|
+ }
|
|
|
+ if(is_null($data) || $data === []){
|
|
|
+ $data = (object)[];
|
|
|
+ }
|
|
|
$this->result($msg, $data, $code, $type, $header);
|
|
|
}
|
|
|
|
|
@@ -182,6 +206,9 @@ class Api
|
|
|
*/
|
|
|
protected function error($msg = '', $data = null, $code = 0, $type = null, array $header = [])
|
|
|
{
|
|
|
+ if(empty($msg)){
|
|
|
+ $msg = __('Invalid parameters');
|
|
|
+ }
|
|
|
$this->result($msg, $data, $code, $type, $header);
|
|
|
}
|
|
|
|
|
@@ -204,6 +231,10 @@ class Api
|
|
|
'time' => Request::instance()->server('REQUEST_TIME'),
|
|
|
'data' => $data,
|
|
|
];
|
|
|
+
|
|
|
+ //日志
|
|
|
+ $this->request_log_update($result);
|
|
|
+
|
|
|
// 如果未设置类型则自动判断
|
|
|
$type = $type ? $type : ($this->request->param(config('var_jsonp_handler')) ? 'jsonp' : $this->responseType);
|
|
|
|
|
@@ -327,4 +358,37 @@ class Api
|
|
|
//刷新Token
|
|
|
$this->request->token();
|
|
|
}
|
|
|
+
|
|
|
+ /*
|
|
|
+ * api 请求日志
|
|
|
+ * */
|
|
|
+ protected function request_log(){
|
|
|
+ //api_request_log
|
|
|
+ $modulename = $this->request->module();
|
|
|
+ $controllername = $this->request->controller();
|
|
|
+ $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);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function request_log_update($log_result){
|
|
|
+ if(defined('API_REQUEST_ID')) { //记录app正常返回结果
|
|
|
+ if(strlen(json_encode($log_result['data'])) > 10000) {
|
|
|
+ $log_result['data'] = '数据太多,不记录';
|
|
|
+ }
|
|
|
+ db('api_request_log')->where('id',API_REQUEST_ID)->update(['result'=>json_encode($log_result)]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
}
|