<?php
namespace app\common\controller;
use app\common\library\Auth;
use think\Config;
use think\exception\HttpResponseException;
use think\exception\ValidateException;
use think\Hook;
use think\Lang;
use think\Loader;
use think\Request;
use think\Response;
class Api
{
protected $request;
protected $failException = false;
protected $batchValidate = false;
protected $beforeActionList = [];
protected $noNeedLogin = [];
protected $noNeedRight = [];
protected $auth = null;
protected $responseType = 'json';
public function __construct(Request $request = null)
{
$this->request = is_null($request) ? Request::instance() : $request;
$this->_initialize();
if ($this->beforeActionList)
{
foreach ($this->beforeActionList as $method => $options)
{
is_numeric($method) ?
$this->beforeAction($options) :
$this->beforeAction($method, $options);
}
}
}
protected function _initialize()
{
$this->request->filter('strip_tags');
$this->auth = Auth::instance();
$modulename = $this->request->module();
$controllername = strtolower($this->request->controller());
$actionname = strtolower($this->request->action());
$token = $this->request->server('HTTP_TOKEN', $this->request->request('token', \think\Cookie::get('token')));
$path = str_replace('.', '/', $controllername) . '/' . $actionname;
$this->auth->setRequestUri($path);
if (!$this->auth->match($this->noNeedLogin))
{
$this->auth->init($token);
if (!$this->auth->isLogin())
{
$this->error(__('Please login first'), null, 401);
}
if (!$this->auth->match($this->noNeedRight))
{
if (!$this->auth->check($path))
{
$this->error(__('You have no permission'), null, 403);
}
}
}
else
{
if ($token)
{
$this->auth->init($token);
}
}
$upload = \app\common\model\Config::upload();
Hook::listen("upload_config_init", $upload);
Config::set('upload', array_merge(Config::get('upload'), $upload));
$this->loadlang($controllername);
}
protected function loadlang($name)
{
Lang::load(APP_PATH . $this->request->module() . '/lang/' . $this->request->langset() . '/' . str_replace('.', '/', $name) . '.php');
}
protected function success($msg = '', $data = null, $code = 1, $type = null, array $header = [])
{
$this->result($msg, $data, $code, $type, $header);
}
protected function error($msg = '', $data = null, $code = 0, $type = null, array $header = [])
{
$this->result($msg, $data, $code, $type, $header);
}
protected function result($msg, $data = null, $code = 0, $type = null, array $header = [])
{
$result = [
'code' => $code,
'msg' => $msg,
'time' => Request::instance()->server('REQUEST_TIME'),
'data' => $data,
];
$type = $type ? $type : ($this->request->param(config('var_jsonp_handler')) ? 'jsonp' : $this->responseType);
if (isset($header['statuscode']))
{
$code = $header['statuscode'];
unset($header['statuscode']);
}
else
{
$code = $code >= 1000 || $code < 200 ? 200 : $code;
}
$response = Response::create($result, $type, $code)->header($header);
throw new HttpResponseException($response);
}
protected function beforeAction($method, $options = [])
{
if (isset($options['only']))
{
if (is_string($options['only']))
{
$options['only'] = explode(',', $options['only']);
}
if (!in_array($this->request->action(), $options['only']))
{
return;
}
}
elseif (isset($options['except']))
{
if (is_string($options['except']))
{
$options['except'] = explode(',', $options['except']);
}
if (in_array($this->request->action(), $options['except']))
{
return;
}
}
call_user_func([$this, $method]);
}
protected function validateFailException($fail = true)
{
$this->failException = $fail;
return $this;
}
protected function validate($data, $validate, $message = [], $batch = false, $callback = null)
{
if (is_array($validate))
{
$v = Loader::validate();
$v->rule($validate);
}
else
{
if (strpos($validate, '.'))
{
list($validate, $scene) = explode('.', $validate);
}
$v = Loader::validate($validate);
!empty($scene) && $v->scene($scene);
}
if ($batch || $this->batchValidate)
$v->batch(true);
if (is_array($message))
$v->message($message);
if ($callback && is_callable($callback))
{
call_user_func_array($callback, [$v, &$data]);
}
if (!$v->check($data))
{
if ($this->failException)
{
throw new ValidateException($v->getError());
}
return $v->getError();
}
return true;
}
}