购物车相关修改
This commit is contained in:
parent
20e891a596
commit
d1c648fac7
69
app/api/controller/order/CartController.php
Normal file
69
app/api/controller/order/CartController.php
Normal file
@ -0,0 +1,69 @@
|
||||
<?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;
|
||||
|
||||
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'],'product_attr_unique'=>$params['product_attr_unique'],'is_fail'=>0,'is_del'=>0])->find();
|
||||
$count=Cart::where(['uid'=>$params['uid'],'is_del'=>0,'is_pay'=>0])->count();
|
||||
if($count>100){
|
||||
return $this->fail('购物车商品不能大于100个,请先结算');
|
||||
}
|
||||
if($result){
|
||||
$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;
|
||||
$res=CartLogic::edit($params,'dec');
|
||||
if($res){
|
||||
return $this->success('修改成功');
|
||||
}else{
|
||||
return $this->fail(CartLogic::getError());
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @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(CartLogic::getError());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
100
app/api/lists/order/CartList.php
Normal file
100
app/api/lists/order/CartList.php
Normal file
@ -0,0 +1,100 @@
|
||||
<?php
|
||||
|
||||
namespace app\api\lists\order;
|
||||
|
||||
|
||||
use app\admin\lists\BaseAdminDataLists;
|
||||
use app\common\lists\ListsSearchInterface;
|
||||
use app\common\model\order\Cart;
|
||||
use app\common\lists\ListsExtendInterface;
|
||||
use app\common\model\store_product_attr_value\StoreProductAttrValue;
|
||||
use app\common\model\store_product_unit\StoreProductUnit;
|
||||
|
||||
/**
|
||||
* 购物车列表
|
||||
* Class RetailOrderList
|
||||
* @package app\api\order
|
||||
*/
|
||||
class CartList extends BaseAdminDataLists implements ListsSearchInterface, ListsExtendInterface
|
||||
{
|
||||
|
||||
protected $total_price=0;
|
||||
|
||||
|
||||
/**
|
||||
* @notes 设置搜索条件
|
||||
* @return \string[][]
|
||||
* @author likeadmin
|
||||
*/
|
||||
public function setSearch(): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 购物车列表
|
||||
* @return array
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
* @date 2024/04/27 11:26
|
||||
*/
|
||||
public function lists($where = []): array
|
||||
{
|
||||
$userId = $this->request->userId;
|
||||
if (!$userId) return [];
|
||||
$where=[
|
||||
'uid'=>$userId,
|
||||
'is_pay'=>0
|
||||
];
|
||||
$list = Cart::where($this->searchWhere)->where($where)
|
||||
->limit($this->limitOffset, $this->limitLength)
|
||||
->order(['cart_id' => 'desc'])
|
||||
->select()->each(function ($item) {
|
||||
|
||||
return $item;
|
||||
})
|
||||
->toArray();
|
||||
|
||||
foreach ($list as $key => &$item) {
|
||||
$find = StoreProductAttrValue::where('unique',$item['product_attr_unique'])
|
||||
->field('product_id,image,purchase')
|
||||
->with(['storeName'])
|
||||
->find();
|
||||
if($find){
|
||||
$item['goods_total_price'] = bcmul($item['cart_num'], $find['purchase'], 2);
|
||||
$this->total_price=bcadd($this->total_price,$item['goods_total_price'],2);
|
||||
$item['imgs'] = $find['image'];
|
||||
$item['sell'] = $find['purchase'];
|
||||
$item['goods_name'] = $find['store_name'];
|
||||
$item['unit_name'] = StoreProductUnit::where('id',$find['unit'])->value('name');
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
return $list;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 购物车数量
|
||||
* @return int
|
||||
* @date 2024/04/27 11:26
|
||||
*/
|
||||
public function count(): int
|
||||
{
|
||||
$userId = $this->request->userId;
|
||||
if (!$userId) return 0;
|
||||
$where=[
|
||||
'uid'=>$userId,
|
||||
'is_pay'=>0
|
||||
];
|
||||
return Cart::where($this->searchWhere)->where($where)->count();
|
||||
}
|
||||
|
||||
public function extend()
|
||||
{
|
||||
return ['total_price'=>$this->total_price];
|
||||
}
|
||||
}
|
98
app/api/logic/order/CartLogic.php
Normal file
98
app/api/logic/order/CartLogic.php
Normal file
@ -0,0 +1,98 @@
|
||||
<?php
|
||||
|
||||
namespace app\api\logic\order;
|
||||
|
||||
|
||||
use app\common\model\order\Cart;
|
||||
use app\common\logic\BaseLogic;
|
||||
use think\facade\Db;
|
||||
|
||||
|
||||
/**
|
||||
* 购物车表逻辑
|
||||
* Class CartLogic
|
||||
* @package app\admin\logic\order
|
||||
*/
|
||||
class CartLogic extends BaseLogic
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @notes 添加购物车表
|
||||
* @param array $params
|
||||
* @return bool
|
||||
* @author likeadmin
|
||||
* @date 2024/04/24 10:37
|
||||
*/
|
||||
public static function add(array $params): bool
|
||||
{
|
||||
Db::startTrans();
|
||||
try {
|
||||
Cart::create([
|
||||
'uid' => $params['uid'],
|
||||
'type' => $params['type']??'',
|
||||
'product_id' => $params['product_id'],
|
||||
'store_id' => $params['store_id']??0,
|
||||
'staff_id' => $params['staff_id']??0,
|
||||
'product_attr_unique' => $params['product_attr_unique'],
|
||||
'cart_num' => $params['cart_num'],
|
||||
'add_time' => \Qiniu\time(),
|
||||
'is_new' => $params['is_new']??0,
|
||||
]);
|
||||
Db::commit();
|
||||
return true;
|
||||
} catch (\Exception $e) {
|
||||
Db::rollback();
|
||||
self::setError($e->getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 编辑购物车表
|
||||
* @param array $params
|
||||
* @return bool
|
||||
* @author likeadmin
|
||||
* @date 2024/04/24 10:37
|
||||
*/
|
||||
public static function edit(array $params,$type='inc'): bool
|
||||
{
|
||||
Db::startTrans();
|
||||
try {
|
||||
Cart::where(['uid'=>$params['uid'],'product_id'=>$params['product_id']])->update(['cart_num'=>$params['cart_num']]);
|
||||
Db::commit();
|
||||
return true;
|
||||
} catch (\Exception $e) {
|
||||
Db::rollback();
|
||||
self::setError($e->getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 删除购物车表
|
||||
* @param array $params
|
||||
* @return bool
|
||||
* @author likeadmin
|
||||
* @date 2024/04/24 10:37
|
||||
*/
|
||||
public static function delete(array $params): bool
|
||||
{
|
||||
return Cart::destroy($params['id']);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 获取购物车表详情
|
||||
* @param $params
|
||||
* @return array
|
||||
* @author likeadmin
|
||||
* @date 2024/04/24 10:37
|
||||
*/
|
||||
public static function detail($params): array
|
||||
{
|
||||
return Cart::findOrEmpty($params['id'])->toArray();
|
||||
}
|
||||
}
|
90
app/api/validate/CartValidate.php
Normal file
90
app/api/validate/CartValidate.php
Normal file
@ -0,0 +1,90 @@
|
||||
<?php
|
||||
|
||||
namespace app\api\validate;
|
||||
|
||||
|
||||
use app\common\validate\BaseValidate;
|
||||
|
||||
|
||||
/**
|
||||
* 购物车表验证器
|
||||
* Class CartValidate
|
||||
* @package app\admin\validate\order
|
||||
*/
|
||||
class CartValidate extends BaseValidate
|
||||
{
|
||||
|
||||
/**
|
||||
* 设置校验规则
|
||||
* @var string[]
|
||||
*/
|
||||
protected $rule = [
|
||||
'cart_num' => 'require|number',
|
||||
'product_attr_unique' => 'require',
|
||||
'store_id' => 'require|number',
|
||||
'staff_id' => 'require|number',
|
||||
'product_id' => 'require|number',
|
||||
];
|
||||
|
||||
|
||||
/**
|
||||
* 参数描述
|
||||
* @var string[]
|
||||
*/
|
||||
protected $field = [
|
||||
'cart_num' => '数量',
|
||||
'store_id' => '店铺id',
|
||||
'staff_id' => '员工id',
|
||||
'product_id' => '商品id',
|
||||
'product_attr_unique' => '商品规格',
|
||||
];
|
||||
|
||||
|
||||
/**
|
||||
* @notes 添加场景
|
||||
* @return CartValidate
|
||||
* @author likeadmin
|
||||
* @date 2024/04/24 10:37
|
||||
*/
|
||||
public function sceneAdd()
|
||||
{
|
||||
return $this->only(['cart_num','product_attr_unique','store_id','product_id']);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 编辑场景
|
||||
* @return CartValidate
|
||||
* @author likeadmin
|
||||
* @date 2024/04/24 10:37
|
||||
*/
|
||||
public function sceneEdit()
|
||||
{
|
||||
return $this->only(['']);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 删除场景
|
||||
* @return CartValidate
|
||||
* @author likeadmin
|
||||
* @date 2024/04/24 10:37
|
||||
*/
|
||||
public function sceneDelete()
|
||||
{
|
||||
return $this->only(['']);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 详情场景
|
||||
* @return CartValidate
|
||||
* @author likeadmin
|
||||
* @date 2024/04/24 10:37
|
||||
*/
|
||||
public function sceneDetail()
|
||||
{
|
||||
return $this->only(['']);
|
||||
}
|
||||
|
||||
}
|
23
app/common/model/order/Cart.php
Normal file
23
app/common/model/order/Cart.php
Normal file
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\model\order;
|
||||
|
||||
|
||||
use app\common\model\BaseModel;
|
||||
use think\model\concern\SoftDelete;
|
||||
|
||||
|
||||
/**
|
||||
* 购物车表模型
|
||||
* Class Cart
|
||||
* @package app\common\model\order
|
||||
*/
|
||||
class Cart extends BaseModel
|
||||
{
|
||||
use SoftDelete;
|
||||
protected $pk = 'cart_id';
|
||||
protected $name = 'cart';
|
||||
protected $deleteTime = 'delete_time';
|
||||
|
||||
|
||||
}
|
@ -4,6 +4,7 @@ namespace app\common\model\store_product_attr_value;
|
||||
|
||||
|
||||
use app\common\model\BaseModel;
|
||||
use app\common\model\store_product\StoreProduct;
|
||||
use think\model\concern\SoftDelete;
|
||||
|
||||
|
||||
@ -18,5 +19,11 @@ class StoreProductAttrValue extends BaseModel
|
||||
protected $name = 'store_product_attr_value';
|
||||
protected $deleteTime = 'delete_time';
|
||||
|
||||
public function storeName()
|
||||
{
|
||||
return $this->belongsTo(StoreProduct::class, 'product_id', 'id')
|
||||
->bind(['store_name','unit']);
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user