SiteController.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. namespace api\modules\v1\controllers;
  3. use Yii;
  4. use yii\web\NotFoundHttpException;
  5. use common\helpers\ResultHelper;
  6. use common\helpers\ArrayHelper;
  7. use common\models\member\Member;
  8. use api\modules\v1\forms\UpPwdForm;
  9. use api\controllers\OnAuthController;
  10. use api\modules\v1\forms\LoginForm;
  11. use api\modules\v1\forms\RefreshForm;
  12. use api\modules\v1\forms\MobileLogin;
  13. use api\modules\v1\forms\SmsCodeForm;
  14. use api\modules\v1\forms\RegisterForm;
  15. /**
  16. * 登录接口
  17. *
  18. *
  19. * Class SiteController
  20. * @package api\modules\v1\controllers
  21. * @author qimall
  22. */
  23. class SiteController extends OnAuthController
  24. {
  25. public $modelClass = '';
  26. /**
  27. * 不用进行登录验证的方法
  28. *
  29. * 例如: ['index', 'update', 'create', 'view', 'delete']
  30. * 默认全部需要验证
  31. *
  32. * @var array
  33. */
  34. protected $authOptional = ['login', 'refresh', 'mobile-login', 'sms-code', 'register', 'up-pwd'];
  35. /**
  36. * 登录根据用户信息返回accessToken
  37. *
  38. * @return array|bool
  39. * @throws NotFoundHttpException
  40. * @throws \yii\base\Exception
  41. */
  42. public function actionLogin()
  43. {
  44. // 返回数据验证失败
  45. return ResultHelper::json(422, '11');
  46. }
  47. /**
  48. * 登出
  49. *
  50. * @return array|mixed
  51. */
  52. public function actionLogout()
  53. {
  54. if (Yii::$app->services->apiAccessToken->disableByAccessToken(Yii::$app->user->identity->access_token)) {
  55. return ResultHelper::json(200, '退出成功');
  56. }
  57. return ResultHelper::json(422, '退出失败');
  58. }
  59. /**
  60. * 重置令牌
  61. *
  62. * @param $refresh_token
  63. * @return array
  64. * @throws NotFoundHttpException
  65. * @throws \yii\base\Exception
  66. */
  67. public function actionRefresh()
  68. {
  69. $model = new RefreshForm();
  70. $model->attributes = Yii::$app->request->post();
  71. if (!$model->validate()) {
  72. return ResultHelper::json(422, $this->getError($model));
  73. }
  74. return Yii::$app->services->apiAccessToken->getAccessToken($model->getUser(), $model->group);
  75. }
  76. /**
  77. * 手机验证码登录Demo
  78. *
  79. * @return array|mixed
  80. * @throws \yii\base\Exception
  81. */
  82. private function actionMobileLogin()
  83. {
  84. $model = new MobileLogin();
  85. $model->attributes = Yii::$app->request->post();
  86. if ($model->validate()) {
  87. return Yii::$app->services->apiAccessToken->getAccessToken($model->getUser(), $model->group);
  88. }
  89. // 返回数据验证失败
  90. return ResultHelper::json(422, $this->getError($model));
  91. }
  92. /**
  93. * 获取验证码
  94. *
  95. * @return int|mixed
  96. * @throws \yii\web\UnprocessableEntityHttpException
  97. */
  98. public function actionSmsCode()
  99. {
  100. $model = new SmsCodeForm();
  101. $model->attributes = Yii::$app->request->post();
  102. if (!$model->validate()) {
  103. return ResultHelper::json(422, $this->getError($model));
  104. }
  105. return $model->send();
  106. }
  107. /**
  108. * 注册
  109. *
  110. * @return array|mixed
  111. * @throws \yii\base\Exception
  112. */
  113. private function actionRegister()
  114. {
  115. $model = new RegisterForm();
  116. $model->attributes = Yii::$app->request->post();
  117. if (!$model->validate()) {
  118. return ResultHelper::json(422, $this->getError($model));
  119. }
  120. $member = new Member();
  121. $member->attributes = ArrayHelper::toArray($model);
  122. $member->merchant_id = !empty($this->getMerchantId()) ? $this->getMerchantId() : 0;
  123. $member->password_hash = Yii::$app->security->generatePasswordHash($model->password);
  124. if (!$member->save()) {
  125. return ResultHelper::json(422, $this->getError($member));
  126. }
  127. return Yii::$app->services->apiAccessToken->getAccessToken($member, $model->group);
  128. }
  129. /**
  130. * 密码重置
  131. *
  132. * @return array|mixed
  133. * @throws \yii\base\Exception
  134. */
  135. private function actionUpPwd()
  136. {
  137. $model = new UpPwdForm();
  138. $model->attributes = Yii::$app->request->post();
  139. if (!$model->validate()) {
  140. return ResultHelper::json(422, $this->getError($model));
  141. }
  142. $member = $model->getUser();
  143. $member->password_hash = Yii::$app->security->generatePasswordHash($model->password);
  144. if (!$member->save()) {
  145. return ResultHelper::json(422, $this->getError($member));
  146. }
  147. return Yii::$app->services->apiAccessToken->getAccessToken($member, $model->group);
  148. }
  149. /**
  150. * 权限验证
  151. *
  152. * @param string $action 当前的方法
  153. * @param null $model 当前的模型类
  154. * @param array $params $_GET变量
  155. * @throws \yii\web\BadRequestHttpException
  156. */
  157. public function checkAccess($action, $model = null, $params = [])
  158. {
  159. // 方法名称
  160. if (in_array($action, ['index', 'view', 'update', 'create', 'delete'])) {
  161. throw new \yii\web\BadRequestHttpException('权限不足');
  162. }
  163. }
  164. }