游客结账登录中间件,游客登录购物车合并
This commit is contained in:
parent
99c32242cb
commit
4cb3bfda91
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -0,0 +1,72 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use Beike\Models\Customer;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Auth\AuthenticationException;
|
||||
use Illuminate\Auth\Middleware\Authenticate as Middleware;
|
||||
|
||||
class CheckoutAuthenticate extends Middleware
|
||||
{
|
||||
/**
|
||||
* Handle an incoming request.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param \Closure $next
|
||||
* @param string[] ...$guards
|
||||
* @return mixed
|
||||
*
|
||||
* @throws AuthenticationException
|
||||
*/
|
||||
public function handle($request, \Closure $next, ...$guards)
|
||||
{
|
||||
if (system_setting('base.guest_checkout', 1)) {
|
||||
return $next($request);
|
||||
}
|
||||
|
||||
$this->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)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -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)
|
||||
);
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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'));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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'));
|
||||
|
|
|
|||
|
|
@ -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');
|
||||
|
|
|
|||
Loading…
Reference in New Issue