DefaultController.php
3.4 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
<?php
namespace app\ht\modules\my\controllers;
use Yii;
use yii\base\Exception;
use yii\web\NotFoundHttpException;
use app\ht\controllers\BaseController;
use app\ht\helpers\Password;
use common\models\SysUser as SysUserModel;
use common\models\SysUserProfile as SysUserProfileModel;
/**
* 账号设置
*/
class DefaultController extends BaseController
{
/**
* 账号设置
*/
public function actionIndex()
{
$model = $this->getUser();
return $this->render('index', [
'model' => $model,
]);
}
/**
* 账号设置
*/
public function actionMyIndex()
{
$model = $this->getUser();
return $this->render('my-index', [
'model' => $model,
]);
}
/**
* 更新执行动作
*/
public function actionDoUpdate()
{
$post = Yii::$app->request->post();
if (empty($post)) {
return;
}
$realname = isset($post['realname']) ? $post['realname'] : '';
$email = isset($post['email']) ? $post['email'] : '';
$mobile = isset($post['mobile']) ? $post['mobile'] : '';
$model = $this->getUser();
$transaction = Yii::$app->db->beginTransaction();
try {
$model->save();
if ($model->profile) {
$profile = $model->profile;
} else {
$profile = Yii::createObject(SysUserProfileModel::className());
$profile->admin_user_id = $model->admin_user_id;
}
$profile->realname = $realname;
$profile->email = $email;
$profile->mobile = $mobile;
$profile->save();
$transaction->commit();
} catch (Exception $e) {
Yii::$app->session->setFlash('danger', '数据保存失败,请重新操作');
$transaction->rollBack();
return $this->redirect(['index']);
}
Yii::$app->session->setFlash('success', '账号更新成功');
return $this->redirect(['index']);
}
/**
* 账号设置
*/
public function actionPassword()
{
return $this->render('password', [
]);
}
/**
* 更新密码执行动作
*/
public function actionDoPassword()
{
$post = Yii::$app->request->post();
if (empty($post)) {
return;
}
$user = $this->getUser();
// 判断原密码
if (!Password::validate($post['old_password'], $user->password_hash)) {
Yii::$app->session->setFlash('danger', '原密码不对,请重新输入');
return $this->redirect(['password']);
}
// 新密码确认
if ($post['password'] != $post['password_confirm']) {
Yii::$app->session->setFlash('danger', '新密码和确认密码不一致,请重新输入');
return $this->redirect(['password']);
}
$user->password = $post['password'];
$user->save();
Yii::$app->session->setFlash('success', '密码更新成功');
return $this->redirect(['password']);
}
protected function getUser()
{
$user = Yii::createObject(SysUserModel::className());
if (($model = $user::findOne(Yii::$app->getUser()->id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('访问页面不存在');
}
}
}