123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- <?php
- namespace api\modules\v1\controllers;
- use api\common\behaviors\QueryParamAuth;
- use api\common\controllers\Controller;
- use api\common\models\User;
- use api\modules\v1\models\LoginForm;
- use common\enums\CodeEnum;
- use common\helpers\wx\Application;
- use common\helpers\wx\mini\PhoneNumber;
- use Yii;
- use yii\helpers\ArrayHelper;
- use yii\web\ServerErrorHttpException;
- class AuthController extends Controller
- {
- public function behaviors()
- {
- return ArrayHelper::merge(parent::behaviors(), [
- [
- 'class' => QueryParamAuth::className(),
- 'tokenParam' => 'token',
- 'optional' => ['login', 'login-by-id']
- ]
- ]);
- }
-
- public function actionLogin()
- {
- $model = new LoginForm();
- $model->load(request()->post(), '');
- if ($model->login()) {
-
- $user = \Yii::$app->user->identity;
- $user->generateAccessToken(request()->headers->get('token'));
- $user->save(false);
- return ['data' => User::findOne($user->id)];
- } else {
- return ['errcode' => CodeEnum::CODE_ERROR, 'errmsg' => '登录失败' . current($model->getErrors())[0]];
- }
- }
-
- public function actionLogout()
- {
- if (\Yii::$app->user->logout()) {
- return [];
- }
- }
- }
|