当前位置:   article > 正文

ThinkPHP5+Redis单例型购物车_thinkphp5.0购物车的

thinkphp5.0购物车的
  1. <?php
  2. /**
  3. * Redis + 单例型购物车
  4. * param $basket 存储商品信息
  5. * param $ins 存储实例化对象
  6. */
  7. namespace lib;
  8. use redis\Redis;
  9. class Cart{
  10. private $expire = 43200; //redis购物车商品缓存过期时间
  11. private $redis = Null;
  12. private $redis_ext = ''; //redis缓存键名拼接字符串
  13. private $cachekey = Null;
  14. private $basket = []; //私有空数组用于存放商品信息
  15. /**
  16. * 购物车初始化,传入用户id
  17. */
  18. public function init($user_id){
  19. $this->redis = Redis::ins(); //调用redis缓存类(连接redis)
  20. $this->redis_ext = '.'.config('system')['project_name_zn']; //redis缓存键名拼接项目中文名称字符串
  21. $this->cachekey = "user.cart.{$user_id}".$this->redis_ext; //redis缓存键名拼接用户ID与项目中文名称字符串为对象用户购物车缓存键名
  22. $this->basket = json_decode($this->redis->get($this->cachekey),true); //获取对象用户的redis购物车商品缓存信息并解码为PHP数组
  23. }
  24. //添加商品 $id 商品ID $attr_id 商品对应属性ID $goodsName 商品名称 $number 加入数量 $price 商品对应属性单价
  25. public function addbasket( $id, $attr_id, $goodsName, $attr_name, $number, $price , $freight ){
  26. //判断对象商品是否已经存在redis购物车商品缓存内
  27. if( $this->isExist($id,$attr_id) ){
  28. //存在时增加该对象商品数量
  29. return $this->add($id, $attr_id ,$number);
  30. }
  31. //对象商品不在redis购物车商品缓存内时
  32. $tmp = [];
  33. $tmp['goods_id'] = intval($id); //对象商品ID
  34. $tmp['attr_id'] = intval($attr_id); //对象商品对应属性ID
  35. $tmp['goods_name'] = $goodsName; //对象商品名称
  36. $tmp['attr_name'] = $attr_name; //对象商品名称
  37. $tmp['goods_number'] = intval($number); //对象商品数量,新增的商品默认加入数量为1
  38. $tmp['price'] = intval($price); //对象商品对应属性单价
  39. $tmp['freight'] = intval($freight); //对象商品运费
  40. $tmp['subtotal'] = $tmp['goods_number'] * $price; //对象商品总价
  41. $this->basket[] = $tmp; //新的商品信息存入redis购物车商品缓存信息解码的PHP数组内,每件属性商品信息对应一个索引键值
  42. //重新将新的购物车商品信息数组编码为json字符串存入对象用户redis购物车商品缓存内
  43. $this->redis->setex($this->cachekey,$this->expire,json_encode($this->basket));
  44. return 1;
  45. }
  46. //判断商品是否已经存在
  47. // $id 商品ID
  48. // $attr_id 商品属性ID
  49. public function isExist($id,$attr_id){
  50. $isExist = false;
  51. //当对象用户redis购物车商品缓存不为空时
  52. if( !empty($this->basket) ){
  53. foreach ($this->basket as $key=>$val){
  54. if( $val['goods_id'] == $id && $attr_id == $val['attr_id'] ){
  55. $isExist = true;
  56. break;
  57. }
  58. }
  59. }
  60. return $isExist;
  61. }
  62. //获取所有商品信息
  63. public function getAll(){
  64. return $this->basket;
  65. }
  66. //获取部分商品信息
  67. public function getPartGoods($ids)
  68. {
  69. $goods = [];
  70. foreach ($ids as $v) {
  71. foreach ($this->basket as $k => $val) {
  72. if ($val['goods_id'] == $v['goods_id'] && $val['attr_id'] == $v['attr_id']) {
  73. $goods[] = $val;
  74. }
  75. }
  76. }
  77. return $goods;
  78. }
  79. //获取部分商品总数
  80. public function getPartGoodsNum($ids)
  81. {
  82. $number = '';
  83. foreach ($ids as $v) {
  84. foreach ($this->basket as $k => $val) {
  85. if ($val['goods_id'] == $v['goods_id'] && $val['attr_id'] == $v['attr_id']) {
  86. $number += $val['goods_number'];
  87. }
  88. }
  89. }
  90. return $number;
  91. }
  92. /*添加商品
  93. * @param $id商品id
  94. * @param $number 添加的数量 默认为1
  95. * @param $type 1为在原有商品数上添加 总商品数= 当前数 + 历史数,2为总商品数 默认为 1
  96. */
  97. public function add($id, $attr_id ,$number){
  98. $goods_number = 0; //加入不成功时默认返回添加数量为0
  99. //商品ID不为空并且商品在redis购物车商品缓存内
  100. if( !empty($id) && $this->isExist($id ,$attr_id) ){
  101. $cache_detail = $this->basket; //获取用户购物车所有商品信息
  102. foreach ($cache_detail as $key=>$val){
  103. if( $val['goods_id'] == $id && $val['attr_id'] == $attr_id){
  104. $val['goods_number'] = $val['goods_number']+$number; //购物车存在该商品时增加该商品数量
  105. $val['subtotal'] = $val['goods_number']*$val['price']; //购物车存在该商品时重新计算该件商品总价
  106. $this->basket[$key] = $val; //购物车存在该商品时重新将新的商品信息放入该商品的redis缓存信息内($key即为该商品的redis缓存键值)
  107. $this->redis->setex($this->cachekey,$this->expire,json_encode($this->basket)); //购物车存在该商品时更新该商品的redis缓存信息
  108. $goods_number = $val['goods_number']; //商品加入成功将商品数量赋值变量返回
  109. break;
  110. }
  111. }
  112. }
  113. return $goods_number; //返回商品数量
  114. }
  115. //减一商品
  116. public function reduce($id, $attr_id ,$number){
  117. $goods_number = 0;
  118. if(!empty($id) && $this->isExist($id ,$attr_id )){
  119. $cache_detail = $this->basket;
  120. foreach ($cache_detail as $key=>$val){
  121. if( $val['goods_id'] == $id && $val['attr_id'] == $attr_id ){
  122. $val['goods_number'] = $val['goods_number']-$number;
  123. $goods_number = $val['goods_number'];
  124. //若为0则删除
  125. if( $val['goods_number'] <= 0 ){
  126. $this->dele($id ,$attr_id);
  127. $this->redis->setex($this->cachekey,$this->expire,json_encode($this->basket));
  128. $goods_number = 0;
  129. break;
  130. }
  131. $val['subtotal'] = $val['goods_number']*$val['price'];
  132. $this->basket[$key] = $val;
  133. $this->redis->setex($this->cachekey,$this->expire,json_encode($this->basket));
  134. break;
  135. }
  136. }
  137. }
  138. return $goods_number;
  139. }
  140. //删除商品
  141. public function dele($ids){
  142. if(is_array($ids)){
  143. foreach ($ids as $v){
  144. foreach ($this->basket as $k=>$val) {
  145. if( $val['goods_id'] == $v['goods_id'] && $val['attr_id'] == $v['attr_id'] ){
  146. array_splice($this->basket,$k,1);
  147. }
  148. }
  149. }
  150. }else{
  151. foreach ($this->basket as $k=>$val) {
  152. if( $val['goods_id'] == $ids){
  153. //unset(self::$basket[$k]);
  154. array_splice($this->basket,$k,1);
  155. }
  156. }
  157. }
  158. $this->redis->setex($this->cachekey,$this->expire,json_encode($this->basket));
  159. return true;
  160. }
  161. //清空购物车
  162. public function emptyCart(){
  163. return $this->redis->del($this->cachekey);
  164. }
  165. //部分商品总价(包含商品运费) $type不为0时商品总价与商品总运费作为关联数组返回
  166. public function getTotalPrices($ids,$type=0)
  167. {
  168. $totalPrice = 0;
  169. $goods_freight = [];
  170. $freight = 0;
  171. foreach ($ids as $v) {
  172. foreach ($this->basket as $k => $val) {
  173. if ($val['goods_id'] == $v['goods_id'] && $val['attr_id'] == $v['attr_id']) {
  174. $totalPrice += $val['subtotal'];
  175. $goods_freight[$v['goods_id']] = $val['freight']; //获取不同商品的运费
  176. }
  177. }
  178. }
  179. //相同商品不同属性只收取一次运费
  180. foreach ( $goods_freight as $value ){
  181. $freight += $value;
  182. }
  183. if ($type == 0){
  184. return $totalPrice+$freight; //总价=商品总价+商品总运费
  185. }else{
  186. return ['total_price'=>$totalPrice,'freight'=>$freight]; //商品总价与商品总运费
  187. }
  188. }
  189. //编辑某商品数量
  190. public function edit($id, $attr_id, $number)
  191. {
  192. if (!empty($id) && $this->isExist($id, $attr_id) && $number > 0) {
  193. $cache_detail = $this->basket;
  194. foreach ($cache_detail as $key => $val) {
  195. if ($val['goods_id'] == $id && $val['attr_id'] == $attr_id) {
  196. $val['goods_number'] = intval($number);
  197. $val['subtotal'] = $val['goods_number'] * $val['price'];
  198. $this->basket[$key] = $val;
  199. return $this->redis->setex($this->cachekey, $this->expire, json_encode($this->basket));
  200. }
  201. }
  202. }
  203. }
  204. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/代码探险家/article/detail/790570
推荐阅读
相关标签
  

闽ICP备14008679号