Esempio n. 1
0
 /**
  * 用户登录
  *
  * @param SignInRequest $request
  * @return \Illuminate\Http\JsonResponse
  * @throws \App\Exceptions\SignInException
  */
 public function doSignIn(SignInRequest $request)
 {
     $loginId = $request->get('login_id');
     $username = $request->get('username');
     $email = $request->get('email');
     $phone = $request->get('phone');
     $password = $request->get('password');
     $remember = $request->get('remember');
     if ($phone) {
         $user = UserManager::signInByPhone($phone, $password, $remember);
     } else {
         if ($email) {
             $user = UserManager::signInByEmail($email, $password, $remember);
         } else {
             if ($username) {
                 $user = UserManager::signInByUsername($username, $password, $remember);
             } else {
                 if (Tools::isEmail($loginId)) {
                     $user = UserManager::signInByEmail($loginId, $password, $remember);
                 } else {
                     if (Tools::isPhone($loginId)) {
                         $user = UserManager::signInByPhone($loginId, $password, $remember);
                     } else {
                         $user = UserManager::signInByUsername($loginId, $password, $remember);
                     }
                 }
             }
         }
     }
     return $this->buildResponse(trans('api.auth.sign_in.success'), Tools::toArray($user));
 }
Esempio n. 2
0
 /**
  * 用户注册
  *
  * @param $username
  * @param $email
  * @param $phone
  * @param $password
  * @return User
  * @throws SignUpException
  */
 public static function signUp($username, $email, $phone, $password)
 {
     if (!is_null($username) && self::isUsernameExists($username)) {
         throw new SignUpException(SignUpException::UsernameExists, ['username' => $username]);
     }
     if (!is_null($email) && self::isEmailExists($email)) {
         throw new SignUpException(SignUpException::EmailExists, ['email' => $email]);
     }
     if (!is_null($phone) && self::isPhoneExists($phone)) {
         throw new SignUpException(SignUpException::PhoneExists, ['phone' => $phone]);
     }
     $user = new User();
     $user->username = $username;
     $user->email = Tools::isEmail($email) ? $email : null;
     $user->phone = Tools::isPhone($phone) ? $phone : null;
     $user->password = bcrypt($password);
     $user->save();
     return $user;
 }