Membercart.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. <?php
  2. namespace app\api\controller;
  3. use think\facade\Lang;
  4. /**
  5. * ============================================================================
  6. *
  7. * ============================================================================
  8. * 版权所有 2014-2028 浙江惠利玛产业互联网有限公司,并保留所有权利。
  9. * 网站地址: https://www.valimart.net/
  10. * ----------------------------------------------------------------------------
  11. *
  12. * ============================================================================
  13. * 购物车控制器
  14. */
  15. class Membercart extends MobileMember {
  16. public function initialize() {
  17. parent::initialize();
  18. Lang::load(base_path() . 'home/lang/' . config('lang.default_lang') . '/cart.lang.php');
  19. }
  20. /**
  21. * @api {POST} api/Membercart/cart_list 购物车列表
  22. * @apiVersion 1.0.0
  23. * @apiGroup Membercart
  24. *
  25. * @apiHeader {String} X-DS-KEY 用户授权token
  26. *
  27. * @apiSuccess {String} code 返回码,10000为成功
  28. * @apiSuccess {String} message 返回消息
  29. * @apiSuccess {Object} result 返回数据
  30. * @apiSuccess {Int} result.cart_count 物品种类数量
  31. * @apiSuccess {Object} result.cart_list 购物车列表,键为店铺ID
  32. * @apiSuccess {Int} result.cart_list.store_id 店铺ID
  33. * @apiSuccess {String} result.cart_list.store_name 店铺名称
  34. * @apiSuccess {String} result.cart_list.store_name 店铺优惠券
  35. * @apiSuccess {String} result.cart_list.goods 商品列表
  36. * @apiSuccess {Int} result.cart_list.goods.bl_id 优惠套餐ID
  37. * @apiSuccess {Int} result.cart_list.goods.buyer_id 买家ID
  38. * @apiSuccess {Int} result.cart_list.goods.cart_id 购物车ID
  39. * @apiSuccess {Int} result.cart_list.goods.gc_id 分类ID
  40. * @apiSuccess {Object} result.cart_list.goods.gift_list 赠品列表
  41. * @apiSuccess {Int} result.cart_list.goods.goods_commonid 商品公共ID
  42. * @apiSuccess {Float} result.cart_list.goods.goods_freight 运费
  43. * @apiSuccess {Int} result.cart_list.goods.goods_id 商品ID
  44. * @apiSuccess {String} result.cart_list.goods.goods_image 商品图片名称
  45. * @apiSuccess {String} result.cart_list.goods.goods_image_url 商品图片完整路径
  46. * @apiSuccess {String} result.cart_list.goods.goods_name 商品名称
  47. * @apiSuccess {Int} result.cart_list.goods.goods_num 购买数量
  48. * @apiSuccess {Float} result.cart_list.goods.goods_price 商品价格
  49. * @apiSuccess {Int} result.cart_list.goods.goods_storage 商品库存
  50. * @apiSuccess {Int} result.cart_list.goods.goods_storage_alarm 预警库存
  51. * @apiSuccess {Int} result.cart_list.goods.goods_vat 是否支持发票 0否1是
  52. * @apiSuccess {Object} result.cart_list.goods.groupbuy_info 抢购信息
  53. * @apiSuccess {Int} result.cart_list.goods.is_goodsfcode 是否F码商品 0否1是
  54. * @apiSuccess {Int} result.cart_list.goods.is_have_gift 是否含赠品 0否1是
  55. * @apiSuccess {Object} result.cart_list.goods.mgdiscount_info 会员折扣信息
  56. * @apiSuccess {Boolean} result.cart_list.goods.state 商品状态 true上架false下架
  57. * @apiSuccess {Boolean} result.cart_list.goods.storage_state 商品库存状态 true充足false不足
  58. * @apiSuccess {Int} result.cart_list.goods.store_id 店铺ID
  59. * @apiSuccess {String} result.cart_list.goods.store_name 店铺名称
  60. * @apiSuccess {Int} result.cart_list.goods.transport_id 售卖区域id
  61. * @apiSuccess {Object} result.cart_list.goods.xianshi_info 秒杀信息
  62. * @apiSuccess {Int} result.cart_list.store_id 店铺ID
  63. * @apiSuccess {String} result.cart_list.store_name 店铺名称
  64. * @apiSuccess {Object[]} result.cart_val 去除result.cart_list键的数组
  65. * @apiSuccess {Float} result.sum 总价
  66. */
  67. public function cart_list() {
  68. $cart_model = model('cart');
  69. $condition = array();
  70. $condition[] = array('buyer_id','=',$this->member_info['member_id']);
  71. $cart_list = $cart_model->getCartList('db', $condition);
  72. // 购物车列表 [得到最新商品属性及促销信息]
  73. $cart_list = model('buy_1', 'logic')->getGoodsCartList($cart_list);
  74. $goods_model = model('goods');
  75. $sum = 0;
  76. $cart_a = array();
  77. $k = 0;
  78. $voucher_model = model('voucher');
  79. foreach ($cart_list as $key => $val) {
  80. $cart_a[$val['store_id']]['store_id'] = $val['store_id'];
  81. $cart_a[$val['store_id']]['store_name'] = $val['store_name'];
  82. //获取店铺代金券
  83. $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)));
  84. // $goods_data = $goods_model->getGoodsOnlineInfoForShare($val['goods_id']);
  85. $cart_a[$val['store_id']]['goods'][$key] = $val;
  86. $cart_a[$val['store_id']]['goods'][$key]['cart_id'] = $val['cart_id'];
  87. $cart_a[$val['store_id']]['goods'][$key]['goods_num'] = $val['goods_num'];
  88. $cart_a[$val['store_id']]['goods'][$key]['goods_image_url'] = goods_cthumb($val['goods_image'], $val['store_id']);
  89. $cart_a[$val['store_id']]['goods'][$key]['goods_state'] = $val['state'];
  90. // if (isset($goods_data['goods_promotion_type'])) {
  91. // $cart_a[$val['store_id']]['goods'][$key]['goods_price'] = $goods_data['goods_promotion_price'];
  92. // }
  93. $cart_a[$val['store_id']]['goods'][$key]['gift_list'] = isset($val['gift_list']) ? $val['gift_list'] : '';
  94. if ($val['bl_id']) {
  95. foreach ($cart_a[$val['store_id']]['goods'][$key]['bl_goods_list'] as $k => $v) {
  96. $cart_a[$val['store_id']]['goods'][$key]['bl_goods_list'][$k]['goods_image_url'] = goods_cthumb($v['goods_image'], $val['store_id']);
  97. }
  98. }
  99. if ($val['state']) {
  100. $cart_list[$key]['goods_sum'] = ds_price_format($val['goods_price'] * $val['goods_num']);
  101. $sum += $cart_list[$key]['goods_sum'];
  102. $k++;
  103. }
  104. }
  105. foreach ($cart_a as $key => $value) {
  106. $value['goods'] = array_values($value['goods']);
  107. $cart_l[] = $value;
  108. }
  109. if (isset($cart_l)) {
  110. $cart_b = array_values($cart_l);
  111. } else {
  112. $cart_b = array();
  113. }
  114. 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());
  115. }
  116. /**
  117. * @api {POST} api/membercart/cart_add 购物车添加
  118. * @apiVersion 1.0.0
  119. * @apiGroup MemberCart
  120. *
  121. * @apiHeader {String} X-DS-KEY 用户授权token
  122. *
  123. * @apiParam {Int} goods_id 商品ID
  124. * @apiParam {Int} quantity 购买数量
  125. * @apiParam {Int} bl_id 组合购买ID
  126. *
  127. * @apiSuccess {String} code 返回码,10000为成功
  128. * @apiSuccess {String} message 返回消息
  129. * @apiSuccess {Object} result 返回数据(购物车主键ID)
  130. */
  131. public function cart_add() {
  132. if (isset($this->member_info) && !$this->member_info['is_buylimit']) {
  133. ds_json_encode(10001, lang('cart_buy_noallow'), '', request()->action());
  134. }
  135. if (config('ds_config.member_auth') && $this->member_info['member_auth_state'] != 3) {
  136. ds_json_encode(10001, lang('cart_buy_noauth'), '', request()->action());
  137. }
  138. $goods_id = intval(input('post.goods_id'));
  139. $quantity = intval(input('post.quantity'));
  140. $bl_id = intval(input('post.bl_id'));
  141. $goods_model = model('goods');
  142. $cart_model = model('cart');
  143. $logic_buy_1 = model('buy_1', 'logic');
  144. if (!$bl_id) {
  145. if ($goods_id <= 0 || $quantity <= 0) {
  146. ds_json_encode(10001, lang('param_error'), '', request()->action());
  147. }
  148. $goods_info = $goods_model->getGoodsOnlineInfoAndPromotionById($goods_id);
  149. //验证是否可以购买
  150. if (empty($goods_info)) {
  151. ds_json_encode(10001, lang('cart_add_goods_not_exists'), '', request()->action());
  152. }
  153. //抢购
  154. $logic_buy_1->getGroupbuyInfo($goods_info, $quantity);
  155. //秒杀
  156. $logic_buy_1->getXianshiInfo($goods_info, $quantity);
  157. if (isset($this->member_info) && $goods_info['store_id'] == $this->member_info['store_id']) {
  158. ds_json_encode(10001, lang('cannot_buy_self_goods'), '', request()->action());
  159. }
  160. if (intval($goods_info['goods_storage']) < 1 || intval($goods_info['goods_storage']) < $quantity) {
  161. ds_json_encode(10001, lang('cart_add_stock_shortage'), '', request()->action());
  162. }
  163. } else {
  164. //优惠套装加入购物车(单套)
  165. $pbundling_model = model('pbundling');
  166. $bl_info = $pbundling_model->getBundlingInfo(array('bl_id' => $bl_id));
  167. if (empty($bl_info) || $bl_info['bl_state'] == '0') {
  168. ds_json_encode(10001, lang('recommendations_buy_separately'), '', request()->action());
  169. }
  170. //检查每个商品是否符合条件,并重新计算套装总价
  171. $bl_goods_list = $pbundling_model->getBundlingGoodsList(array('bl_id' => $bl_id));
  172. $goods_id_array = array();
  173. $bl_amount = 0;
  174. foreach ($bl_goods_list as $goods) {
  175. $goods_id_array[] = $goods['goods_id'];
  176. $bl_amount += $goods['blgoods_price'];
  177. }
  178. $goods_model = model('goods');
  179. $goods_list = $goods_model->getGoodsOnlineListAndPromotionByIdArray($goods_id_array);
  180. foreach ($goods_list as $goods_info) {
  181. if (isset($this->member_info) && $goods_info['store_id'] == $this->member_info['store_id']) {
  182. ds_json_encode(10001, lang('cannot_buy_self_goods'), '', request()->action());
  183. }
  184. if (intval($goods_info['goods_storage']) < 1) {
  185. ds_json_encode(10001, lang('cart_add_stock_shortage'), '', request()->action());
  186. }
  187. }
  188. //优惠套装作为一条记录插入购物车,图片取套装内的第一个商品图
  189. $goods_info = array();
  190. $goods_info['store_id'] = $bl_info['store_id'];
  191. $goods_info['goods_id'] = $goods_list[0]['goods_id'];
  192. $goods_info['goods_storage'] = $goods_list[0]['goods_storage'];
  193. $goods_info['goods_name'] = $bl_info['bl_name'];
  194. $goods_info['goods_price'] = $bl_amount;
  195. $goods_info['goods_num'] = 1;
  196. $goods_info['goods_image'] = $goods_list[0]['goods_image'];
  197. $goods_info['store_name'] = $bl_info['store_name'];
  198. $goods_info['bl_id'] = $bl_id;
  199. $quantity = 1;
  200. }
  201. $param = array();
  202. $param['buyer_id'] = isset($this->member_info) ? $this->member_info['member_id'] : 0;
  203. $param['store_id'] = $goods_info['store_id'];
  204. $param['goods_id'] = $goods_info['goods_id'];
  205. $param['goods_name'] = $goods_info['goods_name'];
  206. $param['goods_price'] = $goods_info['goods_price'];
  207. $param['goods_image'] = $goods_info['goods_image'];
  208. $param['store_name'] = $goods_info['store_name'];
  209. $param['goods_storage'] = $goods_info['goods_storage'];
  210. $param['bl_id'] = $bl_id;
  211. $result = $cart_model->addCart($param, 'db', $quantity);
  212. if ($result) {
  213. ds_json_encode(10000, lang('ds_common_op_succ'), $result, request()->action());
  214. } else {
  215. ds_json_encode(10001, $cart_model->error_message, '', request()->action());
  216. }
  217. }
  218. /**
  219. * @api {POST} api/membercart/cart_del 购物车删除
  220. * @apiVersion 1.0.0
  221. * @apiGroup MemberCart
  222. *
  223. * @apiHeader {String} X-DS-KEY 用户授权token
  224. *
  225. * @apiParam {String} cart_id 购物车主键ID 例3,5,8
  226. *
  227. * @apiSuccess {String} code 返回码,10000为成功
  228. * @apiSuccess {String} message 返回消息
  229. */
  230. public function cart_del() {
  231. $cart_id = input('param.cart_id');
  232. $cart_id_array = ds_delete_param($cart_id);
  233. if ($cart_id_array == FALSE) {
  234. ds_json_encode('10001', lang('param_error'), '', request()->action());
  235. }
  236. $cart_model = model('cart');
  237. if ($cart_id > 0) {
  238. $condition = array();
  239. $condition[] = array('buyer_id', '=', $this->member_info['member_id']);
  240. $condition[] = array('cart_id', 'in', $cart_id_array);
  241. $cart_model->delCart('db', $condition,$this->member_info['member_id']);
  242. }
  243. ds_json_encode(10000, '', 1, request()->action());
  244. }
  245. /**
  246. * @api {POST} api/membercart/cart_edit_quantity 更新购物车购买数量
  247. * @apiVersion 1.0.0
  248. * @apiGroup MemberCart
  249. *
  250. * @apiHeader {String} X-DS-KEY 用户授权token
  251. *
  252. * @apiParam {Int} cart_id 购物车主键ID
  253. * @apiParam {Int} quantity 修改数量
  254. *
  255. * @apiSuccess {String} code 返回码,10000为成功
  256. * @apiSuccess {String} message 返回消息
  257. * @apiSuccess {Object} result 返回数据
  258. * @apiSuccess {Object} result.quantity 数量
  259. * @apiSuccess {Object} result.goods_price 单价
  260. * @apiSuccess {Object} result.total_price 总价
  261. *
  262. */
  263. public function cart_edit_quantity() {
  264. $cart_id = intval(abs(input('post.cart_id')));
  265. $quantity = intval(abs(input('post.quantity')));
  266. if (empty($cart_id) || empty($quantity)) {
  267. ds_json_encode(10001, lang('param_error'), '', request()->action());
  268. }
  269. $cart_model = model('cart');
  270. $cart_info = $cart_model->getCartInfo(array('cart_id' => $cart_id, 'buyer_id' => $this->member_info['member_id']));
  271. //检查是否为本人购物车
  272. if ($cart_info['buyer_id'] != $this->member_info['member_id']) {
  273. ds_json_encode(10001, lang('param_error'), '', request()->action());
  274. }
  275. //检查库存是否充足
  276. if (!$this->_check_goods_storage($cart_info, $quantity, $this->member_info['member_id'])) {
  277. ds_json_encode(10001, lang('goods_quantity_limit'), '', request()->action());
  278. }
  279. $data = array();
  280. $data['goods_num'] = $quantity;
  281. $data['goods_price'] = $cart_info['goods_price'];
  282. $condition = array();
  283. $condition[] = array('cart_id','=',$cart_id);
  284. $condition[] = array('buyer_id','=',$cart_info['buyer_id']);
  285. $update = $cart_model->editCart($data, $condition,$cart_info['buyer_id']);
  286. if ($update) {
  287. $return = array();
  288. $return['quantity'] = $quantity;
  289. $return['goods_price'] = ds_price_format($cart_info['goods_price']);
  290. $return['total_price'] = ds_price_format($cart_info['goods_price'] * $quantity);
  291. ds_json_encode(10000, '', $return, request()->action());
  292. } else {
  293. ds_json_encode(10001, lang('ds_common_op_fail'), '', request()->action());
  294. }
  295. }
  296. /**
  297. * 检查库存是否充足
  298. */
  299. private function _check_goods_storage(& $cart_info, $quantity, $member_id) {
  300. $goods_model = model('goods');
  301. $pbundling_model = model('pbundling');
  302. $logic_buy_1 = model('buy_1', 'logic');
  303. if ($cart_info['bl_id'] == '0') {
  304. //普通商品
  305. $goods_info = $goods_model->getGoodsOnlineInfoAndPromotionById($cart_info['goods_id']);
  306. //抢购
  307. $logic_buy_1->getGroupbuyInfo($goods_info, $quantity);
  308. if (isset($goods_info['ifgroupbuy'])) {
  309. if ($goods_info['upper_limit'] && $quantity > $goods_info['upper_limit']) {
  310. return false;
  311. }
  312. }
  313. //批发
  314. $logic_buy_1->getWholesaleInfo($goods_info, $quantity);
  315. //秒杀
  316. $logic_buy_1->getXianshiInfo($goods_info, $quantity);
  317. if (intval($goods_info['goods_storage']) < $quantity) {
  318. return false;
  319. }
  320. //会员等级折扣
  321. $logic_buy_1->getMgdiscountInfo($goods_info);
  322. $goods_info['cart_id'] = $cart_info['cart_id'];
  323. $goods_info['buyer_id'] = $cart_info['buyer_id'];
  324. $cart_info = $goods_info;
  325. } else {
  326. //优惠套装商品
  327. $bl_goods_list = $pbundling_model->getBundlingGoodsList(array('bl_id' => $cart_info['bl_id']));
  328. $goods_id_array = array();
  329. foreach ($bl_goods_list as $goods) {
  330. $goods_id_array[] = $goods['goods_id'];
  331. }
  332. $bl_goods_list = $goods_model->getGoodsOnlineListAndPromotionByIdArray($goods_id_array);
  333. //如果有商品库存不足,更新购买数量到目前最大库存
  334. foreach ($bl_goods_list as $goods_info) {
  335. if (intval($goods_info['goods_storage']) < $quantity) {
  336. return false;
  337. }
  338. }
  339. }
  340. return true;
  341. }
  342. /**
  343. * @api {POST} api/Membercart/cart_count 检查购物车数量
  344. * @apiVersion 1.0.0
  345. * @apiGroup Membercart
  346. *
  347. * @apiHeader {String} X-DS-KEY 用户授权token
  348. *
  349. * @apiSuccess {String} code 返回码,10000为成功
  350. * @apiSuccess {String} message 返回消息
  351. * @apiSuccess {Object} result 返回数据
  352. * @apiSuccess {Int} result.cart_count 购物车数量
  353. */
  354. public function cart_count() {
  355. $cart_model = model('cart');
  356. $count = $cart_model->getCartCountByMemberId($this->member_info['member_id']);
  357. $data['cart_count'] = $count;
  358. ds_json_encode(10000, '', $data, request()->action());
  359. }
  360. }
  361. ?>