Frontend.php
7.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<?php
namespace app\common\controller;
use app\admin\model\Gg;
use app\admin\model\Menu;
use app\admin\model\Readgg;
use app\admin\model\ReadquestionTall;
use app\admin\model\Readsysnews;
use app\common\library\Auth;
use app\index\service\Index;
use think\Config;
use think\Controller;
use think\Hook;
use think\Lang;
use think\Loader;
use think\Request;
use think\Validate;
/**
* 前台控制器基类
*/
class Frontend extends Controller
{
/**
* 布局模板
* @var string
*/
protected $layout = '';
protected static $model="\app\index\service\Index";
/**
* 无需登录的方法,同时也就不需要鉴权了
* @var array
*/
protected $noNeedLogin = [];
/**
* 无需鉴权的方法,但需要登录
* @var array
*/
protected $noNeedRight = [];
protected $http_style="";
/**
* 权限Auth
* @var Auth
*/
protected $auth = null;
public function _initialize()
{
$this->http_style=Request::instance()->server()['HTTP_USER_AGENT'];
//移除HTML标签
$this->request->filter('trim,strip_tags,htmlspecialchars');
$modulename = $this->request->module();
$controllername = Loader::parseName($this->request->controller());
$actionname = strtolower($this->request->action());
// 如果有使用模板布局
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->error(__('Please login first'), 'index/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);
}
}
$logo=config('site.logo');
$this->assign('logo',cdnurl($logo,true));
$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);
$menu=Menu::where("ismenu","是")->order("weigh asc")->select();
urlIndex($menu,$this->http_style);
$this->assign('menu', $menu);
// $gg=Index::gg(1,1);
$gg= Gg::order("id desc")->select();
$this->assign('titlegg', $gg);
//通过url获取名字
$title=Menu::where("id",Request::instance()->param("pid"))->find();
if(!strpos($this->http_style,"Win")){
$server_url=$title['url'];
$server_url=explode("/",$server_url);
$server_url[1]="api";
$server_url=implode("/",$server_url);
$title['url']=$server_url;
}
$this->assign("title",$title);
//还未读的消息个数
$nums=0;
$list=Gg::order("id desc")->select();
foreach ($list as $l) {
$data = [
"sysnews_id" => $l['id'],
"user_id"=>session('user')['id']
];
$res=Readgg::where($data)->find();
if(!$res){
$nums++;
}
}
$list1=\app\admin\model\Sysnews::order("id desc")->select();
foreach ($list1 as $l) {
$data = [
"sysnews_id" => $l['id'],
"user_id"=>session('user')['id']
];
$res=Readsysnews::where($data)->find();
if(!$res){
$nums++;
}
}
$list2=\app\admin\model\QuestionTall::where(["user_id"=>session('user')['id']])->order("id desc")->select();
foreach ($list2 as $l) {
$data = [
"sysnews_id" => $l['id'],
"user_id"=>session('user')['id']
];
$res=ReadquestionTall::where($data)->find();
if(!$res){
$nums++;
}
}
$this->assign("nums",$nums);
$active=Request::instance()->action();
if($active=="detail"){
$index="/index/";
$controller=Request::instance()->controller();
$active="/index";
// $url11=$index.$controller.$active;
$thing=Request::instance()->param()['id'];
$model1111="app\\admin\\model\\".ucfirst($controller);
$res111=$model1111::where("id",$thing)->find();
if(array_key_exists("pid",Request::instance()->param()) && Request::instance()->param()['pid']==14 || ucfirst($controller)=="Gg"){
$res111=null;
}
$menuSub=Menu::where("id",$res111['style'])->find();
$menuSub['url']=$index.$controller.$active."?pid=".$menuSub['id'];
if(!strpos($this->http_style,"Win")){
$server_url=$menuSub['url'];
$server_url=explode("/",$server_url);
$server_url[1]="api";
$server_url=implode("/",$server_url);
$menuSub['url']=$server_url;
}
$menuSub['status']="1";
$this->assign('title', $menuSub);
}
}
/**
* 加载语言文件
* @param string $name
*/
protected function loadlang($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->post('__token__');
//验证Token
if (!Validate::is($token, "token", ['__token__' => $token])) {
$this->error(__('Token verification error'), '', ['__token__' => $this->request->token()]);
}
//刷新Token
$this->request->token();
}
}