diff --git a/app/Http/Kernel.php b/app/Http/Kernel.php index 3bf4ab8c..5de1dc97 100644 --- a/app/Http/Kernel.php +++ b/app/Http/Kernel.php @@ -72,6 +72,7 @@ class Kernel extends HttpKernel protected $routeMiddleware = [ 'admin_auth' => \App\Http\Middleware\Authenticate::class, 'shop_auth' => \App\Http\Middleware\ShopAuthenticate::class, + 'checkout_auth' => \App\Http\Middleware\CheckoutAuthenticate::class, 'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class, 'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class, 'can' => \Illuminate\Auth\Middleware\Authorize::class, diff --git a/app/Http/Middleware/CheckoutAuthenticate.php b/app/Http/Middleware/CheckoutAuthenticate.php new file mode 100644 index 00000000..3e906c94 --- /dev/null +++ b/app/Http/Middleware/CheckoutAuthenticate.php @@ -0,0 +1,72 @@ +authenticate($request, $guards); + + $customer = current_customer(); + if ($customer->status != 1) { + Auth::guard(Customer::AUTH_GUARD)->logout(); + return redirect(shop_route('login.index')); + } + + return $next($request); + } + + + /** + * Get the path the user should be redirected to when they are not authenticated. + * + * @param Request $request + */ + protected function redirectTo($request) + { + if (!$request->expectsJson()) { + return shop_route('login.index'); + } + } + + + /** + * Handle an unauthenticated user. + * + * @param Request $request + * @param array $guards + * @return void + * + * @throws AuthenticationException + */ + protected function unauthenticated($request, array $guards) + { + if (system_setting('base.guest_checkout', 1)) { + return; + } + throw new AuthenticationException( + trans('common.unauthenticated'), $guards, $this->redirectTo($request) + ); + } +} diff --git a/app/Http/Middleware/ShopAuthenticate.php b/app/Http/Middleware/ShopAuthenticate.php index 1ff4807f..88a90d05 100644 --- a/app/Http/Middleware/ShopAuthenticate.php +++ b/app/Http/Middleware/ShopAuthenticate.php @@ -25,7 +25,7 @@ class ShopAuthenticate extends Middleware $this->authenticate($request, $guards); $customer = current_customer(); - if ($customer && $customer->status != 1) { + if ($customer->status != 1) { Auth::guard(Customer::AUTH_GUARD)->logout(); return redirect(shop_route('login.index')); } @@ -58,9 +58,6 @@ class ShopAuthenticate extends Middleware */ protected function unauthenticated($request, array $guards) { - if (system_setting('base.guest_checkout', 1)) { - return; - } throw new AuthenticationException( trans('common.unauthenticated'), $guards, $this->redirectTo($request) ); diff --git a/beike/Repositories/CartRepo.php b/beike/Repositories/CartRepo.php index 07860ae6..0d4eb857 100644 --- a/beike/Repositories/CartRepo.php +++ b/beike/Repositories/CartRepo.php @@ -114,6 +114,18 @@ class CartRepo } + /** + * 获取所有购物车商品列表 + * + * @param $customerId + * @return Builder[]|Collection + */ + public static function allCartProducts($customerId) + { + return self::allCartProductsBuilder($customerId)->get(); + } + + /** * 当前购物车所有商品 builder * @@ -133,4 +145,16 @@ class CartRepo return $builder; } + + public static function mergeGuestCart($customer, $guestCartProducts) + { + $guestCartProductSkuIds = $guestCartProducts->pluck('product_sku_id'); + self::allCartProductsBuilder($customer->id)->whereIn('product_sku_id', $guestCartProductSkuIds)->delete(); + + foreach ($guestCartProducts as $cartProduct) { + $cartProduct->customer_id = $customer->id; + $cartProduct->save(); + } + + } } diff --git a/beike/Shop/Http/Controllers/Account/LoginController.php b/beike/Shop/Http/Controllers/Account/LoginController.php index d374ed84..41aa11b3 100644 --- a/beike/Shop/Http/Controllers/Account/LoginController.php +++ b/beike/Shop/Http/Controllers/Account/LoginController.php @@ -12,6 +12,7 @@ namespace Beike\Shop\Http\Controllers\Account; use Beike\Models\Customer; +use Beike\Repositories\CartRepo; use Illuminate\Support\Facades\Auth; use Beike\Shop\Http\Requests\LoginRequest; use Beike\Shop\Http\Controllers\Controller; @@ -35,6 +36,7 @@ class LoginController extends Controller public function store(LoginRequest $request) { + $guestCartProduct = CartRepo::allCartProducts(0); if (!auth(Customer::AUTH_GUARD)->attempt($request->only('email', 'password'))) { throw new NotAcceptableHttpException(trans('shop/login.email_or_password_error')); } @@ -44,6 +46,9 @@ class LoginController extends Controller Auth::guard(Customer::AUTH_GUARD)->logout(); throw new NotFoundHttpException(trans('shop/login.customer_inactive')); } + + CartRepo::mergeGuestCart($customer, $guestCartProduct); + return json_success(trans('shop/login.login_successfully')); } } diff --git a/beike/Shop/Http/Controllers/Account/LogoutController.php b/beike/Shop/Http/Controllers/Account/LogoutController.php index 8fbf1fb7..796bea85 100644 --- a/beike/Shop/Http/Controllers/Account/LogoutController.php +++ b/beike/Shop/Http/Controllers/Account/LogoutController.php @@ -26,6 +26,7 @@ class LogoutController extends Controller { Auth::guard(Customer::AUTH_GUARD)->logout(); + $request->session()->regenerate(); $request->session()->regenerateToken(); return redirect(shop_route('login.index')); diff --git a/beike/Shop/Routes/shop.php b/beike/Shop/Routes/shop.php index 27f5e369..ec05657e 100644 --- a/beike/Shop/Routes/shop.php +++ b/beike/Shop/Routes/shop.php @@ -63,7 +63,7 @@ Route::prefix('/') Route::get('register', [RegisterController::class, 'index'])->name('register.index'); Route::post('register', [RegisterController::class, 'store'])->name('register.store'); - Route::middleware('shop_auth:' . Customer::AUTH_GUARD) + Route::middleware('checkout_auth:' . Customer::AUTH_GUARD) ->group(function () { Route::get('carts', [CartController::class, 'index'])->name('carts.index'); Route::post('carts', [CartController::class, 'store'])->name('carts.store');