<?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;

class Api{
	/**
	 * 处理请求
	 *
	 * @param \think\Request $request
	 * @param \Closure       $next
	 * @return Response
	 */
	public function handle($request, \Closure $next){
		$response = $next($request);

		if (is_array($response->getData())) {
			$this->data = array_merge($this->data, $response->getData());
		} else {
			$this->data = $response->getData();
		}

		if ($request->isAjax()) {
			return json($this->data);
		} else {
			if (\is_string($this->data) && $this->data != '') {
				return $response;
			} else {
				return json($this->data);
			}
		}
	}
}
