小程序用户改用门店登录
This commit is contained in:
@@ -34,8 +34,7 @@ class CartController extends BaseMiniController
|
||||
#[PostRoute('/cart')]
|
||||
public function store(MiniCartRequest $request): JsonResponse
|
||||
{
|
||||
$user = $this->currentUser($request);
|
||||
$store = $this->ensureStoreBound($user);
|
||||
$store = $this->currentStore($request);
|
||||
if ($store->level_id <= 0 || $store->level === null) {
|
||||
return $this->error('门店未设置客户等级,无法加购,请联系客服');
|
||||
}
|
||||
@@ -48,9 +47,9 @@ class CartController extends BaseMiniController
|
||||
|
||||
$quantity = (string) $request->validated('quantity');
|
||||
|
||||
$cart = DB::transaction(function () use ($user, $productId, $quantity) {
|
||||
$cart = DB::transaction(function () use ($store, $productId, $quantity) {
|
||||
$row = CartModel::query()
|
||||
->where('user_id', $user->id)
|
||||
->where('store_id', $store->id)
|
||||
->where('product_id', $productId)
|
||||
->lockForUpdate()
|
||||
->first();
|
||||
@@ -67,7 +66,7 @@ class CartController extends BaseMiniController
|
||||
}
|
||||
|
||||
return CartModel::create([
|
||||
'user_id' => $user->id,
|
||||
'store_id' => $store->id,
|
||||
'product_id' => $productId,
|
||||
'quantity' => $quantity,
|
||||
]);
|
||||
@@ -85,11 +84,10 @@ class CartController extends BaseMiniController
|
||||
#[GetRoute('/cart', authorize: true)]
|
||||
public function index(Request $request): JsonResponse
|
||||
{
|
||||
$user = $this->currentUser($request);
|
||||
$store = $this->ensureStoreBound($user);
|
||||
$store = $this->currentStore($request);
|
||||
|
||||
$rows = CartModel::query()
|
||||
->where('user_id', $user->id)
|
||||
->where('store_id', $store->id)
|
||||
->orderBy('id', 'desc')
|
||||
->get();
|
||||
|
||||
@@ -174,12 +172,11 @@ class CartController extends BaseMiniController
|
||||
#[PutRoute('/cart/{id}', authorize: true, where: ['id' => '[0-9]+'])]
|
||||
public function update(int $id, MiniCartRequest $request): JsonResponse
|
||||
{
|
||||
$user = $this->currentUser($request);
|
||||
$this->ensureStoreBound($user);
|
||||
$store = $this->currentStore($request);
|
||||
|
||||
$row = CartModel::query()
|
||||
->where('id', $id)
|
||||
->where('user_id', $user->id)
|
||||
->where('store_id', $store->id)
|
||||
->first();
|
||||
if ($row === null) {
|
||||
throw new RepositoryException('购物车项不存在');
|
||||
@@ -197,12 +194,11 @@ class CartController extends BaseMiniController
|
||||
#[DeleteRoute('/cart/{id}', authorize: true, where: ['id' => '[0-9]+'])]
|
||||
public function destroy(int $id, Request $request): JsonResponse
|
||||
{
|
||||
$user = $this->currentUser($request);
|
||||
$this->ensureStoreBound($user);
|
||||
$store = $this->currentStore($request);
|
||||
|
||||
$deleted = CartModel::query()
|
||||
->where('id', $id)
|
||||
->where('user_id', $user->id)
|
||||
->where('store_id', $store->id)
|
||||
->delete();
|
||||
if ($deleted === 0) {
|
||||
throw new RepositoryException('购物车项不存在');
|
||||
@@ -217,10 +213,9 @@ class CartController extends BaseMiniController
|
||||
#[DeleteRoute('/cart', authorize: true)]
|
||||
public function clear(Request $request): JsonResponse
|
||||
{
|
||||
$user = $this->currentUser($request);
|
||||
$this->ensureStoreBound($user);
|
||||
$store = $this->currentStore($request);
|
||||
|
||||
CartModel::query()->where('user_id', $user->id)->delete();
|
||||
CartModel::query()->where('store_id', $store->id)->delete();
|
||||
|
||||
return $this->success([], '购物车已清空');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user