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

2022/4/13 11:12:28

本文主要是介绍常用登陆验证-前端AuthController.php,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

<?php


namespace app\home;

use app\model\AdminNode;
use app\model\Cases;
use app\model\Illegal;
use think\facade\View;
use app\model\AdminUser;

class AuthController extends BaseController
{
    protected function initialize()
    {
        parent::initialize();

        $uid = session('uid');
        $this->request->user = AdminUser::info(intval($uid));

        if (empty($this->request->user)) {
            if ($this->request->isAjax()) {
                ErrorException('请登录');
            }
            cms_redirect(url('/auth/login'));
        }

        //获取当前访问路由
        $url = strtolower('/' . $this->request->controller() . '/' . $this->request->action());

        $auth = $this->request->user['auth_node']['node'];
        //获取路由映射,权限判断
        $route = !empty(config('auth.route')[$url]) ? config('auth.route')[$url] : $url;

        if ($route != 'allow_access' && $auth != 'all') {
            //获取用户授权路由
            $node = AdminNode::node($auth, 'home');
            if (!in_array($route, $node)) {
                throw new NotFoundException("未授权访问", 10000, 403);
            }
        }

        View::assign(['__is_real' => $this->_check_real($url)]);

        /**
         * 审批统计
         */
        $caseExamineCount = Cases::where(function ($query) {
            if (!in_array('all', $this->request->user['region_id'])) {
                $query->where([
                    ['region_id', 'in', $this->request->user['region_id']],
                    ['region_id', '<>', 0],
                ]);
            }
            $query->where('status', Cases::CASE_STATUS_WAIT);
        })->count();

        View::assign(['user' => $this->request->user, 'examine_count' => $caseExamineCount]);
    }

    private function _check_real(string $url = '')
    {
        if (SystemSetting('user.user_auth') != 1) {
            return true;
        }
        $real_page = '/center/real';
        $allow_list = [
            $real_page,
            '/center/setting',
            '/center/certify',
            '/center/change',
            '/file/upload'
        ];
        /**
         * 有控制台权限的人不需要实名认证
         */
        if ($this->request->user['disable'] == 1) {
            return true;
        }

        /**
         * 跳转实名认证页面
         */
        if (!in_array($url, $allow_list) && $this->request->user['auth'] != 2) {
            cms_redirect(url($real_page));
        }
        return false;
    }
}

 



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


扫一扫关注最新编程教程