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.

67 lines
1.5 KiB

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