1 line
9.3 KiB
PHP
1 line
9.3 KiB
PHP
<?php
|
|
namespace app\api\model\food;
|
|
|
|
use think\facade\Cache;
|
|
use hema\Helper;
|
|
|
|
/**
|
|
* 购物车管理
|
|
*/
|
|
class Cart
|
|
{
|
|
public $error = ''; //错误信息
|
|
private $applet_id; //模板id
|
|
private $shop_id; //门店id
|
|
private $user_id; //用户id
|
|
private $table_id;
|
|
private $cart = []; //购物车列表
|
|
private $clear = false; //是否清空购物车
|
|
|
|
/**
|
|
* 构造方法
|
|
*/
|
|
public function __construct($applet_id, $shop_id, $user_id, $table_id='')
|
|
{
|
|
$this->applet_id = $applet_id;
|
|
$this->shop_id = $shop_id;
|
|
$this->user_id = $user_id;
|
|
$this->table_id = $table_id;
|
|
$cart = Cache::get('food_cart_' . $applet_id) ?: [];
|
|
if(isset($cart[$shop_id])){
|
|
$cart = $cart[$shop_id];
|
|
}
|
|
if(empty($table_id)){
|
|
if(isset($cart['user_' . $user_id])){
|
|
$this->cart = $cart['user_' . $user_id];
|
|
}
|
|
}else{
|
|
if(isset($cart['table_' . $table_id])){
|
|
$this->cart = $cart['table_' . $table_id];
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取购物车内容
|
|
*/
|
|
public function getCart()
|
|
{
|
|
return $this->cart;
|
|
}
|
|
|
|
public function getList($order_mode)
|
|
{
|
|
// 商品列表
|
|
$goodsList = [];
|
|
$goodsIds = array_unique(array_column($this->cart, 'goods_id'));
|
|
foreach ((new Goods)->getListByIds($goodsIds) as $goods) {
|
|
$goodsList[$goods['goods_id']] = $goods;
|
|
}
|
|
$pack_price = 0;//餐盒费
|
|
$min_price = 0;//起送价价
|
|
// 购物车商品列表
|
|
$cartList = [];
|
|
foreach ($this->cart as $key => $cart) {
|
|
// 判断商品不存在则自动删除
|
|
if (!isset($goodsList[$cart['goods_id']])) {
|
|
$this->delete($cart['goods_id'], $cart['goods_sku_id']);
|
|
continue;
|
|
}
|
|
$goods = clone $goodsList[$cart['goods_id']];
|
|
// 商品sku信息
|
|
$goods['goods_sku_id'] = $cart['goods_sku_id'];
|
|
// 商品sku不存在则自动删除
|
|
if (!$goods['goods_sku'] = $goods->getGoodsSku($cart['goods_sku_id'])) {
|
|
$this->delete($cart['goods_id'], $cart['goods_sku_id']);
|
|
continue;
|
|
}
|
|
// 判断商品是否下架
|
|
if ($goods['goods_status']['value'] != 10) {
|
|
$this->setError('很抱歉,商品 [' . $goods['goods_name'] . '] 已下架');
|
|
}
|
|
//计算餐盒费
|
|
if($order_mode == 20 OR $order_mode == 30){
|
|
if($goods['pack_price']>0){
|
|
$pack_price = $pack_price + $goods['pack_price']*$cart['goods_num'];
|
|
}
|
|
}
|
|
// 商品单价
|
|
$goods['goods_price'] = $goods['goods_sku']['goods_price'];
|
|
// 商品总价
|
|
$goods['total_num'] = $cart['goods_num'];
|
|
$goods['total_price'] = $total_price = bcmul($goods['goods_price'], $cart['goods_num'], 2);
|
|
$cartList[] = $goods->hidden(['category', 'content', 'spec']);
|
|
}
|
|
// 商品总金额
|
|
$orderTotalPrice = Helper::getArrayColumnSum($cartList, 'total_price');
|
|
//餐盒费加入总价
|
|
$order_pay_price = $orderTotalPrice + $pack_price;
|
|
|
|
//如果是外卖
|
|
if($order_mode == 20){
|
|
$delivery = Setting::getItem('delivery');
|
|
if($delivery['min_price'] > $order_pay_price){
|
|
$min_price = $delivery['min_price'] - $order_pay_price;// 起送价价
|
|
}
|
|
}
|
|
return [
|
|
'coupon_list' => null, //优惠券列表
|
|
'gift' => null,//赠品
|
|
'goods_list' => $cartList, // 商品列表
|
|
'order_total_num' => $this->getTotalNum(), // 商品总数量
|
|
'order_total_price' => Helper::number2($orderTotalPrice),// 商品总金额
|
|
'order_pay_price' => Helper::number2($order_pay_price), // 实际支付金额+餐盒费
|
|
'pack_price' => Helper::number2($pack_price), // 餐盒费用
|
|
'min_price' => Helper::number2($min_price),//最低启送价
|
|
'delivery_price' => Helper::number2(0),//配送费用
|
|
'activity_price' => Helper::number2(0),//优惠费用
|
|
'vip_price' => Helper::number2(0),//优惠费用
|
|
'address' => null,//收货地址
|
|
'intra_region' => false,//是否超出配送范围
|
|
'free_delivery' => false,//免配送费
|
|
'has_error' => $this->hasError(),
|
|
'error_msg' => $this->getError(),
|
|
];
|
|
}
|
|
/**
|
|
* 购物车操作
|
|
*/
|
|
public function edit($data)
|
|
{
|
|
//商品数量为空,什么也不做
|
|
if($data['goods_num'] == ''){
|
|
return true;
|
|
}
|
|
//商品数量0为删除
|
|
if($data['goods_num'] == 0){
|
|
return $this->delete($data);
|
|
}
|
|
|
|
// 购物车商品索引
|
|
$index = $data['goods_id'] . '_' . $data['goods_sku_id'];
|
|
// 商品信息
|
|
$goods = Goods::detail($data['goods_id']);
|
|
// 判断商品是否下架
|
|
if ($goods['goods_status']['value'] != 10) {
|
|
$this->setError('很抱歉,该商品已下架');
|
|
return false;
|
|
}
|
|
$data['create_time'] = time();
|
|
//判断起售数量
|
|
if($data['goods_num'] < (int)$goods['start_sale']){
|
|
$data['goods_num'] = (int)$goods['start_sale'];
|
|
}
|
|
$this->cart[$index] = $data;
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* 添加购物车
|
|
*/
|
|
public function add($data)
|
|
{
|
|
// 购物车商品索引
|
|
$index = $data['goods_id'] . '_' . $data['goods_sku_id'];
|
|
// 商品信息
|
|
$goods = Goods::detail($data['goods_id']);
|
|
// 商品sku信息
|
|
//$goods['goods_sku'] = $goods->getGoodsSku($data['goods_sku_id']);
|
|
// 判断商品是否下架
|
|
if ($goods['goods_status']['value'] != 10) {
|
|
$this->setError('很抱歉,该商品已下架');
|
|
return false;
|
|
}
|
|
if(!empty($data['cart_type']) && intval($data['cart_type']) === 2){
|
|
unset($data['cart_type']);
|
|
$data['create_time'] = time();
|
|
if(isset($this->cart[$index])){
|
|
$this->cart[$index]['goods_num'] = $this->cart[$index]['goods_num'] + $data['goods_num'];
|
|
}else{
|
|
// $data['goods_num'] = $goods['start_sale'];
|
|
$this->cart[$index] = $data;
|
|
}
|
|
}else{
|
|
$data['create_time'] = time();
|
|
if(isset($this->cart[$index])){
|
|
$this->cart[$index]['goods_num'] = $this->cart[$index]['goods_num'] + $data['goods_num'];
|
|
}else{
|
|
$data['goods_num'] = $goods['start_sale'];
|
|
$this->cart[$index] = $data;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* 减少购物车中某商品数量
|
|
*/
|
|
public function sub($data)
|
|
{
|
|
$index = $data['goods_id'] . '_' . $data['goods_sku_id'];
|
|
$goods_num = $this->cart[$index]['goods_num'] - 1;// 商品信息
|
|
$goods = Goods::detail($data['goods_id']);
|
|
if($goods_num >= 1 and $goods_num >= $goods['start_sale']){
|
|
$this->cart[$index]['goods_num'] = $goods_num;
|
|
}else{
|
|
unset($this->cart[$index]); //清空该商品
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* 删除购物车中指定商品
|
|
*/
|
|
public function delete($data)
|
|
{
|
|
//为空是商城批量删除
|
|
if(empty($data['goods_id'])){
|
|
$indexArr = strpos( $data['goods_sku_id'], ',') !== false ? explode(',', $data['goods_sku_id']) : [ $data['goods_sku_id']];
|
|
foreach ($indexArr as $index) {
|
|
if (isset($this->cart[$index])) unset($this->cart[$index]);
|
|
}
|
|
}else{
|
|
$index = $data['goods_id'] . '_' . $data['goods_sku_id'];
|
|
if (isset($this->cart[$index])) unset($this->cart[$index]);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* 获取当前用户购物车商品总数量
|
|
*/
|
|
public function getTotalNum()
|
|
{
|
|
return array_sum(array_column($this->cart, 'goods_num'));
|
|
}
|
|
|
|
/**
|
|
* 析构方法
|
|
* 将cart数据保存到缓存文件
|
|
*/
|
|
public function __destruct()
|
|
{
|
|
if($this->clear !== true){
|
|
$cart = Cache::get('food_cart_' . $this->applet_id) ?: [];
|
|
if(empty($this->table_id)){
|
|
$cart[$this->shop_id]['user_' . $this->user_id] = $this->cart;
|
|
}else{
|
|
$cart[$this->shop_id]['table_' . $this->table_id] = $this->cart;
|
|
}
|
|
Cache::set('food_cart_' . $this->applet_id, $cart, 86400);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 清空当前用户购物车
|
|
*/
|
|
public function clearAll()
|
|
{
|
|
$this->clear = true;
|
|
$cart = Cache::get('food_cart_' . $this->applet_id) ?: [];
|
|
if(empty($this->table_id)){
|
|
$cart[$this->shop_id]['user_' . $this->user_id] = [];
|
|
}else{
|
|
$cart[$this->shop_id]['table_' . $this->table_id] = [];
|
|
}
|
|
Cache::set('food_cart_' . $this->applet_id,$cart, 86400);
|
|
|
|
}
|
|
|
|
/**
|
|
* 设置错误信息
|
|
*/
|
|
private function setError($error)
|
|
{
|
|
empty($this->error) && $this->error = $error;
|
|
}
|
|
|
|
/**
|
|
* 是否存在错误
|
|
*/
|
|
private function hasError()
|
|
{
|
|
return !empty($this->error);
|
|
}
|
|
|
|
/**
|
|
* 获取错误信息
|
|
*/
|
|
public function getError()
|
|
{
|
|
return $this->error;
|
|
}
|
|
|
|
} |