Cart.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. <?php
  2. namespace app\api\model;
  3. use think\Cache;
  4. use app\api\model\Goods as GoodsModel;
  5. use app\common\library\helper;
  6. use app\common\service\wechat\wow\Shoping as WowService;
  7. /**
  8. * 购物车管理
  9. * Class Cart
  10. * @package app\api\model
  11. */
  12. class Cart
  13. {
  14. /* @var string $error 错误信息 */
  15. public $error = '';
  16. /* @var \think\Model|\think\Collection $user 用户信息 */
  17. private $user;
  18. /* @var int $user_id 用户id */
  19. private $user_id;
  20. /* @var int $wxapp_id 小程序商城id */
  21. private $wxapp_id;
  22. /* @var array $cart 购物车列表 */
  23. private $cart = [];
  24. /* @var bool $clear 是否清空购物车 */
  25. private $clear = false;
  26. /**
  27. * 构造方法
  28. * Cart constructor.
  29. * @param \think\Model|\think\Collection $user
  30. */
  31. public function __construct($user)
  32. {
  33. $this->user = $user;
  34. $this->user_id = $this->user['user_id'];
  35. $this->wxapp_id = $this->user['wxapp_id'];
  36. $this->cart = Cache::get('cart_' . $this->user_id) ?: [];
  37. }
  38. /**
  39. * 购物车列表 (含商品信息)
  40. * @return array
  41. * @param string $cartIds 请求参数
  42. * @throws \think\Exception
  43. * @throws \think\db\exception\DataNotFoundException
  44. * @throws \think\db\exception\ModelNotFoundException
  45. * @throws \think\exception\DbException
  46. */
  47. public function getList($cartIds)
  48. {
  49. // 获取购物车商品列表
  50. return $this->getOrderGoodsList($cartIds);
  51. }
  52. /**
  53. * 获取购物车列表
  54. * @param string|null $cartIds 购物车索引集 (为null时则获取全部)
  55. * @return array
  56. */
  57. public function getCartList($cartIds = null)
  58. {
  59. if (empty($cartIds)) return $this->cart;
  60. $cartList = [];
  61. $indexArr = (strpos($cartIds, ',') !== false) ? explode(',', $cartIds) : [$cartIds];
  62. foreach ($indexArr as $index) {
  63. isset($this->cart[$index]) && $cartList[$index] = $this->cart[$index];
  64. }
  65. return $cartList;
  66. }
  67. /**
  68. * 获取购物车中的商品列表
  69. * @param $cartIds
  70. * @return array|bool
  71. * @throws \think\Exception
  72. * @throws \think\db\exception\DataNotFoundException
  73. * @throws \think\db\exception\ModelNotFoundException
  74. * @throws \think\exception\DbException
  75. */
  76. private function getOrderGoodsList($cartIds)
  77. {
  78. // 购物车商品列表
  79. $goodsList = [];
  80. // 获取购物车列表
  81. $cartList = $this->getCartList($cartIds);
  82. if (empty($cartList)) {
  83. $this->setError('当前购物车没有商品');
  84. return $goodsList;
  85. }
  86. // 购物车中所有商品id集
  87. $goodsIds = array_unique(helper::getArrayColumn($cartList, 'goods_id'));
  88. // 获取并格式化商品数据
  89. $sourceData = (new GoodsModel)->getListByIds($goodsIds);
  90. $sourceData = helper::arrayColumn2Key($sourceData, 'goods_id');
  91. // 格式化购物车数据列表
  92. foreach ($cartList as $key => $item) {
  93. // 判断商品不存在则自动删除
  94. if (!isset($sourceData[$item['goods_id']])) {
  95. $this->delete($key);
  96. continue;
  97. }
  98. /* @var GoodsModel $goods 商品信息 */
  99. $goods = clone $sourceData[$item['goods_id']];
  100. // 判断商品是否已删除
  101. if ($goods['is_delete']) {
  102. $this->delete($key);
  103. continue;
  104. }
  105. // 商品sku信息
  106. $goods['goods_sku'] = GoodsModel::getGoodsSku($goods, $item['goods_sku_id']);
  107. $goods['goods_sku_id'] = $item['goods_sku_id'];
  108. $goods['spec_sku_id'] = $goods['goods_sku']['spec_sku_id'];
  109. // 商品sku不存在则自动删除
  110. if (empty($goods['goods_sku'])) {
  111. $this->delete($key);
  112. continue;
  113. }
  114. // 商品单价
  115. $goods['goods_price'] = $goods['goods_sku']['goods_price'];
  116. // 购买数量
  117. $goods['total_num'] = $item['goods_num'];
  118. // 商品总价
  119. $goods['total_price'] = bcmul($goods['goods_price'], $item['goods_num'], 2);
  120. $goodsList[] = $goods->hidden(['category', 'content', 'image', 'sku']);
  121. }
  122. return $goodsList;
  123. }
  124. /**
  125. * 加入购物车
  126. * @param int $goodsId 商品id
  127. * @param int $goodsNum 加入购物车的数量
  128. * @param string $goodsSkuId 商品sku索引
  129. * @return bool
  130. * @throws \app\common\exception\BaseException
  131. * @throws \think\exception\DbException
  132. */
  133. public function add($goodsId, $goodsNum, $goodsSkuId)
  134. {
  135. // 购物车商品索引
  136. $index = "{$goodsId}_{$goodsSkuId}";
  137. // 加入购物车后的商品数量
  138. $cartGoodsNum = $goodsNum + (isset($this->cart[$index]) ? $this->cart[$index]['goods_num'] : 0);
  139. // 获取商品信息
  140. $goods = GoodsModel::detail($goodsId);
  141. // 验证商品能否加入
  142. if (!$this->checkGoods($goods, $goodsSkuId, $cartGoodsNum)) {
  143. return false;
  144. }
  145. // 将商品同步到好物圈
  146. if (!$this->isExistGoodsId($goodsId)) {
  147. (new WowService($this->wxapp_id))->add($this->user, [$goods]);
  148. }
  149. // 记录到购物车列表
  150. $this->cart[$index] = [
  151. 'goods_id' => $goodsId,
  152. 'goods_num' => $cartGoodsNum,
  153. 'goods_sku_id' => $goodsSkuId,
  154. 'create_time' => time()
  155. ];
  156. return true;
  157. }
  158. /**
  159. * 验证购物车中是否存在某商品
  160. * @param $goodsId
  161. * @return bool
  162. */
  163. private function isExistGoodsId($goodsId)
  164. {
  165. foreach ($this->cart as $item) {
  166. if ($item['goods_id'] == $goodsId) return true;
  167. }
  168. return false;
  169. }
  170. /**
  171. * 验证商品是否可以购买
  172. * @param GoodsModel $goods 商品信息
  173. * @param string $goodsSkuId 商品sku索引
  174. * @param $cartGoodsNum
  175. * @return bool
  176. */
  177. private function checkGoods($goods, $goodsSkuId, $cartGoodsNum)
  178. {
  179. // 判断商品是否下架
  180. if (!$goods || $goods['is_delete'] || $goods['goods_status']['value'] != 10) {
  181. $this->setError('很抱歉,商品信息不存在或已下架');
  182. return false;
  183. }
  184. // 商品sku信息
  185. $goods['goods_sku'] = GoodsModel::getGoodsSku($goods, $goodsSkuId);
  186. // 判断商品库存
  187. if ($cartGoodsNum > $goods['goods_sku']['stock_num']) {
  188. $this->setError('很抱歉,商品库存不足');
  189. return false;
  190. }
  191. return true;
  192. }
  193. /**
  194. * 减少购物车中某商品数量
  195. * @param int $goodsId
  196. * @param string $goodsSkuId
  197. */
  198. public function sub($goodsId, $goodsSkuId)
  199. {
  200. $index = "{$goodsId}_{$goodsSkuId}";
  201. $this->cart[$index]['goods_num'] > 1 && $this->cart[$index]['goods_num']--;
  202. }
  203. /**
  204. * 删除购物车中指定商品
  205. * @param string $cartIds (支持字符串ID集)
  206. */
  207. public function delete($cartIds)
  208. {
  209. $indexArr = strpos($cartIds, ',') !== false ? explode(',', $cartIds) : [$cartIds];
  210. foreach ($indexArr as $index) {
  211. if (isset($this->cart[$index])) unset($this->cart[$index]);
  212. }
  213. }
  214. /**
  215. * 获取当前用户购物车商品总数量(含件数)
  216. * @return int
  217. */
  218. public function getTotalNum()
  219. {
  220. return helper::getArrayColumnSum($this->cart, 'goods_num');
  221. }
  222. /**
  223. * 获取当前用户购物车商品总数量(不含件数)
  224. * @return int
  225. */
  226. public function getGoodsNum()
  227. {
  228. return count($this->cart);
  229. }
  230. /**
  231. * 析构方法
  232. * 将cart数据保存到缓存文件
  233. */
  234. public function __destruct()
  235. {
  236. $this->clear !== true && Cache::set('cart_' . $this->user_id, $this->cart, 86400 * 15);
  237. }
  238. /**
  239. * 清空当前用户购物车
  240. * @param null $cartIds
  241. */
  242. public function clearAll($cartIds = null)
  243. {
  244. if (empty($cartIds)) {
  245. $this->clear = true;
  246. Cache::rm('cart_' . $this->user_id);
  247. } else {
  248. $this->delete($cartIds);
  249. }
  250. }
  251. /**
  252. * 设置错误信息
  253. * @param $error
  254. */
  255. private function setError($error)
  256. {
  257. empty($this->error) && $this->error = $error;
  258. }
  259. /**
  260. * 获取错误信息
  261. * @return string
  262. */
  263. public function getError()
  264. {
  265. return $this->error;
  266. }
  267. }