You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
excel_handle/app/BaseController.php

182 lines
4.3 KiB

<?php
declare (strict_types = 1);
namespace app;
use Exception;
use think\App;
use think\exception\HttpResponseException;
use think\exception\ValidateException;
use think\facade\View;
use think\Validate;
/**
* 控制器基础类
*/
abstract class BaseController
{
/**
* Request实例
* @var \think\Request
*/
protected $request;
/**
* 应用实例
* @var \think\App
*/
protected $app;
/**
* 是否批量验证
* @var bool
*/
protected $batchValidate = false;
/**
* 控制器中间件
* @var array
*/
protected $middleware = [];
/**
* @var View
*/
protected $view;
/**
* @var array
*/
protected $layui_data;
/**
* @var array
*/
protected $json_data;
/**
* 构造方法
* @access public
* @param App $app 应用对象
*/
public function __construct(App $app)
{
$this->app = $app;
$this->request = $this->app->request;
$this->view = $this->app->view;
// 控制器初始化
$this->initialize();
}
// 初始化
protected function initialize()
{
if($this->request->isAjax() && $this->request->action() != 'login'){
$lastAction = session('last_action') ?: time();
if (session('login_time') === null || time() - $lastAction > 600) {
session('login_time', null);
$this->json_data['code'] = '-1';
$this->json_data['msg'] = '登录超时';
throw new HttpResponseException(json($this->json_data));
}else if($this->request->action() != 'process'){
session('last_action', time());
}
}
$this->layui_data = [
'code' => 0,
'msg' => '请求成功',
'count' => 0,
'data' => [],
];
$this->json_data = ['code' => 1, 'msg' => '操作成功'];
}
/**
* 验证数据
* @access protected
* @param array $data 数据
* @param string|array $validate 验证器名或者验证规则数组
* @param array $message 提示信息
* @param bool $batch 是否批量验证
* @return array|string|true
* @throws ValidateException
*/
protected function validate(array $data, $validate, array $message = [], bool $batch = false)
{
if (is_array($validate)) {
$v = new Validate();
$v->rule($validate);
} else {
if (strpos($validate, '.')) {
// 支持场景
[$validate, $scene] = explode('.', $validate);
}
$class = false !== strpos($validate, '\\') ? $validate : $this->app->parseClass('validate', $validate);
$v = new $class();
if (!empty($scene)) {
$v->scene($scene);
}
}
$v->message($message);
// 是否批量验证
if ($batch || $this->batchValidate) {
$v->batch(true);
}
return $v->failException(true)->check($data);
}
/**
* 加载模板输出
* @access protected
* @param string $template 模板文件名
* @param array $vars 模板输出变量
* @return string
* @throws Exception
*/
protected function fetch($template = '', $vars = [])
{
return $this->view->fetch($template, $vars);
}
/**
* 渲染内容输出
* @access protected
* @param string $content 模板内容
* @param array $vars 模板输出变量
* @return mixed
*/
protected function display($content = '', $vars = [])
{
return $this->view->display($content, $vars);
}
/**
* 模板变量赋值
* @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 array|string $engine 引擎参数
* @return $this
*/
protected function engine($engine)
{
$this->view->engine($engine);
return $this;
}
}