Membercart.php 18 KB

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