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
<?php
namespace backend\controllers;
use common\core\Controller;
use Yii;
use yii\data\ActiveDataProvider;
use yii\data\Pagination;
use yii\helpers\Url;
use common\modelsgii\Admin;
/**
* ---------------------------------------
* 后台父类控制器
* 后台所有控制器都继承自该类
* ---------------------------------------
* @author longfei phphome@qq.com
*/
class BaseController extends Controller
{
/**
* ---------------------------------------
* 后台构造函数
* ---------------------------------------
*/
public function init()
{
/* 判断是否登录 */
/*if (\Yii::$app->user->getIsGuest()) {
$this->redirect(Url::toRoute(['/login/login']));
Yii::$app->end();
}*/
/*if (!Yii::$app->request->isGet) {
AdminLog::saveLog($this);
}*/
}
/**
* ---------------------------------------
* 标记当前位置到cookie供后续跳转调用
* ---------------------------------------
*/
public function setForward()
{
\Yii::$app->getSession()->setFlash('__forward__', $_SERVER['REQUEST_URI']);
}
/**
* ---------------------------------------
* 获取之前标记的cookie位置
* ---------------------------------------
* @param string $default
* @return mixed
*/
public function getForward($default = '')
{
$default = $default ? $default : Url::toRoute([Yii::$app->controller->id . '/index']);
if (Yii::$app->getSession()->hasFlash('__forward__')) {
return Yii::$app->getSession()->getFlash('__forward__');
} else {
return $default;
}
}
/**
* ---------------------------------------
* 传统分页列表数据集获取方法
* ---------------------------------------
* @param \yii\db\ActiveRecord $model 模型名或模型实例
* @param array $where where查询条件
* @param array|string $order 排序条件
* @return array|false
*/
public function lists($model, $where = [], $order = '')
{
$query = $model::find()->where($where);
$countQuery = clone $query;
$pages = new Pagination([
'totalCount' => $countQuery->count(),
'defaultPageSize' => 10,
]);
$data = $query->orderBy($order)->offset($pages->offset)
->limit($pages->limit)
->all();
return [$data, $pages];
}
/**
* ---------------------------------------
* dataProvider列表数据集获取方法
* ---------------------------------------
* @param \yii\db\ActiveRecord $model 模型名或模型实例
* @param array $where where查询条件
* @param array|string $order 排序条件
* @return \yii\data\ActiveDataProvider
*/
public function lists1($model, $where = [], $order = '')
{
$query = $model::find()->where($where)->orderBy($order)->asArray();
$dataProvider = new ActiveDataProvider([
'query' => $query,
'pagination' => [
'pageSize' => 10,
],
]);
return $dataProvider;
}
/**
* ---------------------------------------
* 修改数据表一条记录的一条值
* ---------------------------------------
* @param \yii\db\ActiveRecord $model 模型名称
* @param array $data 数据
* @return \yii\db\ActiveRecord|boolean
*/
public function saveRow($model, $data)
{
if (empty($data)) {
return false;
}
if ($model->load($data, '') && $model->validate()) {
/* 添加到数据库中,save()会自动验证rule */
if ($model->save()) {
return $model;
} else {
return false;
}
} else {
return false;
}
}
/**
* ---------------------------------------
* 由表主键删除数据表中的多条记录
* ---------------------------------------
* @param \yii\db\ActiveRecord $model 模型名称,供M函数使用的参数
* @param string $pk 修改的数据
* @return boolean
*/
public function delRow($model, $pk = 'id')
{
$ids = Yii::$app->request->param($pk, 0);
$ids = implode(',', array_unique((array) $ids));
if (empty($ids)) {
return false;
}
$_where = $pk . ' in(' . $ids . ')';
if ($model::deleteAll($_where)) {
return true;
} else {
return false;
}
}
/**
* ----------------------------------------------
* 操作错误跳转的快捷方法
* @access protected
* -----------------------------------------------
* @param string $message 错误信息
* @param string $jumpUrl 页面跳转地址
* @param mixed $ajax 是否为Ajax方式 当数字时指定跳转时间
* @return void
*/
protected function error($message = '', $jumpUrl = '', $ajax = false)
{
$this->dispatchJump($message, 0, $jumpUrl, $ajax);
}
/**
* ----------------------------------------------
* 操作成功跳转的快捷方法
* @access protected
* ----------------------------------------------
* @param string $message 提示信息
* @param string $jumpUrl 页面跳转地址
* @param mixed $ajax 是否为Ajax方式 当数字时指定跳转时间
* @return void
*/
protected function success($message = '', $jumpUrl = '', $ajax = false)
{
$this->dispatchJump($message, 1, $jumpUrl, $ajax);
}
/**
* ----------------------------------------------
* 默认跳转操作 支持错误导向和正确跳转
* 调用模板显示 默认为public目录下面的success页面
* 提示页面为可配置 支持模板标签
* @access private
* ----------------------------------------------
* @param string $message 提示信息
* @param int $status 状态
* @param string $jumpUrl 页面跳转地址
* @param mixed $ajax 是否为Ajax方式 当数字时指定跳转时间
* @return void
*/
private function dispatchJump($message, $status = 1, $jumpUrl = '', $ajax = false)
{
$jumpUrl = !empty($jumpUrl) ? (is_array($jumpUrl) ? Url::toRoute($jumpUrl) : $jumpUrl) : '';
if (true === $ajax || Yii::$app->request->isAjax) {
// AJAX提交
$data = is_array($ajax) ? $ajax : array();
$data['info'] = $message;
$data['status'] = $status;
$data['url'] = $jumpUrl;
$this->ajaxReturn($data);
}
// 成功操作后默认停留1秒
$waitSecond = 3;
if ($status) {
//发送成功信息
$message = $message ? $message : '提交成功'; // 提示信息
// 默认操作成功自动返回操作前页面
echo $this->renderFile(Yii::$app->params['action_success'], [
'message' => $message,
'waitSecond' => $waitSecond,
'jumpUrl' => $jumpUrl,
]);
} else {
$message = $message ? $message : '发生错误了'; // 提示信息
// 默认发生错误的话自动返回上页
$jumpUrl = "javascript:history.back(-1);";
echo $this->renderFile(Yii::$app->params['action_error'], [
'code' => 0,
'message' => $message,
'waitSecond' => $waitSecond,
'jumpUrl' => $jumpUrl,
]);
}
//Yii::$app->end();
exit;
}
/**
* ------------------------------------------------
* Ajax方式返回数据到客户端
* @access protected
* ------------------------------------------------
* @param mixed $data 要返回的数据
* @return void
*/
protected function ajaxReturn($data)
{
// 返回JSON数据格式到客户端 包含状态信息
header('Content-Type:application/json; charset=utf-8');
echo json_encode($data);
//Yii::$app->end();
exit;
}
/**
* 获取所有可以访问action的用户
*
* @return array
*/
public function getAccessAdmins($permission_name = '')
{
//获取所有管理员
if (empty($permission_name)) {
$permission_name = Yii::$app->controller->module->requestedRoute;
}
$auth = Yii::$app->authManager;
$admins = Admin::find()->select(['uid', 'username'])->asArray()->all();
foreach ($admins as $key => $value) {
if (!$auth->checkAccess($value['uid'], $permission_name)) {
unset($admins[$key]);
}
}
$admins = array_column($admins, 'username', 'uid');
return $admins;
}
protected function initParams($p = [],$fields = [], $isFilter = false)
{
$params = [];
if (!empty($fields)) {
foreach ($fields as $key => $value) {
if (isset($p[$value]) && (!empty($p[$value]) || $p[$value] == 0)) {
$params[$value] = $p[$value];
} else {
if (!$isFilter) $params[$value] = null;
}
}
}
return $params;
}
}