AuthController.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. namespace addons\Store\api\modules\v2\controllers;
  3. use addons\Store\common\models\StoreAdmin;
  4. use Yii;
  5. use api\controllers\OnAuthController;
  6. use yii\db\Exception;
  7. /**
  8. * 默认控制器
  9. *
  10. * Class DefaultController
  11. * @package addons\Store\api\modules\v2\controllers
  12. */
  13. class AuthController extends OnAuthController
  14. {
  15. public $modelClass = '';
  16. /**
  17. * 不用进行登录验证的方法
  18. *
  19. * 例如: ['index', 'update', 'create', 'view', 'delete']
  20. * 默认全部需要验证
  21. *
  22. * @var array
  23. */
  24. protected $authOptional = ['index','login'];
  25. public function actionLogin(){
  26. $params = Yii::$app->request->post();
  27. $res = Yii::$app->services->validate->validate($params, [
  28. [['username'], 'required'],
  29. [['password'], 'safe'],
  30. ]);
  31. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  32. $params['mall_id'] = $this->mall_id;
  33. $token = StoreAdmin::loginByH5($params);
  34. if(empty($token['access_token'])) return $this->error(StoreAdmin::getStaticError());
  35. $token['access_store_token'] = $token['access_token'];
  36. unset($token['access_token']);
  37. return $this->success('登录成功', $token);
  38. }
  39. }