123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421 |
- <?php
- // +----------------------------------------------------------------------
- // | ThinkPHP [ WE CAN DO IT JUST THINK ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
- // +----------------------------------------------------------------------
- // | Author: liu21st <liu21st@gmail.com>
- // +----------------------------------------------------------------------
-
- namespace app;
-
- // header('Access-Control-Allow-Origin:*');
- // header('Access-Control-Allow-Headers:x-requested-with,content-type,Authorization,token');
- // header('Access-Control-Allow-Methods:GET,POST,OPTIONS,DELETE,PUT');
- // header('Access-Control-Allow-Credentials:true');
-
- use think\App;
- use think\Container;
- use think\exception\ValidateException;
- use think\Response;
- use traits\controller\Jump;
-
- class BaseController
- {
-
- use Jump;
-
- /**
- * 视图类实例
- * @var \think\View
- */
- protected $view;
-
- /**
- * Request实例
- * @var \think\Request
- */
- protected $request;
-
- /**
- * 验证失败是否抛出异常
- * @var bool
- */
- protected $failException = false;
-
- /**
- * 是否批量验证
- * @var bool
- */
- protected $batchValidate = false;
-
- /**
- * 前置操作方法列表(即将废弃)
- * @var array $beforeActionList
- */
- protected $beforeActionList = [];
-
- /**
- * 控制器中间件
- * @var array
- */
- protected $middleware = [];
-
- /**
- * 无需签名
- * @var array
- */
- protected $noSize = [];
-
- /**
- * 用户信息
- * @var array
- */
- protected $userInfo = [];
-
- /**
- * 无需登录的方法,同时也就不需要鉴权了
- * @var array
- */
- protected $noNeedLogin = [];
-
- /**
- * 无需安全验证
- * @var array
- */
- protected $noSecure = [];
-
- protected $param = [];
-
- /**
- * 构造方法
- * @access public
- */
- public function __construct(App $app = null)
- {
- $this->app = $app ?: Container::get('app');
- $this->request = $this->app['request'];
- $this->view = $this->app['view'];
-
- $this->initialize();
-
- $this->registerMiddleware();
-
- // 前置操作方法 即将废弃
- foreach ((array)$this->beforeActionList as $method => $options) {
- is_numeric($method) ?
- $this->beforeAction($options) :
- $this->beforeAction($method, $options);
- }
- }
-
- //是否需要登录
- protected static function checklogin($arr)
- {
- if (isset($_SERVER['REQUEST_URI'])) {
- $action = $_SERVER['REQUEST_URI'];
- } else {
- return false;
- }
- $action = substr($action, strrpos($action, '/') + 1);
- if (strrpos($action, '?') !== false) {
- $action = substr($action, 0, strrpos($action, '?'));
- }
- $arr = is_array($arr) ? $arr : explode(',', $arr);
- if (!$arr) {
- return false;
- }
- $arr = array_map('strtolower', $arr);
- // 是否存在
- if (in_array(strtolower($action), $arr) || in_array('*', $arr)) {
- return true;
- }
- // 没找到匹配
- return false;
- }
-
- private function getParam($method)
- {
- $arr = [];
- switch ($method) {
- case 'post':
- $arr = $_POST;
- break;
- case 'get':
- $arr = $_GET;
- break;
- case 'delete':
- parse_str(file_get_contents('php://input'), $arr);
- break;
- case 'put':
- parse_str(file_get_contents('php://input'), $arr);
- break;
- case 'patch':
- parse_str(file_get_contents('php://input'), $arr);
- break;
- }
- return $arr;
- }
-
- // 初始化
- protected function initialize()
- {
- $function = request()->action(true);
- $module = request()->module();
- $controller = request()->controller();
- $classname = "\app\\$module\\validate\\$controller";
- if (!self::checklogin($this->noNeedLogin)) {//true为不用验证
- $controller_classname = "\app\\$module\\model\\$controller";
- $directory = new $controller_classname();
- if (method_exists($directory, $function)) {
- $token = $this->request->header();//获取请求头token
- if (!isset($token['token'])) {
- tojson(10001);
- } else {
- $this->userInfo = getToken($token['token']);
- }
- }
- }
- $method = $this->request->method();
- $method = strtolower($method);
- $param = $this->getParam($method);
- if (class_exists($classname)) {
- $validate = new $classname;
- if ($validate->hasScene($function)) {//验证场景
- if (!$validate->scene($function)->check($param)) {//验证器
- tojson('10000', $validate->getError());
- }
- }
- }
-
- //安全验证
- nosecure($method, $this->noSecure, $function);
-
- //验签
- verifysign($param, $this->noSize);//加上验签
-
- if (isset($param['page']) && !empty($param['page'])) {
- $param['page'] = $param['page'] - 1;
- if (!isset($param['pageSize'])) {
- $page = 0;
- $pageSize = 0;
- } else {
- $page = $param['pageSize'] * $param['page'];
- $pageSize = $param['pageSize'];
- }
- } else {
- $page = 0;
- $pageSize = 0;
- }
- $this->request->page = $page;
- $this->request->pageSize = $pageSize;
- $this->param = $param;
- }
-
- // 注册控制器中间件
- public function registerMiddleware()
- {
- foreach ($this->middleware as $key => $val) {
- if (!is_int($key)) {
- $only = $except = null;
-
- if (isset($val['only'])) {
- $only = array_map(function ($item) {
- return strtolower($item);
- }, $val['only']);
- } elseif (isset($val['except'])) {
- $except = array_map(function ($item) {
- return strtolower($item);
- }, $val['except']);
- }
-
- if (isset($only) && !in_array($this->request->action(), $only)) {
- continue;
- } elseif (isset($except) && in_array($this->request->action(), $except)) {
- continue;
- } else {
- $val = $key;
- }
- }
-
- $this->app['middleware']->controller($val);
- }
- }
-
- /**
- * 前置操作
- * @access protected
- * @param string $method 前置操作方法名
- * @param array $options 调用参数 ['only'=>[...]] 或者['except'=>[...]]
- */
- protected function beforeAction($method, $options = [])
- {
- if (isset($options['only'])) {
- if (is_string($options['only'])) {
- $options['only'] = explode(',', $options['only']);
- }
-
- $only = array_map(function ($val) {
- return strtolower($val);
- }, $options['only']);
-
- if (!in_array($this->request->action(), $only)) {
- return;
- }
- } elseif (isset($options['except'])) {
- if (is_string($options['except'])) {
- $options['except'] = explode(',', $options['except']);
- }
-
- $except = array_map(function ($val) {
- return strtolower($val);
- }, $options['except']);
-
- if (in_array($this->request->action(), $except)) {
- return;
- }
- }
-
- call_user_func([$this, $method]);
- }
-
- /**
- * 加载模板输出
- * @access protected
- * @param string $template 模板文件名
- * @param array $vars 模板输出变量
- * @param array $config 模板参数
- * @return mixed
- */
- protected function fetch($template = '', $vars = [], $config = [])
- {
- return Response::create($template, 'view')->assign($vars)->config($config);
- }
-
- /**
- * 渲染内容输出
- * @access protected
- * @param string $content 模板内容
- * @param array $vars 模板输出变量
- * @param array $config 模板参数
- * @return mixed
- */
- protected function display($content = '', $vars = [], $config = [])
- {
- return Response::create($content, 'view')->assign($vars)->config($config)->isContent(true);
- }
-
- /**
- * 模板变量赋值
- * @access protected
- * @param mixed $name 要显示的模板变量
- * @param mixed $value 变量的值
- * @return $this
- */
- protected function assign($name, $value = '')
- {
- $this->view->assign($name, $value);
-
- return $this;
- }
-
- /**
- * 视图过滤
- * @access protected
- * @param Callable $filter 过滤方法或闭包
- * @return $this
- */
- protected function filter($filter)
- {
- $this->view->filter($filter);
-
- return $this;
- }
-
- /**
- * 初始化模板引擎
- * @access protected
- * @param array|string $engine 引擎参数
- * @return $this
- */
- protected function engine($engine)
- {
- $this->view->engine($engine);
-
- return $this;
- }
-
- /**
- * 设置验证失败后是否抛出异常
- * @access protected
- * @param bool $fail 是否抛出异常
- * @return $this
- */
- protected function validateFailException($fail = true)
- {
- $this->failException = $fail;
-
- return $this;
- }
-
- /**
- * 验证数据
- * @access protected
- * @param array $data 数据
- * @param string|array $validate 验证器名或者验证规则数组
- * @param array $message 提示信息
- * @param bool $batch 是否批量验证
- * @param mixed $callback 回调方法(闭包)
- * @return array|string|true
- * @throws ValidateException
- */
- protected function validate($data, $validate, $message = [], $batch = false, $callback = null)
- {
- if (is_array($validate)) {
- $v = $this->app->validate();
- $v->rule($validate);
- } else {
- if (strpos($validate, '.')) {
- // 支持场景
- list($validate, $scene) = explode('.', $validate);
- }
- $v = $this->app->validate($validate);
- if (!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;
- }
-
- public function __debugInfo()
- {
- $data = get_object_vars($this);
- unset($data['app'], $data['request']);
-
- return $data;
- }
-
- }
|