Cart.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <?php
  2. namespace app\api\model\order;
  3. use think\facade\Cache;
  4. use app\api\model\product\Product as ProductModel;
  5. use app\common\library\helper;
  6. /**
  7. * 购物车管理
  8. */
  9. class Cart
  10. {
  11. // 错误信息
  12. public $error = '';
  13. //用户
  14. private $user;
  15. // 用户id
  16. private $user_id;
  17. // 购物车列表
  18. private static $cart = [];
  19. // $clear 是否清空购物车
  20. private $clear = false;
  21. /**
  22. * 构造方法
  23. */
  24. public function __construct($user)
  25. {
  26. $this->user = $user;
  27. $this->user_id = $user['user_id'];
  28. static::$cart = Cache::get('cart_' . $this->user_id) ?: [];
  29. }
  30. /**
  31. * 购物车列表 (含商品信息)
  32. */
  33. public function getList($cartIds = null)
  34. {
  35. // 获取购物车商品列表
  36. return $this->getOrderProductList($cartIds);
  37. }
  38. /**
  39. * 获取购物车列表
  40. */
  41. public function getCartList($cartIds = null)
  42. {
  43. if (empty($cartIds)) return static::$cart;
  44. $cartList = [];
  45. $indexArr = (strpos($cartIds, ',') !== false) ? explode(',', $cartIds) : [$cartIds];
  46. foreach ($indexArr as $index) {
  47. isset(static::$cart[$index]) && $cartList[$index] = static::$cart[$index];
  48. }
  49. return $cartList;
  50. }
  51. /**
  52. * 获取购物车中的商品列表
  53. */
  54. public function getOrderProductList($cartIds)
  55. {
  56. // 购物车商品列表
  57. $productList = [];
  58. // 获取购物车列表
  59. $cartList = $this->getCartList($cartIds);
  60. if (empty($cartList)) {
  61. $this->setError('当前购物车没有商品');
  62. return $productList;
  63. }
  64. // 购物车中所有商品id集
  65. $productIds = array_unique(helper::getArrayColumn($cartList, 'product_id'));
  66. // 获取并格式化商品数据
  67. $sourceData = (new ProductModel)->getListByIds($productIds);
  68. $sourceData = helper::arrayColumn2Key($sourceData, 'product_id');
  69. // p($cartList);
  70. // 格式化购物车数据列表
  71. foreach ($cartList as $key => $item) {
  72. // 判断商品不存在则自动删除
  73. if (!isset($sourceData[$item['product_id']])) {
  74. $this->delete($key);
  75. continue;
  76. }
  77. // 商品信息
  78. $product = clone $sourceData[$item['product_id']];
  79. // 判断商品是否已删除
  80. if ($product['is_delete']) {
  81. $this->delete($key);
  82. continue;
  83. }
  84. // 商品sku信息
  85. $product['product_sku'] = ProductModel::getProductSku($product, $item['product_sku_id']);
  86. $product['product_sku_id'] = $item['product_sku_id'];
  87. $product['spec_sku_id'] = $product['product_sku']['spec_sku_id'];
  88. // 商品pv
  89. $product['pv'] = $product['product_sku']['pv'];
  90. // 商品总pv
  91. $product['total_pv'] = bcmul($product['pv'], $item['product_num']);
  92. // 商品sku不存在则自动删除
  93. if (empty($product['product_sku'])) {
  94. $this->delete($key);
  95. continue;
  96. }
  97. // 商品单价
  98. $product['product_price'] = $product['product_sku']['product_price'];
  99. // 购买数量
  100. $product['total_num'] = $item['product_num'];
  101. // 商品总价
  102. $product['total_price'] = bcmul($product['product_price'], $item['product_num'], 2);
  103. $productList[] = $product->hidden(['category', 'content', 'image']);
  104. }
  105. return $productList;
  106. }
  107. /**
  108. * 加入购物车
  109. */
  110. public function add($productId, $productNum, $productSkuId)
  111. {
  112. // 购物车商品索引
  113. $index = "{$productId}_{$productSkuId}";
  114. // 加入购物车后的商品数量
  115. $cartProductNum = $productNum + (isset(static::$cart[$index]) ? static::$cart[$index]['product_num'] : 0);
  116. // 获取商品信息
  117. $product = ProductModel::detail($productId);
  118. // 验证商品能否加入
  119. if (!$this->checkProduct($product, $productSkuId, $cartProductNum)) {
  120. return false;
  121. }
  122. // 记录到购物车列表
  123. static::$cart[$index] = [
  124. 'product_id' => $productId,
  125. 'product_num' => $cartProductNum,
  126. 'product_sku_id' => $productSkuId,
  127. 'create_time' => time()
  128. ];
  129. return true;
  130. }
  131. /**
  132. * 验证商品是否可以购买
  133. */
  134. private function checkProduct($product, $productSkuId, $cartProductNum)
  135. {
  136. // 判断商品是否下架
  137. if (!$product || $product['is_delete'] || $product['product_status']['value'] != 10) {
  138. $this->setError('很抱歉,商品信息不存在或已下架');
  139. return false;
  140. }
  141. // 商品sku信息
  142. $product['product_sku'] = ProductModel::getProductSku($product, $productSkuId);
  143. // 判断商品库存
  144. if ($cartProductNum > $product['product_sku']['stock_num']) {
  145. $this->setError('很抱歉,商品库存不足');
  146. return false;
  147. }
  148. return true;
  149. }
  150. /**
  151. * 减少购物车中某商品数量
  152. */
  153. public function sub($productId, $productSkuId)
  154. {
  155. $index = "{$productId}_{$productSkuId}";
  156. static::$cart[$index]['product_num'] > 1 && static::$cart[$index]['product_num']--;
  157. }
  158. /**
  159. * 删除购物车中指定商品
  160. * @param string $cartIds (支持字符串ID集)
  161. */
  162. public function delete($cartIds)
  163. {
  164. $indexArr = array_filter(trim(strpos($cartIds, ','), ',') !== false ? explode(',', $cartIds) : [$cartIds]);
  165. foreach ($indexArr as $index) {
  166. if (isset(static::$cart[$index])) unset(static::$cart[$index]);
  167. }
  168. }
  169. /**
  170. * 获取当前用户购物车商品总数量(含件数)
  171. */
  172. public function getTotalNum()
  173. {
  174. return helper::getArrayColumnSum(static::$cart, 'product_num');
  175. }
  176. /**
  177. * 获取当前用户购物车商品总数量(不含件数)
  178. */
  179. public function getProductNum()
  180. {
  181. return count(static::$cart);
  182. }
  183. /**
  184. * 析构方法
  185. * 将cart数据保存到缓存文件
  186. */
  187. public function __destruct()
  188. {
  189. $this->clear !== true && Cache::set('cart_' . $this->user_id, static::$cart, 86400 * 15);
  190. }
  191. /**
  192. * 清空当前用户购物车
  193. */
  194. public function clearAll($cartIds = null)
  195. {
  196. if (empty($cartIds)) {
  197. $this->clear = true;
  198. Cache::delete('cart_' . $this->user_id);
  199. } else {
  200. $this->delete($cartIds);
  201. }
  202. }
  203. /**
  204. * 设置错误信息
  205. */
  206. private function setError($error)
  207. {
  208. empty($this->error) && $this->error = $error;
  209. }
  210. /**
  211. * 获取错误信息
  212. */
  213. public function getError()
  214. {
  215. return $this->error;
  216. }
  217. }