<?php
|
|
namespace app\api\controller\branch;
|
|
use app\api\controller\Controller;
|
use app\api\model\branch\Activity as ActivityModel;
|
use app\api\model\branch\ActivityCart as CartModel;
|
use app\api\model\settings\Setting as SettingModel;
|
use app\common\model\branch\ActivityProductPrice as ProductPriceModel;
|
|
/**
|
* 购物车控制器
|
*/
|
class ActivityCart extends Controller
|
{
|
private $user;
|
|
// $model
|
private $model;
|
|
/**
|
* 构造方法
|
*/
|
public function initialize()
|
{
|
$this->user = $this->getUser(false);
|
$this->model = new CartModel();
|
}
|
|
|
/**
|
* 购物车列表
|
*/
|
public function lists()
|
{
|
$model = new CartModel();
|
$post_data = $this->postData();
|
$activity_id = $post_data['activity_id'];
|
// 购物车商品列表
|
$productList = $model->getList($this->user,[],$activity_id);
|
//是否显示店铺信息
|
$store_open = SettingModel::getStoreOpen();
|
return $this->renderSuccess('', compact('productList', 'store_open'));
|
}
|
|
/**
|
* 加入购物车
|
* @param int $product_id 商品id
|
* @param int $product_num 商品数量
|
* @param string $product_sku_id 商品sku索引
|
*/
|
public function add()
|
{
|
$data = $this->request->param();
|
$product_id = $data['product_id'];
|
$product_num = $data['total_num'];
|
$spec_sku_id = $data['spec_sku_id'];
|
$activity_id = $data['activity_id'];
|
|
/*验证权限*/
|
$activity_model = new ActivityModel();
|
$detail = $activity_model->detail($activity_id);
|
if (!$detail) {
|
return $this->renderError('活动不存在');
|
}
|
if ($detail['status_text']['status'] != 1) {
|
return $this->renderError('活动未开始');
|
}
|
$model = new CartModel();
|
if (!$model->add($this->user, $product_id, $product_num, $spec_sku_id, $activity_id)) {
|
return $this->renderError($model->getError() ?: '加入购物车失败');
|
}
|
// 购物车商品总数量
|
$totalNum = $model->getProductNum($this->user,$activity_id);
|
return $this->renderSuccess('加入购物车成功', ['cart_total_num' => $totalNum]);
|
}
|
|
/**
|
* 减少购物车商品数量
|
* @param $product_id
|
* @param $product_sku_id
|
* @return array
|
*/
|
public function sub($product_id, $spec_sku_id, $activity_id = 0)
|
{
|
$model = new CartModel();
|
|
/*验证权限*/
|
$activity_model = new ActivityModel();
|
$detail = $activity_model->detail($activity_id);
|
if (!$detail) {
|
return $this->renderError('活动不存在');
|
}
|
if ($detail['status_text']['status'] != 1) {
|
return $this->renderError('活动未开始');
|
}
|
$model->sub($this->user, $product_id, $spec_sku_id, $activity_id);
|
return $this->renderSuccess('');
|
}
|
|
/**
|
* 删除购物车中指定商品
|
* @param $product_sku_id (支持字符串ID集)
|
* @return array
|
*/
|
public function delete($cart_id, $activity_id = 0)
|
{
|
/*验证权限*/
|
$activity_model = new ActivityModel();
|
$detail = $activity_model->detail($activity_id);
|
if (!$detail) {
|
return $this->renderError('活动不存在');
|
}
|
if ($detail['status_text']['status'] != 1) {
|
return $this->renderError('活动未开始');
|
}
|
$this->model->setDelete($this->user, $cart_id);
|
return $this->renderSuccess('删除成功');
|
}
|
}
|