• 常用登陆验证前端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;
        }
    }
  • 相关阅读:
    第八章:数组
    第六章:循环结构(二)
    第五章:循环结构(一)
    第二章:变量 数据类型和运算符
    第三章:选择结构(一)
    第四章:选择结构(二)
    第一章:初识java
    第6章 数据筛选和排序
    第四章 实现Windows程序的数据更新
    第五章 实现Windows程序的数据绑定
  • 原文地址:https://www.cnblogs.com/silen0119/p/16139033.html
Copyright © 2020-2023  润新知