validate([ 'username' => 'required', 'password' => 'required', ]); $user = User::where('phone', $request->input('username'))->first(); if (!$user) { throw new BaseException('手机号不存在'); } if (!Hash::check($request->input('password'), $user->password)) { throw new BaseException('用户名或密码错误'); } // 更新第三方账户 $openid = $request->input('openid'); $open_type = $request->input('open_type'); if ($openid && $open_type) { UserSocialite::where(['openid' => $openid, 'type' => SocialiteType::from($open_type), 'user_type' => $user->getMorphClass()])->update([ 'user_id' => $user->id, ]); } $token = $user->createToken('client')->plainTextToken; return $this->response()->success(['token' => $token, 'user' => $user]); } public function register(Request $request) { $service = UserService::make(); if (!$service->store($request->all())) { throw new BaseException($service->getError()); } $user = User::where('phone', $request->input('phone'))->first(); // 更新第三方账户 $openid = $request->input('openid'); $open_type = $request->input('open_type'); if ($openid && $open_type) { UserSocialite::where(['openid' => $openid, 'type' => SocialiteType::from($open_type), 'user_type' => $user->getMorphClass()])->update([ 'user_id' => $user->id, ]); } $token = $user->createToken('client')->plainTextToken; return $this->response()->success(['token' => $token, 'user' => $user]); } }