|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace app\modules\v1\controllers; |
|
4
|
|
|
|
|
5
|
|
|
use app\core\exceptions\InternalException; |
|
6
|
|
|
use app\core\exceptions\InvalidArgumentException; |
|
7
|
|
|
use app\core\models\User; |
|
8
|
|
|
use app\core\requests\JoinRequest; |
|
9
|
|
|
use app\core\requests\LoginRequest; |
|
10
|
|
|
use app\core\traits\ServiceTrait; |
|
11
|
|
|
use Yii; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* User controller for the `v1` module |
|
15
|
|
|
*/ |
|
16
|
|
|
class UserController extends ActiveController |
|
17
|
|
|
{ |
|
18
|
|
|
use ServiceTrait; |
|
19
|
|
|
|
|
20
|
|
|
public $modelClass = User::class; |
|
21
|
|
|
public $noAuthActions = ['join', 'login']; |
|
22
|
|
|
|
|
23
|
|
|
public function actions() |
|
24
|
|
|
{ |
|
25
|
|
|
$actions = parent::actions(); |
|
26
|
|
|
// 注销系统自带的实现方法 |
|
27
|
|
|
unset($actions['update'], $actions['index'], $actions['delete'], $actions['create']); |
|
28
|
|
|
return $actions; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @return User |
|
33
|
|
|
* @throws InternalException |
|
34
|
|
|
* @throws InvalidArgumentException |
|
35
|
|
|
*/ |
|
36
|
|
|
public function actionJoin() |
|
37
|
|
|
{ |
|
38
|
|
|
$params = Yii::$app->request->bodyParams; |
|
39
|
|
|
$data = $this->validate(new JoinRequest(), $params); |
|
40
|
|
|
|
|
41
|
|
|
/** @var JoinRequest $data */ |
|
42
|
|
|
return $this->userService->createUser($data); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @return string[] |
|
47
|
|
|
* @throws InvalidArgumentException|\Throwable |
|
48
|
|
|
*/ |
|
49
|
|
|
public function actionLogin() |
|
50
|
|
|
{ |
|
51
|
|
|
$params = Yii::$app->request->bodyParams; |
|
52
|
|
|
$this->validate(new LoginRequest(), $params); |
|
53
|
|
|
$token = $this->userService->getToken(); |
|
54
|
|
|
$user = Yii::$app->user->identity; |
|
55
|
|
|
|
|
56
|
|
|
return [ |
|
|
|
|
|
|
57
|
|
|
'user' => $user, |
|
58
|
|
|
'token' => (string)$token, |
|
59
|
|
|
]; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function actionRefreshToken() |
|
63
|
|
|
{ |
|
64
|
|
|
$user = Yii::$app->user->identity; |
|
65
|
|
|
$token = $this->userService->getToken(); |
|
66
|
|
|
return [ |
|
67
|
|
|
'user' => $user, |
|
68
|
|
|
'token' => (string)$token, |
|
69
|
|
|
]; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|