Frontend.php
7.0 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
<?php
namespace app\common\controller;
use app\common\library\Auth;
use app\index\model\Car;
use think\Config;
use think\Controller;
use think\Db;
use think\Hook;
use think\Lang;
use think\Loader;
use think\Validate;
/**
* 前台控制器基类
*/
class Frontend extends Controller
{
/**
* 布局模板
* @var string
*/
protected $layout = '';
/**
* 无需登录的方法,同时也就不需要鉴权了
* @var array
*/
protected $noNeedLogin = [];
/**
* 无需鉴权的方法,但需要登录
* @var array
*/
protected $noNeedRight = [];
/**
* 权限Auth
* @var Auth
*/
protected $auth = null;
protected $store = null;
public function _initialize()
{
dump(session('user'));
//移除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_view');
$param = $this->request->param();
$url = str_replace('.html','',$this->request->url(true));
dump($url);
$str = '';
foreach($param as $key => $p){
$str .= "/$key/$p";
}
$target_url = !empty($param['goods_id']) ? url('goods/detail',array('goods_id'=>$param['goods_id']),false,true) : $url.$str;
// dump($url.$str);
$target_url = url('index/user/login_view',array('target_url'=>rawurlencode($target_url)));
// dump($target_url);
if($this->request->isAjax()){
$this->error(__('Please login first'), $target_url);
}else{
$this->redirect($target_url);
}
}
// 判断是否需要验证权限
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));
// 如果已登录,判断入驻情况
$is_store = $store_type = $is_valid = $over_time = $bubble = 0;
if($this->auth->isLogin()) {
$this->store = Db::name('store')->where('user_id',$this->auth->id)->find();
if($this->store) {
$this->store['is_store'] = $is_store = 1;
// 判断会员身份(普通、超级)
if($this->store['vip_passtime'] > time()) {
$this->store['is_valid'] = $is_valid = 1;
$this->store['store_type'] = $store_type = 1;
$this->store['over_time'] = $over_time = date('Y.m.d',$this->store['vip_passtime']);
}
if($this->store['svip_passtime'] > time()) {
$this->store['is_valid'] = $is_valid = 1;
$this->store['store_type'] = $store_type = 2;
$this->store['over_time'] = $over_time = date('Y.m.d',$this->store['svip_passtime']);
}
}
// 购物车气泡
$carModel = new Car();
$bubble = $carModel->countData(['user_id'=>$this->auth->id]);
}
$this->assign('is_store', $is_store); // 是否已入驻
$this->assign('store_type', $store_type); // 入驻类型
$this->assign('is_valid', $is_valid); // 是否过期
$this->assign('over_time', $over_time); // 过期时间
$this->assign('bubble',$bubble);// 购物车气泡
// 配置信息后
Hook::listen("config_init", $config);
// 友情链接
$link = Db::name('link')->select();
$this->assign('link',$link);
// 加载当前控制器语言包
$this->loadlang($controllername);
$this->assign('site', $site);
$this->assign('token', $token);
$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->post('__token__');
//验证Token
if (!Validate::is($token, "token", ['__token__' => $token])) {
$this->error(__('Token verification error'), '', ['__token__' => $this->request->token()]);
}
//刷新Token
$this->request->token();
}
}