<?php
// +----------------------------------------------------------------------
// | SentCMS [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.tensent.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: molong <molong@tensent.cn> <http://www.tensent.cn>
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace app\middleware;

use app\services\auth\UsersLogService;

class Api{

	/**
	 * 处理请求
	 *
	 * @param \think\Request $request
	 * @param \Closure       $next
	 * @return Response
	 */
	public function handle($request, \Closure $next){
		$request->pageConfig = array(
			'list_rows' => $request->param('pageSize', 30),
			'page'      => $request->param('page', 1),
		);
		$response = $next($request);
		
		//记录用户操作记录
		app()->make(UsersLogService::class)->record($request, $response->getCode());
		if ($request->isAjax() || is_array($response->getData())) {
			return json($response->getData());
		} else {
			return $response;
		}
	}
}
