cashwarden /
api
| 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|\Throwable |
||
| 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 [ |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 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 | /** |
||
| 73 | * @return array |
||
| 74 | * @throws \yii\base\Exception |
||
| 75 | */ |
||
| 76 | public function actionResetToken() |
||
| 77 | { |
||
| 78 | /** @var User $user */ |
||
| 79 | $user = Yii::$app->user->identity; |
||
| 80 | $this->userService->setPasswordResetToken($user); |
||
| 81 | return [ |
||
| 82 | 'reset_token' => $user->password_reset_token, |
||
| 83 | 'expire_in' => params('user.passwordResetTokenExpire') |
||
| 84 | ]; |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @return array |
||
| 89 | */ |
||
| 90 | public function actionGetAuthClients() |
||
| 91 | { |
||
| 92 | return $this->userService->getAuthClients(); |
||
| 93 | } |
||
| 94 | } |
||
| 95 |