Cart.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types = 1);
  12. namespace app\api\controller;
  13. use app\api\model\ActivityDiscount;
  14. use app\api\model\Cart as CartModel;
  15. use app\api\model\GoodsSku as GoodsSkuModel;
  16. use app\api\model\Goods as GoodsModel;
  17. use app\api\model\UserCoupon as UserCouponModel;
  18. use \app\api\model\card\UserRiceCard as UserRiceCardModel;
  19. use app\api\service\Cart as CartService;
  20. use app\api\service\User as UserService;
  21. use app\common\exception\BaseException;
  22. use app\common\library\helper;
  23. use app\common\enum\goods\Status as GoodsStatusEnum;
  24. use think\facade\Cache;
  25. /**
  26. * 购物车管理
  27. * Class Cart
  28. * @package app\api\controller
  29. */
  30. class Cart extends Controller
  31. {
  32. /**
  33. * 购物车商品列表
  34. * @return array|\think\response\Json
  35. * @throws BaseException
  36. * @throws \think\db\exception\DataNotFoundException
  37. * @throws \think\db\exception\DbException
  38. * @throws \think\db\exception\ModelNotFoundException
  39. */
  40. public function list()
  41. {
  42. // 购物车商品列表
  43. $service = new CartService;
  44. $list = $service->getList();
  45. $sum_total_price = 0;
  46. $activity_discount_total_price = 0;
  47. // 整理购物车商品列表
  48. $orderGoodsIds = [];
  49. $goodList = [];
  50. foreach ($list as $item) {
  51. $item['err_status'] = 0;
  52. // if(empty($item['goods']['skuInfo'])){
  53. // $item['err_status'] = 3;
  54. // $item['selected'] = 0;
  55. // }else{
  56. if($item['goods_num']>$item['goods']['skuInfo']['stock_num']|| $item['goods_num']==0){ //库存不足
  57. $item['err_status'] = 1;
  58. $item['selected'] = 0;
  59. }
  60. // }
  61. if($item['goods']['status']==GoodsStatusEnum::OFF_SALE){ //下架商品
  62. $item['err_status'] = 2;
  63. $item['selected'] = 0;
  64. }
  65. if($item['selected'] && $item['err_status']==0){//用户选中的商品勾选后的总金额
  66. $item['total_price'] = $item['goods']['skuInfo']['goods_price']*$item['goods_num'];
  67. (new GoodsModel)->getActivityDiscountDetail($item);
  68. $goodList[] = $item;
  69. $sum_total_price += $item['total_price'];
  70. $activity_discount_total_price += $item['activity_discount_total_price'];
  71. $orderGoodsIds[] = $item['goods_id'];
  72. }
  73. }
  74. if(count($list)){
  75. $list = array_values($list->toArray());
  76. }
  77. // 购物车商品类型数量
  78. $cartTotal = (new CartModel)->getCartGoodsTotal();
  79. $sum_total_price = helper::bcadd($sum_total_price,0,2);
  80. $activity_discount_total_price = helper::bcadd($activity_discount_total_price,0,2);
  81. // 当前用户ID
  82. $user = UserService::getCurrentLoginUser(true);
  83. $user_coupon = [];
  84. $total_price = $sum_total_price-$activity_discount_total_price;
  85. // dd($orderGoodsIds);
  86. if($total_price>0){
  87. // 当前用户可用的优惠券列表(筛选了除外商品)
  88. $user_coupon = UserCouponModel::getUserCouponList($user['user_id'], $goodList, $orderGoodsIds);
  89. $user_coupon = array_values($user_coupon);
  90. }
  91. // //我的可用米卡列表
  92. // $user_rice_card = UserRiceCardModel::getUserRiceCardList($user['user_id']);
  93. return $this->renderSuccess(compact('cartTotal','sum_total_price', 'activity_discount_total_price', 'list', 'user_coupon'));
  94. }
  95. /**
  96. * 购物车商品总数量
  97. * @return array|\think\response\Json
  98. * @throws BaseException
  99. */
  100. public function total() {
  101. $model = new CartModel;
  102. $cartTotal = $model->getCartGoodsTotal();
  103. return $this->renderSuccess(compact('cartTotal'));
  104. }
  105. /**
  106. * 加入购物车
  107. * @param int $goodsId 商品id
  108. * @param string $goodsSkuId 商品sku索引
  109. * @param int $goodsNum 商品数量
  110. * @param int $staffUserId 链接分享者ID
  111. * @return array|\think\response\Json
  112. * @throws BaseException
  113. * @throws \think\db\exception\DataNotFoundException
  114. * @throws \think\db\exception\DbException
  115. * @throws \think\db\exception\ModelNotFoundException
  116. */
  117. public function add(int $goodsId, string $goodsSkuId, int $goodsNum, int $staffUserId = 0)
  118. {
  119. if(!is_numeric($goodsSkuId)){
  120. //自动选择库存足够的商品
  121. $goodsSkuId = GoodsSkuModel::where([['goods_id','=',$goodsId],['stock_num','>=',$goodsNum]])->order('id','asc')->value('goods_sku_id')??'';
  122. if(!is_numeric($goodsSkuId)){
  123. return $this->renderError( '商品不存在');
  124. }
  125. }
  126. $model = new CartModel;
  127. if (!$model->add($goodsId, $goodsSkuId, $goodsNum, $staffUserId)) {
  128. return $this->renderError($model->getError() ?: '加入购物车失败');
  129. }
  130. // 购物车商品总数量
  131. $cartTotal = $model->getCartGoodsTotal();
  132. return $this->renderSuccess(compact('cartTotal'), '加入购物车成功');
  133. }
  134. /**
  135. * 更新购物车商品数量
  136. * @param int $goodsId 商品id
  137. * @param string $goodsSkuId 商品sku索引
  138. * @param int $goodsNum 商品数量
  139. * @return array|\think\response\Json
  140. * @throws BaseException
  141. * @throws \think\db\exception\DataNotFoundException
  142. * @throws \think\db\exception\DbException
  143. * @throws \think\db\exception\ModelNotFoundException
  144. */
  145. public function update(int $goodsId, string $goodsSkuId, int $goodsNum)
  146. {
  147. $model = new CartModel;
  148. if (!$model->sUpdate($goodsId, $goodsSkuId, $goodsNum)) {
  149. return $this->renderError($model->getError() ?: '更新失败');
  150. }
  151. // 购物车商品总数量
  152. $cartTotal = $model->getCartGoodsTotal();
  153. return $this->renderSuccess(compact('cartTotal'), '更新成功');
  154. }
  155. /**
  156. * 删除购物车中指定记录
  157. * @param array $cartIds 购物车ID集, 如果为空删除所有
  158. * @return array|\think\response\Json
  159. * @throws BaseException
  160. */
  161. public function delete(array $cartIds = [])
  162. {
  163. $model = new CartModel;
  164. if (!$model->clear($cartIds)) {
  165. return $this->renderError($model->getError() ?: '操作失败');
  166. }
  167. // 购物车商品总数量
  168. $cartTotal = $model->getCartGoodsTotal();
  169. return $this->renderSuccess(compact('cartTotal'), '操作成功');
  170. }
  171. /**
  172. * 勾选购物车中指定记录
  173. * @param array $cartIds 购物车ID集, 如果为空删除取消全部勾选
  174. * @param int $selected 选中状态 0未选中 1选中
  175. * @return array|\think\response\Json
  176. * @throws BaseException
  177. */
  178. public function goodsSelect(int $selected, array $cartIds = []){
  179. $model = new CartModel;
  180. if (!$model->updateSelected($cartIds,$selected)) {
  181. return $this->renderError($model->getError() ?: '操作失败');
  182. }
  183. return $this->renderSuccess();
  184. }
  185. /**
  186. * 更改购物车商品规格
  187. * @param int $cartId
  188. * @param int $goodsId
  189. * @param string $goodsSkuId
  190. * @return array
  191. * @throws BaseException
  192. * @author: zjwhust
  193. * @Time: 2021/9/26 17:50
  194. */
  195. public function updSku(int $cartId, int $goodsId, string $goodsSkuId){
  196. $model = new CartModel;
  197. if (!$model->updSku($cartId, $goodsId, $goodsSkuId)) {
  198. return $this->renderError($model->getError() ?: '操作失败');
  199. }
  200. return $this->renderSuccess();
  201. }
  202. }