编程语言
首页 > 编程语言> > 常用登陆验证-前端BaseController.php

常用登陆验证-前端BaseController.php

作者:互联网

<?php

declare(strict_types=1);

namespace app\home;

use app\lang\SysCode;
use app\model\SystemSetting;
use think\App;
use think\exception\ValidateException;
use think\Validate;

/**
 * 控制器基础类
 */
abstract class BaseController
{
    /**
     * Request实例
     * @var \think\Request
     */
    protected $request;

    /**
     * 应用实例
     * @var \think\App
     */
    protected $app;

    /**
     * 视图
     * @var \think\view
     */
    protected $view;

    /**
     * 模板目录
     * @var
     */
    protected $template;

    /**
     * 是否批量验证
     * @var bool
     */
    protected $batchValidate = false;

    /**
     * 控制器中间件
     * @var array
     */
    protected $middleware = [];

    /**
     * 构造方法
     * @access public
     * @param  App  $app  应用对象
     */
    public function __construct(App $app)
    {
        $this->app     = $app;
        $this->view    = $this->app->view;
        $this->request = $this->app->request;

        // 控制器初始化
        $this->initialize();
    }

    // 初始化
    protected function initialize()
    {
        //获取模板目录
        $template = SystemSetting('site.site_template');
        $this->template = !empty($template) ? $template : 'default';

        //手机站点
        if (SystemSetting('site.site_mobile')) {
            if (is_mobile() == true || $this->request->param('browser') == 'mobile') {
                $this->template = $this->template . '/' . 'mobile';
            }
        }

        //设置布局
        $this->view->layout($this->template . '/layout/layout');

        //网站是否开启
        $siteOpen = SystemSetting('site.site_open');
        if ($siteOpen == SysCode::STATUS_DISABLE) {
            return NotFoundException('网站已关闭');
        }

        $url = $this->request->url();
        $this->view->assign('url', $url);
    }

    //模板渲染
    protected function display(string $template = '', array $vars = [])
    {
        if (empty($template)) {
            $template = $this->request->action();
            $path = explode('.', strtolower($this->request->controller()));
            $template = implode('/', $path) . '/' . $template;
        }
        return $this->view->fetch($this->template . '/' . $template, $vars);
    }

    /**
     * 验证数据
     * @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);
    }

    public function __call($method, $args)
    {
        if ($this->request->controller() == '404') {
            return response('', 404);
        } else {
            return redirect('/404.html');
        }
    }
}

 

标签:return,BaseController,前端,request,site,template,validate,php,array
来源: https://www.cnblogs.com/silen0119/p/16139028.html