You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
1.5 KiB

  1. <?php
  2. namespace api\logic;
  3. use common\models\User;
  4. use yii\base\Exception;
  5. use Yii;
  6. use yii\web\BadRequestHttpException;
  7. use yii\web\NotFoundHttpException;
  8. use yii\web\ServerErrorHttpException;
  9. class Login
  10. {
  11. const EXPIRE_TIME = 7 * 60 * 60 * 24;
  12. /**
  13. * @return string
  14. * @throws Exception
  15. * @throws NotFoundHttpException
  16. * @throws ServerErrorHttpException
  17. */
  18. public static function wxLogin()
  19. {
  20. $user = self::getWxUser();
  21. Yii::$app->user->login($user, static::EXPIRE_TIME);
  22. $user->expire_at = time() + static::EXPIRE_TIME;
  23. $user->access_token = Yii::$app->security->generateRandomString();
  24. if (!$user->save()) {
  25. throw new ServerErrorHttpException('更新登录信息失败');
  26. }
  27. return $user->access_token;
  28. }
  29. /**
  30. * @return array|User|null
  31. * @throws BadRequestHttpException
  32. * @throws NotFoundHttpException
  33. */
  34. protected static function getWxUser()
  35. {
  36. $openid = Yii::$app->request->getBodyParam('openid');
  37. if (!$openid) {
  38. throw new BadRequestHttpException('缺少参数openid');
  39. }
  40. $user = User::find()
  41. ->where(['status' => User::STATUS_ACTIVE])
  42. ->andWhere(['wx_openid' => $openid])
  43. ->one();
  44. if (!$user) {
  45. throw new NotFoundHttpException('用户未找到');
  46. }
  47. return $user;
  48. }
  49. public function miniProgramLogin()
  50. {
  51. }
  52. protected function getMiniUser()
  53. {
  54. }
  55. }