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

2022/4/13 11:12:29

本文主要是介绍常用登陆验证-前端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');
        }
    }
}

 



这篇关于常用登陆验证-前端BaseController.php的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程