正在显示
1 个修改的文件
包含
198 行增加
和
0 行删除
application/common/controller/Frontend.php
0 → 100644
| 1 | +<?php | ||
| 2 | + | ||
| 3 | +namespace app\common\controller; | ||
| 4 | + | ||
| 5 | +use app\common\library\Auth; | ||
| 6 | +use app\school\model\ManystoreShop; | ||
| 7 | +use think\Config; | ||
| 8 | +use think\Controller; | ||
| 9 | +use think\Hook; | ||
| 10 | +use think\Lang; | ||
| 11 | +use think\Loader; | ||
| 12 | +use think\Route; | ||
| 13 | +use think\Validate; | ||
| 14 | + | ||
| 15 | +/** | ||
| 16 | + * 前台控制器基类 | ||
| 17 | + */ | ||
| 18 | +class Frontend extends Controller | ||
| 19 | +{ | ||
| 20 | + | ||
| 21 | + /** | ||
| 22 | + * 布局模板 | ||
| 23 | + * @var string | ||
| 24 | + */ | ||
| 25 | + protected $layout = ''; | ||
| 26 | + /** | ||
| 27 | + * 无需登录的方法,同时也就不需要鉴权了 | ||
| 28 | + * @var array | ||
| 29 | + */ | ||
| 30 | + protected $noNeedLogin = []; | ||
| 31 | + | ||
| 32 | + /** | ||
| 33 | + * 无需鉴权的方法,但需要登录 | ||
| 34 | + * @var array | ||
| 35 | + */ | ||
| 36 | + protected $noNeedRight = []; | ||
| 37 | + | ||
| 38 | + /** | ||
| 39 | + * 权限Auth | ||
| 40 | + * @var Auth | ||
| 41 | + */ | ||
| 42 | + protected $auth = null; | ||
| 43 | + | ||
| 44 | + protected $education = ['0'=>'学前','1'=>'小学','2'=>'初中','3'=>'高中','4'=>'中专','5'=>'大专','6'=>'本科','7'=>'硕士','8'=>'博士']; | ||
| 45 | + protected $degree = [ 0 => '学士', 1 => '硕士', 2 => '博士']; | ||
| 46 | + protected $position = [ 0 => '助教', 1 => '讲师', 2 => '副教授', 3 => '教授',4 => '副研究员' ]; | ||
| 47 | + protected $school_id = null; | ||
| 48 | + | ||
| 49 | + protected $schoolInfo = []; | ||
| 50 | + public function _initialize() | ||
| 51 | + { | ||
| 52 | + | ||
| 53 | + | ||
| 54 | + | ||
| 55 | + //移除HTML标签 | ||
| 56 | + $this->request->filter('trim,strip_tags,htmlspecialchars'); | ||
| 57 | + $modulename = $this->request->module(); | ||
| 58 | + $controllername = Loader::parseName($this->request->controller()); | ||
| 59 | + $actionname = strtolower($this->request->action()); | ||
| 60 | + //dump(Route::getGroup('project')); | ||
| 61 | + //list($a,$b,$c) = request()->routeInfo()['rule']; | ||
| 62 | + // 如果有使用模板布局 | ||
| 63 | + if ($this->layout) { | ||
| 64 | + $this->view->engine->layout('layout/' . $this->layout); | ||
| 65 | + } | ||
| 66 | + $this->auth = Auth::instance(); | ||
| 67 | + | ||
| 68 | + | ||
| 69 | + | ||
| 70 | + $domains = explode('.',$_SERVER ['HTTP_HOST']); | ||
| 71 | + | ||
| 72 | + unset($domains[count($domains)-1]); | ||
| 73 | + | ||
| 74 | + if (count($domains) <= 2 || count($domains) > 3){ | ||
| 75 | + header("HTTP/1.1 404 Not Found"); | ||
| 76 | + header("Status: 404 Not Found"); | ||
| 77 | + exit; | ||
| 78 | + } | ||
| 79 | + | ||
| 80 | + list($three , $sec , $first) = $domains; | ||
| 81 | + | ||
| 82 | + $school = ManystoreShop::where('subdomain_name',$three)->find(); | ||
| 83 | + if (!$school || $school['status'] != 1){ | ||
| 84 | + header("HTTP/1.1 404 Not Found"); | ||
| 85 | + header("Status: 404 Not Found"); | ||
| 86 | + exit; | ||
| 87 | + } | ||
| 88 | + $this->school_id = $school['id']; | ||
| 89 | + $this->schoolInfo = $school; | ||
| 90 | + $this->assign('schoolInfo',$this->schoolInfo); | ||
| 91 | + | ||
| 92 | + /* $school = ManystoreShop::get(5); | ||
| 93 | + $this->school_id = $school['id']; | ||
| 94 | + $this->assign('schoolInfo',$school);*/ | ||
| 95 | + // token | ||
| 96 | + $token = $this->request->server('HTTP_TOKEN', $this->request->request('token', \think\Cookie::get('token'))); | ||
| 97 | + | ||
| 98 | + $path = str_replace('.', '/', $controllername) . '/' . $actionname; | ||
| 99 | + // 设置当前请求的URI | ||
| 100 | + $this->auth->setRequestUri($path); | ||
| 101 | + // 检测是否需要验证登录 | ||
| 102 | + if (!$this->auth->match($this->noNeedLogin)) { | ||
| 103 | + //初始化 | ||
| 104 | + $this->auth->init($token); | ||
| 105 | + //检测是否登录 | ||
| 106 | + if (!$this->auth->isLogin()) { | ||
| 107 | + $this->redirect('index/student_login/index'); | ||
| 108 | + } | ||
| 109 | + // 判断是否需要验证权限 | ||
| 110 | + if (!$this->auth->match($this->noNeedRight)) { | ||
| 111 | + // 判断控制器和方法判断是否有对应权限 | ||
| 112 | + if (!$this->auth->check($path)) { | ||
| 113 | + $this->error(__('You have no permission')); | ||
| 114 | + } | ||
| 115 | + } | ||
| 116 | + } else { | ||
| 117 | + // 如果有传递token才验证是否登录状态 | ||
| 118 | + if ($token) { | ||
| 119 | + $this->auth->init($token); | ||
| 120 | + } | ||
| 121 | + } | ||
| 122 | + $this->view->assign('user', $this->auth->getUser()); | ||
| 123 | + | ||
| 124 | + // 语言检测 | ||
| 125 | + $lang = strip_tags($this->request->langset()); | ||
| 126 | + | ||
| 127 | + $site = Config::get("site"); | ||
| 128 | + | ||
| 129 | + $upload = \app\common\model\Config::upload(); | ||
| 130 | + | ||
| 131 | + // 上传信息配置后 | ||
| 132 | + Hook::listen("upload_config_init", $upload); | ||
| 133 | + | ||
| 134 | + $mark = '[游客]'; | ||
| 135 | + if ($this->auth->isLogin()){ | ||
| 136 | + $mark = "[{$this->auth->nickname}]"; | ||
| 137 | + } | ||
| 138 | + $upload['mark'] = $mark; | ||
| 139 | + // 配置信息 | ||
| 140 | + $config = [ | ||
| 141 | + 'site' => array_intersect_key($site, array_flip(['name', 'cdnurl', 'version', 'timezone', 'languages'])), | ||
| 142 | + 'upload' => $upload, | ||
| 143 | + 'modulename' => $modulename, | ||
| 144 | + 'controllername' => $controllername, | ||
| 145 | + 'actionname' => $actionname, | ||
| 146 | + 'jsname' => 'frontend/' . str_replace('.', '/', $controllername), | ||
| 147 | + 'moduleurl' => rtrim(url("/{$modulename}", '', false), '/'), | ||
| 148 | + 'language' => $lang | ||
| 149 | + ]; | ||
| 150 | + $config = array_merge($config, Config::get("view_replace_str")); | ||
| 151 | + | ||
| 152 | + Config::set('upload', array_merge(Config::get('upload'), $upload)); | ||
| 153 | + // 配置信息后 | ||
| 154 | + Hook::listen("config_init", $config); | ||
| 155 | + $userInfo = $this->auth->getUser(); | ||
| 156 | + $this->assign('userInfo',$userInfo); | ||
| 157 | + // 加载当前控制器语言包 | ||
| 158 | + $this->loadlang($controllername); | ||
| 159 | + $this->assign('site', $site); | ||
| 160 | + $this->assign('config', $config); | ||
| 161 | + } | ||
| 162 | + | ||
| 163 | + /** | ||
| 164 | + * 加载语言文件 | ||
| 165 | + * @param string $name | ||
| 166 | + */ | ||
| 167 | + protected function loadlang($name) | ||
| 168 | + { | ||
| 169 | + $name = Loader::parseName($name); | ||
| 170 | + Lang::load(APP_PATH . $this->request->module() . '/lang/' . $this->request->langset() . '/' . str_replace('.', '/', $name) . '.php'); | ||
| 171 | + } | ||
| 172 | + | ||
| 173 | + /** | ||
| 174 | + * 渲染配置信息 | ||
| 175 | + * @param mixed $name 键名或数组 | ||
| 176 | + * @param mixed $value 值 | ||
| 177 | + */ | ||
| 178 | + protected function assignconfig($name, $value = '') | ||
| 179 | + { | ||
| 180 | + $this->view->config = array_merge($this->view->config ? $this->view->config : [], is_array($name) ? $name : [$name => $value]); | ||
| 181 | + } | ||
| 182 | + | ||
| 183 | + /** | ||
| 184 | + * 刷新Token | ||
| 185 | + */ | ||
| 186 | + protected function token() | ||
| 187 | + { | ||
| 188 | + $token = $this->request->param('__token__'); | ||
| 189 | + | ||
| 190 | + //验证Token | ||
| 191 | + if (!Validate::make()->check(['__token__' => $token], ['__token__' => 'require|token'])) { | ||
| 192 | + $this->error(__('Token verification error'), '', ['__token__' => $this->request->token()]); | ||
| 193 | + } | ||
| 194 | + | ||
| 195 | + //刷新Token | ||
| 196 | + $this->request->token(); | ||
| 197 | + } | ||
| 198 | +} |
-
请 注册 或 登录 后发表评论