123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- <?php
- namespace app\fastshop\widget;
- use think\facade\Cookie;
- class Shopping{
-
- public function clearCart(int $uid){
- return model('fastshop/Shopping')->table_cart()->where(['user_id' => $uid])->update(['cart' =>'[]']);
- }
-
- public function cartItem(array $cart){
- $ids = ids(array_keys(array_filter($cart)));
- $result = model('Item')->where(['is_sale' => 2])->whereIn('id',$ids)
- ->field('id,name,img,points,repoints,market_price,sell_price,cost_price,weight')
- ->select()->toArray();
- $item = [];
- foreach ($result as $key => $value) {
- $num = abs(intval($cart[$value['id']]));
- $num = $num <= 0 ? 1: $num;
- $sell_total = money($value['sell_price'] * $num);
-
- $amount = $sell_total;
- $item[$value['id']] = [
- 'id' => $value['id'],
- 'market_price' => money($value['market_price']),
- 'sell_price' => money($value['sell_price']),
- 'weight' => $value['weight'],
- 'num' => $num,
- 'amount' => $amount,
- 'sell_total' => $sell_total,
- 'name' => $value['name'],
- 'img' => $value['img'],
- 'points' => empty($value['points']) ? 0 : $value['points'],
- 'repoints' => empty($value['repoints']) ? 0 : $value['repoints'],
- ];
- }
- return $item;
- }
-
-
- public function saveOrder(array $data){
- $order['order_no'] = 'S'.order_no();
- $order['user_id'] = $data['user_id'];
- $order['member_miniapp_id'] = $data['member_miniapp_id'];
- $order['payment_id'] = $data['payment_id'];
- $order['real_amount'] = $data['real_amount'];
- $order['real_freight'] = $data['real_freight'];
- $order['order_amount'] = $data['order_amount'];
- $order['express_name'] = $data['express_name'];
- $order['express_phone'] = $data['express_phone'];
- $order['express_address'] = $data['express_address'];
- $order['paid_at'] = $data['paid_at'];
- if($data['paid_at'] == 1){
- $order['paid_time'] = time();
- $order['paid_no'] = $order['order_no'];
- }
- $order['status'] = 0;
- $order['is_del'] = 0;
- $order['express_status'] = 0;
- $order['order_starttime'] = time();
- $rel = model('fastshop/Shopping')->insertGetId($order);
- return empty($rel) ? false : $order['order_no'];
- }
-
- public function realAmount(array $item,$miniapp_id){
- $fare = model('fastshop/fare')->get(['member_miniapp_id' => $miniapp_id]);
- $real_amount = 0;
- $real_freight = 0;
- $total = 0;
- foreach($item as $value){
- $real_amount += $value['amount'];
- $weight = $value['weight'] * $value['num'];
- if($weight <= $fare['first_weight'] || 0 == $fare['second_weight']){
- $total = $fare['first_price'];
- }else{
- $weight = $weight - $fare['second_weight'];
- $total = $fare['first_price'] + ceil($weight/$fare['second_weight']) * $fare['second_price'];
- }
- $real_freight += $total;
- }
- $data['real_amount'] = money($real_amount);
- $data['real_freight'] = money($real_freight);
- $data['order_amount'] = money($real_freight+$real_amount);
- return $data;
- }
- }
|