|
|
<?php
|
|
|
namespace app\appapi\controller\v1;
|
|
|
|
|
|
use app\appapi\ApiController;
|
|
|
use think\facade\Db;
|
|
|
use think\facade\Log;
|
|
|
|
|
|
class Cart extends ApiController
|
|
|
{
|
|
|
/**
|
|
|
* 添加購物車
|
|
|
* @author wayne <wwayne.hsu@gmail.com>
|
|
|
* @param string
|
|
|
*
|
|
|
* @return json
|
|
|
*/
|
|
|
public function add(){
|
|
|
|
|
|
$goods = input('post.');
|
|
|
|
|
|
$goods['goods_sn'] = Db::name('goods')->where('goods_id',$goods['goods_id'])->value('goods_sn');
|
|
|
|
|
|
// 資料檢查
|
|
|
if(empty($goods)){
|
|
|
return $this->Error('參數錯誤');
|
|
|
}
|
|
|
|
|
|
try{
|
|
|
$this->addToCart($goods);
|
|
|
}catch(\Exception $e){
|
|
|
return $this->Error($e->getMessage());
|
|
|
}
|
|
|
|
|
|
$do = Db::name('cart')
|
|
|
->field('c.goods_id,rec_id,c.goods_sn,goods_price,goods_thumb,g.goods_name,c.goods_number,c.add_info,g.buymax')
|
|
|
->alias('c')
|
|
|
->leftJoin('goods g','c.goods_id=g.goods_id')
|
|
|
->where('session_id',$this->SessionId);
|
|
|
|
|
|
if($this->uid > 0){
|
|
|
$do->whereor('user_id',$this->uid);
|
|
|
}
|
|
|
|
|
|
$result = $do->select();
|
|
|
|
|
|
return $this->success($result);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 刪除購物車
|
|
|
* @author wayne <wwayne.hsu@gmail.com>
|
|
|
* @param string
|
|
|
*
|
|
|
* @return json
|
|
|
*/
|
|
|
public function delete(){
|
|
|
$id = intval(input('id'));
|
|
|
|
|
|
if(!($id > 0)){
|
|
|
return $this->Error('參數錯誤');
|
|
|
}
|
|
|
|
|
|
try{
|
|
|
Db::name('cart')
|
|
|
->where('rec_id',$id)
|
|
|
->delete();
|
|
|
|
|
|
return $this->Success('刪除成功');
|
|
|
}catch(\Exception $e){
|
|
|
return $this->Error('執行失敗');
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
public function update(){
|
|
|
$item = input();
|
|
|
|
|
|
try{
|
|
|
$rtn = Db::name('cart')
|
|
|
->where('rec_id',$item['rec_id'])
|
|
|
->update(['goods_number'=>$item['num']]);
|
|
|
}catch(\Exception $e){
|
|
|
print_r($e);
|
|
|
return $this->Error('執行失敗');
|
|
|
}
|
|
|
return $this->Success('執行成功');
|
|
|
}
|
|
|
|
|
|
public function getItems(){
|
|
|
$items = Db::name('cart')
|
|
|
->field('c.goods_id,c.goods_sn,rec_id,goods_price,goods_thumb,g.goods_name,c.goods_number,c.add_info,g.buymax')
|
|
|
->alias('c')
|
|
|
->leftJoin('goods g','c.goods_id=g.goods_id')
|
|
|
->where('session_id',$this->SessionId)
|
|
|
->select();
|
|
|
|
|
|
return $this->success($items);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 添加商品到購物車
|
|
|
*
|
|
|
* @access private
|
|
|
* @param integer $goods_id 商品編號
|
|
|
* @param integer $num 商品數量
|
|
|
* @param array $spec 規格值對應的id數組
|
|
|
* @param integer $parent 基本件
|
|
|
* @return boolean
|
|
|
*/
|
|
|
private function addToCart($data)
|
|
|
{
|
|
|
$_parent_id = $data['parent'];
|
|
|
|
|
|
/* 取得商品信息 */
|
|
|
$goods = Db::name('goods')
|
|
|
->where('goods_id',$data['goods_id'])
|
|
|
->find();
|
|
|
|
|
|
if (!$goods)
|
|
|
{
|
|
|
throw new \think\Exception('查無商品');
|
|
|
}
|
|
|
|
|
|
/* 如果是作為配件添加到購物車的,需要先檢查購物車裏面是否已經有基本件 */
|
|
|
// if ($parent > 0)
|
|
|
// {
|
|
|
// $sql = "SELECT COUNT(*) FROM " . $GLOBALS['ecs']->table('cart') .
|
|
|
// " WHERE goods_id='$parent' AND $sql_where AND extension_code <> 'package_buy'";//修改購物車選擇性結算--青蜂網絡www.0769web.net
|
|
|
// if ($GLOBALS['db']->getOne($sql) == 0)
|
|
|
// {
|
|
|
// $GLOBALS['err']->add($GLOBALS['_LANG']['no_basic_goods'], ERR_NO_BASIC_GOODS);
|
|
|
|
|
|
// return false;
|
|
|
// }
|
|
|
// }
|
|
|
|
|
|
/* 是否正在銷售 */
|
|
|
if ($goods['is_on_sale'] == 0)
|
|
|
{
|
|
|
throw new \think\Exception('商品已下架');
|
|
|
}
|
|
|
|
|
|
/* 不是配件時檢查是否允許單獨銷售 */
|
|
|
// if (empty($parent) && $goods['is_alone_sale'] == 0)
|
|
|
// {
|
|
|
// $GLOBALS['err']->add($GLOBALS['_LANG']['cannt_alone_sale'], ERR_CANNT_ALONE_SALE);
|
|
|
|
|
|
// return false;
|
|
|
// }
|
|
|
|
|
|
//TODO: 檢查商品規格
|
|
|
/* 如果商品有規格則取規格商品信息 配件除外 */
|
|
|
// $sql = "SELECT * FROM " .$GLOBALS['ecs']->table('products'). " WHERE goods_id = '$goods_id' LIMIT 0, 1";
|
|
|
// $prod = $GLOBALS['db']->getRow($sql);
|
|
|
|
|
|
// if (is_spec($spec) && !empty($prod))
|
|
|
// {
|
|
|
// $product_info = get_products_info($goods_id, $spec);
|
|
|
// }
|
|
|
// if (empty($product_info))
|
|
|
// {
|
|
|
// $product_info = array('product_number' => '', 'product_id' => 0);
|
|
|
// }
|
|
|
|
|
|
/* 檢查:庫存 */
|
|
|
// if ($GLOBALS['_CFG']['use_storage'] == 1)
|
|
|
// {
|
|
|
// //檢查:商品購買數量是否大於總庫存
|
|
|
// if ($num > $goods['goods_number'])
|
|
|
// {
|
|
|
// $GLOBALS['err']->add(sprintf($GLOBALS['_LANG']['shortage'], $goods['goods_number']), ERR_OUT_OF_STOCK);
|
|
|
|
|
|
// return false;
|
|
|
// }
|
|
|
|
|
|
// //商品存在規格 是貨品 檢查該貨品庫存
|
|
|
// if (is_spec($spec) && !empty($prod))
|
|
|
// {
|
|
|
// if (!empty($spec))
|
|
|
// {
|
|
|
// /* 取規格的貨品庫存 */
|
|
|
// if ($num > $product_info['product_number'])
|
|
|
// {
|
|
|
// $GLOBALS['err']->add(sprintf($GLOBALS['_LANG']['shortage'], $product_info['product_number']), ERR_OUT_OF_STOCK);
|
|
|
|
|
|
// return false;
|
|
|
// }
|
|
|
// }
|
|
|
// }
|
|
|
// }
|
|
|
|
|
|
/* 計算商品的促銷價格 */
|
|
|
// $spec_price = spec_price($spec);
|
|
|
// $goods_price = get_final_price($goods_id, $num, true, $spec);
|
|
|
// $goods['market_price'] += $spec_price;
|
|
|
// $goods_attr = get_goods_attr_info($spec);
|
|
|
// $goods_attr_id = join(',', $spec);
|
|
|
|
|
|
/* 初始化要插入購物車的基本件數據 */
|
|
|
$parent = array(
|
|
|
'user_id' => $this->uid || 0,
|
|
|
'session_id' => $this->SessionId,
|
|
|
'goods_id' => $data['goods_id'],
|
|
|
'goods_sn' => addslashes($goods['goods_sn']),
|
|
|
'product_id' => 0,
|
|
|
'goods_name' => addslashes($goods['goods_name']),
|
|
|
'market_price' => $goods['market_price'],
|
|
|
'goods_attr' => '',
|
|
|
'goods_attr_id' => '',
|
|
|
'is_real' => $goods['is_real'],
|
|
|
'extension_code'=> $goods['extension_code'],
|
|
|
'is_gift' => 0,
|
|
|
'is_shipping' => $goods['is_shipping'],
|
|
|
'rec_type' => 0,
|
|
|
'group_id' => 0,
|
|
|
'goods_price' => $goods['shop_price'],
|
|
|
'goods_number' => 1,
|
|
|
'add_info' => isset($data['cardInfo']) ? json_encode($data['cardInfo']):'',
|
|
|
'buymax' => $goods['buymax'],
|
|
|
);
|
|
|
|
|
|
/* 如果該配件在添加為基本件的配件時,所設置的“配件價格”比原價低,即此配件在價格上提供了優惠, */
|
|
|
/* 則按照該配件的優惠價格賣,但是每一個基本件只能購買一個優惠價格的“該配件”,多買的“該配件”不享 */
|
|
|
/* 受此優惠 */
|
|
|
// $basic_list = array();
|
|
|
// $sql = "SELECT parent_id, goods_price " .
|
|
|
// "FROM " . $GLOBALS['ecs']->table('group_goods') .
|
|
|
// " WHERE goods_id = '$goods_id'" .
|
|
|
// " AND goods_price < '$goods_price'" .
|
|
|
// " AND parent_id = '$_parent_id'" .
|
|
|
// " ORDER BY goods_price";
|
|
|
// $res = $GLOBALS['db']->query($sql);
|
|
|
// while ($row = $GLOBALS['db']->fetchRow($res))
|
|
|
// {
|
|
|
// $basic_list[$row['parent_id']] = $row['goods_price'];
|
|
|
// }
|
|
|
|
|
|
// /* 取得購物車中該商品每個基本件的數量 */
|
|
|
// $basic_count_list = array();
|
|
|
// if ($basic_list)
|
|
|
// {
|
|
|
// $sql = "SELECT goods_id, SUM(goods_number) AS count " .
|
|
|
// "FROM " . $GLOBALS['ecs']->table('cart') .
|
|
|
// " WHERE $sql_where" .//修改購物車選擇性結算--青蜂網絡www.0769web.net
|
|
|
// " AND parent_id = 0" .
|
|
|
// " AND extension_code <> 'package_buy' " .
|
|
|
// " AND goods_id " . db_create_in(array_keys($basic_list)) .
|
|
|
// " GROUP BY goods_id";
|
|
|
// $res = $GLOBALS['db']->query($sql);
|
|
|
// while ($row = $GLOBALS['db']->fetchRow($res))
|
|
|
// {
|
|
|
// $basic_count_list[$row['goods_id']] = $row['count'];
|
|
|
// }
|
|
|
// }
|
|
|
|
|
|
/* 取得購物車中該商品每個基本件已有該商品配件數量,計算出每個基本件還能有幾個該商品配件 */
|
|
|
/* 一個基本件對應一個該商品配件 */
|
|
|
// if ($basic_count_list)
|
|
|
// {
|
|
|
// $sql = "SELECT parent_id, SUM(goods_number) AS count " .
|
|
|
// "FROM " . $GLOBALS['ecs']->table('cart') .
|
|
|
// " WHERE $sql_where" .//修改購物車選擇性結算--青蜂網絡www.0769web.net
|
|
|
// " AND goods_id = '$goods_id'" .
|
|
|
// " AND extension_code <> 'package_buy' " .
|
|
|
// " AND parent_id " . db_create_in(array_keys($basic_count_list)) .
|
|
|
// " GROUP BY parent_id";
|
|
|
// $res = $GLOBALS['db']->query($sql);
|
|
|
// while ($row = $GLOBALS['db']->fetchRow($res))
|
|
|
// {
|
|
|
// $basic_count_list[$row['parent_id']] -= $row['count'];
|
|
|
// }
|
|
|
// }
|
|
|
|
|
|
/* 循環插入配件 如果是配件則用其添加數量依次為購物車中所有屬於其的基本件添加足夠數量的該配件 */
|
|
|
// foreach ($basic_list as $parent_id => $fitting_price)
|
|
|
// {
|
|
|
// /* 如果已全部插入,退出 */
|
|
|
// if ($num <= 0)
|
|
|
// {
|
|
|
// break;
|
|
|
// }
|
|
|
|
|
|
// /* 如果該基本件不再購物車中,執行下一個 */
|
|
|
// if (!isset($basic_count_list[$parent_id]))
|
|
|
// {
|
|
|
// continue;
|
|
|
// }
|
|
|
|
|
|
// /* 如果該基本件的配件數量已滿,執行下一個基本件 */
|
|
|
// if ($basic_count_list[$parent_id] <= 0)
|
|
|
// {
|
|
|
// continue;
|
|
|
// }
|
|
|
|
|
|
// /* 作為該基本件的配件插入 */
|
|
|
// $parent['goods_price'] = max($fitting_price, 0) + $spec_price; //允許該配件優惠價格為0
|
|
|
// $parent['goods_number'] = min($num, $basic_count_list[$parent_id]);
|
|
|
// $parent['parent_id'] = $parent_id;
|
|
|
|
|
|
$do = Db::name('cart')
|
|
|
->where('goods_id',$data['goods_id'])
|
|
|
->where('session_id',$this->SessionId);
|
|
|
|
|
|
if($this->uid>0){
|
|
|
$do->whereor('user_id',$this->uid);
|
|
|
}
|
|
|
|
|
|
$cart_num = $do->count();
|
|
|
|
|
|
if($cart_num > 0){
|
|
|
//已經存在,更新
|
|
|
try{
|
|
|
$do = Db::name('cart')
|
|
|
->where('goods_id',$data['goods_id'])
|
|
|
->where('session_id',$this->SessionId);
|
|
|
if($this->uid>0){
|
|
|
$do->whereor('user_id',$this->uid);
|
|
|
}
|
|
|
if(($goods['buymax'] > 0) && ($cart_num + $data['number'] > $goods['buymax'])){
|
|
|
$goods_number = $goods['buymax'];
|
|
|
$rtn = $do->update(['goods_number'=>$goods_number]);
|
|
|
}else{
|
|
|
$do->inc('goods_number',$data['number']);
|
|
|
$rtn = $do->update();
|
|
|
}
|
|
|
}catch(\Exception $e){
|
|
|
print_r($e->getMessage());
|
|
|
throw new \think\Exception('資料庫添加失敗');
|
|
|
}
|
|
|
}else{
|
|
|
/* 添加 */
|
|
|
$rtn = Db::name('cart')
|
|
|
->insert($parent);
|
|
|
|
|
|
if(!$rtn){
|
|
|
throw new \think\Exception('資料庫添加失敗');
|
|
|
}
|
|
|
}
|
|
|
Log::write(Db::getLastSql());
|
|
|
|
|
|
// /* 改變數量 */
|
|
|
// $num -= $parent['goods_number'];
|
|
|
// }
|
|
|
|
|
|
/* 如果數量不為0,作為基本件插入 */
|
|
|
// if ($num > 0)
|
|
|
// {
|
|
|
// /* 檢查該商品是否已經存在在購物車中 */
|
|
|
// $sql = "SELECT goods_number FROM " .$GLOBALS['ecs']->table('cart').
|
|
|
// " WHERE $sql_where AND goods_id = '$goods_id' ".//修改購物車選擇性結算--青蜂網絡www.0769web.net
|
|
|
// " AND parent_id = 0 AND goods_attr = '" .get_goods_attr_info($spec). "' " .
|
|
|
// " AND extension_code <> 'package_buy' " .
|
|
|
// " AND rec_type = 'CART_GENERAL_GOODS'";
|
|
|
|
|
|
// $row = $GLOBALS['db']->getRow($sql);
|
|
|
|
|
|
// if($row) //如果購物車已經有此物品,則更新
|
|
|
// {
|
|
|
// $num += $row['goods_number'];
|
|
|
// if(is_spec($spec) && !empty($prod) )
|
|
|
// {
|
|
|
// $goods_storage=$product_info['product_number'];
|
|
|
// }
|
|
|
// else
|
|
|
// {
|
|
|
// $goods_storage=$goods['goods_number'];
|
|
|
// }
|
|
|
// if ($GLOBALS['_CFG']['use_storage'] == 0 || $num <= $goods_storage)
|
|
|
// {
|
|
|
// $goods_price = get_final_price($goods_id, $num, true, $spec);
|
|
|
// $sql = "UPDATE " . $GLOBALS['ecs']->table('cart') . " SET goods_number = '$num'" .
|
|
|
// " , goods_price = '$goods_price'".
|
|
|
// " WHERE $sql_where AND goods_id = '$goods_id' ".//修改購物車選擇性結算--青蜂網絡www.0769web.net
|
|
|
// " AND parent_id = 0 AND goods_attr = '" .get_goods_attr_info($spec). "' " .
|
|
|
// " AND extension_code <> 'package_buy' " .
|
|
|
// "AND rec_type = 'CART_GENERAL_GOODS'";
|
|
|
// $GLOBALS['db']->query($sql);
|
|
|
// }
|
|
|
// else
|
|
|
// {
|
|
|
// $GLOBALS['err']->add(sprintf($GLOBALS['_LANG']['shortage'], $num), ERR_OUT_OF_STOCK);
|
|
|
|
|
|
// return false;
|
|
|
// }
|
|
|
// }
|
|
|
// else //購物車沒有此物品,則插入
|
|
|
// {
|
|
|
// // 判斷是否為預售商品 start
|
|
|
// $pre_sale_id = is_pre_sale_goods($goods_id);
|
|
|
// if(!empty($pre_sale_id))
|
|
|
// {
|
|
|
|
|
|
// /* 更新:記錄購物流程類型:預售 */
|
|
|
// $_SESSION['flow_type'] = CART_PRE_SALE_GOODS;
|
|
|
// $_SESSION['extension_code'] = PRE_SALE_CODE;
|
|
|
// $_SESSION['extension_id'] = $pre_sale_id;
|
|
|
|
|
|
// $parent['extension_code'] = PRE_SALE_CODE;
|
|
|
// $parent['rec_type'] = CART_PRE_SALE_GOODS;
|
|
|
|
|
|
// //獲取預售信息
|
|
|
// $pre_sale = pre_sale_info($pre_sale_id, $num);
|
|
|
// if($pre_sale['deposit'] > 0)
|
|
|
// {
|
|
|
// //定金大於0則使用定金金額
|
|
|
// $goods_price = $pre_sale['deposit'];
|
|
|
// }
|
|
|
// else
|
|
|
// {
|
|
|
// //計算當前價格
|
|
|
// $goods_price = $pre_sale['cur_price'];
|
|
|
|
|
|
// //加入規格價格
|
|
|
// if (!empty($spec))
|
|
|
// {
|
|
|
// $spec_price = spec_price($spec);
|
|
|
// $goods_price += $spec_price;
|
|
|
// }
|
|
|
// }
|
|
|
|
|
|
// }
|
|
|
// else
|
|
|
// {
|
|
|
// $goods_price = get_final_price($goods_id, $num, true, $spec);
|
|
|
// }
|
|
|
// // 判斷是否為預售商品 end
|
|
|
// $parent['goods_price'] = max($goods_price, 0);
|
|
|
// $parent['goods_number'] = $num;
|
|
|
// $parent['fencheng'] = $goods['fencheng'];
|
|
|
// $parent['parent_id'] = 0;
|
|
|
// $GLOBALS['db']->autoExecute($GLOBALS['ecs']->table('cart'), $parent, 'INSERT');
|
|
|
// }
|
|
|
// }
|
|
|
|
|
|
/* 把贈品刪除 */
|
|
|
// $sql = "DELETE FROM " . $GLOBALS['ecs']->table('cart') . " WHERE $sql_where AND is_gift <> 0";//修改購物車選擇性結算--青蜂網絡www.0769web.net
|
|
|
// $GLOBALS['db']->query($sql);
|
|
|
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
}
|