336 lines
12 KiB
PHP
336 lines
12 KiB
PHP
<?php
|
|
|
|
namespace app\common\controller;
|
|
|
|
use app\common\library\Auth;
|
|
use think\Config;
|
|
use think\Controller;
|
|
use think\Hook;
|
|
use think\Lang;
|
|
use think\Loader;
|
|
use think\Validate;
|
|
|
|
/**
|
|
* 前台控制器基类
|
|
*/
|
|
class Frontend extends Controller
|
|
{
|
|
|
|
/**
|
|
* 布局模板
|
|
* @var string
|
|
*/
|
|
protected $layout = '';
|
|
|
|
/**
|
|
* 无需登录的方法,同时也就不需要鉴权了
|
|
* @var array
|
|
*/
|
|
protected $noNeedLogin = [];
|
|
protected $searchFields = 'id';
|
|
/**
|
|
* 无需鉴权的方法,但需要登录
|
|
* @var array
|
|
*/
|
|
protected $noNeedRight = [];
|
|
|
|
/**
|
|
* 权限Auth
|
|
* @var Auth
|
|
*/
|
|
protected $auth = null;
|
|
|
|
public function _initialize()
|
|
{
|
|
//移除HTML标签
|
|
$this->request->filter('trim,strip_tags,htmlspecialchars');
|
|
$modulename = $this->request->module();
|
|
$controllername = Loader::parseName($this->request->controller());
|
|
$actionname = strtolower($this->request->action());
|
|
|
|
// 检测IP是否允许
|
|
check_ip_allowed();
|
|
|
|
// 如果有使用模板布局
|
|
if ($this->layout) {
|
|
$this->view->engine->layout('layout/' . $this->layout);
|
|
}
|
|
$this->auth = Auth::instance();
|
|
|
|
// token
|
|
$token = $this->request->server('HTTP_TOKEN', $this->request->request('token', \think\Cookie::get('token')));
|
|
|
|
$path = str_replace('.', '/', $controllername) . '/' . $actionname;
|
|
// 设置当前请求的URI
|
|
$this->auth->setRequestUri($path);
|
|
// 检测是否需要验证登录
|
|
if (!$this->auth->match($this->noNeedLogin)) {
|
|
//初始化
|
|
$this->auth->init($token);
|
|
//检测是否登录
|
|
if (!$this->auth->isLogin()) {
|
|
$this->error(__('Please login first'), 'index/user/login');
|
|
}
|
|
// 判断是否需要验证权限
|
|
if (!$this->auth->match($this->noNeedRight)) {
|
|
// 判断控制器和方法判断是否有对应权限
|
|
if (!$this->auth->check($path)) {
|
|
$this->error(__('You have no permission'));
|
|
}
|
|
}
|
|
} else {
|
|
// 如果有传递token才验证是否登录状态
|
|
if ($token) {
|
|
$this->auth->init($token);
|
|
}
|
|
}
|
|
|
|
$this->view->assign('user', $this->auth->getUser());
|
|
|
|
// 语言检测
|
|
$lang = $this->request->langset();
|
|
$lang = preg_match("/^([a-zA-Z\-_]{2,10})\$/i", $lang) ? $lang : 'zh-cn';
|
|
|
|
$site = Config::get("site");
|
|
|
|
$upload = \app\common\model\Config::upload();
|
|
|
|
// 上传信息配置后
|
|
Hook::listen("upload_config_init", $upload);
|
|
|
|
// 配置信息
|
|
$config = [
|
|
'site' => array_intersect_key($site, array_flip(['name', 'cdnurl', 'version', 'timezone', 'languages'])),
|
|
'upload' => $upload,
|
|
'modulename' => $modulename,
|
|
'controllername' => $controllername,
|
|
'actionname' => $actionname,
|
|
'jsname' => 'frontend/' . str_replace('.', '/', $controllername),
|
|
'moduleurl' => rtrim(url("/{$modulename}", '', false), '/'),
|
|
'language' => $lang
|
|
];
|
|
$config = array_merge($config, Config::get("view_replace_str"));
|
|
|
|
Config::set('upload', array_merge(Config::get('upload'), $upload));
|
|
|
|
// 配置信息后
|
|
Hook::listen("config_init", $config);
|
|
// 加载当前控制器语言包
|
|
$this->loadlang($controllername);
|
|
$this->assign('site', $site);
|
|
$this->assign('config', $config);
|
|
}
|
|
|
|
/**
|
|
* 加载语言文件
|
|
* @param string $name
|
|
*/
|
|
protected function loadlang($name)
|
|
{
|
|
$name = Loader::parseName($name);
|
|
$name = preg_match("/^([a-zA-Z0-9_\.\/]+)\$/i", $name) ? $name : 'index';
|
|
$lang = $this->request->langset();
|
|
$lang = preg_match("/^([a-zA-Z\-_]{2,10})\$/i", $lang) ? $lang : 'zh-cn';
|
|
Lang::load(APP_PATH . $this->request->module() . '/lang/' . $lang . '/' . str_replace('.', '/', $name) . '.php');
|
|
}
|
|
protected function buildparams($userid = null,$searchfields = null, $relationSearch = null)
|
|
{
|
|
$searchfields = is_null($searchfields) ? $this->searchFields : $searchfields;
|
|
$relationSearch = is_null($relationSearch) ? $this->relationSearch : $relationSearch;
|
|
$search = $this->request->get("search", '');
|
|
$filter = $this->request->get("filter", '');
|
|
$op = $this->request->get("op", '', 'trim');
|
|
$sort = $this->request->get("sort", !empty($this->model) && $this->model->getPk() ? $this->model->getPk() : 'id');
|
|
$order = $this->request->get("order", "DESC");
|
|
$offset = $this->request->get("offset/d", 0);
|
|
$limit = $this->request->get("limit/d", 999999);
|
|
//新增自动计算页码
|
|
$page = $limit ? intval($offset / $limit) + 1 : 1;
|
|
if ($this->request->has("page")) {
|
|
$page = $this->request->get("page/d", 1);
|
|
}
|
|
$this->request->get([config('paginate.var_page') => $page]);
|
|
$filter = (array)json_decode($filter, true);
|
|
$op = (array)json_decode($op, true);
|
|
$filter = $filter ? $filter : [];
|
|
$where = [];
|
|
$alias = [];
|
|
$bind = [];
|
|
$name = '';
|
|
$aliasName = '';
|
|
if($userid){
|
|
$where[] = ["userId", $userid];
|
|
}
|
|
if (!empty($this->model) && $this->relationSearch) {
|
|
$name = $this->model->getTable();
|
|
$alias[$name] = Loader::parseName(basename(str_replace('\\', '/', get_class($this->model))));
|
|
$aliasName = $alias[$name] . '.';
|
|
}
|
|
$sortArr = explode(',', $sort);
|
|
foreach ($sortArr as $index => & $item) {
|
|
$item = stripos($item, ".") === false ? $aliasName . trim($item) : $item;
|
|
}
|
|
unset($item);
|
|
$sort = implode(',', $sortArr);
|
|
if ($search) {
|
|
$searcharr = is_array($searchfields) ? $searchfields : explode(',', $searchfields);
|
|
foreach ($searcharr as $k => &$v) {
|
|
$v = stripos($v, ".") === false ? $aliasName . $v : $v;
|
|
}
|
|
unset($v);
|
|
$where[] = [implode("|", $searcharr), "LIKE", "%{$search}%"];
|
|
}
|
|
$index = 0;
|
|
foreach ($filter as $k => $v) {
|
|
if (!preg_match('/^[a-zA-Z0-9_\-\.]+$/', $k)) {
|
|
continue;
|
|
}
|
|
$sym = isset($op[$k]) ? $op[$k] : '=';
|
|
if (stripos($k, ".") === false) {
|
|
$k = $aliasName . $k;
|
|
}
|
|
$v = !is_array($v) ? trim($v) : $v;
|
|
$sym = strtoupper(isset($op[$k]) ? $op[$k] : $sym);
|
|
//null和空字符串特殊处理
|
|
if (!is_array($v)) {
|
|
if (in_array(strtoupper($v), ['NULL', 'NOT NULL'])) {
|
|
$sym = strtoupper($v);
|
|
}
|
|
if (in_array($v, ['""', "''"])) {
|
|
$v = '';
|
|
$sym = '=';
|
|
}
|
|
}
|
|
|
|
switch ($sym) {
|
|
case '=':
|
|
case '<>':
|
|
$where[] = [$k, $sym, (string)$v];
|
|
break;
|
|
case 'LIKE':
|
|
case 'NOT LIKE':
|
|
case 'LIKE %...%':
|
|
case 'NOT LIKE %...%':
|
|
$where[] = [$k, trim(str_replace('%...%', '', $sym)), "%{$v}%"];
|
|
break;
|
|
case '>':
|
|
case '>=':
|
|
case '<':
|
|
case '<=':
|
|
$where[] = [$k, $sym, intval($v)];
|
|
break;
|
|
case 'FINDIN':
|
|
case 'FINDINSET':
|
|
case 'FIND_IN_SET':
|
|
$v = is_array($v) ? $v : explode(',', str_replace(' ', ',', $v));
|
|
$findArr = array_values($v);
|
|
foreach ($findArr as $idx => $item) {
|
|
$bindName = "item_" . $index . "_" . $idx;
|
|
$bind[$bindName] = $item;
|
|
$where[] = "FIND_IN_SET(:{$bindName}, `" . str_replace('.', '`.`', $k) . "`)";
|
|
}
|
|
break;
|
|
case 'IN':
|
|
case 'IN(...)':
|
|
case 'NOT IN':
|
|
case 'NOT IN(...)':
|
|
$where[] = [$k, str_replace('(...)', '', $sym), is_array($v) ? $v : explode(',', $v)];
|
|
break;
|
|
case 'BETWEEN':
|
|
case 'NOT BETWEEN':
|
|
$arr = array_slice(explode(',', $v), 0, 2);
|
|
if (stripos($v, ',') === false || !array_filter($arr, function ($v) {
|
|
return $v != '' && $v !== false && $v !== null;
|
|
})) {
|
|
continue 2;
|
|
}
|
|
//当出现一边为空时改变操作符
|
|
if ($arr[0] === '') {
|
|
$sym = $sym == 'BETWEEN' ? '<=' : '>';
|
|
$arr = $arr[1];
|
|
} elseif ($arr[1] === '') {
|
|
$sym = $sym == 'BETWEEN' ? '>=' : '<';
|
|
$arr = $arr[0];
|
|
}
|
|
$where[] = [$k, $sym, $arr];
|
|
break;
|
|
case 'RANGE':
|
|
case 'NOT RANGE':
|
|
$v = str_replace(' - ', ',', $v);
|
|
$arr = array_slice(explode(',', $v), 0, 2);
|
|
if (stripos($v, ',') === false || !array_filter($arr)) {
|
|
continue 2;
|
|
}
|
|
//当出现一边为空时改变操作符
|
|
if ($arr[0] === '') {
|
|
$sym = $sym == 'RANGE' ? '<=' : '>';
|
|
$arr = $arr[1];
|
|
} elseif ($arr[1] === '') {
|
|
$sym = $sym == 'RANGE' ? '>=' : '<';
|
|
$arr = $arr[0];
|
|
}
|
|
$tableArr = explode('.', $k);
|
|
if (count($tableArr) > 1 && $tableArr[0] != $name && !in_array($tableArr[0], $alias) && !empty($this->model)) {
|
|
//修复关联模型下时间无法搜索的BUG
|
|
$relation = Loader::parseName($tableArr[0], 1, false);
|
|
$alias[$this->model->$relation()->getTable()] = $tableArr[0];
|
|
}
|
|
$where[] = [$k, str_replace('RANGE', 'BETWEEN', $sym) . ' TIME', $arr];
|
|
break;
|
|
case 'NULL':
|
|
case 'IS NULL':
|
|
case 'NOT NULL':
|
|
case 'IS NOT NULL':
|
|
$where[] = [$k, strtolower(str_replace('IS ', '', $sym))];
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
$index++;
|
|
}
|
|
if (!empty($this->model)) {
|
|
$this->model->alias($alias);
|
|
}
|
|
$model = $this->model;
|
|
$where = function ($query) use ($where, $alias, $bind, &$model) {
|
|
if (!empty($model)) {
|
|
$model->alias($alias);
|
|
$model->bind($bind);
|
|
}
|
|
foreach ($where as $k => $v) {
|
|
if (is_array($v)) {
|
|
call_user_func_array([$query, 'where'], $v);
|
|
} else {
|
|
$query->where($v);
|
|
}
|
|
}
|
|
};
|
|
return [$where, $sort, $order, $offset, $limit, $page, $alias, $bind];
|
|
}
|
|
/**
|
|
* 渲染配置信息
|
|
* @param mixed $name 键名或数组
|
|
* @param mixed $value 值
|
|
*/
|
|
protected function assignconfig($name, $value = '')
|
|
{
|
|
$this->view->config = array_merge($this->view->config ? $this->view->config : [], is_array($name) ? $name : [$name => $value]);
|
|
}
|
|
|
|
/**
|
|
* 刷新Token
|
|
*/
|
|
protected function token()
|
|
{
|
|
$token = $this->request->param('__token__');
|
|
|
|
//验证Token
|
|
if (!Validate::make()->check(['__token__' => $token], ['__token__' => 'require|token'])) {
|
|
$this->error(__('Token verification error'), '', ['__token__' => $this->request->token()]);
|
|
}
|
|
|
|
//刷新Token
|
|
$this->request->token();
|
|
}
|
|
}
|