Backend.php
9.1 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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
<?php
namespace app\common\controller;
use app\admin\library\Auth;
use app\common\model\Configvalue;
use think\Config;
use think\Controller;
use think\Lang;
use think\Session;
load_trait('library/traits/Backend');
/**
* 后台控制器基类
*/
class Backend extends Controller
{
/**
* 返回码,默认为null,当设置了该值后将输出json数据
* @var int
*/
protected $code = null;
/**
* 返回内容,默认为null,当设置了该值后将输出json数据
* @var mixed
*/
protected $data = null;
/**
* 返回文本,默认为空
* @var mixed
*/
protected $msg = '';
/**
* 无需登录的方法,同时也就不需要鉴权了
* @var array
*/
protected $noNeedLogin = [];
/**
* 无需鉴权的方法,但需要登录
* @var array
*/
protected $noNeedRight = [];
/**
* 布局模板
* @var string
*/
protected $layout = 'default';
/**
* 权限控制类
* @var Auth
*/
protected $auth = null;
/**
* 快速搜索时执行查找的字段
*/
protected $searchFields = 'id';
/**
* 是否是关联查询
*/
protected $relationSearch = false;
/**
* 是否开启Validate验证
*/
protected $modelValidate = false;
/**
* 是否开启模型场景验证
*/
protected $modelSceneValidate = false;
/**
* 引入后台控制器的traits
*/
use \app\admin\library\traits\Backend;
public function _initialize()
{
$modulename = $this->request->module();
$controllername = strtolower($this->request->controller());
$actionname = strtolower($this->request->action());
$path = '/' . $modulename . '/' . str_replace('.', '/', $controllername) . '/' . $actionname;
// 定义是否Addtabs请求
!defined('IS_ADDTABS') && define('IS_ADDTABS', input("addtabs") ? TRUE : FALSE);
// 定义是否Dialog请求
!defined('IS_DIALOG') && define('IS_DIALOG', input("dialog") ? TRUE : FALSE);
// 定义是否AJAX请求
!defined('IS_AJAX') && define('IS_AJAX', $this->request->isAjax());
$this->auth = Auth::instance();
// 设置当前请求的URI
$this->auth->setRequestUri($path);
// 检测是否需要验证登录
if (!$this->auth->match($this->noNeedLogin))
{
//检测是否登录
if (!$this->auth->isLogin())
{
$url = Session::get('referer');
$url = $url ? $url : $this->request->url();
$this->error(__('Please login first'), url('index/login', ['url' => $url]));
}
// 判断是否需要验证权限
if (!$this->auth->match($this->noNeedRight))
{
// 判断控制器和方法判断是否有对应权限
if (!$this->auth->check($path))
{
$this->error(__('You have no permission'), NULL);
}
}
}
// 非选项卡时重定向
if (!$this->request->isPost() && !IS_AJAX && !IS_ADDTABS && !IS_DIALOG && input("ref") == 'addtabs')
{
$url = preg_replace_callback("/([\?|&]+)ref=addtabs(&?)/i", function($matches) {
return $matches[2] == '&' ? $matches[1] : '';
}, $this->request->url());
$this->redirect('index/index', [], 302, ['referer' => $url]);
exit;
}
// 设置面包屑导航数据
$breadcrumb = $this->auth->getBreadCrumb($path);
array_pop($breadcrumb);
$this->view->breadcrumb = $breadcrumb;
// 如果有使用模板布局
if ($this->layout)
{
$this->view->engine->layout('layout/' . $this->layout);
}
// 语言检测
$lang = Lang::detect();
$site = Config::get("site");
// 配置信息
$config = [
'site' => array_intersect_key($site, array_flip(['name', 'cdnurl', 'version', 'timezone', 'languages'])),
'upload' => Configvalue::upload(),
'modulename' => $modulename,
'controllername' => $controllername,
'actionname' => $actionname,
'jsname' => 'backend/' . str_replace('.', '/', $controllername),
'moduleurl' => url("/{$modulename}", '', false),
'language' => $lang,
'referer' => Session::get("referer")
];
$this->loadlang($controllername);
$this->assign('site', $site);
$this->assign('config', $config);
$this->assign('admin', Session::get('admin'));
}
/**
* 加载语言文件
* @param string $name
*/
protected function loadlang($name)
{
Lang::load(APP_PATH . $this->request->module() . '/lang/' . Lang::detect() . '/' . str_replace('.', '/', $name) . '.php');
}
/**
* 生成查询所需要的条件,排序方式
* @param mixed $searchfields 查询条件
* @param boolean $relationSearch 是否关联查询
* @return array
*/
protected function buildparams($searchfields = null, $relationSearch = null)
{
$searchfields = is_null($searchfields) ? $this->searchFields : $searchfields;
$relationSearch = is_null($relationSearch) ? $this->relationSearch : $relationSearch;
$search = $this->request->get("search", '');
$filter = $this->request->get("filter", '');
$op = $this->request->get("op", '');
$sort = $this->request->get("sort", "id");
$order = $this->request->get("order", "DESC");
$offset = $this->request->get("offset", 0);
$limit = $this->request->get("limit", 0);
$filter = json_decode($filter, TRUE);
$op = json_decode($op, TRUE);
$filter = $filter ? $filter : [];
$where = [];
$modelName = '';
if ($relationSearch)
{
if (!empty($this->model))
{
$class = get_class($this->model);
$name = basename(str_replace('\\', '/', $class));
$name = strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $name));
$modelName = $name . ".";
}
if (stripos($sort, ".") === false)
{
$sort = $modelName . $sort;
}
}
if ($search)
{
$searcharr = is_array($searchfields) ? $searchfields : explode(',', $searchfields);
$searchlist = [];
foreach ($searcharr as $k => $v)
{
$searchlist[] = (stripos($v, ".") !== false ? $v : "{$modelName}`{$v}`") . " LIKE '%{$search}%'";
}
$where[] = "(" . implode(' OR ', $searchlist) . ")";
}
foreach ($filter as $k => $v)
{
$sym = isset($op[$k]) ? $op[$k] : '=';
if (stripos($k, ".") === false)
{
$k = $modelName . $k;
}
$sym = isset($op[$k]) ? $op[$k] : $sym;
switch ($sym)
{
case '=':
case '!=':
case 'LIKE':
case 'NOT LIKE':
$where[] = [$k, $sym, $v];
break;
case '>':
case '>=':
case '<':
case '<=':
$where[] = [$k, $sym, intval($v)];
break;
case 'IN(...)':
case 'NOT IN(...)':
$where[] = [$k, str_replace('(...)', '', $sym), explode(',', $v)];
break;
case 'BETWEEN':
case 'NOT BETWEEN':
$where[] = [$k, $sym, array_slice(explode(',', $v), 0, 2)];
break;
case 'LIKE %...%':
$where[] = [$k, 'LIKE', "%{$v}%"];
break;
case 'IS NULL':
case 'IS NOT NULL':
$where[] = [$k, strtolower(str_replace('IS ', '', $sym))];
break;
default:
break;
}
}
$where = function($query) use ($where) {
foreach ($where as $k => $v)
{
if (is_array($v))
{
call_user_func_array([$query, 'where'], $v);
}
else
{
$query->where($v);
}
}
};
return [$where, $sort, $order, $offset, $limit];
}
/**
* 析构方法
*
*/
public function __destruct()
{
//判断是否设置code值,如果有则变动response对象的正文
if (!is_null($this->code))
{
$this->result($this->data, $this->code, $this->msg, 'json');
}
}
}