82 lines
2.2 KiB
PHP
82 lines
2.2 KiB
PHP
<?php
|
|
namespace app\api\controller\food;
|
|
|
|
use app\api\controller\food\Controller;
|
|
use app\api\model\food\Cart as CartModel;
|
|
|
|
/**
|
|
* 购物车管理
|
|
*/
|
|
class Cart extends Controller
|
|
{
|
|
private $model;
|
|
|
|
/**
|
|
* 构造方法
|
|
*/
|
|
public function initialize()
|
|
{
|
|
parent::initialize();
|
|
$table_id = $this->request->param('table_id');
|
|
$this->model = new CartModel($this->applet_id, $this->shop_id, $this->user_id, $table_id);
|
|
}
|
|
|
|
/**
|
|
* 一键清空购物车
|
|
*/
|
|
public function clearAll()
|
|
{
|
|
$this->model->clearAll();
|
|
return $this->renderMsg('清除成功');
|
|
}
|
|
|
|
/**
|
|
* 更改购物车
|
|
*/
|
|
public function edit()
|
|
{
|
|
if (!$this->model->edit($this->request->post())) {
|
|
return $this->renderError($this->model->getError() ?: '操作失败');
|
|
}
|
|
$total_num = $this->model->getTotalNum();
|
|
return $this->renderSuccess(['cart_total_num' => $total_num], '操作成功');
|
|
}
|
|
|
|
/**
|
|
* 加入购物车
|
|
*/
|
|
public function add()
|
|
{
|
|
if (!$this->model->add($this->request->post())) {
|
|
return $this->renderError($this->model->getError() ?: '操作失败');
|
|
}
|
|
$total_num = $this->model->getTotalNum();
|
|
return $this->renderSuccess(['cart_total_num' => $total_num], '操作成功');
|
|
}
|
|
|
|
/**
|
|
* 减少购物车商品数量
|
|
*/
|
|
public function sub()
|
|
{
|
|
if (!$this->model->sub($this->request->post())) {
|
|
return $this->renderError($this->model->getError() ?: '操作成功');
|
|
}
|
|
$total_num = $this->model->getTotalNum();
|
|
return $this->renderSuccess(['cart_total_num' => $total_num], '操作成功');
|
|
}
|
|
|
|
/**
|
|
* 删除购物车中指定商品
|
|
*/
|
|
public function delete()
|
|
{
|
|
if (!$this->model->delete($this->request->post())) {
|
|
return $this->renderError($this->model->getError() ?: '操作成功');
|
|
}
|
|
$total_num = $this->model->getTotalNum();
|
|
return $this->renderSuccess(['cart_total_num' => $total_num], '操作成功');
|
|
}
|
|
|
|
}
|