库存不足
This commit is contained in:
parent
87e8b16a38
commit
3503560202
|
|
@ -3,6 +3,7 @@
|
||||||
namespace Beike\Shop\Http\Controllers;
|
namespace Beike\Shop\Http\Controllers;
|
||||||
|
|
||||||
use Beike\Models\ProductSku;
|
use Beike\Models\ProductSku;
|
||||||
|
use Beike\Shop\Http\Requests\CartRequest;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Contracts\View\View;
|
use Illuminate\Contracts\View\View;
|
||||||
use Beike\Shop\Services\CartService;
|
use Beike\Shop\Services\CartService;
|
||||||
|
|
@ -84,11 +85,11 @@ class CartController extends Controller
|
||||||
/**
|
/**
|
||||||
* 创建购物车
|
* 创建购物车
|
||||||
*
|
*
|
||||||
* @param Request $request
|
* @param CartRequest $request
|
||||||
* @return mixed
|
* @return mixed
|
||||||
* @throws \Exception
|
* @throws \Exception
|
||||||
*/
|
*/
|
||||||
public function store(Request $request)
|
public function store(CartRequest $request)
|
||||||
{
|
{
|
||||||
$skuId = $request->sku_id;
|
$skuId = $request->sku_id;
|
||||||
$quantity = $request->quantity ?? 1;
|
$quantity = $request->quantity ?? 1;
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,58 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* CartRequest.php
|
||||||
|
*
|
||||||
|
* @copyright 2022 opencart.cn - All Rights Reserved
|
||||||
|
* @link http://www.guangdawangluo.com
|
||||||
|
* @author Edward Yang <yangjin@opencart.cn>
|
||||||
|
* @created 2022-08-26 14:21:32
|
||||||
|
* @modified 2022-08-26 14:21:32
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Beike\Shop\Http\Requests;
|
||||||
|
|
||||||
|
use Beike\Models\ProductSku;
|
||||||
|
use Illuminate\Foundation\Http\FormRequest;
|
||||||
|
use Illuminate\Validation\Rule;
|
||||||
|
|
||||||
|
class CartRequest extends FormRequest
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Determine if the user is authorized to make this request.
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function authorize(): bool
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the validation rules that apply to the request.
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function rules()
|
||||||
|
{
|
||||||
|
$skuId = (int)$this->get('sku_id');
|
||||||
|
|
||||||
|
return [
|
||||||
|
'sku_id' => 'required|int',
|
||||||
|
'quantity' => ['required', 'int', function ($attribute, $value, $fail) use ($skuId) {
|
||||||
|
$skuQuantity = ProductSku::query()->where('id', $skuId)->value('quantity');
|
||||||
|
if ($value > $skuQuantity) {
|
||||||
|
$fail(trans('cart.stock_out'));
|
||||||
|
}
|
||||||
|
}],
|
||||||
|
'buy_now' => 'bool'
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function attributes()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'sku_id' => trans('cart.sku_id'),
|
||||||
|
'quantity' => trans('cart.quantity'),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,14 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* cart.php
|
||||||
|
*
|
||||||
|
* @copyright 2022 opencart.cn - All Rights Reserved
|
||||||
|
* @link http://www.guangdawangluo.com
|
||||||
|
* @author Edward Yang <yangjin@opencart.cn>
|
||||||
|
* @created 2022-08-26 15:25:29
|
||||||
|
* @modified 2022-08-26 15:25:29
|
||||||
|
*/
|
||||||
|
|
||||||
|
return [
|
||||||
|
'stock_out' => 'Stock Out',
|
||||||
|
];
|
||||||
|
|
@ -0,0 +1,14 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* cart.php
|
||||||
|
*
|
||||||
|
* @copyright 2022 opencart.cn - All Rights Reserved
|
||||||
|
* @link http://www.guangdawangluo.com
|
||||||
|
* @author Edward Yang <yangjin@opencart.cn>
|
||||||
|
* @created 2022-08-26 15:25:29
|
||||||
|
* @modified 2022-08-26 15:25:29
|
||||||
|
*/
|
||||||
|
|
||||||
|
return [
|
||||||
|
'stock_out' => '库存不足',
|
||||||
|
];
|
||||||
Loading…
Reference in New Issue