SiteController.php
5.35 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
<?php
namespace app\ht\controllers;
use Yii;
use yii\filters\AccessControl;
use app\ht\helpers\Password;
use common\helpers\ImageManager;
use common\helpers\Utils;
use common\models\SysUser as SysUserModel;
use domain\admin\models\LoginLog as LoginLogModel;
use function json_encode;
use function time;
use function explode;
use function date;
use function end;
use function round;
use function in_array;
use function implode;
use function rand;
/**
* Class SiteController
* @package app\ht\controllers
*/
class SiteController extends BaseController
{
/**
* @param \yii\base\Action $action
* @return bool
* @throws \yii\web\BadRequestHttpException
*/
public function beforeAction($action)
{
$this->enableCsrfValidation = false;
return parent::beforeAction($action);
}
/**
* @inheritdoc
*/
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'actions' => ['login', 'error', 'forgot', 'reset', 'do-login'],
'allow' => true,
// 'roles' => ['?'],
],
[
'actions' => ['logout', 'index', 'region', 'test', 'editor-upload'],
'allow' => true,
'roles' => ['@'],
],
],
],
];
}
/**
* author: alen <948306245@qq.com>
* 默认首页 (工程师列表)
* 可以在config/main.php中重写 defaultRoute
* @return string
*/
public function actionIndex()
{
//return $this->redirect("home/welcome/index");
$this->layout = '/main';
$username = Yii::$app->user->identity->username;
return $this->render('index', [
'username' => $username,
]);
}
/**
* 错误页面
* @return string
*/
public function actionError()
{
$this->layout = '/error';
$exception = Yii::$app->errorHandler->exception;
$code = isset($exception->statusCode)?$exception->statusCode:404;
if (Yii::$app->request->isAjax) {
return json_encode(['success' => false]);
}
// 针对 $.ajaxFileUpload控件,无法识别是ajax
// $routes = [
// 'trade/order-import/order-import',
// 'trade/order/express-import'
// ];
// foreach ($routes as $r) {
// if (strpos($_SERVER['REQUEST_URI'], $r) !== false) {
// return json_encode(['success' => false]);
// }
// }
$viewTpl = 'error/' . $code . '.php';
return $this->render($viewTpl);
}
/**
* 登陆页面
* @return string
*/
public function actionLogin()
{
$this->layout = '/login';
return $this->render('login');
}
/**
* 执行登陆动作
* @return \yii\web\Response
*/
public function actionDoLogin()
{
$post = Yii::$app->request->post();
if (empty($post['name']) || empty($post['password'])) {
Yii::$app->session->setFlash('error', '请输入账号和密码');
return $this->redirect('site/login');
}
/*
* 接收表单提交的数据
*/
$username = $post['name'];
$password = $post['password'];
$sysUser = SysUserModel::findOne(['username' => $username]);
if (!$sysUser) {
Yii::$app->session->setFlash('error', '账号不存在,请重新输入');
return $this->redirect(['site/login']);
} elseif ($sysUser->is_enable == 0) {
Yii::$app->session->setFlash('error', '账号被禁用, 请联系管理员');
return $this->redirect(['site/login']);
} elseif (Password::validate($password, $sysUser['password_hash'])) { // 验证密码
// 使用配置文件的登录超时常量
$duration = Yii::$app->user->authTimeout;
Yii::$app->getUser()->login($sysUser, $duration);
// 记录登录这信息
$log = new LoginLogModel();
$log->user_id = $sysUser->id;
$log->username = $sysUser->username;
$log->client_ip = Utils::clientIp();
$log->client = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
$log->referer_url = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
$log->created_at = time();
$log->save();
return $this->goHome();
} else {
Yii::$app->session->setFlash('error', '账号与密码不匹配,请重新输入');
return $this->redirect(['site/login']);
}
}
/**
* 执行退出动作
* @return \yii\web\Response
*/
public function actionLogout()
{
Yii::$app->user->logout();
return $this->goHome();
}
/**
* 编辑器服务端配置
*/
public function actionEditorUpload()
{
$maxSize = 1048576; // 1024 * 1024 = 1MB
}
/**
* 编辑器提示框显示
* @param $msg
*/
protected function editorAlert($msg)
{
echo json_encode(array('error' => 1, 'message' => $msg));
exit;
}
}