Frontend.php 13.2 KB
<?php

namespace app\common\controller;

use app\common\library\Auth;
use think\Config;
use think\Controller;
use think\Hook;
use think\Lang;
use think\Loader;
use think\Validate;
use think\Db;
use app\admin\library\Auth as adminauth;
/**
 * 前台控制器基类
 */
class Frontend extends Controller
{

    /**
     * 布局模板
     * @var string
     */
    protected $layout = '';

    /**
     * 无需登录的方法,同时也就不需要鉴权了
     * @var array
     */
    protected $noNeedLogin = [];

    /**
     * 无需鉴权的方法,但需要登录
     * @var array
     */
    protected $noNeedRight = [];
    
    /**
     * 分页数量
     * @var int
     */
    protected $limit = 10;

    /**
     * 权限Auth
     * @var Auth
     */
    protected $auth = null;

    public function _initialize()
    {
        //移除HTML标签
        $this->request->filter('trim,strip_tags,htmlspecialchars');
        $modulename = $this->request->module();
        $controllername = Loader::parseName($this->request->controller());
        $actionname = strtolower($this->request->action());
        
        //获取模块
        $request= \think\Request::instance();
        $module = $request->module();
        
        //自动登录后台
        if($module == 'inspection' || $module == 'equipment'){
            $adminauth = new adminauth();
            $res = $adminauth->login('yushen','shuiku2022');
        }
        
        // 检测IP是否允许
        check_ip_allowed();

        // 如果有使用模板布局
        if ($this->layout) {
            $this->view->engine->layout('layout/' . $this->layout);
        }
        $this->auth = Auth::instance();

        // token
        $token = $this->request->server('HTTP_TOKEN', $this->request->request('token', \think\Cookie::get('token')));

        $path = str_replace('.', '/', $controllername) . '/' . $actionname;
        // 设置当前请求的URI
        $this->auth->setRequestUri($path);
        
        // 检测是否需要验证登录
        if (!$this->auth->match($this->noNeedLogin)) {
            //初始化
            $this->auth->init($token);
            //检测是否登录
            if (!$this->auth->isLogin()) {
                $this->redirect($module.'/user/login');
                $this->error(__('Please login first'), $module.'/user/login');
            }
            // 判断是否需要验证权限
            if (!$this->auth->match($this->noNeedRight)) {
                // 判断控制器和方法判断是否有对应权限
                if (!$this->auth->check($path)) {
                    
                    $this->error(__('You have no permission'));
                }
            }
        } else {
            // 如果有传递token才验证是否登录状态
            if ($token) {
                $this->auth->init($token);
            }
        }

        $this->view->assign('user', $this->auth->getUser());

        // 语言检测
        $lang = strip_tags($this->request->langset());

        $site = Config::get("site");

        $upload = \app\common\model\Config::upload();

        // 上传信息配置后
        Hook::listen("upload_config_init", $upload);

        // 配置信息
        $config = [
            'site'           => array_intersect_key($site, array_flip(['name', 'cdnurl', 'version', 'timezone', 'languages'])),
            'upload'         => $upload,
            'modulename'     => $modulename,
            'controllername' => $controllername,
            'actionname'     => $actionname,
            'jsname'         => 'frontend/' . str_replace('.', '/', $controllername),
            'moduleurl'      => rtrim(url("/{$modulename}", '', false), '/'),
            'language'       => $lang
        ];
        $config = array_merge($config, Config::get("view_replace_str"));

        Config::set('upload', array_merge(Config::get('upload'), $upload));

        // 配置信息后
        Hook::listen("config_init", $config);
        // 加载当前控制器语言包
        $this->loadlang($controllername);
        $this->assign('site', $site);
        $this->assign('config', $config);
    }

    /**
     * 加载语言文件
     * @param string $name
     */
    protected function loadlang($name)
    {
        $name =  Loader::parseName($name);
        Lang::load(APP_PATH . $this->request->module() . '/lang/' . $this->request->langset() . '/' . str_replace('.', '/', $name) . '.php');
    }

    /**
     * 渲染配置信息
     * @param mixed $name  键名或数组
     * @param mixed $value 值
     */
    protected function assignconfig($name, $value = '')
    {
        $this->view->config = array_merge($this->view->config ? $this->view->config : [], is_array($name) ? $name : [$name => $value]);
    }

    /**
     * 刷新Token
     */
    protected function token()
    {
        $token = $this->request->param('__token__');

        //验证Token
        if (!Validate::make()->check(['__token__' => $token], ['__token__' => 'require|token'])) {
            $this->error(__('Token verification error'), '', ['__token__' => $this->request->token()]);
        }

        //刷新Token
        $this->request->token();
    }

    /**
     * 根据地区类型和id判断用户是否有权限访问
     * @param $type 地区类型:province:省id,state:州id,county:县/市id,reservoir:水库id
     * @param $id
     */
    protected function reservoir_auth($type='reservoir',$id){
         $array = ['province','state','county','reservoir'];
         if(!in_array($type,$array)){
             $this->error(__('You have no permission'));
         }
         $userid = $this->auth->getUser()->id;
         $reservoiruser = Db::name('reservoir_user')->where('uid','=',$userid)->find();
         if(!$reservoiruser){
             $this->error(__('You have no permission'));
         }
         if($reservoiruser['reservoir_id'] >= 1){
              if($id != $reservoiruser['reservoir_id']){
                  $this->error(__('You have no permission'));
              }
         }
         elseif($reservoiruser['county_id'] >= 1){
              if($type == 'reservoir'){
                  $_id = Db::name('reservoir_list')->where('id','=',$id)->field($type.'_id')->find();
                  if($_id != $reservoiruser[$type.'_id']){
                      $this->error(__('You have no permission'));
                  }
              }
              elseif($type == 'county' && $id != $reservoiruser['county_id']){
                      $this->error(__('You have no permission'));
              }
              elseif($type == 'state' || $type == 'province'){
                  $this->error(__('You have no permission'));
              }
         }
         elseif($reservoiruser['state_id'] >= 1){
             if($type == 'reservoir' || $type == 'county'){
                 $_id = Db::name('reservoir_list')->where('id','=',$id)->field($type.'_id')->find();
                 if($_id != $reservoiruser[$type.'_id']){
                     $this->error(__('You have no permission'));
                 }
             }
             elseif($type == 'state' && $id != $reservoiruser['state_id']){
                 $this->error(__('You have no permission'));
             }
             elseif($type == 'province'){
                 $this->error(__('You have no permission'));
             }
         }
         elseif($reservoiruser['province_id'] >= 1){
             if($type == 'reservoir' || $type == 'county' || $type == 'state'){
                 $_id = Db::name('reservoir_list')->where('id','=',$id)->field($type.'_id')->find();
                 if($_id != $reservoiruser[$type.'_id']){
                     $this->error(__('You have no permission'));
                 }
             }
             elseif($type == 'province' && $id != $reservoiruser['province_id']){
                 $this->error(__('You have no permission'));
             }
         }

    }



    protected function reservoir_list(){
        $userid = $this->auth->getUser()->id;
        $reservoiruser = Db::name('reservoir_user')->where('uid','=',$userid)->find();
        if($reservoiruser){
          return $this->reservoir_tree($reservoiruser['province_id'],$reservoiruser['state_id'],$reservoiruser['county_id'],$reservoiruser['reservoir_id']);
        }
        else{
            return false;
        }
    }

    /**
     * @param $province_id
     * @param $state_id
     * @param $county_id
     * @param $reservoir_id
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function reservoir_tree($province_id,$state_id,$county_id,$reservoir_id){
         if($reservoir_id >= 1){
             $province = Db::name('reservoir_province')->where('id','=',$province_id)->field('id,name')->select();
             $state = Db::name('reservoir_state')->where('id','=',$state_id)->field('id,name,province_id')->select();
             $county = Db::name('reservoir_county')->where('id','=',$county_id)->field('id,name,state_id')->select();
             $reservoir = Db::name('reservoir_list')->where('id','=',$reservoir_id)->field('id,name,county_id')->select();
         }
         elseif($county_id >= 1){
             $province = Db::name('reservoir_province')->where('id','=',$province_id)->field('id,name')->select();
             $state = Db::name('reservoir_state')->where('id','=',$state_id)->field('id,name,province_id')->select();
             $county = Db::name('reservoir_county')->where('id','=',$county_id)->field('id,name,state_id')->select();
             $reservoir = Db::name('reservoir_list')->where('county_id','=',$county_id)->field('id,name,county_id')->select();
         }
         elseif($state_id >= 1){
             $province = Db::name('reservoir_province')->where('id','=',$province_id)->field('id,name')->select();
             $state = Db::name('reservoir_state')->where('id','=',$state_id)->field('id,name,province_id')->select();
             $county = Db::name('reservoir_county')->where('state_id','=',$state_id)->field('id,name,state_id')->select();
             $reservoir = Db::name('reservoir_list')->where('state_id','=',$state_id)->field('id,name,county_id')
                 ->order('county_id')->select();
         }
         elseif($province_id >= 1){
             $province = Db::name('reservoir_province')->where('id','=',$province_id)->field('id,name')->select();
             $state = Db::name('reservoir_state')->where('province_id','=',$province_id)->field('id,name,province_id')->select();
             $county = Db::name('reservoir_county')->where('province_id','=',$province_id)->field('id,name,state_id')->select();
             $reservoir = Db::name('reservoir_list')->where('province_id','=',$province_id)->field('id,name,county_id')
                 ->order('county_id')->select();
         }
         elseif($reservoir_id == 0){
             $province = Db::name('reservoir_province')->field('id,name')->select();
             $state = Db::name('reservoir_state')->field('id,name,province_id')->select();
             $county = Db::name('reservoir_county')->field('id,name,state_id')->select();
             $reservoir = Db::name('reservoir_list')->field('id,name,county_id')->select();
         }
         $list = [];
         $cnt1 = 0;
         $cnt2 = 0;
         $cnt3 = 0;
         foreach($province as $k => $v){
             $list[$k]['id'] = $v['id'];
             $list[$k]['name'] = $v['name'];
             $list[$k]['nocheck'] = true;
             foreach ($state as $k2 => $v2){
                 if($v2['province_id'] == $v['id']){
                     $list[$k]['children'][$cnt1]['id'] = $v2['id'];
                     $list[$k]['children'][$cnt1]['name'] = $v2['name'];
                     $list[$k]['children'][$cnt1]['nocheck'] = true;
                 }
                 else{
                     continue;
                 }
                 foreach ($county as $k3 => $v3){
                     if($v3['state_id'] == $v2['id']){
                         $list[$k]['children'][$cnt1]['children'][$cnt2]['id'] = $v3['id'];
                         $list[$k]['children'][$cnt1]['children'][$cnt2]['name'] = $v3['name'];
                         $list[$k]['children'][$cnt1]['children'][$cnt2]['nocheck'] = true;
                     }
                     else{
                         continue;
                     }
                     foreach($reservoir as $k4 => $v4){
                         if($v4['county_id'] == $v3['id']){
                             $list[$k]['children'][$cnt1]['children'][$cnt2]['children'][$cnt3]['id'] = $v4['id'];
                             $list[$k]['children'][$cnt1]['children'][$cnt2]['children'][$cnt3]['name'] = $v4['name'];
                             $list[$k]['children'][$cnt1]['children'][$cnt2]['children'][$cnt3]['nocheck'] = true;
                         }
                         else{
                             continue;
                         }
                         $cnt3++;
                     }
                     $cnt3 = 0;
                     $cnt2++;
                 }
                 $cnt2 = 0;
                 $cnt1++;
             }
             $cnt1 = 0;
         }
         $res['list'] = $list;
         $res['reservoir'] = $reservoir;
         return $res;
    }



}