宝体数据调用接口
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.
 
 
 
 
 
 

204 lines
5.4 KiB

<?php
declare (strict_types = 1);
namespace app;
use think\App;
use think\exception\ValidateException;
use think\Validate;
use think\facade\View;
use think\facade\Config;
use think\facade\Request;
/**
* 控制器基础类
*/
abstract class BaseController
{
/**
* Request实例
* @var \think\Request
*/
protected $request;
/**
* 应用实例
* @var \think\App
*/
protected $app;
/**
* 是否批量验证
* @var bool
*/
protected $batchValidate = false;
/**
* 控制器中间件
* @var array
*/
protected $middleware = [];
/* @var array $admin 商家登录信息 */
protected $admin;
/* @var string $route 当前控制器名称 */
protected $controller = '';
/* @var string $route 当前方法名称 */
protected $action = '';
/* @var string $route 当前路由uri */
protected $routeUri = '';
/* @var string $route 当前路由:分组名称 */
protected $group = '';
/* @var array $allowAllAction 登录验证白名单 */
protected $allowAllAction = [
// 登录页面
'passport/login',
];
/* @var array $notLayoutAction 无需全局layout */
protected $notLayoutAction = [
// 登录页面
'passport/login',
];
/**
* 构造方法
* @access public
* @param App $app 应用对象
*/
public function __construct(App $app)
{
$this->app = $app;
$this->request = $this->app->request;
// 控制器初始化
$this->initialize();
// 全局layout
$this->layout();
}
/**
* 全局layout模板输出
* @throws \Exception
*/
private function layout()
{
// 验证当前请求是否在白名单
if (!in_array($this->routeUri, $this->notLayoutAction)) {
// 输出到view
View::assign([
'base_url' => base_url(), // 当前域名
'admin_url' => url('/admin'), // 后台模块url
'group' => $this->group,
'menus' => $this->menus(), // 后台菜单
'admin' => $this->admin, // 商家登录信息
'request' => Request::instance() // Request对象
]);
}
}
// 初始化
protected function initialize()
{}
/**
* 验证数据
* @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);
}
/**
* 解析当前路由参数 (分组名称、控制器名称、方法名)
*/
protected function getRouteinfo()
{
// 控制器名称
$this->controller = toUnderScore($this->request->controller());
// 方法名称
$this->action = $this->request->action();
// 控制器分组 (用于定义所属模块)
$groupstr = strstr($this->controller, '.', true);
$this->group = $groupstr !== false ? $groupstr : $this->controller;
// 当前uri
$this->routeUri = $this->controller . '/' . $this->action;
}
/**
* 后台菜单配置
* @return array
*/
private function menus()
{
foreach ($data = Config::get('menus') as $group => $first) {
$data[$group]['active'] = $group === $this->group;
// 遍历:二级菜单
if (isset($first['submenu'])) {
foreach ($first['submenu'] as $secondKey => $second) {
// 二级菜单所有uri
$secondUris = isset($second['uris']) ? $second['uris'] : [$second['index']];
// 二级菜单:active
!isset($data[$group]['submenu'][$secondKey]['active'])
&& $data[$group]['submenu'][$secondKey]['active'] = in_array($this->routeUri, $secondUris);
}
}
}
return $data;
}
/**
* 验证登录状态
* @return bool
*/
private function checkLogin()
{
// 验证当前请求是否在白名单
if (in_array($this->routeUri, $this->allowAllAction)) {
return true;
}
// 验证登录状态
if (empty($this->admin)
|| (int)$this->admin['is_login'] !== 1
) {
$this->redirect('passport/login');
return false;
}
return true;
}
}