-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInfoController.php
More file actions
128 lines (112 loc) · 4.2 KB
/
InfoController.php
File metadata and controls
128 lines (112 loc) · 4.2 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
<?php
namespace app\admin\controller;
use app\admin\controller\repository\AdminRepository as Repository;
use app\components\Component;
use app\exception\ValidationException;
use app\model\Admin as Model;
use support\facade\Auth;
use support\Request;
use Webman\Http\Response;
use WebmanTech\AmisAdmin\Amis\DetailAttribute;
use WebmanTech\AmisAdmin\Amis\FormField;
use WebmanTech\AmisAdmin\Amis\Page;
use WebmanTech\AmisAdmin\Repository\AbsRepository;
/**
* 当前登录用户信息.
*/
class InfoController
{
/**
* @var string|class-string<AbsRepository>
*/
protected string $repository = Repository::class;
protected string $routeInfo = 'admin.info';
protected string $routeInfoUpdate = 'admin.info.update';
protected string $routeChangePassword = 'admin.info.changePassword';
/**
* 用户信息页面.
*/
public function page(): Response
{
$page = Page::make()
->withBody(1, [
'type' => 'tabs',
'tabsMode' => 'strong',
'tabs' => [
[
'title' => '基本信息',
'tab' => [
'type' => 'form',
'title' => '基本信息',
'mode' => 'horizontal',
'initApi' => route($this->routeInfo),
'api' => 'post:' . route($this->routeInfoUpdate),
'body' => [
DetailAttribute::make()->name('username')->label('用户名'),
FormField::make()->name('name')->label('名称')->required(),
],
],
],
[
'title' => '修改密码',
'tab' => [
'type' => 'form',
'title' => '修改密码',
'mode' => 'horizontal',
'api' => 'post:' . route($this->routeChangePassword),
'body' => [
['type' => 'alert', 'body' => '修改密码后会刷新 Access Token', 'level' => 'info', 'showIcon' => true],
FormField::make()->name('old_password')->label('原密码')->required()->typeInputPassword(),
FormField::make()->name('new_password')->label('新密码')->required()->typeInputPassword(),
FormField::make()->name('new_password_confirmation')->label('新密码确认')->required()->typeInputPassword(),
],
],
],
],
]);
return admin_response($page);
}
/**
* 登录用户信息.
*/
public function index(): Response
{
return admin_response(Auth::guard()->getUser());
}
/**
* 更新用户信息.
*/
public function update(Request $request): Response
{
$this->repository()->update($request->only(['name']), Auth::guard()->getId());
return admin_response('ok');
}
/**
* 修改面目.
*/
public function changePassword(Request $request): Response
{
$validator = validator($request->only(['old_password']), [
'old_password' => 'required|string',
]);
$data = $validator->validate();
/** @var Model $model */
$model = Auth::guard()->getUser();
if (!Component::security()->validatePassword($data['old_password'], $model->password)) {
throw new ValidationException(['old_password' => '原密码错误']);
}
$repository = $this->repository();
if (!method_exists($repository, 'resetPassword')) {
throw new \Exception('repository 必须实现 resetPassword 方法');
}
$repository->resetPassword($request->only(['new_password', 'new_password_confirmation']), $model->id);
return (new AuthController())->logout();
}
/**
* @return AbsRepository
*/
protected function repository(): AbsRepository
{
return new $this->repository();
}
}