Shopping.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 mall\basic;
  10. use mall\utils\BC;
  11. use mall\utils\Tool;
  12. use think\facade\Db;
  13. use think\facade\Session;
  14. class Shopping {
  15. public static function get($cart){
  16. $data = [
  17. "item"=>[],
  18. "activity_id"=>0,
  19. "type"=>0,
  20. "promotions"=>0,
  21. "discount"=>0,
  22. "goods_weight"=>0,
  23. "real_freight"=>0,
  24. "payable_freight"=>0,
  25. "real_amount"=>0,
  26. "payable_amount"=>0,
  27. "order_total"=>0,
  28. "order_amount"=>0,
  29. "exp"=>0,
  30. "point"=>0
  31. ];
  32. foreach($cart as $key=>$val){
  33. $products = [];
  34. if(Db::name("goods_item")->where("goods_id",$val["goods_id"])->count()){
  35. $products = Db::name("goods_item")
  36. ->where("goods_id",$val["goods_id"])
  37. ->where("spec_key",$val["spec_key"])->find();
  38. if(empty($products)){
  39. throw new \Exception("您选择的商品已下架",0);
  40. }
  41. if($val["nums"] > $products["store_nums"]){
  42. throw new \Exception("您选择的商品库存不足",0);
  43. }
  44. }
  45. $goods = Db::name("goods")->where("id",$val["goods_id"])->find();
  46. if(empty($goods)){
  47. throw new \Exception("您选择的商品已下架",0);
  48. }
  49. if(empty($products) && $val["nums"] > $goods["store_nums"]){
  50. throw new \Exception("您选择的商品库存不足",0);
  51. }
  52. $data['item'][$key] = [
  53. "goods_id"=>$goods["id"],
  54. "distribution_id"=>$goods["delivery_id"],
  55. "title"=>$goods["title"],
  56. "goods_no"=>$goods["goods_number"],
  57. "thumb_image"=>Tool::thumb($goods["photo"],"medium",true),
  58. "goods_nums"=>$val["nums"],
  59. "goods_weight"=>$goods["goods_weight"],
  60. "market_price"=>$goods["market_price"],
  61. "real_price"=>$goods["sell_price"],
  62. "sell_price"=>$goods["sell_price"],
  63. "goods_array"=>"",
  64. "fictitious_array"=>""
  65. //"cost_price"=>$goods["cost_price"]
  66. ];
  67. if($goods["goods_type"] != 0){
  68. $data['item'][$key]["fictitious_array"] = json_encode([
  69. "goods_type"=>$goods["goods_type"],
  70. "value"=>$goods["goods_type"] == 1 ? $goods["card_id"] : ($goods["goods_type"] == 2 ? $goods["goods_content"] : $goods["down_url"]),
  71. ],JSON_UNESCAPED_UNICODE);
  72. }
  73. $data["point"] = BC::add($goods["point"] * $val["nums"],$data["point"],0);
  74. $data["exp"] = BC::add($goods["exp"] * $val["nums"],$data["exp"],0);
  75. if(!empty($products)){
  76. $data['item'][$key]["goods_weight"] = $products["goods_weight"];
  77. $data['item'][$key]["market_price"] = $products["market_price"];
  78. $data['item'][$key]["real_price"] = $products["sell_price"];
  79. $data['item'][$key]["sell_price"] = $products["sell_price"];
  80. //$data['item'][$key]["cost_price"] = $products["cost_price"];
  81. $data['item'][$key]["product_id"] = $products["id"];
  82. $data['item'][$key]["spec_key"] = $products["spec_key"];
  83. $data['item'][$key]["goods_array"] = Attribute::get($goods["id"],$products["spec_key"]);
  84. $data["goods_weight"] = BC::add($products["goods_weight"] * $val["nums"],$data["goods_weight"]);
  85. $data["real_amount"] = BC::add($products["sell_price"] * $val["nums"],$data["real_amount"]);
  86. $data["payable_amount"] = BC::add($products["sell_price"] * $val["nums"],$data["payable_amount"]);
  87. }else{
  88. $data["goods_weight"] = BC::add($goods["goods_weight"] * $val["nums"],$data["goods_weight"]);
  89. $data["real_amount"] = BC::add($goods["sell_price"] * $val["nums"],$data["real_amount"]);
  90. $data["payable_amount"] = BC::add($goods["sell_price"] * $val["nums"],$data["payable_amount"]);
  91. }
  92. $data["activity_id"] = $val["activity_id"];
  93. $data["type"] = $val["type"];
  94. }
  95. return $data;
  96. }
  97. public static function add($id,$sku_id,$num=1){
  98. $num = $num ?: 1;
  99. if($num <= 0){
  100. $num = 1;
  101. }
  102. $goods = Db::name("goods")->where("id",$id)->find();
  103. if(empty($goods) || $goods["status"] != 0){
  104. throw new \Exception("该商品已下架!",0);
  105. }
  106. $products = null;
  107. if(Db::name("goods_item")->where("goods_id",$id)->count()){
  108. $products = Db::name("goods_item")->where([
  109. "id"=>$sku_id,
  110. "goods_id"=>$id
  111. ])->find();
  112. if(empty($products)){
  113. throw new \Exception("该商品已下架。",0);
  114. }
  115. }
  116. if(!empty($products)){
  117. if($products["store_nums"] < $num){
  118. throw new \Exception("您选择的商品库存不足",0);
  119. }
  120. }else{
  121. if($goods["store_nums"] < $num){
  122. throw new \Exception("您选择的商品库存不足",0);
  123. }
  124. }
  125. $cart = [
  126. "session_id"=>session_id(),
  127. "user_id"=>Users::get("id"),
  128. "goods_id"=>$id,
  129. "product_id"=>!empty($products["id"]) ? $products["id"] : 0,
  130. "spec_key"=>!empty($products["spec_key"]) ? $products["spec_key"] : "",
  131. "sell_price"=>!empty($products["sell_price"]) ? $products["sell_price"] : $goods["sell_price"],
  132. "cost_price"=>!empty($products["cost_price"]) ? $products["cost_price"] : $goods["cost_price"],
  133. "market_price"=>!empty($products["market_price"]) ? $products["market_price"] : $goods["market_price"],
  134. "goods_weight"=>!empty($products["goods_weight"]) ? $products["goods_weight"] : $goods["goods_weight"],
  135. "goods_nums"=>$num,
  136. "create_time"=>time()
  137. ];
  138. $map = [
  139. "goods_id"=>$id,
  140. "user_id"=>Users::get("id")
  141. ];
  142. if(!empty($products["spec_key"])){
  143. $map["spec_key"] = $products["spec_key"];
  144. }
  145. if(Db::name("cart")->where($map)->count()){
  146. Db::name("cart")->where($map)->update([
  147. "goods_nums"=>$num,
  148. "update_time"=>time()
  149. ]);
  150. }else{
  151. Db::name("cart")->insert($cart);
  152. }
  153. return true;
  154. }
  155. public static function cartAmount(){
  156. $row = Db::name("cart")
  157. ->field("SUM(sell_price * goods_nums) as price")
  158. ->where("user_id",Session::get("user_id"))->find();
  159. return isset($row["price"]) ? $row["price"] : "0.00";
  160. }
  161. public static function delete($id){
  162. if(empty($id)){
  163. throw new \Exception("非法参数",0);
  164. }
  165. if(!is_array($id)){
  166. $id = array_map("intval",explode(",",$id));
  167. }
  168. return Db::name("cart")
  169. ->where("id","in",$id)
  170. ->where("user_id",Users::get("id"))
  171. ->delete();
  172. }
  173. }