Cart.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | A3Mall
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2020 http://www.a3-mall.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: xzncit <158373108@qq.com>
  8. // +----------------------------------------------------------------------
  9. namespace app\api\service;
  10. use app\common\exception\BaseException;
  11. use mall\basic\Users;
  12. use mall\utils\Tool;
  13. use think\facade\Config;
  14. use app\common\models\Cart as CartModel;
  15. use app\common\models\goods\Goods as GoodsModel;
  16. use app\common\models\goods\GoodsItem as GoodsItemModel;
  17. use app\common\models\goods\GoodsAttribute as GoodsAttributeModel;
  18. class Cart extends Service {
  19. /**
  20. * 获取列表数据
  21. * @param $data
  22. * @return array
  23. * @throws BaseException
  24. * @throws \think\db\exception\DataNotFoundException
  25. * @throws \think\db\exception\DbException
  26. * @throws \think\db\exception\ModelNotFoundException
  27. */
  28. public static function getList($data){
  29. $size = Config::get("website.pageSize");
  30. $page = $data["page"]??1;
  31. $condition = ["user_id"=>Users::get("id")];
  32. $count = CartModel::where($condition)->count();
  33. $result = CartModel::where($condition)->order("id","desc")->page($page,$size)->select()->toArray();
  34. $array = [ "list"=>[], "page"=>$page, "total"=>0, "size"=>$size ];
  35. $total = ceil($count / $size);
  36. $array["total"] = $total;
  37. if($total == $page -1){
  38. throw new BaseException("没有数据了哦!",-1,$array);
  39. }
  40. $array["list"] = self::getGoodsList($result);
  41. return $array;
  42. }
  43. /**
  44. * 获取商品列表
  45. * @param $result
  46. * @return array
  47. * @throws \think\db\exception\DataNotFoundException
  48. * @throws \think\db\exception\DbException
  49. * @throws \think\db\exception\ModelNotFoundException
  50. */
  51. public static function getGoodsList($result){
  52. $data = [];
  53. $goodsModel = new GoodsModel();
  54. $goodsItemModel = new GoodsItemModel();
  55. $goodsAttributeModel = new GoodsAttributeModel();
  56. foreach($result as $key=>$value){
  57. $goods = $goodsModel
  58. ->where("id",$value["goods_id"])
  59. ->where("status",0)->find();
  60. if(empty($goods)){
  61. CartModel::where("id",$value["id"])->delete();
  62. continue;
  63. }
  64. if($goodsItemModel->where(["goods_id"=>$value["goods_id"]])->count() && $value["product_id"] <= 0){
  65. CartModel::where("id",$value["id"])->delete();
  66. continue;
  67. }
  68. $data[$key] = [
  69. "id"=>$value["id"],
  70. "title"=>$goods["title"],
  71. "price"=>$goods["sell_price"],
  72. "photo"=>Tool::thumb($goods["photo"],"medium",true),
  73. "nums"=>$goods["store_nums"],
  74. "goods_nums"=>$value["goods_nums"],
  75. "goods_id"=>$value["goods_id"],
  76. "product_id"=>$value["product_id"],
  77. ];
  78. if($value["product_id"] > 0){
  79. $products = $goodsItemModel->where([
  80. "goods_id"=>$value["goods_id"],
  81. "spec_key"=>$value["spec_key"],
  82. ])->find();
  83. if(empty($products)){
  84. unset($data[$key]);
  85. CartModel::where("id",$value["id"])->delete();
  86. continue;
  87. }
  88. $arr = explode(",",$value["spec_key"]);
  89. $attr = [];
  90. foreach ($arr as $val){
  91. $spec = explode(":",$val);
  92. $attribute = $goodsAttributeModel->where([
  93. "goods_id"=>$value["goods_id"],
  94. "attr_id"=>$spec[0],
  95. "attr_data_id"=>$spec[1]
  96. ])->find();
  97. $attr[] = $attribute["name"] . ":" . $attribute["value"];
  98. }
  99. $data[$key]["attr"] = implode(",",$attr);
  100. $data[$key]["price"] = $products["sell_price"];
  101. $data[$key]["nums"] = $products["store_nums"];
  102. $data[$key]["product_id"] = $products["id"];
  103. }
  104. }
  105. return $data;
  106. }
  107. /**
  108. * 删除购物车商品
  109. * @param $id
  110. * @return int
  111. * @throws BaseException
  112. */
  113. public static function delete($id){
  114. if(empty($id)){
  115. throw new BaseException("非法参数",0);
  116. }
  117. if(!is_array($id)){
  118. $id = array_map("intval",explode(",",$id));
  119. }
  120. CartModel::where("id","in",$id)->where("user_id",Users::get("id"))->delete();
  121. return CartModel::where("user_id",Users::get("id"))->count();
  122. }
  123. }