Membercart.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. <?php
  2. namespace app\api\controller;
  3. use think\facade\Lang;
  4. /**
  5. *
  6. *
  7. * ----------------------------------------------------------------------------
  8. *
  9. * 购物车控制器
  10. */
  11. class Membercart extends MobileMember
  12. {
  13. public function initialize()
  14. {
  15. parent::initialize();
  16. Lang::load(base_path() . 'home/lang/' . config('lang.default_lang') . '/cart.lang.php');
  17. }
  18. /**
  19. * @api {POST} api/Membercart/cart_list 购物车列表
  20. * @apiVersion 1.0.0
  21. * @apiGroup Membercart
  22. *
  23. * @apiHeader {String} X-DS-KEY 用户授权token
  24. *
  25. * @apiSuccess {String} code 返回码,10000为成功
  26. * @apiSuccess {String} message 返回消息
  27. * @apiSuccess {Object} result 返回数据
  28. * @apiSuccess {Int} result.cart_count 物品种类数量
  29. * @apiSuccess {Object} result.cart_list 购物车列表,键为店铺ID
  30. * @apiSuccess {Int} result.cart_list.store_id 店铺ID
  31. * @apiSuccess {String} result.cart_list.store_name 店铺名称
  32. * @apiSuccess {String} result.cart_list.store_name 店铺优惠券
  33. * @apiSuccess {String} result.cart_list.goods 商品列表
  34. * @apiSuccess {Int} result.cart_list.goods.bl_id 优惠套餐ID
  35. * @apiSuccess {Int} result.cart_list.goods.buyer_id 买家ID
  36. * @apiSuccess {Int} result.cart_list.goods.cart_id 购物车ID
  37. * @apiSuccess {Int} result.cart_list.goods.gc_id 分类ID
  38. * @apiSuccess {Object} result.cart_list.goods.gift_list 赠品列表
  39. * @apiSuccess {Int} result.cart_list.goods.goods_commonid 商品公共ID
  40. * @apiSuccess {Float} result.cart_list.goods.goods_freight 运费
  41. * @apiSuccess {Int} result.cart_list.goods.goods_id 商品ID
  42. * @apiSuccess {String} result.cart_list.goods.goods_image 商品图片名称
  43. * @apiSuccess {String} result.cart_list.goods.goods_image_url 商品图片完整路径
  44. * @apiSuccess {String} result.cart_list.goods.goods_name 商品名称
  45. * @apiSuccess {Int} result.cart_list.goods.goods_num 购买数量
  46. * @apiSuccess {Float} result.cart_list.goods.goods_price 商品价格
  47. * @apiSuccess {Int} result.cart_list.goods.goods_storage 商品库存
  48. * @apiSuccess {Int} result.cart_list.goods.goods_storage_alarm 预警库存
  49. * @apiSuccess {Int} result.cart_list.goods.goods_vat 是否支持发票 0否1是
  50. * @apiSuccess {Object} result.cart_list.goods.groupbuy_info 抢购信息
  51. * @apiSuccess {Int} result.cart_list.goods.is_goodsfcode 是否F码商品 0否1是
  52. * @apiSuccess {Int} result.cart_list.goods.is_have_gift 是否含赠品 0否1是
  53. * @apiSuccess {Object} result.cart_list.goods.mgdiscount_info 会员折扣信息
  54. * @apiSuccess {Boolean} result.cart_list.goods.state 商品状态 true上架false下架
  55. * @apiSuccess {Boolean} result.cart_list.goods.storage_state 商品库存状态 true充足false不足
  56. * @apiSuccess {Int} result.cart_list.goods.store_id 店铺ID
  57. * @apiSuccess {String} result.cart_list.goods.store_name 店铺名称
  58. * @apiSuccess {Int} result.cart_list.goods.transport_id 售卖区域id
  59. * @apiSuccess {Object} result.cart_list.goods.xianshi_info 秒杀信息
  60. * @apiSuccess {Int} result.cart_list.store_id 店铺ID
  61. * @apiSuccess {String} result.cart_list.store_name 店铺名称
  62. * @apiSuccess {Object[]} result.cart_val 去除result.cart_list键的数组
  63. * @apiSuccess {Float} result.sum 总价
  64. */
  65. public function cart_list()
  66. {
  67. $cart_model = model('cart');
  68. $condition = array();
  69. $condition[] = array('buyer_id', '=', $this->member_info['member_id']);
  70. $cart_list = $cart_model->getCartList('db', $condition);
  71. // 购物车列表 [得到最新商品属性及促销信息]
  72. $cart_list = model('buy_1', 'logic')->getGoodsCartList($cart_list);
  73. $goods_model = model('goods');
  74. $sum = 0;
  75. $cart_a = array();
  76. $k = 0;
  77. $voucher_model = model('voucher');
  78. foreach ($cart_list as $key => $val) {
  79. $cart_a[$val['store_id']]['store_id'] = $val['store_id'];
  80. $cart_a[$val['store_id']]['store_name'] = $val['store_name'];
  81. //获取店铺代金券
  82. $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)));
  83. // $goods_data = $goods_model->getGoodsOnlineInfoForShare($val['goods_id']);
  84. $cart_a[$val['store_id']]['goods'][$key] = $val;
  85. $cart_a[$val['store_id']]['goods'][$key]['cart_id'] = $val['cart_id'];
  86. $cart_a[$val['store_id']]['goods'][$key]['goods_num'] = $val['goods_num'];
  87. $cart_a[$val['store_id']]['goods'][$key]['goods_image_url'] = goods_cthumb($val['goods_image'], $val['store_id']);
  88. $cart_a[$val['store_id']]['goods'][$key]['goods_state'] = $val['state'];
  89. // if (isset($goods_data['goods_promotion_type'])) {
  90. // $cart_a[$val['store_id']]['goods'][$key]['goods_price'] = $goods_data['goods_promotion_price'];
  91. // }
  92. $cart_a[$val['store_id']]['goods'][$key]['gift_list'] = isset($val['gift_list']) ? $val['gift_list'] : '';
  93. if ($val['bl_id']) {
  94. foreach ($cart_a[$val['store_id']]['goods'][$key]['bl_goods_list'] as $k => $v) {
  95. $cart_a[$val['store_id']]['goods'][$key]['bl_goods_list'][$k]['goods_image_url'] = goods_cthumb($v['goods_image'], $val['store_id']);
  96. }
  97. }
  98. if ($val['state']) {
  99. $cart_list[$key]['goods_sum'] = ds_price_format($val['goods_price'] * $val['goods_num']);
  100. $sum += $cart_list[$key]['goods_sum'];
  101. $k++;
  102. }
  103. }
  104. foreach ($cart_a as $key => $value) {
  105. $value['goods'] = array_values($value['goods']);
  106. $cart_l[] = $value;
  107. }
  108. if (isset($cart_l)) {
  109. $cart_b = array_values($cart_l);
  110. } else {
  111. $cart_b = array();
  112. }
  113. 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());
  114. }
  115. /**
  116. * @api {POST} api/membercart/cart_add 购物车添加
  117. * @apiVersion 1.0.0
  118. * @apiGroup MemberCart
  119. *
  120. * @apiHeader {String} X-DS-KEY 用户授权token
  121. *
  122. * @apiParam {Int} goods_id 商品ID
  123. * @apiParam {Int} quantity 购买数量
  124. * @apiParam {Int} bl_id 组合购买ID
  125. *
  126. * @apiSuccess {String} code 返回码,10000为成功
  127. * @apiSuccess {String} message 返回消息
  128. * @apiSuccess {Object} result 返回数据(购物车主键ID)
  129. */
  130. public function cart_add()
  131. {
  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. {
  232. $cart_id = input('param.cart_id');
  233. $cart_id_array = ds_delete_param($cart_id);
  234. if ($cart_id_array == FALSE) {
  235. ds_json_encode('10001', lang('param_error'), '', request()->action());
  236. }
  237. $cart_model = model('cart');
  238. if ($cart_id > 0) {
  239. $condition = array();
  240. $condition[] = array('buyer_id', '=', $this->member_info['member_id']);
  241. $condition[] = array('cart_id', 'in', $cart_id_array);
  242. $cart_model->delCart('db', $condition, $this->member_info['member_id']);
  243. }
  244. ds_json_encode(10000, '', 1, request()->action());
  245. }
  246. /**
  247. * @api {POST} api/membercart/cart_edit_quantity 更新购物车购买数量
  248. * @apiVersion 1.0.0
  249. * @apiGroup MemberCart
  250. *
  251. * @apiHeader {String} X-DS-KEY 用户授权token
  252. *
  253. * @apiParam {Int} cart_id 购物车主键ID
  254. * @apiParam {Int} quantity 修改数量
  255. *
  256. * @apiSuccess {String} code 返回码,10000为成功
  257. * @apiSuccess {String} message 返回消息
  258. * @apiSuccess {Object} result 返回数据
  259. * @apiSuccess {Object} result.quantity 数量
  260. * @apiSuccess {Object} result.goods_price 单价
  261. * @apiSuccess {Object} result.total_price 总价
  262. *
  263. */
  264. public function cart_edit_quantity()
  265. {
  266. $cart_id = intval(abs(input('post.cart_id')));
  267. $quantity = intval(abs(input('post.quantity')));
  268. if (empty($cart_id) || empty($quantity)) {
  269. ds_json_encode(10001, lang('param_error'), '', request()->action());
  270. }
  271. $cart_model = model('cart');
  272. $cart_info = $cart_model->getCartInfo(array('cart_id' => $cart_id, 'buyer_id' => $this->member_info['member_id']));
  273. //检查是否为本人购物车
  274. if ($cart_info['buyer_id'] != $this->member_info['member_id']) {
  275. ds_json_encode(10001, lang('param_error'), '', request()->action());
  276. }
  277. //检查库存是否充足
  278. if (!$this->_check_goods_storage($cart_info, $quantity, $this->member_info['member_id'])) {
  279. ds_json_encode(10001, lang('goods_quantity_limit'), '', request()->action());
  280. }
  281. $data = array();
  282. $data['goods_num'] = $quantity;
  283. $data['goods_price'] = $cart_info['goods_price'];
  284. $condition = array();
  285. $condition[] = array('cart_id', '=', $cart_id);
  286. $condition[] = array('buyer_id', '=', $cart_info['buyer_id']);
  287. $update = $cart_model->editCart($data, $condition, $cart_info['buyer_id']);
  288. if ($update) {
  289. $return = array();
  290. $return['quantity'] = $quantity;
  291. $return['goods_price'] = ds_price_format($cart_info['goods_price']);
  292. $return['total_price'] = ds_price_format($cart_info['goods_price'] * $quantity);
  293. ds_json_encode(10000, '', $return, request()->action());
  294. } else {
  295. ds_json_encode(10001, lang('ds_common_op_fail'), '', request()->action());
  296. }
  297. }
  298. /**
  299. * 检查库存是否充足
  300. */
  301. private function _check_goods_storage(&$cart_info, $quantity, $member_id)
  302. {
  303. $goods_model = model('goods');
  304. $pbundling_model = model('pbundling');
  305. $logic_buy_1 = model('buy_1', 'logic');
  306. if ($cart_info['bl_id'] == '0') {
  307. //普通商品
  308. $goods_info = $goods_model->getGoodsOnlineInfoAndPromotionById($cart_info['goods_id']);
  309. //抢购
  310. $logic_buy_1->getGroupbuyInfo($goods_info, $quantity);
  311. if (isset($goods_info['ifgroupbuy'])) {
  312. if ($goods_info['upper_limit'] && $quantity > $goods_info['upper_limit']) {
  313. return false;
  314. }
  315. }
  316. //批发
  317. $logic_buy_1->getWholesaleInfo($goods_info, $quantity);
  318. //秒杀
  319. $logic_buy_1->getXianshiInfo($goods_info, $quantity);
  320. if (intval($goods_info['goods_storage']) < $quantity) {
  321. return false;
  322. }
  323. //会员等级折扣
  324. $logic_buy_1->getMgdiscountInfo($goods_info);
  325. $goods_info['cart_id'] = $cart_info['cart_id'];
  326. $goods_info['buyer_id'] = $cart_info['buyer_id'];
  327. $cart_info = $goods_info;
  328. } else {
  329. //优惠套装商品
  330. $bl_goods_list = $pbundling_model->getBundlingGoodsList(array('bl_id' => $cart_info['bl_id']));
  331. $goods_id_array = array();
  332. foreach ($bl_goods_list as $goods) {
  333. $goods_id_array[] = $goods['goods_id'];
  334. }
  335. $bl_goods_list = $goods_model->getGoodsOnlineListAndPromotionByIdArray($goods_id_array);
  336. //如果有商品库存不足,更新购买数量到目前最大库存
  337. foreach ($bl_goods_list as $goods_info) {
  338. if (intval($goods_info['goods_storage']) < $quantity) {
  339. return false;
  340. }
  341. }
  342. }
  343. return true;
  344. }
  345. /**
  346. * @api {POST} api/Membercart/cart_count 检查购物车数量
  347. * @apiVersion 1.0.0
  348. * @apiGroup Membercart
  349. *
  350. * @apiHeader {String} X-DS-KEY 用户授权token
  351. *
  352. * @apiSuccess {String} code 返回码,10000为成功
  353. * @apiSuccess {String} message 返回消息
  354. * @apiSuccess {Object} result 返回数据
  355. * @apiSuccess {Int} result.cart_count 购物车数量
  356. */
  357. public function cart_count()
  358. {
  359. $cart_model = model('cart');
  360. $count = $cart_model->getCartCountByMemberId($this->member_info['member_id']);
  361. $data['cart_count'] = $count;
  362. ds_json_encode(10000, '', $data, request()->action());
  363. }
  364. }