multi-store/app/api/controller/order/CartController.php
mkm 40ec3e5ee0 feat: 修改了订单相关的API,优化了支付逻辑;
fix: 修复了用户地址、商品库存等错误;
refactor: 重构了登录逻辑,提高了代码可读性;
style: 调整了代码格式,使其更加规范;
test: 增加了订单支付的测试用例;
docs: 更新了相关文档;
build: 更新了依赖;
ops: 优化了服务器性能;
chore: 更新了.gitignore文件;
2024-08-27 11:56:48 +08:00

112 lines
3.6 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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;
use app\common\model\store_branch_product\StoreBranchProduct;
use app\common\model\store_product_unit\StoreProductUnit;
class CartController extends BaseApiController
{
public function list()
{
return $this->dataLists(new CartList());
}
/**
* @notes 添加购物车
*/
public function create()
{
$params = (new CartValidate())->post()->goCheck('add');
$params['uid'] = $this->request->userId;
$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();
//判断起批发价
$branchProduct = StoreBranchProduct::where(
[
'product_id' => $params['product_id'],
'store_id' => $params['store_id']
]
)->find();
if (!$branchProduct) {
return $this->fail('商品不存在');
}
$params['cart_num']=intval($params['cart_num']);
if ($params['cart_num'] < $branchProduct['batch']) {
return $this->fail('起批发量低于最低值' . $branchProduct['batch']);
}
if (convertNumber($params['cart_num']) === false) {
$is_bulk = StoreProductUnit::where('id', $branchProduct['unit'])->value('is_bulk');
if ($is_bulk == 0) {
return $this->fail('非计量商品,不能有小数,请编辑购物车');
}
}
//数量下单判断
$count = Cart::where(['uid' => $params['uid'], 'delete_time' => null, 'is_pay' => 0])->count();
if ($count > 100) {
return $this->fail('购物车商品不能大于100个请先结算');
}
//数量下单判断
// $stock = StoreBranchProduct::where(
// ['product_id'=>$params['product_id'],
// 'store_id'=>$params['store_id']
// ])->value('stock')??0;
// if ($params['cart_num'] >$stock) {
// return $this->fail('库存数量不足');
// }
if ($result) {
if (isset($params['type']) && $params['type'] == 1) {
$res = CartLogic::add($params);
} else {
$res = CartLogic::edit($params);
}
} else {
$res = CartLogic::add($params);
}
if ($res) {
return $this->success('添加成功');
} else {
return $this->fail(CartLogic::getError());
}
}
/**
* @notes 修改购物车
*/
public function change()
{
$params = (new CartValidate())->post()->goCheck('change');
$params['uid'] = $this->request->userId;
$params['cart_num']=intval($params['cart_num']);
$res = CartLogic::edit($params, 'dec');
if ($res) {
return $this->success('修改成功');
} else {
return $this->fail('修改失败');
}
}
/**
* @notes 删除购物车
*/
public function delete()
{
$params = (new CartValidate())->post()->goCheck('delete');
$params['uid'] = $this->request->userId;
$res = CartLogic::delete($params);
if ($res) {
return $this->success('删除成功');
} else {
return $this->fail('删除失败');
}
}
}