123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404 |
- <?php
- namespace app\api\controller;
- use think\facade\Lang;
- /**
-
- *
-
- *
- * ----------------------------------------------------------------------------
- *
-
- * 购物车控制器
- */
- class Membercart extends MobileMember
- {
- public function initialize()
- {
- parent::initialize();
- Lang::load(base_path() . 'home/lang/' . config('lang.default_lang') . '/cart.lang.php');
- }
- /**
- * @api {POST} api/Membercart/cart_list 购物车列表
- * @apiVersion 1.0.0
- * @apiGroup Membercart
- *
- * @apiHeader {String} X-DS-KEY 用户授权token
- *
- * @apiSuccess {String} code 返回码,10000为成功
- * @apiSuccess {String} message 返回消息
- * @apiSuccess {Object} result 返回数据
- * @apiSuccess {Int} result.cart_count 物品种类数量
- * @apiSuccess {Object} result.cart_list 购物车列表,键为店铺ID
- * @apiSuccess {Int} result.cart_list.store_id 店铺ID
- * @apiSuccess {String} result.cart_list.store_name 店铺名称
- * @apiSuccess {String} result.cart_list.store_name 店铺优惠券
- * @apiSuccess {String} result.cart_list.goods 商品列表
- * @apiSuccess {Int} result.cart_list.goods.bl_id 优惠套餐ID
- * @apiSuccess {Int} result.cart_list.goods.buyer_id 买家ID
- * @apiSuccess {Int} result.cart_list.goods.cart_id 购物车ID
- * @apiSuccess {Int} result.cart_list.goods.gc_id 分类ID
- * @apiSuccess {Object} result.cart_list.goods.gift_list 赠品列表
- * @apiSuccess {Int} result.cart_list.goods.goods_commonid 商品公共ID
- * @apiSuccess {Float} result.cart_list.goods.goods_freight 运费
- * @apiSuccess {Int} result.cart_list.goods.goods_id 商品ID
- * @apiSuccess {String} result.cart_list.goods.goods_image 商品图片名称
- * @apiSuccess {String} result.cart_list.goods.goods_image_url 商品图片完整路径
- * @apiSuccess {String} result.cart_list.goods.goods_name 商品名称
- * @apiSuccess {Int} result.cart_list.goods.goods_num 购买数量
- * @apiSuccess {Float} result.cart_list.goods.goods_price 商品价格
- * @apiSuccess {Int} result.cart_list.goods.goods_storage 商品库存
- * @apiSuccess {Int} result.cart_list.goods.goods_storage_alarm 预警库存
- * @apiSuccess {Int} result.cart_list.goods.goods_vat 是否支持发票 0否1是
- * @apiSuccess {Object} result.cart_list.goods.groupbuy_info 抢购信息
- * @apiSuccess {Int} result.cart_list.goods.is_goodsfcode 是否F码商品 0否1是
- * @apiSuccess {Int} result.cart_list.goods.is_have_gift 是否含赠品 0否1是
- * @apiSuccess {Object} result.cart_list.goods.mgdiscount_info 会员折扣信息
- * @apiSuccess {Boolean} result.cart_list.goods.state 商品状态 true上架false下架
- * @apiSuccess {Boolean} result.cart_list.goods.storage_state 商品库存状态 true充足false不足
- * @apiSuccess {Int} result.cart_list.goods.store_id 店铺ID
- * @apiSuccess {String} result.cart_list.goods.store_name 店铺名称
- * @apiSuccess {Int} result.cart_list.goods.transport_id 售卖区域id
- * @apiSuccess {Object} result.cart_list.goods.xianshi_info 秒杀信息
- * @apiSuccess {Int} result.cart_list.store_id 店铺ID
- * @apiSuccess {String} result.cart_list.store_name 店铺名称
- * @apiSuccess {Object[]} result.cart_val 去除result.cart_list键的数组
- * @apiSuccess {Float} result.sum 总价
- */
- public function cart_list()
- {
- $cart_model = model('cart');
- $condition = array();
- $condition[] = array('buyer_id', '=', $this->member_info['member_id']);
- $cart_list = $cart_model->getCartList('db', $condition);
- // 购物车列表 [得到最新商品属性及促销信息]
- $cart_list = model('buy_1', 'logic')->getGoodsCartList($cart_list);
- $goods_model = model('goods');
- $sum = 0;
- $cart_a = array();
- $k = 0;
- $voucher_model = model('voucher');
- foreach ($cart_list as $key => $val) {
- $cart_a[$val['store_id']]['store_id'] = $val['store_id'];
- $cart_a[$val['store_id']]['store_name'] = $val['store_name'];
- //获取店铺代金券
- $cart_a[$val['store_id']]['voucher_list'] = $voucher_model->getVouchertemplateList(array(array('vouchertemplate_store_id', '=', $val['store_id']), array('vouchertemplate_if_private', '=', 0), array('vouchertemplate_state', '=', 1), array('vouchertemplate_enddate', '>', TIMESTAMP)));
- // $goods_data = $goods_model->getGoodsOnlineInfoForShare($val['goods_id']);
- $cart_a[$val['store_id']]['goods'][$key] = $val;
- $cart_a[$val['store_id']]['goods'][$key]['cart_id'] = $val['cart_id'];
- $cart_a[$val['store_id']]['goods'][$key]['goods_num'] = $val['goods_num'];
- $cart_a[$val['store_id']]['goods'][$key]['goods_image_url'] = goods_cthumb($val['goods_image'], $val['store_id']);
- $cart_a[$val['store_id']]['goods'][$key]['goods_state'] = $val['state'];
- // if (isset($goods_data['goods_promotion_type'])) {
- // $cart_a[$val['store_id']]['goods'][$key]['goods_price'] = $goods_data['goods_promotion_price'];
- // }
- $cart_a[$val['store_id']]['goods'][$key]['gift_list'] = isset($val['gift_list']) ? $val['gift_list'] : '';
- if ($val['bl_id']) {
- foreach ($cart_a[$val['store_id']]['goods'][$key]['bl_goods_list'] as $k => $v) {
- $cart_a[$val['store_id']]['goods'][$key]['bl_goods_list'][$k]['goods_image_url'] = goods_cthumb($v['goods_image'], $val['store_id']);
- }
- }
- if ($val['state']) {
- $cart_list[$key]['goods_sum'] = ds_price_format($val['goods_price'] * $val['goods_num']);
- $sum += $cart_list[$key]['goods_sum'];
- $k++;
- }
- }
- foreach ($cart_a as $key => $value) {
- $value['goods'] = array_values($value['goods']);
- $cart_l[] = $value;
- }
- if (isset($cart_l)) {
- $cart_b = array_values($cart_l);
- } else {
- $cart_b = array();
- }
- ds_json_encode(10000, '', array('cart_list' => $cart_a, 'sum' => ds_price_format($sum), 'cart_count' => count($cart_list), 'cart_val' => $cart_b), request()->action());
- }
- /**
- * @api {POST} api/membercart/cart_add 购物车添加
- * @apiVersion 1.0.0
- * @apiGroup MemberCart
- *
- * @apiHeader {String} X-DS-KEY 用户授权token
- *
- * @apiParam {Int} goods_id 商品ID
- * @apiParam {Int} quantity 购买数量
- * @apiParam {Int} bl_id 组合购买ID
- *
- * @apiSuccess {String} code 返回码,10000为成功
- * @apiSuccess {String} message 返回消息
- * @apiSuccess {Object} result 返回数据(购物车主键ID)
- */
- public function cart_add()
- {
- if (isset($this->member_info) && !$this->member_info['is_buylimit']) {
- ds_json_encode(10001, lang('cart_buy_noallow'), '', request()->action());
- }
- if (config('ds_config.member_auth') && $this->member_info['member_auth_state'] != 3) {
- ds_json_encode(10001, lang('cart_buy_noauth'), '', request()->action());
- }
- $goods_id = intval(input('post.goods_id'));
- $quantity = intval(input('post.quantity'));
- $bl_id = intval(input('post.bl_id'));
- $goods_model = model('goods');
- $cart_model = model('cart');
- $logic_buy_1 = model('buy_1', 'logic');
- if (!$bl_id) {
- if ($goods_id <= 0 || $quantity <= 0) {
- ds_json_encode(10001, lang('param_error'), '', request()->action());
- }
- $goods_info = $goods_model->getGoodsOnlineInfoAndPromotionById($goods_id);
- //验证是否可以购买
- if (empty($goods_info)) {
- ds_json_encode(10001, lang('cart_add_goods_not_exists'), '', request()->action());
- }
- //抢购
- $logic_buy_1->getGroupbuyInfo($goods_info, $quantity);
- //秒杀
- $logic_buy_1->getXianshiInfo($goods_info, $quantity);
- if (isset($this->member_info) && $goods_info['store_id'] == $this->member_info['store_id']) {
- ds_json_encode(10001, lang('cannot_buy_self_goods'), '', request()->action());
- }
- if (intval($goods_info['goods_storage']) < 1 || intval($goods_info['goods_storage']) < $quantity) {
- ds_json_encode(10001, lang('cart_add_stock_shortage'), '', request()->action());
- }
- } else {
- //优惠套装加入购物车(单套)
- $pbundling_model = model('pbundling');
- $bl_info = $pbundling_model->getBundlingInfo(array('bl_id' => $bl_id));
- if (empty($bl_info) || $bl_info['bl_state'] == '0') {
- ds_json_encode(10001, lang('recommendations_buy_separately'), '', request()->action());
- }
- //检查每个商品是否符合条件,并重新计算套装总价
- $bl_goods_list = $pbundling_model->getBundlingGoodsList(array('bl_id' => $bl_id));
- $goods_id_array = array();
- $bl_amount = 0;
- foreach ($bl_goods_list as $goods) {
- $goods_id_array[] = $goods['goods_id'];
- $bl_amount += $goods['blgoods_price'];
- }
- $goods_model = model('goods');
- $goods_list = $goods_model->getGoodsOnlineListAndPromotionByIdArray($goods_id_array);
- foreach ($goods_list as $goods_info) {
- if (isset($this->member_info) && $goods_info['store_id'] == $this->member_info['store_id']) {
- ds_json_encode(10001, lang('cannot_buy_self_goods'), '', request()->action());
- }
- if (intval($goods_info['goods_storage']) < 1) {
- ds_json_encode(10001, lang('cart_add_stock_shortage'), '', request()->action());
- }
- }
- //优惠套装作为一条记录插入购物车,图片取套装内的第一个商品图
- $goods_info = array();
- $goods_info['store_id'] = $bl_info['store_id'];
- $goods_info['goods_id'] = $goods_list[0]['goods_id'];
- $goods_info['goods_storage'] = $goods_list[0]['goods_storage'];
- $goods_info['goods_name'] = $bl_info['bl_name'];
- $goods_info['goods_price'] = $bl_amount;
- $goods_info['goods_num'] = 1;
- $goods_info['goods_image'] = $goods_list[0]['goods_image'];
- $goods_info['store_name'] = $bl_info['store_name'];
- $goods_info['bl_id'] = $bl_id;
- $quantity = 1;
- }
- $param = array();
- $param['buyer_id'] = isset($this->member_info) ? $this->member_info['member_id'] : 0;
- $param['store_id'] = $goods_info['store_id'];
- $param['goods_id'] = $goods_info['goods_id'];
- $param['goods_name'] = $goods_info['goods_name'];
- $param['goods_price'] = $goods_info['goods_price'];
- $param['goods_image'] = $goods_info['goods_image'];
- $param['store_name'] = $goods_info['store_name'];
- $param['goods_storage'] = $goods_info['goods_storage'];
- $param['bl_id'] = $bl_id;
- $result = $cart_model->addCart($param, 'db', $quantity);
- if ($result) {
- ds_json_encode(10000, lang('ds_common_op_succ'), $result, request()->action());
- } else {
- ds_json_encode(10001, $cart_model->error_message, '', request()->action());
- }
- }
- /**
- * @api {POST} api/membercart/cart_del 购物车删除
- * @apiVersion 1.0.0
- * @apiGroup MemberCart
- *
- * @apiHeader {String} X-DS-KEY 用户授权token
- *
- * @apiParam {String} cart_id 购物车主键ID 例3,5,8
- *
- * @apiSuccess {String} code 返回码,10000为成功
- * @apiSuccess {String} message 返回消息
- */
- public function cart_del()
- {
- $cart_id = input('param.cart_id');
- $cart_id_array = ds_delete_param($cart_id);
- if ($cart_id_array == FALSE) {
- ds_json_encode('10001', lang('param_error'), '', request()->action());
- }
- $cart_model = model('cart');
- if ($cart_id > 0) {
- $condition = array();
- $condition[] = array('buyer_id', '=', $this->member_info['member_id']);
- $condition[] = array('cart_id', 'in', $cart_id_array);
- $cart_model->delCart('db', $condition, $this->member_info['member_id']);
- }
- ds_json_encode(10000, '', 1, request()->action());
- }
- /**
- * @api {POST} api/membercart/cart_edit_quantity 更新购物车购买数量
- * @apiVersion 1.0.0
- * @apiGroup MemberCart
- *
- * @apiHeader {String} X-DS-KEY 用户授权token
- *
- * @apiParam {Int} cart_id 购物车主键ID
- * @apiParam {Int} quantity 修改数量
- *
- * @apiSuccess {String} code 返回码,10000为成功
- * @apiSuccess {String} message 返回消息
- * @apiSuccess {Object} result 返回数据
- * @apiSuccess {Object} result.quantity 数量
- * @apiSuccess {Object} result.goods_price 单价
- * @apiSuccess {Object} result.total_price 总价
- *
- */
- public function cart_edit_quantity()
- {
- $cart_id = intval(abs(input('post.cart_id')));
- $quantity = intval(abs(input('post.quantity')));
- if (empty($cart_id) || empty($quantity)) {
- ds_json_encode(10001, lang('param_error'), '', request()->action());
- }
- $cart_model = model('cart');
- $cart_info = $cart_model->getCartInfo(array('cart_id' => $cart_id, 'buyer_id' => $this->member_info['member_id']));
- //检查是否为本人购物车
- if ($cart_info['buyer_id'] != $this->member_info['member_id']) {
- ds_json_encode(10001, lang('param_error'), '', request()->action());
- }
- //检查库存是否充足
- if (!$this->_check_goods_storage($cart_info, $quantity, $this->member_info['member_id'])) {
- ds_json_encode(10001, lang('goods_quantity_limit'), '', request()->action());
- }
- $data = array();
- $data['goods_num'] = $quantity;
- $data['goods_price'] = $cart_info['goods_price'];
- $condition = array();
- $condition[] = array('cart_id', '=', $cart_id);
- $condition[] = array('buyer_id', '=', $cart_info['buyer_id']);
- $update = $cart_model->editCart($data, $condition, $cart_info['buyer_id']);
- if ($update) {
- $return = array();
- $return['quantity'] = $quantity;
- $return['goods_price'] = ds_price_format($cart_info['goods_price']);
- $return['total_price'] = ds_price_format($cart_info['goods_price'] * $quantity);
- ds_json_encode(10000, '', $return, request()->action());
- } else {
- ds_json_encode(10001, lang('ds_common_op_fail'), '', request()->action());
- }
- }
- /**
- * 检查库存是否充足
- */
- private function _check_goods_storage(&$cart_info, $quantity, $member_id)
- {
- $goods_model = model('goods');
- $pbundling_model = model('pbundling');
- $logic_buy_1 = model('buy_1', 'logic');
- if ($cart_info['bl_id'] == '0') {
- //普通商品
- $goods_info = $goods_model->getGoodsOnlineInfoAndPromotionById($cart_info['goods_id']);
- //抢购
- $logic_buy_1->getGroupbuyInfo($goods_info, $quantity);
- if (isset($goods_info['ifgroupbuy'])) {
- if ($goods_info['upper_limit'] && $quantity > $goods_info['upper_limit']) {
- return false;
- }
- }
- //批发
- $logic_buy_1->getWholesaleInfo($goods_info, $quantity);
- //秒杀
- $logic_buy_1->getXianshiInfo($goods_info, $quantity);
- if (intval($goods_info['goods_storage']) < $quantity) {
- return false;
- }
- //会员等级折扣
- $logic_buy_1->getMgdiscountInfo($goods_info);
- $goods_info['cart_id'] = $cart_info['cart_id'];
- $goods_info['buyer_id'] = $cart_info['buyer_id'];
- $cart_info = $goods_info;
- } else {
- //优惠套装商品
- $bl_goods_list = $pbundling_model->getBundlingGoodsList(array('bl_id' => $cart_info['bl_id']));
- $goods_id_array = array();
- foreach ($bl_goods_list as $goods) {
- $goods_id_array[] = $goods['goods_id'];
- }
- $bl_goods_list = $goods_model->getGoodsOnlineListAndPromotionByIdArray($goods_id_array);
- //如果有商品库存不足,更新购买数量到目前最大库存
- foreach ($bl_goods_list as $goods_info) {
- if (intval($goods_info['goods_storage']) < $quantity) {
- return false;
- }
- }
- }
- return true;
- }
- /**
- * @api {POST} api/Membercart/cart_count 检查购物车数量
- * @apiVersion 1.0.0
- * @apiGroup Membercart
- *
- * @apiHeader {String} X-DS-KEY 用户授权token
- *
- * @apiSuccess {String} code 返回码,10000为成功
- * @apiSuccess {String} message 返回消息
- * @apiSuccess {Object} result 返回数据
- * @apiSuccess {Int} result.cart_count 购物车数量
- */
- public function cart_count()
- {
- $cart_model = model('cart');
- $count = $cart_model->getCartCountByMemberId($this->member_info['member_id']);
- $data['cart_count'] = $count;
- ds_json_encode(10000, '', $data, request()->action());
- }
- }
|