Shopping.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 https://www.sapixx.com All rights reserved.
  4. * @license Licensed (http://www.apache.org/licenses/LICENSE-2.0).
  5. * @author pillar<ltmn@qq.com>
  6. * 订单逻辑
  7. */
  8. namespace app\fastshop\widget;
  9. use think\facade\Cookie;
  10. class Shopping{
  11. /**
  12. * 清空购物车和订单Cookie
  13. * @return void
  14. */
  15. public function clearCart(int $uid){
  16. return model('fastshop/Shopping')->table_cart()->where(['user_id' => $uid])->update(['cart' =>'[]']);
  17. }
  18. /**
  19. * 读取购物车中IDS的商品信息
  20. * @param [array] $cart 购物车中的数据 ['sku_id' => num]
  21. * @return void
  22. */
  23. public function cartItem(array $cart){
  24. $ids = ids(array_keys(array_filter($cart))); //过滤查询ID
  25. $result = model('Item')->where(['is_sale' => 2])->whereIn('id',$ids)
  26. ->field('id,name,img,points,repoints,market_price,sell_price,cost_price,weight')
  27. ->select()->toArray();
  28. $item = [];
  29. foreach ($result as $key => $value) {
  30. $num = abs(intval($cart[$value['id']]));
  31. $num = $num <= 0 ? 1: $num;
  32. $sell_total = money($value['sell_price'] * $num); //单个商品价格
  33. //最终价格(后期开发,加上商品活动的时候计算)
  34. $amount = $sell_total; //计算付款金额的字段
  35. $item[$value['id']] = [
  36. 'id' => $value['id'],
  37. 'market_price' => money($value['market_price']),
  38. 'sell_price' => money($value['sell_price']),
  39. 'weight' => $value['weight'],
  40. 'num' => $num,
  41. 'amount' => $amount,
  42. 'sell_total' => $sell_total,
  43. 'name' => $value['name'],
  44. 'img' => $value['img'],
  45. 'points' => empty($value['points']) ? 0 : $value['points'],
  46. 'repoints' => empty($value['repoints']) ? 0 : $value['repoints'],
  47. ];
  48. }
  49. return $item;
  50. }
  51. /**
  52. * 保存订单
  53. * @param array $item [计算参数]
  54. * @return array 商品价格信息
  55. */
  56. public function saveOrder(array $data){
  57. $order['order_no'] = 'S'.order_no();
  58. $order['user_id'] = $data['user_id'];
  59. $order['member_miniapp_id'] = $data['member_miniapp_id'];
  60. $order['payment_id'] = $data['payment_id'];
  61. $order['real_amount'] = $data['real_amount'];
  62. $order['real_freight'] = $data['real_freight'];
  63. $order['order_amount'] = $data['order_amount'];
  64. $order['express_name'] = $data['express_name'];
  65. $order['express_phone'] = $data['express_phone'];
  66. $order['express_address'] = $data['express_address'];
  67. $order['paid_at'] = $data['paid_at'];
  68. if($data['paid_at'] == 1){
  69. $order['paid_time'] = time();
  70. $order['paid_no'] = $order['order_no'];
  71. }
  72. $order['status'] = 0;
  73. $order['is_del'] = 0;
  74. $order['express_status'] = 0;
  75. $order['order_starttime'] = time();
  76. $rel = model('fastshop/Shopping')->insertGetId($order);
  77. return empty($rel) ? false : $order['order_no'];
  78. }
  79. /**
  80. * 计算运费多少钱
  81. * @param array $item [计算参数]
  82. * @return array 商品价格信息
  83. */
  84. public function realAmount(array $item,$miniapp_id){
  85. $fare = model('fastshop/fare')->get(['member_miniapp_id' => $miniapp_id]);
  86. $real_amount = 0; //商品总价
  87. $real_freight = 0; //运费总价
  88. $total = 0; //单SKU运费
  89. foreach($item as $value){
  90. $real_amount += $value['amount'];
  91. $weight = $value['weight'] * $value['num'];
  92. if($weight <= $fare['first_weight'] || 0 == $fare['second_weight']){
  93. $total = $fare['first_price'];
  94. }else{
  95. $weight = $weight - $fare['second_weight'];
  96. $total = $fare['first_price'] + ceil($weight/$fare['second_weight']) * $fare['second_price'];
  97. }
  98. $real_freight += $total;
  99. }
  100. $data['real_amount'] = money($real_amount); //商品价格
  101. $data['real_freight'] = money($real_freight); //运费
  102. $data['order_amount'] = money($real_freight+$real_amount); //商品总价+运费
  103. return $data;
  104. }
  105. }