2024-06-01 17:22:46 +08:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace app\api\controller\order;
|
|
|
|
|
|
|
|
|
|
use app\api\logic\order\CartLogic;
|
|
|
|
|
use app\api\validate\CartValidate;
|
|
|
|
|
use app\api\controller\BaseApiController;
|
|
|
|
|
use app\api\lists\order\CartList;
|
|
|
|
|
use app\common\model\order\Cart;
|
2024-06-11 14:35:56 +08:00
|
|
|
|
use app\common\model\store_branch_product\StoreBranchProduct;
|
2024-08-11 21:55:21 +08:00
|
|
|
|
use app\common\model\store_product_unit\StoreProductUnit;
|
2024-06-14 15:53:47 +08:00
|
|
|
|
|
2024-06-01 17:22:46 +08:00
|
|
|
|
class CartController extends BaseApiController
|
|
|
|
|
{
|
2024-06-04 17:21:57 +08:00
|
|
|
|
public function list()
|
|
|
|
|
{
|
2024-06-01 17:22:46 +08:00
|
|
|
|
return $this->dataLists(new CartList());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @notes 添加购物车
|
|
|
|
|
*/
|
2024-06-04 17:21:57 +08:00
|
|
|
|
public function create()
|
|
|
|
|
{
|
2024-06-01 17:22:46 +08:00
|
|
|
|
$params = (new CartValidate())->post()->goCheck('add');
|
2024-06-04 17:21:57 +08:00
|
|
|
|
$params['uid'] = $this->request->userId;
|
2024-08-11 21:55:21 +08:00
|
|
|
|
$result = Cart::where(['uid' => $params['uid'], 'store_id' => $params['store_id'], 'product_id' => $params['product_id'], 'is_fail' => 0, 'is_pay' => 0, 'delete_time' => null])->find();
|
2024-06-20 18:02:20 +08:00
|
|
|
|
|
|
|
|
|
//判断起批发价
|
2024-08-11 21:55:21 +08:00
|
|
|
|
$branchProduct = StoreBranchProduct::where(
|
|
|
|
|
[
|
2024-08-27 10:22:18 +08:00
|
|
|
|
'id' => $params['product_id'],
|
2024-06-20 18:02:20 +08:00
|
|
|
|
]
|
2024-08-11 21:55:21 +08:00
|
|
|
|
)->find();
|
2024-08-27 10:22:18 +08:00
|
|
|
|
// $branchProduct = StoreBranchProduct::where(
|
|
|
|
|
// [
|
|
|
|
|
// 'product_id' => $params['product_id'],
|
|
|
|
|
// 'store_id' => $params['store_id']
|
|
|
|
|
// ]
|
|
|
|
|
// )->find();
|
2024-08-11 21:55:21 +08:00
|
|
|
|
if (!$branchProduct) {
|
|
|
|
|
return $this->fail('商品不存在');
|
|
|
|
|
}
|
2024-08-14 09:46:31 +08:00
|
|
|
|
$params['cart_num']=intval($params['cart_num']);
|
2024-08-11 21:55:21 +08:00
|
|
|
|
if ($params['cart_num'] < $branchProduct['batch']) {
|
|
|
|
|
return $this->fail('起批发量低于最低值' . $branchProduct['batch']);
|
|
|
|
|
}
|
2024-08-26 20:00:51 +08:00
|
|
|
|
|
|
|
|
|
if (convertNumber($params['cart_num']) === false) {
|
|
|
|
|
$is_bulk = StoreProductUnit::where('id', $branchProduct['unit'])->value('is_bulk');
|
|
|
|
|
if ($is_bulk == 0) {
|
|
|
|
|
return $this->fail('非计量商品,不能有小数,请编辑购物车');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2024-08-11 21:55:21 +08:00
|
|
|
|
|
|
|
|
|
//数量下单判断
|
2024-06-04 17:21:57 +08:00
|
|
|
|
$count = Cart::where(['uid' => $params['uid'], 'delete_time' => null, 'is_pay' => 0])->count();
|
|
|
|
|
if ($count > 100) {
|
2024-06-01 17:22:46 +08:00
|
|
|
|
return $this->fail('购物车商品不能大于100个,请先结算');
|
|
|
|
|
}
|
2024-06-11 14:35:56 +08:00
|
|
|
|
//数量下单判断
|
2024-08-11 21:55:21 +08:00
|
|
|
|
// $stock = StoreBranchProduct::where(
|
|
|
|
|
// ['product_id'=>$params['product_id'],
|
|
|
|
|
// 'store_id'=>$params['store_id']
|
|
|
|
|
// ])->value('stock')??0;
|
|
|
|
|
// if ($params['cart_num'] >$stock) {
|
|
|
|
|
// return $this->fail('库存数量不足');
|
|
|
|
|
// }
|
2024-06-04 17:21:57 +08:00
|
|
|
|
if ($result) {
|
2024-08-11 21:55:21 +08:00
|
|
|
|
if (isset($params['type']) && $params['type'] == 1) {
|
2024-06-29 15:06:41 +08:00
|
|
|
|
$res = CartLogic::add($params);
|
2024-08-11 21:55:21 +08:00
|
|
|
|
} else {
|
2024-06-29 15:06:41 +08:00
|
|
|
|
$res = CartLogic::edit($params);
|
|
|
|
|
}
|
2024-06-04 17:21:57 +08:00
|
|
|
|
} else {
|
|
|
|
|
$res = CartLogic::add($params);
|
2024-06-01 17:22:46 +08:00
|
|
|
|
}
|
2024-06-04 17:21:57 +08:00
|
|
|
|
if ($res) {
|
2024-06-01 17:22:46 +08:00
|
|
|
|
return $this->success('添加成功');
|
2024-06-04 17:21:57 +08:00
|
|
|
|
} else {
|
2024-06-01 17:22:46 +08:00
|
|
|
|
return $this->fail(CartLogic::getError());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @notes 修改购物车
|
|
|
|
|
*/
|
2024-06-04 17:21:57 +08:00
|
|
|
|
public function change()
|
|
|
|
|
{
|
2024-06-01 17:22:46 +08:00
|
|
|
|
$params = (new CartValidate())->post()->goCheck('change');
|
2024-06-04 17:21:57 +08:00
|
|
|
|
$params['uid'] = $this->request->userId;
|
2024-08-14 09:46:31 +08:00
|
|
|
|
$params['cart_num']=intval($params['cart_num']);
|
2024-06-04 17:21:57 +08:00
|
|
|
|
$res = CartLogic::edit($params, 'dec');
|
|
|
|
|
if ($res) {
|
2024-06-01 17:22:46 +08:00
|
|
|
|
return $this->success('修改成功');
|
2024-06-04 17:21:57 +08:00
|
|
|
|
} else {
|
2024-06-01 17:22:46 +08:00
|
|
|
|
return $this->fail(CartLogic::getError());
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-06-04 17:21:57 +08:00
|
|
|
|
|
2024-06-01 17:22:46 +08:00
|
|
|
|
/**
|
|
|
|
|
* @notes 删除购物车
|
|
|
|
|
*/
|
2024-06-04 17:21:57 +08:00
|
|
|
|
public function delete()
|
|
|
|
|
{
|
2024-06-01 17:22:46 +08:00
|
|
|
|
$params = (new CartValidate())->post()->goCheck('delete');
|
2024-06-04 17:21:57 +08:00
|
|
|
|
$params['uid'] = $this->request->userId;
|
|
|
|
|
$res = CartLogic::delete($params);
|
|
|
|
|
if ($res) {
|
2024-06-01 17:22:46 +08:00
|
|
|
|
return $this->success('删除成功');
|
2024-06-04 17:21:57 +08:00
|
|
|
|
} else {
|
2024-06-01 17:22:46 +08:00
|
|
|
|
return $this->fail(CartLogic::getError());
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-06-04 16:51:26 +08:00
|
|
|
|
}
|