Browse Source

预下单

lizhen_gitee 6 months ago
parent
commit
fe1dfbfa02

+ 41 - 21
addons/unishop/controller/Order.php

@@ -91,21 +91,24 @@ class Order extends Base
      */
     public function create()
     {
-        $productId = $this->request->post('id', 0);
+        $productId = input('id', 0);
 
         try {
             $user_id = $this->auth->id;
 
             // 单个商品
-            if ($productId) {
+            /*if ($productId) {*/
+                $number = input('number',1,'intval');
+                $number = intval($number);
                 $productId = \addons\unishop\extend\Hashids::decodeHex($productId);
                 $product = (new Product)->where(['id' => $productId, 'switch' => Product::SWITCH_ON, 'deletetime' => null])->find();
                 /** 产品基础数据 **/
-                $spec = $this->request->post('spec', '');
-                $productData[0] = $product->getDataOnCreateOrder($spec);
-            } else {
+                $spec = input('spec', '');
+
+                $productData[0] = $product->getDataOnCreateOrder($spec,$number);
+           /* } else {
                 // 多个商品
-                $cart = $this->request->post('cart');
+                $cart = input('cart');
                 $carts = (new \addons\unishop\model\Cart)
                     ->whereIn('id', $cart)
                     ->with(['product'])
@@ -116,25 +119,25 @@ class Order extends Base
                         $productData[] = $cart->product->getDataOnCreateOrder($cart->spec ? $cart->spec : '', $cart->number);
                     }
                 }
-            }
+            }*/
 
             if (empty($productData) || !$productData) {
                 $this->error(__('Product not exist'));
             }
 
             /** 默认地址 **/
-            $address = (new AddressModel)->where(['user_id' => $user_id, 'is_default' => AddressModel::IS_DEFAULT_YES])->find();
+            /*$address = (new AddressModel)->where(['user_id' => $user_id, 'is_default' => AddressModel::IS_DEFAULT_YES])->find();
             if ($address) {
                 $area = (new Area)->whereIn('id', [$address->province_id, $address->city_id, $address->area_id])->column('name', 'id');
                 $address = $address->toArray();
                 $address['province']['name'] = $area[$address['province_id']];
                 $address['city']['name'] = $area[$address['city_id']];
                 $address['area']['name'] = $area[$address['area_id']];
-            }
+            }*/
 
 
             /** 可用优惠券 **/
-            $coupon = CouponModel::all(function ($query) {
+            /*$coupon = CouponModel::all(function ($query) {
                 $time = time();
                 $query
                     ->where(['switch' => CouponModel::SWITCH_ON])
@@ -143,24 +146,41 @@ class Order extends Base
             });
             if ($coupon) {
                 $coupon = collection($coupon)->toArray();
-            }
+            }*/
 
 
             /** 运费数据 **/
-            $cityId = $address['city_id'] ? $address['city_id'] : 0;
-            $delivery = (new DeliveryRuleModel())->getDelivetyByArea($cityId);
+//            $cityId = $address['city_id'] ? $address['city_id'] : 0;
+//            $delivery = (new DeliveryRuleModel())->getDelivetyByArea($cityId);
 
+            $order_price = '0';
             foreach ($productData as &$product) {
                 $product['image'] = Config::getImagesFullUrl($product['image']);
                 $product['sales_price'] = round($product['sales_price'], 2);
                 $product['market_price'] = round($product['market_price'], 2);
+
+                $order_price = bcadd($order_price,$product['total_price'],2);
             }
 
+            $coupon = Db::name('unishop_coupon_user')->alias('cu')
+                ->field(['c.id','c.title','c.least','c.value','c.starttime','c.endtime'])
+                ->join('unishop_coupon c','cu.coupon_id = c.id','LEFT')
+                ->where('cu.user_id',$this->auth->id)
+                ->where('cu.status',0)
+                ->where('c.deletetime',NULL)
+                ->where('c.switch',1)
+                ->where('c.starttime','<',time())
+                ->where('c.endtime','>',time())
+                ->where('c.least','>=',$order_price)
+                ->select();
+
             $this->success('', [
                 'product' => $productData,
-                'address' => $address,
+//                'address' => $address,
                 'coupon' => $coupon,
-                'delivery' => $delivery['list']
+//                'delivery' => $delivery['list']
+                'order_price' => $order_price,
+                'total_price' => $order_price
             ]);
 
         } catch (Exception $e) {
@@ -190,7 +210,7 @@ class Order extends Base
      */
     public function submit()
     {
-        $data = $this->request->post();
+        $data = input();
         try {
             $validate = Loader::validate('\\addons\\unishop\\validate\\Order');
             if (!$validate->check($data, [], 'submit')) {
@@ -426,12 +446,12 @@ class Order extends Base
      */
     public function comment()
     {
-        $rate = $this->request->post('rate', 5);
-        $anonymous = $this->request->post('anonymous', 0);
-        $comment = $this->request->post('comment');
-        $order_id = $this->request->post('order_id', 0);
+        $rate = input('rate', 5);
+        $anonymous = input('anonymous', 0);
+        $comment = input('comment');
+        $order_id = input('order_id', 0);
         $order_id = \addons\unishop\extend\Hashids::decodeHex($order_id);
-        $product_id = $this->request->post('product_id');
+        $product_id = input('product_id');
         $product_id = \addons\unishop\extend\Hashids::decodeHex($product_id);
 
         $orderProductModel = new \addons\unishop\model\OrderProduct();

+ 1 - 0
addons/unishop/controller/Product.php

@@ -100,6 +100,7 @@ class Product extends Base
                     ->where('cu.status',0)
                     ->where('c.deletetime',NULL)
                     ->where('c.switch',Coupon::SWITCH_ON)
+                    ->where('c.starttime','<',time())
                     ->where('c.endtime','>',time())
                     ->select();
             }

+ 3 - 0
addons/unishop/extend/Product.php

@@ -36,6 +36,9 @@ class Product
                     if ($key) {
                         $data = $specItem[$key];
                     } else {
+                        $specItem['stock'] = intval($specItem['stock']);//数据类型
+                        $specItem['sales'] = intval($specItem['sales']);//数据类型
+
                         $data = $specItem;
                         $data['key'] = $k;
                     }

+ 4 - 1
addons/unishop/model/Product.php

@@ -147,14 +147,17 @@ class Product extends Model
     {
         $data = (new \addons\unishop\extend\Product)->getBaseData($this->getData(), $spec);
         if ($data['stock'] < 1) {
-            throw new Exception('产品库存不足');
+            //throw new Exception('产品库存不足');
         }
         $product = $this->getData();
         $data['title'] = $product['title'];
+        $data['info'] = $product['info'];
         $data['spec'] = $spec;
         $data['number'] = $number;
         $data['id'] = Hashids::encodeHex($product['id']);
 
+        $data['total_price'] = bcmul($data['sales_price'],$number,2);
+
         return $data;
     }